diff --git a/.github/scripts/preview-comment.js b/.github/scripts/preview-comment.js index 9d4262e..e367fd2 100644 --- a/.github/scripts/preview-comment.js +++ b/.github/scripts/preview-comment.js @@ -14,7 +14,7 @@ const generateUrls = ({ sha, owner, repo, commitUrl = null, prNumber = null }) = } } -const generateCommentBody = ({ sha, owner, repo, commitUrl, prNumber }) => { +const generateCommentBody = ({ sha, owner, repo, commitUrl, prNumber, version }) => { const { shortSha, shortUrl } = generateUrls({ sha, owner, repo, commitUrl, prNumber }) return `${BOT_COMMENT_IDENTIFIER} @@ -24,7 +24,14 @@ Try this version: bun add -g ${shortUrl} \`\`\` -###### _[${shortSha}](${commitUrl})_` +###### _[v${version} — ${shortSha}](${commitUrl})_` +} + +const getNextVersion = (existingComment) => { + if (!existingComment?.body) return 1 + const match = existingComment.body.match(/v(\d+) —/) + const currentVersion = Number.parseInt(match[1], 10) + return currentVersion + 1 } const findBotComment = async ({ github, context, issueNumber }) => { @@ -96,7 +103,10 @@ const handlePush = async ({ logger, github, context, body, output, commitUrl }) export async function run(github, context, core) { const output = JSON.parse(readFileSync("output.json", "utf8")) - const sha = context.eventName === "pull_request" ? context.payload.pull_request.head.sha : context.payload.after + const sha = + context.eventName === "pull_request" + ? context.payload.pull_request.head.sha + : context.payload.after const prNumber = context.eventName === "pull_request" ? context.payload.pull_request.number : null const { commitUrl } = generateUrls({ @@ -106,12 +116,19 @@ export async function run(github, context, core) { prNumber, }) + const existingComment = await findBotComment({ + github, + context, + issueNumber: prNumber || context.issue.number, + }) + const body = generateCommentBody({ sha, owner: context.repo.owner, repo: context.repo.repo, commitUrl, prNumber, + version: getNextVersion(existingComment), }) const handlers = { diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..08af527 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,19 @@ +name: Test +on: + pull_request: + push: + branches: ["main"] +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Setup Bun + uses: oven-sh/setup-bun@v2 + with: + bun-version: latest + - name: Install dependencies + run: bun install + - name: Run tests + run: bun test