-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathUpdateVersion.js
More file actions
117 lines (105 loc) · 5.26 KB
/
UpdateVersion.js
File metadata and controls
117 lines (105 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import {readFileSync, writeFileSync} from "fs";
import {execSync} from "child_process";
var GithubToken = process.argv[2];
var PRNumber = process.argv[3];
process.env.GITHUB_TOKEN = GithubToken;
execSync("gh pr checkout " + PRNumber);
console.info("PR #" + PRNumber + " has been checked out.");
// Check if the last commit was made by github-actions[bot]
const lastCommitAuthor = execSync("git log -1 --pretty=format:'%an'").toString().trim();
console.log("Last commit author: " + lastCommitAuthor);
if (lastCommitAuthor === "github-actions[bot]") {
console.log("Last commit was made by github-actions[bot]. Skipping to prevent infinite loop.");
process.exit(0);
}
const JSONFileName = "./Update.json";
const JSFileName = "./XMOJ.user.js";
var JSONFileContent = readFileSync(JSONFileName, "utf8");
var JSFileContent = readFileSync(JSFileName, "utf8");
execSync("git config --global user.email \"github-actions[bot]@users.noreply.github.com\"");
execSync("git config --global user.name \"github-actions[bot]\"");
if (JSONFileContent.includes('//!ci-no-touch')) {
var updatedContent = JSONFileContent.replace('//!ci-no-touch', '');
writeFileSync(JSONFileName, updatedContent, "utf8");
execSync("git config pull.rebase false");
execSync("git pull");
execSync("git commit -a -m \"remove //!ci-no-touch\"");
execSync("git push -f");
console.log('I won\'t touch this. Exiting process.');
process.exit(0);
}
var JSONObject = JSON.parse(JSONFileContent);
var LastJSONVersion = Object.keys(JSONObject.UpdateHistory)[Object.keys(JSONObject.UpdateHistory).length - 1];
var LastJSVersion = JSFileContent.match(/@version\s+(\d+\.\d+\.\d+)/)[1];
var LastVersion = LastJSVersion.split(".");
var LastPR = JSONObject.UpdateHistory[LastJSONVersion].UpdateContents[0].PR;
var LastDescription = JSONObject.UpdateHistory[LastJSONVersion].UpdateContents[0].Description;
var LastReleaseVersionOnline = execSync("gh release list --exclude-pre-releases --limit 1").toString().trim().split("\t")[2];
var NpmVersion = execSync("jq -r '.version' package.json").toString().trim();
console.log("Last JS version : " + LastJSVersion);
console.log("Last JSON version : " + LastJSONVersion);
console.log("Last PR : " + LastPR);
console.log("Last description : " + LastDescription);
console.log("Last release online: " + LastReleaseVersionOnline);
console.log("npm version : " + NpmVersion);
if (LastJSONVersion != LastJSVersion) {
console.error("XMOJ.user.js and Update.json have different patch versions.");
process.exit(1);
}
execSync("git config --global user.email \"github-actions[bot]@users.noreply.github.com\"");
execSync("git config --global user.name \"github-actions[bot]\"");
var CurrentPR = Number(PRNumber);
var CurrentDescription = String(process.argv[4]);
function extractReleaseNotes(body) {
const match = body
.replace(/\r\n/g, "\n")
.match(/<!--\s*release[- ]notes\s*([\s\S]*?)-->/i);
return match ? match[1].trim() : "";
}
var CurrentNotes = extractReleaseNotes(String(process.argv[5] || ""));
if (LastJSVersion != NpmVersion) {
console.warn("Assuming you manually ran npm version.");
} else if (!(LastPR == CurrentPR && NpmVersion == LastJSVersion)) {
execSync("npm version patch");
}
var CurrentVersion = execSync("jq -r '.version' package.json").toString().trim();
console.log("Current version : " + CurrentVersion);
console.log("Current PR : " + CurrentPR);
console.log("Current description: " + CurrentDescription);
console.log("Current notes : " + (CurrentNotes || "No release notes were provided for this release."));
var ChangedFileList = execSync("gh pr diff " + CurrentPR + " --name-only").toString().trim().split("\n");
console.log("Changed files : " + ChangedFileList.join(", "));
let CommitMessage = "";
if (LastPR == CurrentPR && NpmVersion == LastJSVersion) {
console.warn("Warning: PR is the same as last version.");
JSONObject.UpdateHistory[LastJSVersion].UpdateDate = Date.now();
JSONObject.UpdateHistory[LastJSVersion].UpdateContents[0].Description = CurrentDescription;
if (CurrentNotes) {
JSONObject.UpdateHistory[LastJSVersion].Notes = CurrentNotes;
}
CommitMessage = "Update time and description of " + LastJSVersion;
} else if (ChangedFileList.indexOf("XMOJ.user.js") == -1) {
console.warn("XMOJ.user.js is not changed, so the version should not be updated.");
process.exit(0);
} else {
JSONObject.UpdateHistory[CurrentVersion] = {
"UpdateDate": Date.now(),
"Prerelease": true,
"UpdateContents": [{
"PR": CurrentPR,
"Description": CurrentDescription
}],
"Notes": CurrentNotes || "No release notes were provided for this release."
};
writeFileSync(JSFileName, JSFileContent.replace(/@version(\s+)\d+\.\d+\.\d+/, "@version$1" + CurrentVersion), "utf8");
console.warn("XMOJ.user.js has been updated.");
CommitMessage = "Update version info to " + CurrentVersion;
}
console.log("Commit message : " + CommitMessage);
writeFileSync(JSONFileName, JSON.stringify(JSONObject, null, 4), "utf8");
console.warn("Update.json has been updated.");
execSync("git config pull.rebase false");
execSync("git pull");
execSync("git commit -a -m \"" + CommitMessage + "\"");
execSync("git push -f");
console.log("Pushed to GitHub.");