Skip to content

chore: update pkg.pr.new workflow (take 2) #1057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2025
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
3 changes: 2 additions & 1 deletion .github/workflows/pkg.pr.new-comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
name: 'Update comment'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Download artifact
uses: actions/download-artifact@v4
with:
Expand All @@ -20,7 +21,7 @@ jobs:
run-id: ${{ github.event.workflow_run.id }}
- run: ls -R .
- name: 'Post or update comment'
uses: actions/github-script@v6
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/pkg.pr.new.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ jobs:
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
Expand Down
187 changes: 83 additions & 104 deletions tools/pkg.pr.new-comment.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,39 @@
* Used in `/.github/workflows/pkg.pr.new.yml`
*/
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 commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${sha}`;

const pullRequestNumber = await getPullRequestNumber();

const packages = output.packages.map((p) => {
let normalizedUrl = p.url;
if (pullRequestNumber && p.url.endsWith(sha)) {
normalizedUrl = `${p.url.slice(0, -sha.length)}${pullRequestNumber}`;
}
const repoPath = `/${context.repo.owner}/${context.repo.repo}/`;
normalizedUrl = normalizedUrl.replace(repoPath, "/");

return {
name: p.name,
url: normalizedUrl,
};
});

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

const onlineUrl = new URL(
"https://eslint-online-playground.netlify.app/#eslint-plugin-svelte%20with%20typescript",
);
const overrideDeps = {};
for (const p of packages) {
overrideDeps[p.name] = p.url;
}
onlineUrl.searchParams.set("overrideDeps", JSON.stringify(overrideDeps));
const body = `${botCommentIdentifier}
// eslint-disable-next-line no-console -- For debugging on github actions.
console.log('pkg-pr-new publish output:', JSON.stringify(output));

const sha = output.sha;
const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${sha}`;

const pullRequestNumber = await getPullRequestNumber();

const packages = output.packages.map((p) => {
let normalizedUrl = p.url;
if (pullRequestNumber && p.url.endsWith(sha)) {
normalizedUrl = `${p.url.slice(0, -sha.length)}${pullRequestNumber}`;
}
const repoPath = `/${context.repo.owner}/${context.repo.repo}/`;
normalizedUrl = normalizedUrl.replace(repoPath, '/');

return {
name: p.name,
url: normalizedUrl
};
});

const botCommentIdentifier = '<!-- posted by pkg.pr.new-comment.mjs -->';

const onlineUrl = new URL(
'https://eslint-online-playground.netlify.app/#eslint-plugin-svelte%20with%20typescript'
);
const overrideDeps = {};
for (const p of packages) {
overrideDeps[p.name] = p.url;
}
onlineUrl.searchParams.set('overrideDeps', JSON.stringify(overrideDeps));
const body = `${botCommentIdentifier}

## Try the Instant Preview in Online Playground

Expand All @@ -46,79 +43,61 @@ export default async function ({ github, context, output }) {
## Install the Instant Preview to Your Local

\`\`\`
npm i ${packages.map((p) => p.url).join(" ")}
npm i ${packages.map((p) => p.url).join(' ')}
\`\`\`

## Published Instant Preview Packages:

${packages.map((p) => `- ${p.name}: ${p.url}`).join("\n")}
${packages.map((p) => `- ${p.name}: ${p.url}`).join('\n')}

[View Commit](${commitUrl})`;

if (pullRequestNumber) {
await createOrUpdateComment(pullRequestNumber);
} else {
/* eslint-disable no-console -- For debugging on github actions. */
console.log(
"No open pull request found for this push. Logging publish information to console:",
);
console.log(`\n${"=".repeat(50)}`);
console.log(body);
console.log(`\n${"=".repeat(50)}`);
/* 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;
}

async function findBotComment(issueNumber) {
if (!issueNumber) return null;
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
// eslint-disable-next-line camelcase -- The ID defined in the GitHub API.
issue_number: issueNumber,
});
return comments.data.find((comment) =>
comment.body.includes(botCommentIdentifier),
);
}

async function createOrUpdateComment(issueNumber) {
const existingComment = await findBotComment(issueNumber);
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
// eslint-disable-next-line camelcase -- The ID defined in the GitHub API.
comment_id: existingComment.id,
body,
});
} else {
await github.rest.issues.createComment({
// eslint-disable-next-line camelcase -- The ID defined in the GitHub API.
issue_number: issueNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body,
});
}
}
if (pullRequestNumber) {
await createOrUpdateComment(pullRequestNumber);
} else {
/* eslint-disable no-console -- For debugging on github actions. */
console.log(
'No open pull request found for this push. Logging publish information to console:'
);
console.log(`\n${'='.repeat(50)}`);
console.log(body);
console.log(`\n${'='.repeat(50)}`);
/* eslint-enable no-console -- For debugging on github actions. */
}

async function getPullRequestNumber() {
return output.number;
}

async function findBotComment(issueNumber) {
if (!issueNumber) return null;
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
// eslint-disable-next-line camelcase -- The ID defined in the GitHub API.
issue_number: issueNumber
});
return comments.data.find((comment) => comment.body.includes(botCommentIdentifier));
}

async function createOrUpdateComment(issueNumber) {
const existingComment = await findBotComment(issueNumber);
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
// eslint-disable-next-line camelcase -- The ID defined in the GitHub API.
comment_id: existingComment.id,
body
});
} else {
await github.rest.issues.createComment({
// eslint-disable-next-line camelcase -- The ID defined in the GitHub API.
issue_number: issueNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body
});
}
}
}