Skip to content

Commit

Permalink
Rename and fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rehlma committed Apr 15, 2024
1 parent 26ed9cf commit 61d1e2f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 73 deletions.
47 changes: 16 additions & 31 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29070,9 +29070,10 @@ function getPrDiff(octokit, base, head) {
function findTodos(prDiff) {
// Find first number in string
const regex = /(\d+)/;
const todos = prDiff
const fileTodos = prDiff
.map(file => {
const patch = file.patch;
// TODO: Add support to ignore files
if (patch === undefined || file.filename.endsWith('.yml'))
return;
const lines = patch.split('\n');
Expand All @@ -29085,24 +29086,24 @@ function findTodos(prDiff) {
return;
const startLineNumer = parseInt(match[0]);
// get all todos from the patch map them to the line number
const todos = lines
const todoItems = lines
.map((line, index) => {
const todo = getTodoIfFound(line);
if (todo === undefined)
return;
return {
line: startLineNumer + index,
content: todo,
added: line.startsWith('+')
isNew: line.startsWith('+')
};
})
.filter((todo) => todo !== undefined);
if (todos.length == 0)
if (todoItems.length === 0)
return;
return { filename: file.filename, todos: todos };
return { filename: file.filename, todos: todoItems };
})
.filter((todo) => todo !== undefined);
return todos;
return fileTodos;
}
exports.findTodos = findTodos;
function getTodoIfFound(line) {
Expand All @@ -29112,7 +29113,7 @@ function getTodoIfFound(line) {
return;
return match[1];
}
function commentPr(octokit, prNumber, botName, todos) {
function commentPr(octokit, prNumber, botName, fileTodos) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
const { owner, repo } = github.context.repo;
Expand All @@ -29138,33 +29139,20 @@ function commentPr(octokit, prNumber, botName, todos) {
}
}
console.log(`Add found todos as comments to PR #${prNumber}`);
for (const todo of todos) {
const addedTodos = todo.todos.filter(todo => todo.added);
const removedTodos = todo.todos.filter(todo => !todo.added);
for (const fileTodo of fileTodos) {
const addedTodos = fileTodo.todos.filter(todo => todo.isNew);
for (const innerTodo of addedTodos) {
yield octokit.rest.pulls.createReviewComment({
owner,
repo,
pull_number: prNumber,
body: generateComment(innerTodo),
commit_id: headSha,
path: todo.filename,
path: fileTodo.filename,
side: 'RIGHT',
line: innerTodo.line
});
}
// for (const innerTodo of removedTodos) {
// await octokit.rest.pulls.createReviewComment({
// owner,
// repo,
// pull_number: prNumber,
// body: generateComment(innerTodo),
// commit_id: headSha,
// path: todo.filename,
// side: 'LEFT',
// line: innerTodo.line
// })
// }
}
console.log('Current head sha is:', headSha);
try {
Expand All @@ -29176,13 +29164,10 @@ function commentPr(octokit, prNumber, botName, todos) {
}
});
}
function sum(numbers) {
return numbers.reduce((acc, curr) => acc + curr, 0);
}
function generateComment(todo) {
let comment = 'A new Todo was found. If you want to fix it later on, mark it as ignore.\n';
comment += `*${todo.content}*\n`;
if (todo.added) {
if (todo.isNew) {
comment += `- [ ] Ignore`;
}
else {
Expand All @@ -29193,6 +29178,7 @@ function generateComment(todo) {
}
function updateCommitStatus(octokit, prNumber, botName) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
const { owner, repo } = github.context.repo;
// Get all comments on the pull request
const { data: comments } = yield octokit.rest.pulls.listReviewComments({
Expand All @@ -29203,8 +29189,7 @@ function updateCommitStatus(octokit, prNumber, botName) {
console.log('Found comments:', comments.length);
let todoCount = 0;
let doneCount = 0;
comments.forEach(comment => {
var _a, _b, _c;
for (const comment of comments) {
if (((_a = comment.user) === null || _a === void 0 ? void 0 : _a.login) === botName) {
// 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);
Expand All @@ -29218,7 +29203,7 @@ function updateCommitStatus(octokit, prNumber, botName) {
todoCount += 1;
}
}
});
}
// Update the commit status
yield createCommitStatus(octokit, doneCount, todoCount);
});
Expand All @@ -29234,7 +29219,7 @@ function createCommitStatus(octokit, doneCount, todoCount) {
owner,
repo,
sha: headSha,
state: state,
state,
description: `${doneCount}/${todoCount} TODOs solved`,
context: 'TODO Finder'
});
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

57 changes: 20 additions & 37 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import { PrDiff, Todo, InnerTodo } from './types'
import { PrDiff, FileTodos, TodoItem } from './types'

export async function run(): Promise<void> {
try {
Expand Down Expand Up @@ -60,13 +60,14 @@ async function getPrDiff(
return response?.data?.files || []
}

export function findTodos(prDiff: PrDiff): Todo[] {
export function findTodos(prDiff: PrDiff): FileTodos[] {
// Find first number in string
const regex = /(\d+)/

const todos: Todo[] = prDiff
const fileTodos: FileTodos[] = prDiff
.map(file => {
const patch = file.patch
// TODO: Add support to ignore files
if (patch === undefined || file.filename.endsWith('.yml')) return

const lines = patch.split('\n')
Expand All @@ -79,24 +80,24 @@ export function findTodos(prDiff: PrDiff): Todo[] {
const startLineNumer = parseInt(match[0])

// get all todos from the patch map them to the line number
const todos: InnerTodo[] = lines
const todoItems: TodoItem[] = lines
.map((line, index) => {
const todo = getTodoIfFound(line)
if (todo === undefined) return
return {
line: startLineNumer + index,
content: todo,
added: line.startsWith('+')
isNew: line.startsWith('+')
}
})
.filter((todo): todo is InnerTodo => todo !== undefined)
.filter((todo): todo is TodoItem => todo !== undefined)

if (todos.length == 0) return
if (todoItems.length === 0) return

return { filename: file.filename, todos: todos }
return { filename: file.filename, todos: todoItems }
})
.filter((todo): todo is Todo => todo !== undefined)
return todos
.filter((todo): todo is FileTodos => todo !== undefined)
return fileTodos
}

function getTodoIfFound(line: string): string | undefined {
Expand All @@ -110,7 +111,7 @@ async function commentPr(
octokit: ReturnType<typeof github.getOctokit>,
prNumber: number,
botName: string,
todos: Todo[]
fileTodos: FileTodos[]
): Promise<void> {
const { owner, repo } = github.context.repo
const issueNumber = github.context.payload.pull_request?.number
Expand Down Expand Up @@ -139,9 +140,8 @@ async function commentPr(

console.log(`Add found todos as comments to PR #${prNumber}`)

for (const todo of todos) {
const addedTodos = todo.todos.filter(todo => todo.added)
const removedTodos = todo.todos.filter(todo => !todo.added)
for (const fileTodo of fileTodos) {
const addedTodos = fileTodo.todos.filter(todo => todo.isNew)

for (const innerTodo of addedTodos) {
await octokit.rest.pulls.createReviewComment({
Expand All @@ -150,24 +150,11 @@ async function commentPr(
pull_number: prNumber,
body: generateComment(innerTodo),
commit_id: headSha,
path: todo.filename,
path: fileTodo.filename,
side: 'RIGHT',
line: innerTodo.line
})
}

// for (const innerTodo of removedTodos) {
// await octokit.rest.pulls.createReviewComment({
// owner,
// repo,
// pull_number: prNumber,
// body: generateComment(innerTodo),
// commit_id: headSha,
// path: todo.filename,
// side: 'LEFT',
// line: innerTodo.line
// })
// }
}

console.log('Current head sha is:', headSha)
Expand All @@ -180,15 +167,11 @@ async function commentPr(
}
}

function sum(numbers: number[]): number {
return numbers.reduce((acc, curr) => acc + curr, 0)
}

function generateComment(todo: InnerTodo): string {
function generateComment(todo: TodoItem): string {
let comment =
'A new Todo was found. If you want to fix it later on, mark it as ignore.\n'
comment += `*${todo.content}*\n`
if (todo.added) {
if (todo.isNew) {
comment += `- [ ] Ignore`
} else {
comment += `- [x] Ignore`
Expand All @@ -214,7 +197,7 @@ async function updateCommitStatus(

let todoCount = 0
let doneCount = 0
comments.forEach(comment => {
for (const comment of comments) {
if (comment.user?.login === botName) {
// Check if the comment contains a markdown checkbox which is checked
const matches = comment.body?.match(/- \[x\]/gi)
Expand All @@ -228,7 +211,7 @@ async function updateCommitStatus(
todoCount += 1
}
}
})
}

// Update the commit status
await createCommitStatus(octokit, doneCount, todoCount)
Expand All @@ -249,7 +232,7 @@ async function createCommitStatus(
owner,
repo,
sha: headSha,
state: state,
state,
description: `${doneCount}/${todoCount} TODOs solved`,
context: 'TODO Finder'
})
Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ export type PrDiff = {
previous_filename?: string | undefined
}[]

export type Todo = {
export type FileTodos = {
filename: string
todos: InnerTodo[]
todos: TodoItem[]
}

export type InnerTodo = {
export type TodoItem = {
line: number
content: string
added: boolean
isNew: boolean
}

0 comments on commit 61d1e2f

Please sign in to comment.