Skip to content

Commit da27c0e

Browse files
authored
chore: repository maintenance (#427)
1 parent caef3c1 commit da27c0e

File tree

5 files changed

+202
-4
lines changed

5 files changed

+202
-4
lines changed

.github/workflows/NodeCI.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ jobs:
7676
node-version: ${{ matrix.node-version }}
7777
- name: Install Target Packages
7878
run: |+
79-
npm i -D eslint@8
79+
npm i -D eslint@8 vue-eslint-parser@9
8080
npx rimraf node_modules
8181
npm install
8282
- name: Install Packages
@@ -96,7 +96,7 @@ jobs:
9696
node-version: ${{ matrix.node-version }}
9797
- name: Install Target Packages
9898
run: |+
99-
npm i -D eslint@6 mocha@7
99+
npm i -D eslint@6 mocha@7 vue-eslint-parser@9
100100
npx rimraf node_modules
101101
npm install
102102
- name: Test
@@ -114,7 +114,7 @@ jobs:
114114
node-version: ${{ matrix.node-version }}
115115
- name: Install Target Packages
116116
run: |+
117-
npm i -D eslint@7
117+
npm i -D eslint@7 vue-eslint-parser@9
118118
npx rimraf node_modules
119119
npm install
120120
- name: Test

.github/workflows/Release.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ on:
77

88
jobs:
99
release:
10+
# prevents this action from running on forks
11+
if: github.repository == 'ota-meshi/eslint-plugin-jsonc'
12+
permissions:
13+
id-token: write # Required for OIDC
14+
contents: write # to create release (changesets/action)
15+
pull-requests: write # to create pull request (changesets/action)
1016
name: Release
1117
runs-on: ubuntu-latest
1218
steps:
@@ -31,4 +37,3 @@ jobs:
3137
title: "chore: release eslint-plugin-jsonc"
3238
env:
3339
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34-
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
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-jsonc'
16+
name: Update comment
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v5
20+
- name: Download artifact
21+
uses: actions/download-artifact@v5
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@v8
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Publish to pkg.pr.new
2+
on:
3+
pull_request:
4+
branches: [master]
5+
push:
6+
branches: [master]
7+
tags: ["!**"]
8+
9+
jobs:
10+
build:
11+
if: github.repository == 'ota-meshi/eslint-plugin-jsonc'
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v5
16+
- uses: actions/setup-node@v4
17+
- name: Install Packages
18+
run: npm install -f
19+
- name: Build
20+
run: npm run build
21+
env:
22+
SYNCKIT_TS_RUNNER: oxc
23+
- run: npx pkg-pr-new publish --compact '.' --json output.json --comment=off
24+
- name: Add metadata to output
25+
uses: actions/github-script@v8
26+
with:
27+
github-token: ${{ secrets.GITHUB_TOKEN }}
28+
script: |
29+
const fs = require('fs');
30+
const output = JSON.parse(fs.readFileSync('output.json', 'utf8'));
31+
output.number = context.issue.number;
32+
output.event_name = context.eventName;
33+
output.ref = context.ref;
34+
output.sha = context.eventName === 'pull_request'
35+
? context.payload.pull_request.head.sha
36+
: context.payload.after;
37+
fs.writeFileSync('output.json', JSON.stringify(output), 'utf8');
38+
- name: Upload output
39+
uses: actions/upload-artifact@v4
40+
with:
41+
name: output
42+
path: ./output.json
43+
44+
- run: ls -R .

tools/pkg.pr.new-comment.mjs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* Used in `/.github/workflows/pkg.pr.new.yml`
3+
*/
4+
export default async function ({ github, context, output }) {
5+
// eslint-disable-next-line no-console -- For debugging on github actions.
6+
console.log("pkg-pr-new publish output:", JSON.stringify(output));
7+
8+
const sha = output.sha;
9+
const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${sha}`;
10+
11+
const pullRequestNumber = await getPullRequestNumber();
12+
13+
const packages = output.packages.map((p) => {
14+
let normalizedUrl = p.url;
15+
if (pullRequestNumber && p.url.endsWith(sha)) {
16+
normalizedUrl = `${p.url.slice(0, -sha.length)}${pullRequestNumber}`;
17+
}
18+
const repoPath = `/${context.repo.owner}/${context.repo.repo}/`;
19+
normalizedUrl = normalizedUrl.replace(repoPath, "/");
20+
21+
return {
22+
name: p.name,
23+
url: normalizedUrl,
24+
};
25+
});
26+
27+
const botCommentIdentifier = "<!-- posted by pkg.pr.new-comment.mjs -->";
28+
29+
const onlineUrl = new URL(
30+
"https://eslint-online-playground.netlify.app/#eslint-plugin-jsonc",
31+
);
32+
const overrideDeps = {};
33+
for (const p of packages) {
34+
overrideDeps[p.name] = p.url;
35+
}
36+
onlineUrl.searchParams.set("overrideDeps", JSON.stringify(overrideDeps));
37+
const body = `${botCommentIdentifier}
38+
39+
- Try the Instant Preview
40+
41+
[ESLint Online Playground](${onlineUrl})
42+
43+
- Preview to Your Local
44+
45+
\`\`\`
46+
npm i ${packages.map((p) => p.url).join(" ")}
47+
\`\`\`
48+
49+
- Published Instant Preview Packages:
50+
${packages.map((p) => `- ${p.name}: ${p.url}`).join("\n ")}
51+
52+
- [View Commit](${commitUrl})`;
53+
54+
if (pullRequestNumber) {
55+
await createOrUpdateComment(pullRequestNumber);
56+
} else {
57+
/* eslint-disable no-console -- For debugging on github actions. */
58+
console.log(
59+
"No open pull request found for this push. Logging publish information to console:",
60+
);
61+
console.log(`\n${"=".repeat(50)}`);
62+
console.log(body);
63+
console.log(`\n${"=".repeat(50)}`);
64+
/* eslint-enable no-console -- For debugging on github actions. */
65+
}
66+
67+
/**
68+
* Get the pull request number from the output.
69+
*/
70+
function getPullRequestNumber() {
71+
return output.number;
72+
}
73+
74+
/**
75+
* Find the bot comment in the issue.
76+
*/
77+
async function findBotComment(issueNumber) {
78+
if (!issueNumber) return null;
79+
const comments = await github.rest.issues.listComments({
80+
owner: context.repo.owner,
81+
repo: context.repo.repo,
82+
// eslint-disable-next-line camelcase -- The ID defined in the GitHub API.
83+
issue_number: issueNumber,
84+
});
85+
return comments.data.find((comment) =>
86+
comment.body.includes(botCommentIdentifier),
87+
);
88+
}
89+
90+
/**
91+
* Create or update the bot comment in the issue.
92+
*/
93+
async function createOrUpdateComment(issueNumber) {
94+
const existingComment = await findBotComment(issueNumber);
95+
if (existingComment) {
96+
await github.rest.issues.updateComment({
97+
owner: context.repo.owner,
98+
repo: context.repo.repo,
99+
// eslint-disable-next-line camelcase -- The ID defined in the GitHub API.
100+
comment_id: existingComment.id,
101+
body,
102+
});
103+
} else {
104+
await github.rest.issues.createComment({
105+
// eslint-disable-next-line camelcase -- The ID defined in the GitHub API.
106+
issue_number: issueNumber,
107+
owner: context.repo.owner,
108+
repo: context.repo.repo,
109+
body,
110+
});
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)