Skip to content

Commit

Permalink
ci(actions): add preview builds (#3)
Browse files Browse the repository at this point in the history
* ci(actions): add preview config

* fix: use bun in preview workflow

* chore(test): change to test previews

* revert: revert last test change

* ci(actions): update preview config

* ci(actions): exclude tags from preview

* ci(actions): add custom comment script

* fix(actions): fix comment script

* ci(actions): test comment script changes

* ci(actions): test again

* ci(actions): test more changes

* fix(actions): typo

* fix(actions): fix commit url in preview comment

* ci(actions): test stuff

* ci(actions): add pr number to preview url

* fix: fix logs

* ci(actions): give up with logs

* revert: revert test changes

* ci(actions): limit preview on push to main only

* revert(restore): bring back logging stuff

* ci(actions): config cq
  • Loading branch information
renejfc authored Jan 23, 2025
1 parent 33f652b commit 93a40b9
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 5 deletions.
124 changes: 124 additions & 0 deletions .github/scripts/preview-comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { readFileSync } from "node:fs"

const BOT_COMMENT_IDENTIFIER = "### 🧪 CLI Preview Build."
const COMMIT_LENGTH = 7

const generateUrls = ({ sha, owner, repo, commitUrl = null, prNumber = null }) => {
const shortSha = sha.substring(0, COMMIT_LENGTH)
const shortUrl = `https://pkg.pr.new/${owner}/${repo}@${prNumber ?? shortSha}`

return {
shortSha,
shortUrl,
commitUrl: commitUrl ?? `https://github.com/${owner}/${repo}/commit/${sha}`,
}
}

const generateCommentBody = ({ sha, owner, repo, commitUrl, prNumber }) => {
const { shortSha, shortUrl } = generateUrls({ sha, owner, repo, commitUrl, prNumber })

return `${BOT_COMMENT_IDENTIFIER}
Try this version:
\`\`\`sh
bun add -g ${shortUrl}
\`\`\`
###### _[${shortSha}](${commitUrl})_`
}

const findBotComment = async ({ github, context, issueNumber }) => {
const comments = await github.rest.issues.listComments({
...context.repo,
issue_number: issueNumber,
})

return comments.data.find((comment) => comment.body.includes(BOT_COMMENT_IDENTIFIER))
}

const createOrUpdateComment = async ({ github, context, body, issueNumber }) => {
const existingComment = await findBotComment({ github, context, issueNumber })

if (existingComment) {
return await github.rest.issues.updateComment({
...context.repo,
comment_id: existingComment.id,
body,
})
}

return github.rest.issues.createComment({
...context.repo,
issue_number: issueNumber,
body,
})
}

const logPublishInfo = async ({ logger, output, commitUrl }) => {
logger(`\n${"=".repeat(50)}`)
logger("Preview Information")
logger("=".repeat(50))
logger("\nCLI Preview Ready:")
logger(`- ${output.packages[0].url}`)
logger(`\nCommit URL: ${commitUrl}`)
logger(`\n${"=".repeat(50)}`)
}

const handlePullRequest = async ({ github, context, body }) => {
if (!context.issue.number) return
return await createOrUpdateComment({
github,
context,
body,
issueNumber: context.issue.number,
})
}

const handlePush = async ({ logger, github, context, body, output, commitUrl }) => {
const pullRequests = await github.rest.pulls.list({
...context.repo,
state: "open",
head: `${context.repo.owner}:${context.ref.replace("refs/heads/", "")}`,
})

if (pullRequests.data.length > 0) {
return await createOrUpdateComment({
github,
context,
body,
issueNumber: pullRequests.data[0].number,
})
}

logger("No open pull request found for this push. Logging publish information to console:")
await logPublishInfo({ logger, 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 prNumber = context.eventName === "pull_request" ? context.payload.pull_request.number : null

const { commitUrl } = generateUrls({
sha,
owner: context.repo.owner,
repo: context.repo.repo,
prNumber,
})

const body = generateCommentBody({
sha,
owner: context.repo.owner,
repo: context.repo.repo,
commitUrl,
prNumber,
})

const handlers = {
pull_request: () => handlePullRequest({ github, context, body }),
push: () => handlePush({ logger: core.info, github, context, body, output, commitUrl }),
}

const handler = handlers[context.eventName]
if (handler) await handler()
}
28 changes: 28 additions & 0 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Preview Any Commit
on:
pull_request:
push:
branches:
- "main"
tags:
- "!**" # excludes all tags
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: latest
- name: Install deps
run: bun install
- name: Publish preview
run: bunx pkg-pr-new publish --packageManager=bun --json output.json --comment=off
- name: Post or update comment
uses: actions/github-script@v7
with:
script: |
const { run } = await import('${{ github.workspace }}/.github/scripts/preview-comment.js')
await run(github, context, core)
9 changes: 4 additions & 5 deletions .github/workflows/quality.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
name: Code Quality
on:
push:
branches: ["*"]
pull_request:
branches: ["*"]
push:
branches: ["main"]
jobs:
biome:
runs-on: ubuntu-latest
Expand All @@ -22,8 +21,8 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Bun
uses: oven-sh/setup-bun@v1
uses: oven-sh/setup-bun@v2
- name: Install dependencies
run: bun install
- name: Typecheck (TSC)
run: bun run typecheck
run: bun run typecheck

0 comments on commit 93a40b9

Please sign in to comment.