-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrelease-make-it-native.mjs
More file actions
207 lines (171 loc) · 6.82 KB
/
Copy pathrelease-make-it-native.mjs
File metadata and controls
207 lines (171 loc) · 6.82 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import fs from "fs";
import path from "path";
import { Octokit } from "@octokit/rest";
import { fileURLToPath } from "url";
import simpleGit from "simple-git";
const required = [
"MIN_VERSION",
"GITHUB_TOKEN",
"GITHUB_SHA",
"GITHUB_REPOSITORY",
"GITHUB_REPOSITORY_OWNER",
"PAT",
];
const missing = required.filter((k) => !process.env[k]);
if (missing.length) {
console.error("Missing env vars:", missing.join(", "));
process.exit(1);
}
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const MIN_VERSION = process.env.MIN_VERSION;
const GITHUB_PAT = process.env.PAT;
const GIT_AUTHOR_NAME = "MendixMobile";
const GIT_AUTHOR_EMAIL = "moo@mendix.com";
// Changelog Settings
const CHANGELOG_BRANCH_NAME = `update-changelog-v${MIN_VERSION}`;
// Docs Repo Settings
const DOCS_REPO_NAME = "docs";
const DOCS_REPO_OWNER = "MendixMobile";
const DOCS_UPSTREAM_OWNER = "mendix";
const DOCS_BRANCH_NAME = `update-mobile-release-notes-v${MIN_VERSION}`;
const TARGET_FILE =
"content/en/docs/releasenotes/mobile/make-it-native-parent/make-it-native.md";
const octokit = new Octokit({ auth: GITHUB_PAT });
function getToday() {
const today = new Date();
const yyyy = today.getFullYear();
const mm = String(today.getMonth() + 1).padStart(2, "0");
const dd = String(today.getDate()).padStart(2, "0");
return `${yyyy}-${mm}-${dd}`;
}
// Changelog
function extractUnreleasedChangelog() {
const changelogPath = path.resolve(__dirname, "../CHANGELOG.md");
const changelog = fs.readFileSync(changelogPath, "utf-8");
const unreleasedRegex =
/^## \[Unreleased\](.*?)(?=^## \[\d+\.\d+\.\d+\][^\n]*|\Z)/ms;
const match = changelog.match(unreleasedRegex);
if (!match) throw new Error("No [Unreleased] section found!");
const unreleasedContent = match[1].trim();
if (!unreleasedContent) throw new Error("No changes under [Unreleased]!");
return { changelog, unreleasedContent, changelogPath };
}
function updateChangelog({ changelog, unreleasedContent, changelogPath }) {
const today = getToday();
const newSection = `## [${MIN_VERSION}] Make it Native - ${today}\n\n${unreleasedContent}\n\n`;
const unreleasedRegex =
/^## \[Unreleased\](.*?)(?=^## \[\d+\.\d+\.\d+\][^\n]*|\Z)/ms;
const updatedChangelog = changelog.replace(
unreleasedRegex,
`## [Unreleased]\n\n${newSection}`
);
fs.writeFileSync(changelogPath, updatedChangelog, "utf-8");
}
async function createPRUpdateChangelog() {
const git = simpleGit();
await git.addConfig("user.name", GIT_AUTHOR_NAME, ["--global"]);
await git.addConfig("user.email", GIT_AUTHOR_EMAIL, ["--global"]);
// Get the current branch name (the one selected in GitHub Actions UI)
const currentBranch = await git.revparse(["--abbrev-ref", "HEAD"]);
await git.checkoutLocalBranch(CHANGELOG_BRANCH_NAME);
await git.add("CHANGELOG.md");
await git.commit(`chore: update CHANGELOG for v${MIN_VERSION}`);
await git.push("origin", CHANGELOG_BRANCH_NAME, ["--force-with-lease"]);
await octokit.pulls.create({
owner: process.env.GITHUB_REPOSITORY_OWNER,
repo: process.env.GITHUB_REPOSITORY.split("/")[1],
title: `Update CHANGELOG for v${MIN_VERSION}`,
head: CHANGELOG_BRANCH_NAME,
base: currentBranch,
body: "**Note:** Please do not take any action on this pull request unless it has been reviewed and approved by a member of the Mobile team.",
draft: true,
});
}
// Docs
function injectUnreleasedToDoc(docPath, unreleasedContent) {
const doc = fs.readFileSync(docPath, "utf-8");
const frontmatterMatch = doc.match(/^---[\s\S]*?---/);
if (!frontmatterMatch) throw new Error("Frontmatter not found!");
const frontmatter = frontmatterMatch[0];
const rest = doc.slice(frontmatter.length).trimStart();
const firstParagraphMatch = rest.match(/^(.*?\n)(\s*\n)/s);
if (!firstParagraphMatch) throw new Error("First paragraph not found!");
const firstParagraph = firstParagraphMatch[1];
const afterFirstParagraph = rest.slice(firstParagraph.length).trimStart();
const date = new Date();
const formattedDate = date.toLocaleDateString("en-US", {
year: "numeric",
month: "short",
day: "numeric",
});
const title = `## Android ${MIN_VERSION} / iOS ${MIN_VERSION}\n\n**Release date: ${formattedDate}**`;
return `${frontmatter}\n\n${firstParagraph}\n${title}\n\n${unreleasedContent}\n\n${afterFirstParagraph}`;
}
async function cloneDocsRepo() {
const git = simpleGit();
await git.clone(
`https://x-access-token:${GITHUB_PAT}@github.com/${DOCS_REPO_OWNER}/${DOCS_REPO_NAME}.git`
);
process.chdir(DOCS_REPO_NAME);
await git.addConfig("user.name", GIT_AUTHOR_NAME, false, "global");
await git.addConfig("user.email", GIT_AUTHOR_EMAIL, false, "global");
}
async function checkoutLocalBranch(git) {
await git.checkoutLocalBranch(DOCS_BRANCH_NAME);
}
async function updateDocsMiNReleaseNotes(unreleasedContent) {
const newDocContent = injectUnreleasedToDoc(TARGET_FILE, unreleasedContent);
fs.writeFileSync(TARGET_FILE, newDocContent, "utf-8");
}
async function createPRUpdateDocsMiNReleaseNotes(git) {
await git.add(TARGET_FILE);
await git.commit(`docs: update mobile release notes for v${MIN_VERSION}`);
await git.push("origin", DOCS_BRANCH_NAME, ["--force"]);
const prBody = `
Automated sync of the latest release notes for v${MIN_VERSION} from [make-it-native](https://github.com/mendix/make-it-native).
---
**Note:**
This pull request was automatically generated by an automation process managed by the Mobile team.
**Please do not take any action on this pull request unless it has been reviewed and approved by a member of the Mobile team.**
`;
await octokit.pulls.create({
owner: DOCS_UPSTREAM_OWNER,
repo: DOCS_REPO_NAME,
title: `Update mobile app release notes for v${MIN_VERSION}`,
head: `${DOCS_REPO_OWNER}:${DOCS_BRANCH_NAME}`,
base: "development",
body: prBody,
draft: true,
});
}
// Update MiN Changelog in MiN repo
async function updateMiNChangelog(changelog, unreleasedContent, changelogPath) {
try {
updateChangelog({ changelog, unreleasedContent, changelogPath });
await createPRUpdateChangelog();
} catch (err) {
console.error("❌ Updating MiN Changelog failed:", err);
process.exit(1);
}
}
// Update MiN Release Notes in Docs repo
async function updateMiNReleaseNotes(unreleasedContent) {
try {
await cloneDocsRepo();
const git = simpleGit();
await checkoutLocalBranch(git);
updateDocsMiNReleaseNotes(unreleasedContent);
await createPRUpdateDocsMiNReleaseNotes(git);
} catch (err) {
console.error("❌ Updating MiN Release Notes failed:", err);
process.exit(1);
}
}
(async () => {
const { changelog, unreleasedContent, changelogPath } =
extractUnreleasedChangelog();
await updateMiNChangelog(changelog, unreleasedContent, changelogPath);
process.chdir("..");
await updateMiNReleaseNotes(unreleasedContent);
})();