android-merge.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // SPDX-FileCopyrightText: 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. // Note: This is a GitHub Actions script
  4. // It is not meant to be executed directly on your machine without modifications
  5. const fs = require("fs");
  6. // which label to check for changes
  7. const CHANGE_LABEL_MAINLINE = 'android-merge';
  8. const CHANGE_LABEL_EA = 'android-ea-merge';
  9. // how far back in time should we consider the changes are "recent"? (default: 24 hours)
  10. const DETECTION_TIME_FRAME = (parseInt(process.env.DETECTION_TIME_FRAME)) || (24 * 3600 * 1000);
  11. const BUILD_EA = process.env.BUILD_EA == 'true';
  12. const MAINLINE_TAG = process.env.MAINLINE_TAG;
  13. async function checkBaseChanges(github) {
  14. // query the commit date of the latest commit on this branch
  15. const query = `query($owner:String!, $name:String!, $ref:String!) {
  16. repository(name:$name, owner:$owner) {
  17. ref(qualifiedName:$ref) {
  18. target {
  19. ... on Commit { id pushedDate oid }
  20. }
  21. }
  22. }
  23. }`;
  24. const variables = {
  25. owner: 'suyu-emu',
  26. name: 'suyu',
  27. ref: 'refs/heads/master',
  28. };
  29. const result = await github.graphql(query, variables);
  30. const pushedAt = result.repository.ref.target.pushedDate;
  31. console.log(`Last commit pushed at ${pushedAt}.`);
  32. const delta = new Date() - new Date(pushedAt);
  33. if (delta <= DETECTION_TIME_FRAME) {
  34. console.info('New changes detected, triggering a new build.');
  35. return true;
  36. }
  37. console.info('No new changes detected.');
  38. return false;
  39. }
  40. async function checkAndroidChanges(github) {
  41. if (checkBaseChanges(github)) return true;
  42. const pulls = getPulls(github, false);
  43. for (let i = 0; i < pulls.length; i++) {
  44. let pull = pulls[i];
  45. if (new Date() - new Date(pull.headRepository.pushedAt) <= DETECTION_TIME_FRAME) {
  46. console.info(`${pull.number} updated at ${pull.headRepository.pushedAt}`);
  47. return true;
  48. }
  49. }
  50. console.info("No changes detected in any tagged pull requests.");
  51. return false;
  52. }
  53. async function tagAndPush(github, owner, repo, execa, commit=false) {
  54. let altToken = process.env.ALT_GITHUB_TOKEN;
  55. if (!altToken) {
  56. throw `Please set ALT_GITHUB_TOKEN environment variable. This token should have write access to ${owner}/${repo}.`;
  57. }
  58. const query = `query ($owner:String!, $name:String!) {
  59. repository(name:$name, owner:$owner) {
  60. refs(refPrefix: "refs/tags/", orderBy: {field: TAG_COMMIT_DATE, direction: DESC}, first: 10) {
  61. nodes { name }
  62. }
  63. }
  64. }`;
  65. const variables = {
  66. owner: owner,
  67. name: repo,
  68. };
  69. const tags = await github.graphql(query, variables);
  70. const tagList = tags.repository.refs.nodes;
  71. let lastTag = 'android-1';
  72. for (let i = 0; i < tagList.length; ++i) {
  73. if (tagList[i].name.includes('android-')) {
  74. lastTag = tagList[i].name;
  75. break;
  76. }
  77. }
  78. const tagNumber = /\w+-(\d+)/.exec(lastTag)[1] | 0;
  79. const channel = repo.split('-')[1];
  80. const newTag = `${channel}-${tagNumber + 1}`;
  81. console.log(`New tag: ${newTag}`);
  82. if (commit) {
  83. let channelName = channel[0].toUpperCase() + channel.slice(1);
  84. console.info(`Committing pending commit as ${channelName} ${tagNumber + 1}`);
  85. await execa("git", ['commit', '-m', `${channelName} ${tagNumber + 1}`]);
  86. }
  87. console.info('Pushing tags to GitHub ...');
  88. await execa("git", ['tag', newTag]);
  89. await execa("git", ['remote', 'add', 'target', `https://${altToken}@github.com/${owner}/${repo}.git`]);
  90. await execa("git", ['push', 'target', 'master', '-f']);
  91. await execa("git", ['push', 'target', 'master', '--tags']);
  92. console.info('Successfully pushed new changes.');
  93. }
  94. async function tagAndPushEA(github, owner, repo, execa) {
  95. let altToken = process.env.ALT_GITHUB_TOKEN;
  96. if (!altToken) {
  97. throw `Please set ALT_GITHUB_TOKEN environment variable. This token should have write access to ${owner}/${repo}.`;
  98. }
  99. const query = `query ($owner:String!, $name:String!) {
  100. repository(name:$name, owner:$owner) {
  101. refs(refPrefix: "refs/tags/", orderBy: {field: TAG_COMMIT_DATE, direction: DESC}, first: 10) {
  102. nodes { name }
  103. }
  104. }
  105. }`;
  106. const variables = {
  107. owner: owner,
  108. name: repo,
  109. };
  110. const tags = await github.graphql(query, variables);
  111. const tagList = tags.repository.refs.nodes;
  112. let lastTag = 'ea-1';
  113. for (let i = 0; i < tagList.length; ++i) {
  114. if (tagList[i].name.includes('ea-')) {
  115. lastTag = tagList[i].name;
  116. break;
  117. }
  118. }
  119. const tagNumber = /\w+-(\d+)/.exec(lastTag)[1] | 0;
  120. const newTag = `ea-${tagNumber + 1}`;
  121. console.log(`New tag: ${newTag}`);
  122. console.info('Pushing tags to GitHub ...');
  123. await execa("git", ["remote", "add", "android", "https://gitlab.com/suyu-emu/suyu-android.git"]);
  124. await execa("git", ["fetch", "android"]);
  125. await execa("git", ['tag', newTag]);
  126. await execa("git", ['push', 'android', `${newTag}`]);
  127. fs.writeFile('tag-name.txt', newTag, (err) => {
  128. if (err) throw 'Could not write tag name to file!'
  129. })
  130. console.info('Successfully pushed new changes.');
  131. }
  132. async function generateReadme(pulls, context, mergeResults, execa) {
  133. let baseUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/`;
  134. let output =
  135. "| Pull Request | Commit | Title | Author | Merged? |\n|----|----|----|----|----|\n";
  136. for (let pull of pulls) {
  137. let pr = pull.number;
  138. let result = mergeResults[pr];
  139. output += `| [${pr}](${baseUrl}/pull/${pr}) | [\`${result.rev || "N/A"}\`](${baseUrl}/pull/${pr}/files) | ${pull.title} | [${pull.author.login}](https://github.com/${pull.author.login}/) | ${result.success ? "Yes" : "No"} |\n`;
  140. }
  141. output +=
  142. "\n\nEnd of merge log. You can find the original README.md below the break.\n\n-----\n\n";
  143. output += fs.readFileSync("./README.md");
  144. fs.writeFileSync("./README.md", output);
  145. await execa("git", ["add", "README.md"]);
  146. }
  147. async function fetchPullRequests(pulls, repoUrl, execa) {
  148. console.log("::group::Fetch pull requests");
  149. for (let pull of pulls) {
  150. let pr = pull.number;
  151. console.info(`Fetching PR ${pr} ...`);
  152. await execa("git", [
  153. "fetch",
  154. "-f",
  155. "--no-recurse-submodules",
  156. repoUrl,
  157. `pull/${pr}/head:pr-${pr}`,
  158. ]);
  159. }
  160. console.log("::endgroup::");
  161. }
  162. async function mergePullRequests(pulls, execa) {
  163. let mergeResults = {};
  164. console.log("::group::Merge pull requests");
  165. await execa("git", ["config", "--global", "user.name", "suyubot"]);
  166. await execa("git", [
  167. "config",
  168. "--global",
  169. "user.email",
  170. "suyu\x40suyu-emu\x2eorg", // prevent email harvesters from scraping the address
  171. ]);
  172. let hasFailed = false;
  173. for (let pull of pulls) {
  174. let pr = pull.number;
  175. console.info(`Merging PR ${pr} ...`);
  176. try {
  177. const process1 = execa("git", [
  178. "merge",
  179. "--squash",
  180. "--no-edit",
  181. `pr-${pr}`,
  182. ]);
  183. process1.stdout.pipe(process.stdout);
  184. await process1;
  185. const process2 = execa("git", ["commit", "-m", `Merge suyu-emu#${pr}`]);
  186. process2.stdout.pipe(process.stdout);
  187. await process2;
  188. const process3 = await execa("git", ["rev-parse", "--short", `pr-${pr}`]);
  189. mergeResults[pr] = {
  190. success: true,
  191. rev: process3.stdout,
  192. };
  193. } catch (err) {
  194. console.log(
  195. `::error title=#${pr} not merged::Failed to merge pull request: ${pr}: ${err}`
  196. );
  197. mergeResults[pr] = { success: false };
  198. hasFailed = true;
  199. await execa("git", ["reset", "--hard"]);
  200. }
  201. }
  202. console.log("::endgroup::");
  203. if (hasFailed) {
  204. throw 'There are merge failures. Aborting!';
  205. }
  206. return mergeResults;
  207. }
  208. async function resetBranch(execa) {
  209. console.log("::group::Reset master branch");
  210. let hasFailed = false;
  211. try {
  212. await execa("git", ["remote", "add", "source", "https://gitlab.com/suyu-emu/suyu.git"]);
  213. await execa("git", ["fetch", "source"]);
  214. const process1 = await execa("git", ["rev-parse", "source/master"]);
  215. const headCommit = process1.stdout;
  216. await execa("git", ["reset", "--hard", headCommit]);
  217. } catch (err) {
  218. console.log(`::error title=Failed to reset master branch`);
  219. hasFailed = true;
  220. }
  221. console.log("::endgroup::");
  222. if (hasFailed) {
  223. throw 'Failed to reset the master branch. Aborting!';
  224. }
  225. }
  226. async function getPulls(github) {
  227. const query = `query ($owner:String!, $name:String!, $label:String!) {
  228. repository(name:$name, owner:$owner) {
  229. pullRequests(labels: [$label], states: OPEN, first: 100) {
  230. nodes {
  231. number title author { login }
  232. }
  233. }
  234. }
  235. }`;
  236. const mainlineVariables = {
  237. owner: 'suyu-emu',
  238. name: 'suyu',
  239. label: CHANGE_LABEL_MAINLINE,
  240. };
  241. const mainlineResult = await github.graphql(query, mainlineVariables);
  242. const pulls = mainlineResult.repository.pullRequests.nodes;
  243. if (BUILD_EA) {
  244. const eaVariables = {
  245. owner: 'suyu-emu',
  246. name: 'suyu',
  247. label: CHANGE_LABEL_EA,
  248. };
  249. const eaResult = await github.graphql(query, eaVariables);
  250. const eaPulls = eaResult.repository.pullRequests.nodes;
  251. return pulls.concat(eaPulls);
  252. }
  253. return pulls;
  254. }
  255. async function getMainlineTag(execa) {
  256. console.log(`::group::Getting mainline tag android-${MAINLINE_TAG}`);
  257. let hasFailed = false;
  258. try {
  259. await execa("git", ["remote", "add", "mainline", "https://gitlab.com/suyu-emu/suyu-android.git"]);
  260. await execa("git", ["fetch", "mainline", "--tags"]);
  261. await execa("git", ["checkout", `tags/android-${MAINLINE_TAG}`]);
  262. await execa("git", ["submodule", "update", "--init", "--recursive"]);
  263. } catch (err) {
  264. console.log('::error title=Failed pull tag');
  265. hasFailed = true;
  266. }
  267. console.log("::endgroup::");
  268. if (hasFailed) {
  269. throw 'Failed pull mainline tag. Aborting!';
  270. }
  271. }
  272. async function mergebot(github, context, execa) {
  273. // Reset our local copy of master to what appears on suyu-emu/suyu - master
  274. await resetBranch(execa);
  275. const pulls = await getPulls(github);
  276. let displayList = [];
  277. for (let i = 0; i < pulls.length; i++) {
  278. let pull = pulls[i];
  279. displayList.push({ PR: pull.number, Title: pull.title });
  280. }
  281. console.info("The following pull requests will be merged:");
  282. console.table(displayList);
  283. await fetchPullRequests(pulls, "https://gitlab.com/suyu-emu/suyu", execa);
  284. const mergeResults = await mergePullRequests(pulls, execa);
  285. if (BUILD_EA) {
  286. await tagAndPushEA(github, 'suyu-emu', `suyu-android`, execa);
  287. } else {
  288. await generateReadme(pulls, context, mergeResults, execa);
  289. await tagAndPush(github, 'suyu-emu', `suyu-android`, execa, true);
  290. }
  291. }
  292. module.exports.mergebot = mergebot;
  293. module.exports.checkAndroidChanges = checkAndroidChanges;
  294. module.exports.tagAndPush = tagAndPush;
  295. module.exports.checkBaseChanges = checkBaseChanges;
  296. module.exports.getMainlineTag = getMainlineTag;