From 0e33330aeb350e393f0265d5e6f9d74dadab1e48 Mon Sep 17 00:00:00 2001 From: baseballyama Date: Sun, 5 Nov 2023 20:59:56 +0900 Subject: [PATCH] [fix] Fix action on comment (again) --- packages/ai-craftsman/src/bin.ts | 9 ++-- packages/ai-craftsman/src/utils/git.ts | 74 +++++++++++++------------- 2 files changed, 43 insertions(+), 40 deletions(-) diff --git a/packages/ai-craftsman/src/bin.ts b/packages/ai-craftsman/src/bin.ts index c760363..61e8710 100644 --- a/packages/ai-craftsman/src/bin.ts +++ b/packages/ai-craftsman/src/bin.ts @@ -70,10 +70,11 @@ const main = async () => { const rules = await readCodingRules(); const promises: (() => Promise)[] = []; let commented = false; - const targetBranch = isIncremental - ? await git.getLatestCommitIdByTheApp() - : await git.getBaseRef(); - for (const { diff, path } of git.getDiff(targetBranch)) { + let { base, head } = await git.getRef(); + if (isIncremental) { + base = await git.getLatestCommitIdByTheApp(); + } + for (const { diff, path } of git.getDiff(base, head)) { if (excludePatterns.some((pattern) => pattern.test(path))) { console.log(`SKIP REVIEW: ${path}`); continue; diff --git a/packages/ai-craftsman/src/utils/git.ts b/packages/ai-craftsman/src/utils/git.ts index 782c53d..e8ba7df 100644 --- a/packages/ai-craftsman/src/utils/git.ts +++ b/packages/ai-craftsman/src/utils/git.ts @@ -2,6 +2,7 @@ import github from "@actions/github"; import * as fs from "node:fs"; import { execSync } from "node:child_process"; import { Octokit } from "@octokit/rest"; +import { env } from "../config.js"; const githubToken = process.env["GITHUB_TOKEN"] ?? ""; @@ -18,22 +19,33 @@ const getOwnerAndRepo = () => { return { owner: owner?.login ?? "", repo: name ?? "" }; }; -const getPullNumberAndCommitId = async () => { - const githubEvent = JSON.parse(await fs.promises.readFile(eventPath, "utf8")); - const pullNumber = github.context.payload.pull_request?.number ?? 0; - const commitId = githubEvent.pull_request.head.sha ?? ""; - return { pullNumber, commitId }; +const getPullNumber = () => { + let number = github.context.payload.pull_request?.number; + if (number) return number; + number = github.context.issue?.number; + if (number) return number; + throw new Error("Cannot get pull request number."); +}; + +export const getCommitId = async (): Promise => { + const octokit = getOctokit(); + const { owner, repo } = getOwnerAndRepo(); + const { data } = await octokit.rest.pulls.get({ + owner, + repo, + pull_number: getPullNumber(), + }); + return data.head.sha; }; export const isPrDraft = async (): Promise => { try { const octokit = getOctokit(); const { owner, repo } = getOwnerAndRepo(); - const { pullNumber } = await getPullNumberAndCommitId(); const { data } = await octokit.rest.pulls.get({ owner, repo, - pull_number: pullNumber, + pull_number: getPullNumber(), }); return data.draft === true || data.title.startsWith("[WIP]"); } catch (error) { @@ -46,16 +58,15 @@ export const getCommentsOrderByCreatedAtDesc = async () => { try { const octokit = getOctokit(); const { owner, repo } = getOwnerAndRepo(); - const { pullNumber } = await getPullNumberAndCommitId(); const { data: reviews } = await octokit.rest.pulls.listReviewComments({ owner, repo, - pull_number: pullNumber, + pull_number: getPullNumber(), }); const { data: comments } = await octokit.rest.issues.listComments({ owner, repo, - issue_number: pullNumber, + issue_number: getPullNumber(), }); return [...reviews, ...comments].sort((a, b) => { @@ -88,12 +99,11 @@ export const postComment = async (body: string) => { try { const octokit = getOctokit(); const { owner, repo } = getOwnerAndRepo(); - const { pullNumber, commitId } = await getPullNumberAndCommitId(); await octokit.rest.issues.createComment({ owner, repo, - issue_number: pullNumber, - body: appendCommitId(body, commitId), + issue_number: getPullNumber(), + body: appendCommitId(body, await getCommitId()), }); } catch (error) { console.error(error); @@ -109,11 +119,11 @@ export const postReviewComment = async ( try { const octokit = getOctokit(); const { owner, repo } = getOwnerAndRepo(); - const { pullNumber, commitId } = await getPullNumberAndCommitId(); + const commitId = await getCommitId(); await octokit.rest.pulls.createReviewComment({ owner, repo, - pull_number: pullNumber, + pull_number: getPullNumber(), commit_id: commitId, path, start_line: startLine, @@ -126,19 +136,18 @@ export const postReviewComment = async ( } }; -export const getBaseRef = async () => { - const ref = github.context.payload.pull_request?.["base"]?.ref; - if (ref) return ref; +export const getRef = async () => { + const base = github.context.payload.pull_request?.["base"]?.ref; + const head = github.context.payload.pull_request?.["head"]?.ref; + if (base && head) return { base, head }; const { owner, repo } = getOwnerAndRepo(); - const pullNumber = github.context.payload.issue?.["pull_request"]?.number; const octokit = getOctokit(); const { data: pullRequest } = await octokit.pulls.get({ owner, repo, - pull_number: pullNumber, + pull_number: getPullNumber(), }); - - return pullRequest.base.ref; + return { base: pullRequest.base.ref, head: pullRequest.head.ref }; }; export interface Diff { @@ -146,19 +155,12 @@ export interface Diff { diff: string; } -export const getDiff = (targetBranch: string): Diff[] => { - const currentBranch = execSync("git rev-parse --abbrev-ref HEAD") - .toString() - .trim(); - - const branch = - currentBranch === targetBranch - ? `HEAD~1..HEAD` - : `origin/${targetBranch}..${currentBranch}`; - - const filePaths = execSync( - `git --no-pager diff --minimal --name-only ${branch}` - ) +export const getDiff = (base: string, head: string): Diff[] => { + const command = `git --no-pager diff --minimal --name-only ${base}..${head}`; + if (env.debug) { + console.debug(`Execute command: ${command}`); + } + const filePaths = execSync(command) .toString() .split("\n") .map((ln) => ln.trim()) @@ -169,7 +171,7 @@ export const getDiff = (targetBranch: string): Diff[] => { if (!fs.existsSync(path)) { continue; } - const diff = execSync(`git --no-pager diff ${branch} ${path}`) + const diff = execSync(`git --no-pager diff ${base}..${head} ${path}`) .toString() .trim(); diffs.push({ path, diff });