-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
postPreReleasesToForum.ts
185 lines (146 loc) · 5.42 KB
/
postPreReleasesToForum.ts
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
import { pathExists } from 'fs-extra';
import { readFile, writeFile } from 'fs/promises';
import { GitHubRelease, gitHubLatestReleases, gitHubLinkify } from './tool-utils';
import { config, createPost, createTopic, getForumTopPostByExternalId, getTopicByExternalId, updatePost } from './utils/discourse';
import { compareVersions } from 'compare-versions';
import dayjs = require('dayjs');
import { getRootDir } from '@joplin/utils';
interface State {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
processedReleases: Record<string, any>;
}
enum Platform {
Desktop = 'Desktop',
Android = 'Android',
iOS = 'iOS',
}
const stateFilePath = `${__dirname}/postPreReleasesToForum.json`;
const betaCategoryId = 10;
const loadState = async (): Promise<State> => {
if (await pathExists(stateFilePath)) {
const content = await readFile(stateFilePath, 'utf-8');
return JSON.parse(content) as State;
}
return {
processedReleases: {},
};
};
const saveState = async (state: State) => {
await writeFile(stateFilePath, JSON.stringify(state, null, '\t'), 'utf-8');
};
const getPatchVersion = (tagName: string) => {
if (tagName.includes('-')) tagName = tagName.split('-')[1];
return tagName.substring(1);
};
const getMinorVersion = (tagName: string) => {
const s = getPatchVersion(tagName).split('.');
return `${s[0]}.${s[1]}`;
};
const getExternalId = (platform: string, minorVersion: string) => {
let prefix = '';
if (platform !== Platform.Desktop) {
prefix = `${platform.toLowerCase()}-`;
}
return `prerelease-${prefix}${minorVersion.replace(/\./g, '-')}`;
};
const getDownloadInfo = (platform: Platform) => {
const infos: Record<Platform, string> = {
[Platform.Desktop]: 'Download the latest pre-releases from here: <https://github.com/laurent22/joplin/releases>',
[Platform.Android]: 'Download the latest pre-releases from here: <https://github.com/laurent22/joplin-android/tags>',
[Platform.iOS]: 'In order to try the iOS pre-release, you will need to join the Joplin iOS beta program from here: <https://testflight.apple.com/join/p5iLVzrG>',
};
return infos[platform];
};
const getReleasesFromMarkdown = async (filePath: string) => {
const content = await readFile(filePath, 'utf-8');
const lines = content.split('\n');
const releases: GitHubRelease[] = [];
let currentRelease: GitHubRelease = null;
for (let line of lines) {
line = line.trim();
if (!line) continue;
if (line.startsWith('##')) {
const matches = line.match(/## \[(.*?)\]/);
if (!matches) throw new Error(`Could not parse version: ${line}`);
const tag = matches[1];
currentRelease = {
tag_name: tag,
body: '',
assets: [],
draft: false,
html_url: `https://github.com/laurent22/joplin-android/releases/tag/${tag}`,
prerelease: false,
upload_url: '',
};
releases.push(currentRelease);
continue;
}
if (currentRelease) {
if (currentRelease.body) currentRelease.body += '\n';
currentRelease.body += line;
}
}
releases.sort((a, b) => compareVersions(getPatchVersion(a.tag_name), getPatchVersion(b.tag_name)));
return releases;
};
const processReleases = async (releases: GitHubRelease[], platform: Platform, startFromVersion: string, state: State) => {
for (const release of releases) {
const minorVersion = getMinorVersion(release.tag_name);
const patchVersion = getPatchVersion(release.tag_name);
if (compareVersions(startFromVersion, minorVersion) > 0) continue;
if (!state.processedReleases[release.tag_name]) {
console.info(`Processing release ${release.tag_name}`);
const externalId = getExternalId(platform, minorVersion);
const postBody = `## [v${patchVersion}](${release.html_url})\n\n${gitHubLinkify(release.body)}`;
let topic = await getTopicByExternalId(externalId);
const topicTitle = `${platform} pre-release v${minorVersion} is now available (Updated ${dayjs(new Date()).format('DD/MM/YYYY')})`;
if (!topic) {
console.info('No topic exists - creating one...');
topic = await createTopic({
title: topicTitle,
raw: `${getDownloadInfo(platform)}\n\n* * *\n\n${postBody}`,
category: betaCategoryId,
external_id: externalId,
});
} else {
console.info('A topic exists - appending the new pre-release to it...');
const topPost = await getForumTopPostByExternalId(externalId);
await updatePost(topPost.id, {
title: topicTitle,
raw: `${topPost.raw}\n\n${postBody}`,
edit_reason: 'Auto-updated by script',
});
await createPost(topic.id, {
raw: postBody,
});
}
state.processedReleases[release.tag_name] = true;
await saveState(state);
}
}
return state;
};
const main = async () => {
const rootDir = await getRootDir();
const argv = require('yargs').argv;
config.key = argv._[0];
config.username = argv._[1];
let state = await loadState();
{
const releases = await gitHubLatestReleases(1, 50);
releases.sort((a, b) => compareVersions(a.tag_name, b.tag_name));
state = await processReleases(releases, Platform.Desktop, '2.13', state);
}
{
const releases = await getReleasesFromMarkdown(`${rootDir}/readme/about/changelog/android.md`);
state = await processReleases(releases, Platform.Android, '2.14', state);
}
{
const releases = await getReleasesFromMarkdown(`${rootDir}/readme/about/changelog/ios.md`);
await processReleases(releases, Platform.iOS, '12.14', state);
}
};
main().catch((error) => {
console.error('Fatal error', error);
process.exit(1);
});