Skip to content

Commit 90bd12e

Browse files
committed
Extract CI scripts in separate files
1 parent 77102b8 commit 90bd12e

13 files changed

+268
-155
lines changed

.github/scripts/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
package-lock.json

.github/scripts/commit-and-tag.cjs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2025 New Vector Ltd.
2+
//
3+
// SPDX-License-Identifier: AGPL-3.0-only
4+
// Please see LICENSE in the repository root for full details.
5+
6+
// @ts-check
7+
8+
/** @param {import('@actions/github-script').AsyncFunctionArguments} AsyncFunctionArguments */
9+
module.exports = async ({ github, context }) => {
10+
const fs = require("node:fs/promises");
11+
const { owner, repo } = context.repo;
12+
const version = process.env.VERSION;
13+
const parent = context.sha;
14+
if (!version) throw new Error("VERSION is not defined");
15+
16+
const files = [
17+
"Cargo.toml",
18+
"Cargo.lock",
19+
"tools/syn2mas/package.json",
20+
"tools/syn2mas/package-lock.json",
21+
];
22+
23+
/** @type {{path: string, mode: "100644", type: "blob", sha: string}[]} */
24+
const tree = [];
25+
for (const file of files) {
26+
const content = await fs.readFile(file);
27+
const blob = await github.rest.git.createBlob({
28+
owner,
29+
repo,
30+
content: content.toString("base64"),
31+
encoding: "base64",
32+
});
33+
console.log(`Created blob for ${file}:`, blob.data.url);
34+
35+
tree.push({
36+
path: file,
37+
mode: "100644",
38+
type: "blob",
39+
sha: blob.data.sha,
40+
});
41+
}
42+
43+
const treeObject = await github.rest.git.createTree({
44+
owner,
45+
repo,
46+
tree,
47+
base_tree: parent,
48+
});
49+
console.log("Created tree:", treeObject.data.url);
50+
51+
const commit = await github.rest.git.createCommit({
52+
owner,
53+
repo,
54+
message: version,
55+
parents: [parent],
56+
tree: treeObject.data.sha,
57+
});
58+
console.log("Created commit:", commit.data.url);
59+
60+
const tag = await github.rest.git.createTag({
61+
owner,
62+
repo,
63+
tag: `v${version}`,
64+
message: version,
65+
type: "commit",
66+
object: commit.data.sha,
67+
});
68+
console.log("Created tag:", tag.data.url);
69+
70+
return { commit: commit.data.sha, tag: tag.data.sha };
71+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2025 New Vector Ltd.
2+
//
3+
// SPDX-License-Identifier: AGPL-3.0-only
4+
// Please see LICENSE in the repository root for full details.
5+
6+
// @ts-check
7+
8+
/** @param {import('@actions/github-script').AsyncFunctionArguments} AsyncFunctionArguments */
9+
module.exports = async ({ github, context }) => {
10+
const { owner, repo } = context.repo;
11+
const branch = process.env.BRANCH;
12+
const sha = process.env.SHA;
13+
if (!sha) throw new Error("SHA is not defined");
14+
15+
await github.rest.git.createRef({
16+
owner,
17+
repo,
18+
ref: `refs/heads/${branch}`,
19+
sha,
20+
});
21+
console.log(`Created branch ${branch} from ${sha}`);
22+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2025 New Vector Ltd.
2+
//
3+
// SPDX-License-Identifier: AGPL-3.0-only
4+
// Please see LICENSE in the repository root for full details.
5+
6+
// @ts-check
7+
8+
/** @param {import('@actions/github-script').AsyncFunctionArguments} AsyncFunctionArguments */
9+
module.exports = async ({ github, context }) => {
10+
const { owner, repo } = context.repo;
11+
const version = process.env.VERSION;
12+
const tagSha = process.env.TAG_SHA;
13+
14+
if (!version) throw new Error("VERSION is not defined");
15+
if (!tagSha) throw new Error("TAG_SHA is not defined");
16+
17+
const tag = await github.rest.git.createRef({
18+
owner,
19+
repo,
20+
ref: `refs/tags/v${version}`,
21+
sha: tagSha,
22+
});
23+
console.log("Created tag ref:", tag.data.url);
24+
};

.github/scripts/merge-back.cjs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2025 New Vector Ltd.
2+
//
3+
// SPDX-License-Identifier: AGPL-3.0-only
4+
// Please see LICENSE in the repository root for full details.
5+
6+
// @ts-check
7+
8+
/** @param {import('@actions/github-script').AsyncFunctionArguments} AsyncFunctionArguments */
9+
module.exports = async ({ github, context }) => {
10+
const { owner, repo } = context.repo;
11+
const sha = process.env.SHA;
12+
const branch = `ref-merge/${sha}`;
13+
if (!sha) throw new Error("SHA is not defined");
14+
15+
await github.rest.git.createRef({
16+
owner,
17+
repo,
18+
ref: `refs/heads/${branch}`,
19+
sha,
20+
});
21+
console.log(`Created branch ${branch} to ${sha}`);
22+
23+
// Create a PR to merge the branch back to main
24+
const pr = await github.rest.pulls.create({
25+
owner,
26+
repo,
27+
head: branch,
28+
base: "main",
29+
title: "Automatic merge back to main",
30+
body: "This pull request was automatically created by the release workflow. It merges the release branch back to main.",
31+
maintainer_can_modify: true,
32+
});
33+
console.log(
34+
`Created pull request #${pr.data.number} to merge the release branch back to main`,
35+
);
36+
console.log(`PR URL: ${pr.data.html_url}`);
37+
38+
// Add the `T-Task` label to the PR
39+
await github.rest.issues.addLabels({
40+
owner,
41+
repo,
42+
issue_number: pr.data.number,
43+
labels: ["T-Task"],
44+
});
45+
46+
// Enable auto-merge on the PR
47+
await github.graphql(
48+
`
49+
mutation AutoMerge($id: ID!) {
50+
enablePullRequestAutoMerge(input: {
51+
pullRequestId: $id,
52+
mergeMethod: MERGE,
53+
}) {
54+
clientMutationId
55+
}
56+
}
57+
`,
58+
{ id: pr.data.node_id },
59+
);
60+
};

.github/scripts/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"private": true,
3+
"devDependencies": {
4+
"@actions/github-script": "github:actions/github-script",
5+
"typescript": "^5.7.3"
6+
}
7+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2025 New Vector Ltd.
2+
//
3+
// SPDX-License-Identifier: AGPL-3.0-only
4+
// Please see LICENSE in the repository root for full details.
5+
6+
// @ts-check
7+
8+
/** @param {import('@actions/github-script').AsyncFunctionArguments} AsyncFunctionArguments */
9+
module.exports = async ({ github, context }) => {
10+
const { owner, repo } = context.repo;
11+
const branch = process.env.BRANCH;
12+
const sha = process.env.SHA;
13+
if (!sha) throw new Error("SHA is not defined");
14+
15+
await github.rest.git.updateRef({
16+
owner,
17+
repo,
18+
ref: `heads/${branch}`,
19+
sha,
20+
});
21+
console.log(`Updated branch ${branch} to ${sha}`);
22+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2025 New Vector Ltd.
2+
//
3+
// SPDX-License-Identifier: AGPL-3.0-only
4+
// Please see LICENSE in the repository root for full details.
5+
6+
// @ts-check
7+
8+
/** @param {import('@actions/github-script').AsyncFunctionArguments} AsyncFunctionArguments */
9+
module.exports = async ({ github, context }) => {
10+
const { owner, repo } = context.repo;
11+
const sha = context.sha;
12+
13+
const tag = await github.rest.git.updateRef({
14+
owner,
15+
repo,
16+
force: true,
17+
ref: "tags/unstable",
18+
sha,
19+
});
20+
console.log("Updated tag ref:", tag.data.url);
21+
};

.github/workflows/build.yaml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -444,21 +444,18 @@ jobs:
444444
path: artifacts
445445
merge-multiple: true
446446

447+
- name: Checkout the code
448+
uses: actions/checkout@v4.2.2
449+
with:
450+
sparse-checkout: |
451+
.github/scripts
452+
447453
- name: Update unstable git tag
448454
uses: actions/github-script@v7.0.1
449455
with:
450456
script: |
451-
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
452-
const sha = process.env.GITHUB_SHA;
453-
454-
const tag = await github.rest.git.updateRef({
455-
owner,
456-
repo,
457-
force: true,
458-
ref: 'tags/unstable',
459-
sha,
460-
});
461-
console.log("Updated tag ref:", tag.data.url);
457+
const script = require('./.github/scripts/update-unstable-tag.cjs');
458+
await script({ core, github, context });
462459
463460
- name: Update unstable release
464461
uses: softprops/action-gh-release@v2

.github/workflows/merge-back.yaml

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,59 +14,22 @@ jobs:
1414
name: Merge back the reference to main
1515
runs-on: ubuntu-24.04
1616

17+
permissions:
18+
contents: read
19+
1720
steps:
21+
- name: Checkout the code
22+
uses: actions/checkout@v4
23+
with:
24+
sparse-checkout: |
25+
.github/scripts
26+
1827
- name: Push branch and open a PR
1928
uses: actions/github-script@v7.0.1
2029
env:
2130
SHA: ${{ inputs.sha }}
2231
with:
2332
github-token: ${{ secrets.BOT_GITHUB_TOKEN }}
2433
script: |
25-
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');
26-
const sha = process.env.SHA;
27-
const branch = `ref-merge/${sha}`;
28-
const ref = `heads/${branch}`;
29-
30-
await github.rest.git.createRef({
31-
owner,
32-
repo,
33-
ref,
34-
sha,
35-
});
36-
console.log(`Created branch ${branch} to ${sha}`);
37-
38-
// Create a PR to merge the branch back to main
39-
const pr = await github.rest.pulls.create({
40-
owner,
41-
repo,
42-
head: branch,
43-
base: 'main',
44-
title: `Automatic merge back to main`,
45-
body: `This pull request was automatically created by the release workflow. It merges the release branch back to main.`,
46-
maintainer_can_modify: true,
47-
});
48-
console.log(`Created pull request #${pr.data.number} to merge the release branch back to main`);
49-
console.log(`PR URL: ${pr.data.html_url}`);
50-
51-
// Add the `T-Task` label to the PR
52-
await github.rest.issues.addLabels({
53-
owner,
54-
repo,
55-
issue_number: pr.data.number,
56-
labels: ['T-Task'],
57-
});
58-
59-
// Enable auto-merge on the PR
60-
await github.graphql(
61-
`
62-
mutation AutoMerge($id: ID!) {
63-
enablePullRequestAutoMerge(input: {
64-
pullRequestId: $id,
65-
mergeMethod: MERGE,
66-
}) {
67-
clientMutationId
68-
}
69-
}
70-
`,
71-
{ id: pr.data.node_id },
72-
);
34+
const script = require('./.github/scripts/merge-back.js');
35+
await script({ core, github, context });

0 commit comments

Comments
 (0)