-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (106 loc) · 2.98 KB
/
index.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
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
import path from 'node:path';
import fs from 'node:fs';
import createClient from './client.js';
const readySteady = async ({owner, repo, tag, force = false, files} = {}) => {
const client = createClient();
if (!await isTagAvailable({client, owner, repo, tag})) {
throw new Error(`tag is not available: ${tag}`);
}
const releaseName = tag.slice(1);
const existingDraftRelease = await getExistingDraftRelease({client, owner, repo, releaseName});
if (existingDraftRelease) {
if (force) {
await deleteDraftRelease({client, owner, repo, release: existingDraftRelease});
} else {
throw new Error('existing draft release found, and force not used');
}
}
const newDraftRelease = await createDraftRelease({client, owner, repo, tag, releaseName});
await uploadFilesToDraftRelease({client, fs, owner, repo, release: newDraftRelease, files});
return getExistingDraftRelease({client, owner, repo, releaseName});
};
const isTagAvailable = async ({client, owner, repo, tag} = {}) => {
try {
await client.rest.repos.getReleaseByTag({
owner,
repo,
tag,
});
return false;
} catch {
return true;
}
};
const getExistingDraftRelease = async ({client, owner, repo, releaseName} = {}) => {
const iterator = client.paginate.iterator(client.rest.repos.listReleases, {
owner,
repo,
per_page: 100, // eslint-disable-line camelcase
});
let existingDraft = null;
for await (const {data: releases} of iterator) {
if (existingDraft !== null) {
break;
}
for (const release of releases) {
if (existingDraft !== null) {
break;
}
if (release.name === releaseName && release.draft) {
existingDraft = release;
}
}
}
return existingDraft;
};
const deleteDraftRelease = async ({client, owner, repo, release} = {}) => {
if (release.draft === false) {
throw new Error('release is not a draft');
}
await client.rest.repos.deleteRelease({
owner,
repo,
release_id: release.id, // eslint-disable-line camelcase
});
return true;
};
const createDraftRelease = async ({client, owner, repo, tag, releaseName} = {}) => {
const release = await client.rest.repos.createRelease({
owner,
repo,
tag_name: tag, // eslint-disable-line camelcase
name: releaseName,
draft: true,
prerelease: false,
});
return release;
};
const uploadFilesToDraftRelease = async ({client, fs, owner, repo, release, files} = {}) => {
if (release.data.draft === false) {
throw new Error('release is not a draft');
}
const uploads = [];
for (const file of files) {
const fileData = fs.readFileSync(file);
const stats = fs.statSync(file);
uploads.push(client.rest.repos.uploadReleaseAsset({
owner,
repo,
release_id: release.data.id, // eslint-disable-line camelcase
name: path.basename(file),
data: fileData,
headers: {
'Content-Length': stats.size,
},
}));
}
return Promise.all(uploads);
};
export default readySteady;
export {
isTagAvailable,
getExistingDraftRelease,
deleteDraftRelease,
createDraftRelease,
uploadFilesToDraftRelease,
};