Welcome to mirror list, hosted at ThFree Co, Russian Federation.

publish-release.js « scripts - github.com/sualko/cloud_piwik.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 59ee4f1b8fa912b21f78cb411a0d707e3d7d0e23 (plain)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* eslint-disable @typescript-eslint/no-var-requires */
require('colors').setTheme({
	verbose: 'cyan',
	warn: 'yellow',
	error: 'red',
});

const fs = require('fs');
const path = require('path');
const https = require('https');
const { Octokit } = require('@octokit/rest');
const execa = require('execa');
const inquirer = require('inquirer');
const git = require('simple-git/promise')();
const package = require('../package.json');

require('dotenv').config();

const isDryRun = process.argv.indexOf('--dry-run') > 1;
const commitMessage = `release: ${package.version} :tada:`;
const tagName = `v${package.version}`;
const files = [
	path.join(__dirname, '..', 'archives', `cloud_piwik-${package.version}.tar.gz`),
	path.join(__dirname, '..', 'archives', `cloud_piwik-${package.version}.tar.gz.asc`),
	path.join(__dirname, '..', 'archives', `cloud_piwik-${package.version}.tar.gz.ncsig`),
	path.join(__dirname, '..', 'archives', `cloud_piwik-${package.version}.tar.gz.sig`),
];

function pull() {
	return git.pull('origin', 'master');
}

async function notAlreadyTagged() {
	if ((await git.tags()).all.includes(tagName)) {
		throw 'version already tagged';
	}
}

async function lastCommitNotBuild() {
	return (await git.log(['-1'])).latest.message !== commitMessage;
}

async function isMasterBranch() {
	return (await git.branch()) === 'master';
}

async function editChangeLog(changeLog) {
	const answers = await inquirer.prompt([{
		type: 'editor',
		name: 'changeLog',
		message: 'You have now the possibility to edit the change log',
		default: changeLog,
	}]);

	return answers.changeLog;
}

async function hasArchiveAndSignatures() {
	return files.map(file => fs.existsSync(file)).indexOf(false) < 0;
}

async function stageAllFiles() {
	if (isDryRun) {
		return;
	}

	const gitProcess = execa('git', ['add', '-u']);

	gitProcess.stdout.pipe(process.stdout);

	return gitProcess;
}

function showStagedDiff() {
	const gitProcess = execa('git', ['diff', '--staged']);

	gitProcess.stdout.pipe(process.stdout);

	return gitProcess;
}

async function keypress() {
	return inquirer.prompt([{
		type: 'input',
		name: 'keypress',
		message: 'Press any key to continue... (where is the any key?)',
	}]);
}

function commit() {
	if (isDryRun) {
		return;
	}

	return git.commit(commitMessage, ['-S', '-n']);
}

async function wantToContinue(message) {
	const answers = await inquirer.prompt([{
		type: 'confirm',
		name: 'continue',
		message,
		default: false,
	}]);

	if (!answers.continue) {
		process.exit(10);
	}
}

function push() {
	if (isDryRun) {
		return;
	}

	return git.push('origin', 'master');
}

