-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathrelease-native-template.mjs
More file actions
262 lines (215 loc) · 8.88 KB
/
Copy pathrelease-native-template.mjs
File metadata and controls
262 lines (215 loc) · 8.88 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import fs from "fs";
import os from "os";
import path from "path";
import { Octokit } from "@octokit/rest";
import { fileURLToPath } from "url";
import simpleGit from "simple-git";
const required = [
"MENDIX_MOBILE_DOCS_PR_GITHUB_PAT",
"STUDIO_PRO_MAJOR",
];
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 DOCS_CLONE_PREFIX = "mendix-docs-";
const MENDIX_MOBILE_DOCS_PR_GITHUB_PAT =
process.env.MENDIX_MOBILE_DOCS_PR_GITHUB_PAT;
const NATIVE_TEMPLATE_VERSION = readVersionFromPackageJson();
const NATIVE_TEMPLATE_MAJOR = NATIVE_TEMPLATE_VERSION.split(".")[0];
const STUDIO_PRO_MAJOR = process.env.STUDIO_PRO_MAJOR;
const GIT_AUTHOR_NAME = "MendixMobile";
const GIT_AUTHOR_EMAIL = "moo@mendix.com";
// Native Template Repo Settings
const NT_REPO_OWNER = process.env.GITHUB_REPOSITORY_OWNER;
const NT_REPO_NAME = process.env.GITHUB_REPOSITORY.split("/")[1];
const NT_CHANGELOG_BRANCH_NAME = `update-changelog-v${NATIVE_TEMPLATE_VERSION}`;
// Docs Repo Settings
const DOCS_REPO_NAME = "docs";
const DOCS_REPO_OWNER = "MendixMobile";
const DOCS_UPSTREAM_OWNER = "mendix";
const DOCS_BRANCH_NAME = `update-native-template-release-notes-v${NATIVE_TEMPLATE_VERSION}`;
const DOCS_PARENT_DIR = `content/en/docs/releasenotes/mobile/native-template/nt-studio-pro-${STUDIO_PRO_MAJOR}-parent`;
const TARGET_FILE = `${DOCS_PARENT_DIR}/nt-${NATIVE_TEMPLATE_MAJOR}-rn.md`;
const octokit = new Octokit({ auth: MENDIX_MOBILE_DOCS_PR_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}`;
}
function extractUnreleasedChangelog() {
const changelogPath = path.resolve(
path.join(__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 buildFrontmatter() {
return `---\ntitle: "Native Template ${NATIVE_TEMPLATE_MAJOR}"\nurl: /releasenotes/mobile/nt-${NATIVE_TEMPLATE_MAJOR}-rn/\nweight: 1\ndescription: "Native Template ${NATIVE_TEMPLATE_MAJOR}"\n---`;
}
// Changelog Update for Native Template Repo
function updateChangelog({ changelog, unreleasedContent, changelogPath }) {
const today = getToday();
const newSection = `## [${NATIVE_TEMPLATE_VERSION}] - ${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");
}
// Raise a PR to update the CHANGELOG.md in the native-template repo
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(NT_CHANGELOG_BRANCH_NAME);
await git.add("CHANGELOG.md");
await git.commit(`chore: update CHANGELOG for v${NATIVE_TEMPLATE_VERSION}`);
await git.push("origin", NT_CHANGELOG_BRANCH_NAME, ["--force"]);
const prBody = `
Automated update of CHANGELOG.md for v${NATIVE_TEMPLATE_VERSION}.
This PR moves the \`[Unreleased]\` section content to a new versioned section \`[${NATIVE_TEMPLATE_VERSION}]\`.
---
**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: NT_REPO_OWNER,
repo: NT_REPO_NAME,
title: `Update CHANGELOG for v${NATIVE_TEMPLATE_VERSION}`,
head: NT_CHANGELOG_BRANCH_NAME,
base: currentBranch,
body: prBody,
draft: true,
});
}
async function updateNTChangelog(changelog, unreleasedContent, changelogPath) {
try {
updateChangelog({ changelog, unreleasedContent, changelogPath });
await createPRUpdateChangelog();
} catch (err) {
console.error("❌ Updating NT Changelog failed:", err);
process.exit(1);
}
}
// Docs
function injectUnreleasedToDoc(docPath, unreleasedContent) {
if (!fs.existsSync(DOCS_PARENT_DIR)) {
console.log(`Parent directory not found. Creating: ${DOCS_PARENT_DIR}`);
fs.mkdirSync(DOCS_PARENT_DIR, { recursive: true });
}
const date = new Date();
const formattedDate = date.toLocaleDateString("en-US", {
year: "numeric",
month: "short",
day: "numeric",
});
const releaseHeading = `## ${NATIVE_TEMPLATE_VERSION}\n\n**Release date: ${formattedDate}**`;
if (!fs.existsSync(docPath)) {
console.log(`${docPath} not found — creating new file for Native Template ${NATIVE_TEMPLATE_MAJOR}.`);
return `${buildFrontmatter()}\n\n${releaseHeading}\n\n${unreleasedContent}\n`;
}
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 firstReleaseHeadingIndex = rest.search(/^##\s+\d+\.\d+\.\d+/m);
const beforeReleases =
firstReleaseHeadingIndex > 0
? `${rest.slice(0, firstReleaseHeadingIndex).trimEnd()}\n\n`
: "";
const releaseSections =
firstReleaseHeadingIndex >= 0
? rest.slice(firstReleaseHeadingIndex).trimStart()
: rest.trimStart();
return `${frontmatter}\n\n${beforeReleases}${releaseHeading}\n\n${unreleasedContent}\n\n${releaseSections}`;
}
async function cloneDocsRepo() {
const git = simpleGit();
const docsCloneDir = fs.mkdtempSync(
path.join(os.tmpdir(), DOCS_CLONE_PREFIX),
);
await git.clone(
`https://x-access-token:${MENDIX_MOBILE_DOCS_PR_GITHUB_PAT}@github.com/${DOCS_REPO_OWNER}/${DOCS_REPO_NAME}.git`,
docsCloneDir,
["--depth", "1"],
);
process.chdir(docsCloneDir);
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 updateDocsNTReleaseNotes(unreleasedContent) {
const newDocContent = injectUnreleasedToDoc(TARGET_FILE, unreleasedContent);
fs.writeFileSync(TARGET_FILE, newDocContent, "utf-8");
}
async function createPRUpdateDocsNTReleaseNotes(git) {
await git.add(TARGET_FILE);
await git.commit(
`docs: update mobile release notes for v${NATIVE_TEMPLATE_VERSION}`,
);
await git.push("origin", DOCS_BRANCH_NAME, ["--force"]);
const prBody = `
Automated sync of the latest release notes for v${NATIVE_TEMPLATE_VERSION} from [native-template](https://github.com/mendix/native-template).
---
**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${NATIVE_TEMPLATE_VERSION}`,
head: `${DOCS_REPO_OWNER}:${DOCS_BRANCH_NAME}`,
base: "development",
body: prBody,
draft: true,
});
}
// Update NT Release Notes in Docs repo
async function updateNTReleaseNotes(unreleasedContent) {
try {
await cloneDocsRepo();
const git = simpleGit();
await checkoutLocalBranch(git);
updateDocsNTReleaseNotes(unreleasedContent);
await createPRUpdateDocsNTReleaseNotes(git);
} catch (err) {
console.error("❌ Updating NT Release Notes in Docs failed:", err);
process.exit(1);
}
}
function readVersionFromPackageJson() {
const packageJsonPath = path.resolve(
path.join(__dirname, "..", "..", "package.json"),
);
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
return packageJson.version;
}
(async () => {
const { changelog, unreleasedContent, changelogPath } =
extractUnreleasedChangelog();
// Update CHANGELOG.md in native-template repo
await updateNTChangelog(changelog, unreleasedContent, changelogPath);
// Update release notes in docs repo
await updateNTReleaseNotes(unreleasedContent);
})();