Skip to content

Commit ccab109

Browse files
yxtaydependabot[bot]github-actions[bot]ematipico
authored
feat: github_action[bot] create and merge update PR (#24)
* feat: merge protected branch * Apply suggestions from code review Co-authored-by: YuXuan Tay <[email protected]> --------- Signed-off-by: YuXuan Tay <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Emanuele Stoppa <[email protected]>
1 parent 360a407 commit ccab109

File tree

5 files changed

+213
-14
lines changed

5 files changed

+213
-14
lines changed

.github/workflows/dependabot-pr.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ jobs:
1818
if: ${{ contains(needs.dependabot-metadata.outputs.dependency-names, 'biome') }}
1919
permissions:
2020
contents: write
21+
pull-requests: write
2122
uses: ./.github/workflows/update.yml

.github/workflows/update.js

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ async function main() {
1616
return;
1717
}
1818

19-
await setGitConfig();
19+
await setGitConfig(missingVersions);
2020
for (const version of missingVersions) {
21-
console.log(`Updating to ${PACKAGE_NAME} ${version}`);
21+
console.log(getMessage(version));
2222
await updateFiles(version);
23-
await commitAndPushTag(version);
23+
await commitTag(version);
2424
}
2525

26-
await git("push", "origin", `HEAD:refs/heads/${DEFAULT_BRANCH}`);
26+
await mergePullRequest(missingVersions);
2727
}
2828

2929
async function getMissingVersions() {
@@ -37,6 +37,12 @@ async function getAllVersions() {
3737
return allVersions.filter((v) => !v.includes("nightly")).sort();
3838
}
3939

40+
async function getNodePackageVersions(packageName) {
41+
const { stdout } = await exec(`npm view ${packageName} --json`);
42+
const output = JSON.parse(stdout);
43+
return output.versions;
44+
}
45+
4046
async function getExistingVersions() {
4147
const existingTags = await git("tag", "--list");
4248
return existingTags
@@ -45,17 +51,21 @@ async function getExistingVersions() {
4551
.sort();
4652
}
4753

48-
async function setGitConfig() {
54+
function getMessage(version) {
55+
return `build(deps): bump ${PACKAGE_NAME} to ${version}`;
56+
}
57+
58+
async function setGitConfig(versions) {
4959
const email = `${GITHUB_ACTOR_ID}+${GITHUB_ACTOR}@users.noreply.github.com`;
60+
5061
await git("config", "user.name", GITHUB_ACTOR);
5162
await git("config", "user.email", email);
5263
await git("checkout", DEFAULT_BRANCH);
64+
await git("checkout", "-b", getBranchName(versions.at(-1)));
5365
}
5466

55-
async function getNodePackageVersions(packageName) {
56-
const { stdout } = await exec(`npm view ${packageName} --json`);
57-
const output = JSON.parse(stdout);
58-
return output.versions;
67+
function getBranchName(version) {
68+
return `build/bump-${PACKAGE_NAME.replace("/", "-")}-${version}`;
5969
}
6070

6171
async function updateFiles(version) {
@@ -82,12 +92,25 @@ async function updatePackageJson(version) {
8292
);
8393
}
8494

85-
async function commitAndPushTag(version) {
95+
async function commitTag(version) {
8696
const tag = `v${version}`;
97+
const message = getMessage(version);
98+
8799
await git("add", "README.md", "package.json", "package-lock.json");
88-
await git("commit", "-m", `"MAINT: upgrade to ${PACKAGE_NAME} ${version}"`);
89-
await git("tag", tag);
90-
await git("push", "origin", tag);
100+
await git("commit", "--message", `"${message}"`);
101+
await git("tag", "--annotate", tag, "--message", `"${message}"`);
102+
}
103+
104+
async function mergePullRequest(versions) {
105+
await git(
106+
"push",
107+
"--follow-tags",
108+
"--set-upstream",
109+
"origin",
110+
getBranchName(versions.at(-1))
111+
);
112+
await exec(`gh pr create --fill --title "${getMessage(versions.at(-1))}"`);
113+
await exec("gh pr merge --auto --merge --delete-branch");
91114
}
92115

93116
async function git(...cmd) {

.github/workflows/update.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ jobs:
1010
runs-on: ubuntu-latest
1111
permissions:
1212
contents: write
13+
pull-requests: write
1314
steps:
1415
- uses: actions/checkout@v4
1516
with:
@@ -23,3 +24,4 @@ jobs:
2324
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
2425
GITHUB_ACTOR: github-actions[bot]
2526
GITHUB_ACTOR_ID: 41898282
27+
GH_TOKEN: ${{ github.token }}

package-lock.json

Lines changed: 173 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
"dependencies": {
99
"@biomejs/biome": "1.9.4"
1010
}
11-
}
11+
}

0 commit comments

Comments
 (0)