org.yuzu_emu.yuzu/.github/workflows/merge.js

122 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-10-12 03:52:25 +00:00
// Note: This is a GitHub Actions script
// It is not meant to be executed directly on your machine without modifications
const fs = require("fs");
const check_query = `query($name: String!){
repository(name: $name, owner: "flathub") {
pullRequests(states: OPEN, first: 50, baseRefName: "master", orderBy: {field: CREATED_AT, direction: DESC}) {
nodes {
id number headRefName mergeable
author { login }
}
}
}
}`;
const close_pr_mutation = `mutation cpm_{num} {
2022-10-13 04:09:37 +00:00
closePullRequest(input: {pullRequestId: "{id}"}) { clientMutationId }
2022-10-12 03:52:25 +00:00
}`;
async function closePullRequests(should_close, github) {
if (should_close.length > 0) {
console.log("Closing other pull requests ...");
let mut = "";
for (let i = 0; i < should_close.length; i++) {
2022-10-13 05:26:53 +00:00
mut += close_pr_mutation
.replace("{num}", i)
.replace("{id}", should_close[i].id);
2022-10-12 03:52:25 +00:00
mut += "\n";
}
await github.graphql(mut);
console.log("Pull requests closed.");
}
}
async function incrementVersion() {
2022-10-13 05:26:53 +00:00
const manifest = fs.readFileSync("org.yuzu_emu.yuzu.json", {
encoding: "utf8",
});
2022-10-12 03:52:25 +00:00
const version = /mainline-0-(\d+)/.exec(manifest)[1];
2022-10-13 05:26:53 +00:00
const new_manifest = manifest
.replace(/-DDISPLAY_VERSION=\d+/, `-DDISPLAY_VERSION=${version}`)
.replace(/-DBUILD_TAG=mainline-\d+/, `-DBUILD_TAG=mainline-${version}`);
2022-10-12 03:52:25 +00:00
fs.writeFileSync("org.yuzu_emu.yuzu.json", new_manifest);
}
async function mergeChanges(branch, execa) {
async function amendCommit() {
2022-10-13 06:09:49 +00:00
await execa("git", ["config", "--global", "user.name", "yuzubot"]);
await execa("git", [
"config",
"--global",
"user.email",
"yuzu\x40yuzu-emu\x2eorg", // prevent email harvesters from scraping the address
]);
2022-10-13 05:26:53 +00:00
await execa("git", ["add", "org.yuzu_emu.yuzu.json"]);
2022-10-13 04:09:37 +00:00
// amend the commit to include the version change
const p1 = execa("git", ["commit", "--amend", "-C", "HEAD"]);
p1.stdout.pipe(process.stdout);
2022-10-13 04:09:37 +00:00
await p1;
}
async function hasChanges() {
const p = await execa("git", [
"status",
"--porcelain",
"org.yuzu_emu.yuzu.json",
]);
2022-10-15 05:40:07 +00:00
if (p.stdout.length > 20) {
console.info("Version was bumped by this script.");
return true;
}
return false;
}
try {
const p = execa("git", ["merge", "--ff-only", `origin/${branch}`]);
p.stdout.pipe(process.stdout);
await p;
// bump the version number
await incrementVersion();
if (await hasChanges()) await amendCommit();
2022-10-12 03:52:25 +00:00
} catch (err) {
console.log(
2022-10-13 05:26:53 +00:00
`::error title=Merge failed::Failed to merge pull request: ${err}`
2022-10-12 03:52:25 +00:00
);
2022-10-12 21:44:41 +00:00
return;
2022-10-12 03:52:25 +00:00
}
2022-10-13 05:26:53 +00:00
const p = execa("git", ["push", "origin", `master:${branch}`, "-f"]);
p.stdout.pipe(process.stdout);
2022-10-13 04:09:37 +00:00
await p;
2022-10-12 04:35:27 +00:00
await new Promise((r) => setTimeout(r, 2000));
2022-10-13 04:09:37 +00:00
// wait a while for GitHub to process the pull request update
const p1 = execa("git", ["push", "origin"]);
p1.stdout.pipe(process.stdout);
2022-10-13 04:09:37 +00:00
await p1;
2022-10-12 03:52:25 +00:00
}
async function checkChanges(github, context) {
const variables = {
name: context.repo.repo,
};
const result = await github.graphql(check_query, variables);
const prs = result.repository.pullRequests.nodes;
const auto_prs = prs.filter(
2022-10-13 05:26:53 +00:00
(pr) => pr.author.login === "flathubbot" && pr.mergeable === "MERGEABLE"
2022-10-12 03:52:25 +00:00
);
if (auto_prs.length < 1) {
console.warn("No pull requests available for merge.");
return null;
}
const chosen = auto_prs[0];
const should_close = auto_prs.slice(1);
2022-10-12 21:12:20 +00:00
console.log(`Selected pull request: #${chosen.number}`);
2022-10-12 03:52:25 +00:00
await closePullRequests(should_close, github);
return chosen.headRefName;
}
module.exports.checkChanges = checkChanges;
module.exports.mergeChanges = mergeChanges;