Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: linear ticket checker #902

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/linear-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: linear ticket check
on:
pull_request:
types:
- opened
- reopened
- synchronize
- ready_for_review

jobs:
check:
runs-on: ubuntu-latest
env:
LINEAR_API_KEY: ${{ secrets.LINEAR_API_KEY }}

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
cache-dependency-path: tools/linear-checker/package-lock.json

- name: Check comment issues
working-directory: tools/linear-checker
run: npm install && npm run check
106 changes: 106 additions & 0 deletions tools/linear-checker/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions tools/linear-checker/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "linear-checker",
"version": "1.0.0",
"description": "Checks if issues in the code comments are assigned to a Linear ticket",
"main": "src/index.js",
"author": "Metatype Team",
"type": "module",
"scripts": {
"check": "node ./src/index.js"
},
"dependencies": {
"@linear/sdk": "^32.0.0"
}
}
95 changes: 95 additions & 0 deletions tools/linear-checker/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
// SPDX-License-Identifier: MPL-2.0

import { execSync } from "node:child_process";
import { LinearClient } from "@linear/sdk";

const apiKey = process.env.LINEAR_API_KEY;

let metaTeam, linearClient;

async function getIssue(number) {
if (!linearClient) {
linearClient = new LinearClient({ apiKey });
metaTeam = await linearClient.team("MET");
}

const issues = await metaTeam.issues({
filter: {
number: { eq: number },
},
});

return issues.nodes[0];
}

function getAdditions(diff) {
const [header, ...changes] = diff.split("\n");
const [_, match] = header.match(/\+(\d+)/);
const startLine = parseInt(match);
return changes
.filter((c) => c.startsWith("+"))
.map((addition, i) => ({
addition: addition.slice(1),
line: startLine + i,
}));
}

const command = "git diff --color=never --unified=0 main...HEAD";
const output = execSync(command, { encoding: "utf-8" });

const fileAdditions = output
.split(/(?=^diff --git)/m)
.map((file) => file.split(/(?=^@@)/m))
.map((blocks) => {
const [header, ...diffs] = blocks;
const [_, fileName] = header.match(/^\+\+\+ b\/(.+)/m);
const additions = diffs.flatMap((d) => getAdditions(d));
return { fileName, additions };
});

const issues = fileAdditions.flatMap(({ fileName, additions }) =>
additions
.map(({ addition, line }) => {
const [_, type, desc] = addition.match(/(TODO|FIXME):? (.+)/) ?? [];
const [__, match] = desc ? (desc.match(/MET-(\d+)/) ?? []) : [];
const ticket = match && parseInt(match);
return { file: fileName, type, desc, line, ticket, source: addition };
})
.filter((issue) => issue.desc),
);

let foundInvalidIssue = false;

for (const issue of issues) {
const { file, type, desc, line, ticket, source } = issue;

if (!issue.ticket) {
console.error(
`Error: A Linear ticket was not found for the issue "${type}: ${desc}" in the file "${file}" at line ${line}.`,
"Consider creating a Linear ticket for this issue and referencing it in the comment.",
);
console.error(`${line} | ${source}`);
foundInvalidIssue = true;
} else {
const issue = await getIssue(ticket);

if (!issue) {
console.error(
`Error: The ticket MET-${ticket} referenced in the file "${file}" at line ${line} does not exist`,
);
console.error(`${line} | ${source}`);
foundInvalidIssue = true;
} else {
console.info(
`Info: Found a Linear ticket "MET-${ticket}" in the file "${file}" at line ${line} (${type}: ${desc})`,
);
}
}
}

if (!issues.length) {
console.log("No issues or tickets found");
} else if (foundInvalidIssue) {
process.exit(1);
}
Loading