Skip to content

Commit 054461d

Browse files
committed
update pkg.pr.new script
1 parent 64b0934 commit 054461d

File tree

4 files changed

+73
-29
lines changed

4 files changed

+73
-29
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Update pkg.pr.new comment
2+
3+
on:
4+
workflow_run:
5+
workflows: [Publish to pkg.pr.new]
6+
types:
7+
- completed
8+
9+
permissions:
10+
issues: write
11+
pull-requests: write
12+
13+
jobs:
14+
build:
15+
if: github.repository == 'ota-meshi/eslint-plugin-astro'
16+
name: Update comment
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v4
20+
- name: Download artifact
21+
uses: actions/download-artifact@v4
22+
with:
23+
name: output
24+
github-token: ${{ secrets.GITHUB_TOKEN }}
25+
run-id: ${{ github.event.workflow_run.id }}
26+
- run: ls -R .
27+
- name: Post or update comment
28+
uses: actions/github-script@v7
29+
with:
30+
github-token: ${{ secrets.GITHUB_TOKEN }}
31+
script: |
32+
const fs = require('fs');
33+
const output = JSON.parse(fs.readFileSync('output.json', 'utf8'));
34+
const { default: process } = await import('${{ github.workspace }}/tools/pkg.pr.new-comment.mjs')
35+
36+
await process({github, context, core, output})

.github/workflows/pkg.pr.new.yml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88

99
jobs:
1010
build:
11+
if: github.repository == 'ota-meshi/eslint-plugin-astro'
1112
runs-on: ubuntu-latest
1213

1314
steps:
@@ -18,12 +19,24 @@ jobs:
1819
- name: Build
1920
run: npm run build
2021
- run: npx pkg-pr-new publish --json output.json --comment=off
21-
- uses: actions/github-script@v7
22+
- name: Add metadata to output
23+
uses: actions/github-script@v7
2224
with:
2325
github-token: ${{ secrets.GITHUB_TOKEN }}
2426
script: |
2527
const fs = require('fs');
2628
const output = JSON.parse(fs.readFileSync('output.json', 'utf8'));
27-
const { default: process } = await import('${{ github.workspace }}/.github/workflows/pkg.pr.new-comment.mjs')
29+
output.number = context.issue.number;
30+
output.event_name = context.eventName;
31+
output.ref = context.ref;
32+
output.sha = context.eventName === 'pull_request'
33+
? context.payload.pull_request.head.sha
34+
: context.payload.after;
35+
fs.writeFileSync('output.json', JSON.stringify(output), 'utf8');
36+
- name: Upload output
37+
uses: actions/upload-artifact@v4
38+
with:
39+
name: output
40+
path: ./output.json
2841

29-
await process({github, context, core, output})
42+
- run: ls -R .

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"eslint": ">=8.57.0"
7878
},
7979
"devDependencies": {
80+
"@actions/github": "^6.0.0",
8081
"@astrojs/compiler": "^2.0.0",
8182
"@astrojs/mdx": "^4.0.0",
8283
"@astrojs/svelte": "^7.0.0",

.github/workflows/pkg.pr.new-comment.mjs renamed to tools/pkg.pr.new-comment.mjs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
/**
22
* Used in `/.github/workflows/pkg.pr.new.yml`
33
*/
4+
/**
5+
* @param {object} params
6+
* @param {import('@actions/github/lib/utils').GitHub} params.github
7+
* @param {import('@actions/github/lib/context').Context} params.context
8+
* @param {object} params.output
9+
* @param {string} params.output.sha
10+
* @param {string} params.output.number
11+
* @param {Array<{name: string, url: string}>} params.output.packages
12+
*/
413
export default async function ({ github, context, output }) {
514
// eslint-disable-next-line no-console -- For debugging on github actions.
615
console.log("pkg-pr-new publish output:", JSON.stringify(output))
716

8-
const sha =
9-
context.eventName === "pull_request"
10-
? context.payload.pull_request.head.sha
11-
: context.payload.after
17+
const sha = output.sha
1218
const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${sha}`
1319

1420
const pullRequestNumber = await getPullRequestNumber()
@@ -21,17 +27,15 @@ export default async function ({ github, context, output }) {
2127
const repoPath = `/${context.repo.owner}/${context.repo.repo}/`
2228
normalizedUrl = normalizedUrl.replace(repoPath, "/")
2329

24-
return {
25-
name: p.name,
26-
url: normalizedUrl,
27-
}
30+
return { name: p.name, url: normalizedUrl }
2831
})
2932

3033
const botCommentIdentifier = "<!-- posted by pkg.pr.new-comment.mjs -->"
3134

3235
const onlineUrl = new URL(
3336
"https://eslint-online-playground.netlify.app/#eslint-plugin-astro",
3437
)
38+
/** @type {Record<string,string>} */
3539
const overrideDeps = {}
3640
for (const p of packages) {
3741
overrideDeps[p.name] = p.url
@@ -71,48 +75,37 @@ ${packages.map((p) => `- ${p.name}: ${p.url}`).join("\n")}
7175
/**
7276
* Get the pull request number from the context.
7377
*/
74-
async function getPullRequestNumber() {
75-
if (context.eventName === "pull_request") {
76-
if (context.issue.number) {
77-
return context.issue.number
78-
}
79-
} else if (context.eventName === "push") {
80-
const pullRequests = await github.rest.pulls.list({
81-
owner: context.repo.owner,
82-
repo: context.repo.repo,
83-
state: "open",
84-
head: `${context.repo.owner}:${context.ref.replace("refs/heads/", "")}`,
85-
})
86-
87-
if (pullRequests.data.length > 0) {
88-
return pullRequests.data[0].number
89-
}
90-
}
91-
return null
78+
function getPullRequestNumber() {
79+
return output.number
9280
}
9381

9482
/**
9583
* Find the bot comment in the pull request.
84+
* @param {string} issueNumber The pull request number.
9685
*/
9786
async function findBotComment(issueNumber) {
9887
if (!issueNumber) return null
88+
// @ts-expect-error -- The ID defined in the GitHub API.
9989
const comments = await github.rest.issues.listComments({
10090
owner: context.repo.owner,
10191
repo: context.repo.repo,
10292
// eslint-disable-next-line camelcase -- The ID defined in the GitHub API.
10393
issue_number: issueNumber,
10494
})
95+
// @ts-expect-error
10596
return comments.data.find((comment) =>
10697
comment.body.includes(botCommentIdentifier),
10798
)
10899
}
109100

110101
/**
111102
* Create or update the bot comment in the pull request.
103+
* @param {string} issueNumber The pull request number.
112104
*/
113105
async function createOrUpdateComment(issueNumber) {
114106
const existingComment = await findBotComment(issueNumber)
115107
if (existingComment) {
108+
// @ts-expect-error -- The ID defined in the GitHub API.
116109
await github.rest.issues.updateComment({
117110
owner: context.repo.owner,
118111
repo: context.repo.repo,
@@ -121,6 +114,7 @@ ${packages.map((p) => `- ${p.name}: ${p.url}`).join("\n")}
121114
body,
122115
})
123116
} else {
117+
// @ts-expect-error -- The ID defined in the GitHub API.
124118
await github.rest.issues.createComment({
125119
// eslint-disable-next-line camelcase -- The ID defined in the GitHub API.
126120
issue_number: issueNumber,

0 commit comments

Comments
 (0)