-
Notifications
You must be signed in to change notification settings - Fork 30
/
createGitHubRelease.js
111 lines (102 loc) · 3.22 KB
/
createGitHubRelease.js
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
import path from 'path';
import fs from 'fs';
import globby from 'globby';
import { Octokit } from '@octokit/rest';
import mime from 'mime-types';
import { getRepoInfo } from 'shipjs-lib';
import runStep from '../runStep';
import { getChangelog } from '../../helper';
import { arrayify, print } from '../../util';
export default async ({ version, config, dir, dryRun }) =>
await runStep(
{
title: 'Creating a release on GitHub repository',
},
async () => {
const {
remote,
getTagName,
releases: { assetsToUpload, extractChangelog } = {},
} = config;
const tagNames = arrayify(getTagName({ version }));
for await (const tagName of tagNames) {
// extract matching changelog
const getChangelogFn = extractChangelog || getChangelog;
const changelog = getChangelogFn({ version, dir });
const content = changelog || '';
// handle assets
const assetPaths = await getAssetPaths({
assetsToUpload,
dir,
version,
tagName,
});
if (dryRun) {
print('Creating a release with the following:');
print(` - content: ${content}`);
if (assetPaths.length > 0) {
print(` - assets: ${assetPaths.join(' ')}`);
}
return;
}
const { owner, name: repo } = getRepoInfo(remote, dir);
const octokit = new Octokit({
auth: `token ${process.env.GITHUB_TOKEN}`,
});
const {
data: { upload_url }, // eslint-disable-line camelcase
} = await octokit.repos.createRelease({
owner,
repo,
tag_name: tagName, // eslint-disable-line camelcase
name: tagName,
body: content,
});
if (assetPaths.length > 0) {
for (const assetPath of assetPaths) {
const file = path.resolve(dir, assetPath);
octokit.repos.uploadReleaseAsset({
file: fs.readFileSync(file),
headers: {
'content-length': fs.statSync(file).size,
'content-type': mime.lookup(file),
},
name: path.basename(file),
url: upload_url, // eslint-disable-line camelcase
});
}
}
}
}
);
async function getAssetPaths({ assetsToUpload, dir, version, tagName }) {
if (!assetsToUpload) {
return [];
}
const assetPaths = [];
if (typeof assetsToUpload === 'function') {
// function
// assetsToUpload: ({dir, version, tagName}) => [...]
const files = await Promise.resolve(
assetsToUpload({ dir, version, tagName })
);
assetPaths.push(...files);
} else if (Array.isArray(assetsToUpload) && assetsToUpload.length > 0) {
// list
// assetsToUpload: ['package.json', 'dist/*.zip']
for (const asset of assetsToUpload) {
const files = await globby(asset, { cwd: dir });
if (files) {
assetPaths.push(...files);
}
}
} else if (typeof assetsToUpload === 'string') {
// string
// assetsToUpload: 'archive.zip'
const files = await globby(assetsToUpload, { cwd: dir });
if (files) {
assetPaths.push(...files);
}
}
return assetPaths;
}