diff --git a/.github/workflows/pkg.pr.new-comment.yml b/.github/workflows/pkg.pr.new-comment.yml new file mode 100644 index 00000000..c3934c47 --- /dev/null +++ b/.github/workflows/pkg.pr.new-comment.yml @@ -0,0 +1,32 @@ +name: Update pkg.pr.new comment + +on: + workflow_run: + workflows: [Publish to pkg.pr.new] + types: + - completed + +jobs: + build: + if: github.repository == 'sveltejs/svelte-eslint-parser' + name: Update comment + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Download artifact + uses: actions/download-artifact@v4 + with: + name: output + github-token: ${{ secrets.GITHUB_TOKEN }} + run-id: ${{ github.event.workflow_run.id }} + - run: ls -R . + - name: Post or update comment + uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const fs = require('fs'); + const output = JSON.parse(fs.readFileSync('output.json', 'utf8')); + const { default: process } = await import('${{ github.workspace }}/tools/pkg.pr.new-comment.mjs') + + await process({github, context, core, output}) diff --git a/.github/workflows/pkg.pr.new.yml b/.github/workflows/pkg.pr.new.yml index 60cf0ca5..20d5d4aa 100644 --- a/.github/workflows/pkg.pr.new.yml +++ b/.github/workflows/pkg.pr.new.yml @@ -8,6 +8,7 @@ on: jobs: build: + if: github.repository == 'sveltejs/svelte-eslint-parser' runs-on: ubuntu-latest steps: @@ -19,12 +20,24 @@ jobs: - name: Build run: pnpm run build - run: pnpx pkg-pr-new publish --compact --json output.json --comment=off - - uses: actions/github-script@v7 + - name: Add metadata to output + uses: actions/github-script@v7 with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | const fs = require('fs'); const output = JSON.parse(fs.readFileSync('output.json', 'utf8')); - const { default: process } = await import('${{ github.workspace }}/tools/pkg.pr.new-comment.mjs') + output.number = context.issue.number; + output.event_name = context.eventName; + output.ref = context.ref; + output.sha = context.eventName === 'pull_request' + ? context.payload.pull_request.head.sha + : context.payload.after; + fs.writeFileSync('output.json', JSON.stringify(output), 'utf8'); + - name: Upload output + uses: actions/upload-artifact@v4 + with: + name: output + path: ./output.json - await process({github, context, core, output}) + - run: ls -R . diff --git a/tools/pkg.pr.new-comment.mjs b/tools/pkg.pr.new-comment.mjs index 689971e2..abc63aa9 100644 --- a/tools/pkg.pr.new-comment.mjs +++ b/tools/pkg.pr.new-comment.mjs @@ -5,13 +5,10 @@ export default async function ({ github, context, output }) { // eslint-disable-next-line no-console -- For debugging on github actions. console.log("pkg-pr-new publish output:", JSON.stringify(output)); - const sha = - context.eventName === "pull_request" - ? context.payload.pull_request.head.sha - : context.payload.after; + const sha = output.sha; const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${sha}`; - const pullRequestNumber = await getPullRequestNumber(); + const pullRequestNumber = getPullRequestNumber(); const packages = output.packages.map((p) => { let normalizedUrl = p.url; @@ -68,24 +65,8 @@ ${packages.map((p) => `- ${p.name}: ${p.url}`).join("\n")} /* eslint-enable no-console -- For debugging on github actions. */ } - async function getPullRequestNumber() { - if (context.eventName === "pull_request") { - if (context.issue.number) { - return context.issue.number; - } - } else if (context.eventName === "push") { - const pullRequests = await github.rest.pulls.list({ - owner: context.repo.owner, - repo: context.repo.repo, - state: "open", - head: `${context.repo.owner}:${context.ref.replace("refs/heads/", "")}`, - }); - - if (pullRequests.data.length > 0) { - return pullRequests.data[0].number; - } - } - return null; + function getPullRequestNumber() { + return output.number; } async function findBotComment(issueNumber) {