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

Make it work with multi diff patches #5

Merged
merged 3 commits into from
Oct 29, 2024
Merged
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
49 changes: 0 additions & 49 deletions .github/workflows/linter.yml

This file was deleted.

3 changes: 2 additions & 1 deletion __test__/comment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { generateComment } from '../src/comment'

describe('setting comment body and checkbox', () => {
it('should be used correctly', () => {
const commentBodyTemplate = `A new Todo was discovered. If it is not a priority right now, consider marking it for later attention.\n{todo}\n`
const commentBodyTemplate =
'A new Todo was discovered. If it is not a priority right now, consider marking it for later attention.\n{todo}\n'
const commentCheckboxTemplate = 'Ignore'

const todo = {
Expand Down
79 changes: 79 additions & 0 deletions __test__/test_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,82 @@ export const mixedTodoMatcherDiff: PrDiff = [
+ // fixme sixth todo`
}
]

export const multiDiffInPatch: PrDiff = [
{
sha: '111111111',
filename: 'first.dart',
status: 'added',
additions: 1,
deletions: 0,
changes: 1,
blob_url: '',
raw_url: '',
contents_url: '',
patch: `@@ -128,12 +128,14 @@ class A {
return B(
a: c.a,
b: c.b,
+ c: c.c,
);
}
if (c is D) {
return B(
a: c.a,
b: c.b,
+ c: c.c,
);
}
if (c is E) {
@@ -239,9 +241,15 @@ class B {
final String? a;
final String? b;

+ // TODO remove again
+ /// Comment
+ /// comment contains @@
+ final bool? c;
+
const B({
this.a,
this.b,
+ this.c,
});

@override
@@ -250,26 +258,31 @@ class B {
(other is B &&
runtimeType == other.runtimeType &&
a == other.a &&
- b == other.b);
+ b == other.b &&
+ c == other.c);

@override
- int get hashCode => a.hashCode ^ b.hashCode;
+ int get hashCode =>
+ a.hashCode ^ b.hashCode ^ c.hashCode;

@override
String toString() {
return 'B{ '
'a: $a, '
'b: $b, '
+ 'c: $c'
'}';
}

B copyWith({
String? a,
String? b,
+ bool? c,
}) {
return B(
a: a ?? this.a,
b: b ?? this.b,
+ c: c ?? this.c,
);
}
}`
}
]
16 changes: 16 additions & 0 deletions __test__/todo-finder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,22 @@ describe('extract Todos', () => {
expect(fileTodos).toEqual(expectedTodos)
})

it('should find todos in multi diff patches', () => {
const customMatcher = '{}'
const fileTodos = findTodos(testData.multiDiffInPatch, [], customMatcher)

const expectedTodos: Todo[] = [
{
filename: 'first.dart',
line: 244,
content: 'TODO remove again',
isAdded: true
}
]

expect(fileTodos).toEqual(expectedTodos)
})

it('should correctly exclude files', () => {
const exclude = ['**/*.yml', '**/excluded/*']

Expand Down
151 changes: 91 additions & 60 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29314,7 +29314,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.run = void 0;
exports.getTodosForDiff = exports.run = void 0;
const core = __importStar(__nccwpck_require__(2186));
const github = __importStar(__nccwpck_require__(5438));
const todo_finder_1 = __nccwpck_require__(2461);
Expand Down Expand Up @@ -29454,13 +29454,13 @@ function updateCommitStatus(octokit, prNumber, botName) {
console.log('Comment:', comment.line, comment.body);
// Check if the comment contains a markdown checkbox which is checked
const matches = (_b = comment.body) === null || _b === void 0 ? void 0 : _b.match(/- \[x\]/gi);
if (matches) {
if (matches != null) {
doneCount += 1;
todoCount += 1;
}
// Check if the comment contains a markdown checkbox which is unchecked
const uncheckedMatches = (_c = comment.body) === null || _c === void 0 ? void 0 : _c.match(/- \[ \]/gi);
if (uncheckedMatches) {
if (uncheckedMatches != null) {
todoCount += 1;
}
}
Expand Down Expand Up @@ -29493,6 +29493,26 @@ function createCommitStatus(octokit, doneCount, todoCount) {
}
});
}
function getTodosForDiff(pat, owner, repo, base, head) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
console.log('PAT:', pat);
console.log('Owner:', owner);
console.log('Repo:', repo);
console.log('Base:', base);
console.log('Head:', head);
const octokit = github.getOctokit(pat);
const response = yield octokit.rest.repos.compareCommitsWithBasehead({
owner,
repo,
basehead: `${base}...${head}`
});
const prDiff = ((_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.files) || [];
const todos = (0, todo_finder_1.findTodos)(prDiff, [], '{}');
console.log('Todos:', JSON.stringify(todos));
});
}
exports.getTodosForDiff = getTodosForDiff;


/***/ }),
Expand All @@ -29509,69 +29529,83 @@ function findTodos(prDiff, exclude = [], customTodoMatcherString = '{}') {
// Find first number in string
const firstAddedLineRegex = /\+(\d+)/;
const firstRemovedLineRegex = /-(\d+)/;
// Find new diff line @@ -128,12 +128,14 @@ class XYZ {
const newDiffLineRegex = /(@@ [-|+]?\d+,\d+ [-|+]?\d+,\d+ @@)(@@ [-|+]?\d+,\d+ [-|+]?\d+,\d+ @@)*/gm;
const todos = [];
for (const file of prDiff) {
const excluded = exclude.some(pattern => (0, minimatch_1.minimatch)(file.filename, pattern));
const patch = file.patch;
if (patch === undefined || excluded)
continue;
const lines = patch.split('\n');
if (lines === undefined || lines.length === 0)
// split patch into blocks
const matches = patch.match(newDiffLineRegex);
if (matches === null)
continue;
// remove first line and get the line number where the patch starts
const firstLine = lines.shift();
let addedStartLineNumer;
const addedMatch = firstLine === null || firstLine === void 0 ? void 0 : firstLine.match(firstAddedLineRegex);
if (addedMatch !== undefined &&
addedMatch !== null &&
(addedMatch === null || addedMatch === void 0 ? void 0 : addedMatch.length) > 1) {
addedStartLineNumer = parseInt(addedMatch[1]);
}
let removedStartLineNumer;
const removedMatch = firstLine === null || firstLine === void 0 ? void 0 : firstLine.match(firstRemovedLineRegex);
if (removedMatch !== undefined &&
removedMatch !== null &&
(removedMatch === null || removedMatch === void 0 ? void 0 : removedMatch.length) > 1) {
removedStartLineNumer = parseInt(removedMatch[1]);
}
if (addedStartLineNumer === undefined ||
removedStartLineNumer === undefined) {
continue;
}
// get all todos from the patch map them to the line number
let currentAddedLine = addedStartLineNumer;
let currentRemovedLine = removedStartLineNumer;
// get custom todo matcher for the file
const customMatcher = getTodoMatcherForFile(file.filename, customTodoMatcherString);
for (const line of lines) {
const isAdded = line.startsWith('+');
const isDeleted = line.startsWith('-');
const todo = getTodoIfFound(line, customMatcher);
if (isDeleted) {
if (todo !== undefined) {
todos.push({
filename: file.filename,
line: currentRemovedLine,
content: todo,
isAdded: false
});
}
currentRemovedLine += 1;
// blocks are split by diff line and the actual diff
const blocks = patch
.split(newDiffLineRegex)
.filter(block => block !== '' && block !== undefined);
// combine diff line and diff to get the actual diff block
const diffs = matches.map((_, index) => blocks[index] + blocks[index + 1]);
for (const diff of diffs) {
const lines = diff.split('\n');
if (lines === undefined || lines.length === 0)
continue;
// remove first line and get the line number where the patch block starts
const firstLine = lines.shift();
let addedStartLineNumer;
const addedMatch = firstLine === null || firstLine === void 0 ? void 0 : firstLine.match(firstAddedLineRegex);
if (addedMatch !== undefined &&
addedMatch !== null &&
(addedMatch === null || addedMatch === void 0 ? void 0 : addedMatch.length) > 1) {
addedStartLineNumer = parseInt(addedMatch[1]);
}
else if (isAdded) {
if (todo !== undefined) {
todos.push({
filename: file.filename,
line: currentAddedLine,
content: todo,
isAdded: true
});
}
currentAddedLine += 1;
let removedStartLineNumer;
const removedMatch = firstLine === null || firstLine === void 0 ? void 0 : firstLine.match(firstRemovedLineRegex);
if (removedMatch !== undefined &&
removedMatch !== null &&
(removedMatch === null || removedMatch === void 0 ? void 0 : removedMatch.length) > 1) {
removedStartLineNumer = parseInt(removedMatch[1]);
}
else {
currentAddedLine += 1;
currentRemovedLine += 1;
if (addedStartLineNumer === undefined ||
removedStartLineNumer === undefined) {
continue;
}
// get all todos from the patch block map them to the line number
let currentAddedLine = addedStartLineNumer;
let currentRemovedLine = removedStartLineNumer;
// get custom todo matcher for the file
const customMatcher = getTodoMatcherForFile(file.filename, customTodoMatcherString);
for (const line of lines) {
const isAdded = line.startsWith('+');
const isDeleted = line.startsWith('-');
const todo = getTodoIfFound(line, customMatcher);
if (isDeleted) {
if (todo !== undefined) {
todos.push({
filename: file.filename,
line: currentRemovedLine,
content: todo,
isAdded: false
});
}
currentRemovedLine += 1;
}
else if (isAdded) {
if (todo !== undefined) {
todos.push({
filename: file.filename,
line: currentAddedLine,
content: todo,
isAdded: true
});
}
currentAddedLine += 1;
}
else {
currentAddedLine += 1;
currentRemovedLine += 1;
}
}
}
}
Expand Down Expand Up @@ -33398,9 +33432,6 @@ var __webpack_exports__ = {};
var exports = __webpack_exports__;

Object.defineProperty(exports, "__esModule", ({ value: true }));
/**
* The entrypoint for the action.
*/
const main_1 = __nccwpck_require__(399);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
(0, main_1.run)();
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

Loading