-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrelease-make-it-native.js
More file actions
196 lines (164 loc) · 6.36 KB
/
release-make-it-native.js
File metadata and controls
196 lines (164 loc) · 6.36 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
import fs from "fs";
import path from "path";
import { Octokit } from "@octokit/rest";
import simpleGit from "simple-git";
const docsRepo = {
owner: "MendixMobile",
repo: "docs",
};
const currentRepo = {
owner: "mendix",
repo: "make-it-native",
};
const VERSION = process.env.VERSION;
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const PAT = process.env.PAT || GITHUB_TOKEN;
if (!VERSION) {
console.error("VERSION env variable is required!");
process.exit(1);
}
const CHANGELOG_BRANCH = `changelog-update-v${VERSION}`;
// Docs specific variables
const RELEASE_NOTES_BRANCH_NAME = `update-mobile-release-notes-v${VERSION}`;
const localDocsRepoPath = path.join(process.cwd(), "../test/", docsRepo.repo);
const TARGET_FILE = `${localDocsRepoPath}/content/en/docs/releasenotes/mobile/make-it-native-parent/make-it-native-10.md`;
const octokit = new Octokit({ auth: GITHUB_TOKEN });
const docsOctokit = new Octokit({ auth: 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 extractUnreleased() {
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 = `## [${VERSION}] Make it Native - ${today}\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 createRelease(unreleasedContent) {
await octokit.git.createRef({
owner: process.env.GITHUB_REPOSITORY_OWNER,
repo: process.env.GITHUB_REPOSITORY.split("/")[1],
ref: `refs/tags/v${VERSION}`,
sha: process.env.GITHUB_SHA,
});
await octokit.repos.createRelease({
owner: process.env.GITHUB_REPOSITORY_OWNER,
repo: process.env.GITHUB_REPOSITORY.split("/")[1],
tag_name: `v${VERSION}`,
name: `v${VERSION}`,
body: unreleasedContent,
draft: false,
prerelease: false,
});
}
async function prChangelogUpdate() {
const git = simpleGit();
await git.checkoutLocalBranch(CHANGELOG_BRANCH);
await git.add("CHANGELOG.md");
await git.commit(`chore: update CHANGELOG for v${VERSION}`);
await git.push("origin", CHANGELOG_BRANCH, ["--force"]);
await octokit.pulls.create({
owner: currentRepo.owner,
repo: currentRepo.repo,
title: `Update CHANGELOG for v${VERSION}`,
head: CHANGELOG_BRANCH,
base: "development",
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,
});
process.chdir("..");
}
async function updateDocs(unreleasedContent) {
const currentPath = process.cwd();
console.log("Cloning docs repo");
const git = simpleGit();
await git
.outputHandler((command, stdout, stderr) => {
stdout.pipe(process.stdout);
stderr.pipe(process.stderr);
stdout.on("data", (data) => {
// Print data
console.log(data.toString("utf8"));
});
})
.clone(
`https://x-access-token:${PAT}@github.com/${docsRepo.owner}/${docsRepo.repo}.git`,
localDocsRepoPath,
{
"--depth": "1",
}
);
console.log("Cloned docs repo");
process.chdir(localDocsRepoPath);
const docsGit = simpleGit();
console.log("Checking out branch");
await docsGit.checkoutLocalBranch(RELEASE_NOTES_BRANCH_NAME);
console.log("Checked out branch");
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();
return `${frontmatter}\n\n${firstParagraph}\n${unreleasedContent}\n\n${afterFirstParagraph}`;
}
const newDocContent = injectUnreleasedToDoc(TARGET_FILE, unreleasedContent);
console.log("Writing new doc content");
fs.writeFileSync(TARGET_FILE, newDocContent, "utf-8");
console.log("New doc content written");
await docsGit.add(TARGET_FILE);
console.log("Added file");
await docsGit.commit(`docs: update mobile release notes for v${VERSION}`);
console.log("Committed file");
await docsGit.push("origin", RELEASE_NOTES_BRANCH_NAME, ["--force"]);
console.log("Pushed file");
const prBody = `
Automated sync of the latest release notes for v${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 docsOctokit.pulls.create({
owner: docsRepo.owner,
repo: docsRepo.repo,
title: `Update mobile app release notes for v${VERSION}`,
head: `${docsRepo.owner}:${RELEASE_NOTES_BRANCH_NAME}`,
base: "development",
body: prBody,
draft: true,
});
process.chdir(currentPath);
console.log("Changed directory");
}
(async () => {
const { changelog, unreleasedContent, changelogPath } = extractUnreleased();
await createRelease(unreleasedContent);
updateChangelog({ changelog, unreleasedContent, changelogPath });
await prChangelogUpdate();
await updateDocs(unreleasedContent);
console.log("Release, changelog update, and docs PR completed!");
})();