Skip to content
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
23 changes: 20 additions & 3 deletions .github/scripts/preview-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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 }) => {
Expand Down Expand Up @@ -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({
Expand All @@ -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 = {
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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