async function createGithubRelease(changeLog) {
	if (!process.env.GITHUB_TOKEN) {
		throw 'Github token missing';
	}

	const octokit = new Octokit({
		auth: process.env.GITHUB_TOKEN,
		userAgent: 'custom releaser for sualko/cloud_piwik',
	});

	const origin = (await git.remote(['get-url', 'origin'])).trim();
	const matches = origin.match(/^git@github\.com:(.+)\/(.+)\.git$/);

	if (!matches) {
		throw 'Origin is not configured or no ssh url';
	}

	const owner = matches[1];
	const repo = matches[2];
	const releaseOptions = {
		owner,
		repo,
		// eslint-disable-next-line @typescript-eslint/camelcase
		tag_name: tagName,
		name: `Matomo Integration ${tagName}`,
		body: changeLog.replace(/^## [^\n]+\n/, ''),
		prerelease: !/^\d+\.\d+\.\d+$/.test(package.version),
	};

	if (isDryRun) {
		console.log('github release options', releaseOptions);
		return [];
	}

	const releaseResponse = await octokit.repos.createRelease(releaseOptions);

	console.log(`Release created, see ${releaseResponse.data.html_url}`.verbose);

	function getMimeType(filename) {
		if (filename.endsWith('.asc') || filename.endsWith('sig')) {
			return 'application/pgp-signature';
		}

		if (filename.endsWith('.tar.gz')) {
			return 'application/gzip';
		}

		if (filename.endsWith('.ncsig')) {
			return 'text/plain';
		}

		return 'application/octet-stream';
	}

	const assetUrls = await Promise.all(files.map(async file => {
		const filename = path.basename(file);
		const uploadOptions = {
			owner,
			repo,
			// eslint-disable-next-line @typescript-eslint/camelcase
			release_id: releaseResponse.data.id,
			data: fs.createReadStream(file),
			headers: {
				'content-type': getMimeType(filename),
				'content-length': fs.statSync(file)['size'],
			},
			name: filename,
		};

		const assetResponse = await octokit.repos.uploadReleaseAsset(uploadOptions);

		console.log(`Asset uploaded: ${assetResponse.data.name}`.verbose);

		return assetResponse.data.browser_download_url;
	}));

	return assetUrls;
}

async function uploadToNextcloudStore(archiveUrl) {
	if(!process.env.NEXTCLOUD_TOKEN) {
		throw 'Nextcloud token missing';
	}

	const hostname = 'apps.nextcloud.com';
	const apiEndpoint = '/api/v1/apps/releases';
	const signatureFile = files.find(file => file.endsWith('.ncsig'));
	const data = JSON.stringify({
		download: archiveUrl,
		signature: fs.readFileSync(signatureFile, 'utf-8'),
		nightly: false,
	});
	const options = {
		hostname,
		port: 443,
		path: apiEndpoint,
		method: 'POST',
		headers: {
			'Content-Type': 'application/json',
			'Content-Length': data.length,
			Authorization: `Token ${process.env.NEXTCLOUD_TOKEN}`,
		},
	};

	if (isDryRun) {
		console.log('nextcloud app store request', options, data);
		return;
	}

	return new Promise((resolve, reject) => {
		const req = https.request(options, res => {
			if (res.statusCode === 200) {
				console.log('App release was updated successfully'.verbose);
				resolve();
			} else if (res.statusCode === 201) {
				console.log('App release was created successfully'.verbose);
				resolve();
			} else if (res.statusCode === 400) {
				reject('App release was not accepted');
			} else {
				reject('App release rejected with status ' + res.statusCode);
			}

			res.on('data', d => {
				process.stdout.write(d);
			});
		});

		req.on('error', error => {
			reject(error);
		});

		req.write(data);
		req.end();
	});
}

async function run() {
	if (isDryRun) {
		console.log('INFO: You executed this script in dry run mode.'.verbose);
	}

	await pull();
	console.log('✔ pulled latest changes'.green);

	await notAlreadyTagged();
	console.log('✔ not already tagged'.green);

	await lastCommitNotBuild();
	console.log('✔ last commit is no build commit'.green);

	await isMasterBranch();
	console.log('✔ this is the master branch'.green);

	let changeLog = await editChangeLog('');
	console.log('✔ change log updated'.green);

	console.log(changeLog);

	console.log('Press any key to continue...');
	await keypress();

	await hasArchiveAndSignatures();
	console.log('✔ found archive and signatures'.green);

	await stageAllFiles();
	console.log('✔ all files staged'.green);

	await showStagedDiff();

	await wantToContinue('Should I commit those changes?');

	await commit();
	console.log('✔ All files commited'.green);

	await wantToContinue('Should I push all pending commits?');

	await push();
	console.log('✔ All commits pushed'.green);

	await wantToContinue('Should I continue to create a Github release?');

	const assetUrls = await createGithubRelease(changeLog);
	console.log('✔ released on github'.green);

	const archiveAssetUrl = assetUrls.find(url => url.endsWith('.tar.gz'));
	console.log(`Asset url for Nextcloud store: ${archiveAssetUrl}`.verbose);

	await wantToContinue('Should I continue to upload the release to the app store?');

	await uploadToNextcloudStore(archiveAssetUrl);
	console.log('✔ released in Nextcloud app store'.green);
};

run().catch(err => {
	console.log(`✘ ${err.toString()}`.error);
});