-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathGithub.ts
More file actions
212 lines (195 loc) · 5.63 KB
/
Copy pathGithub.ts
File metadata and controls
212 lines (195 loc) · 5.63 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import fs from "fs";
import path from "path";
import mime from "mime-types";
import retry from "async-retry";
import { Octokit } from "@octokit/rest";
import { getRepoSlugFromManifest } from "./getRepoSlugFromManifest";
export class Github {
octokit: Octokit;
owner: string;
repo: string;
repoSlug: string;
constructor(dir: string) {
const repoSlug =
getRepoSlugFromManifest(dir) ||
process.env.TRAVIS_REPO_SLUG ||
process.env.GITHUB_REPOSITORY;
if (!repoSlug)
throw Error(
"manifest.repository must be properly defined to create a Github release"
);
const [owner, repo] = repoSlug.split("/");
if (!owner) throw Error(`repoSlug "${repoSlug}" hasn't an owner`);
if (!repo) throw Error(`repoSlug "${repoSlug}" hasn't a repo`);
this.owner = owner;
this.repo = repo;
this.repoSlug = repoSlug;
// OAuth2 token from Github
if (!process.env.GITHUB_TOKEN)
throw Error("GITHUB_TOKEN ENV (OAuth2) is required");
this.octokit = new Octokit({
auth: `token ${process.env.GITHUB_TOKEN}`
});
}
async assertRepoExists(): Promise<void> {
try {
await this.octokit.repos.get({ owner: this.owner, repo: this.repo });
} catch (e) {
if (e.status === 404)
throw Error(
`Repo does not exist: ${this.repoSlug}. Check the manifest.repository object and correct the repo URL`
);
e.message = `Error verifying repo ${this.repoSlug}: ${e.message}`;
throw e;
}
}
/**
* Deletes a tag only if exists, does not throw on 404
* @param tag "v0.2.0", "release/patch"
*/
async deleteTagIfExists(tag: string): Promise<void> {
try {
await this.octokit.git.deleteRef({
owner: this.owner,
repo: this.repo,
ref: `tags/${tag}`
});
} catch (e) {
// Ignore error if the reference does not exist, can be deleted latter
if (!e.message.includes("Reference does not exist")) {
e.message = `Error deleting tag ${tag}: ${e.message}`;
throw e;
}
}
}
/**
* Creates a Github tag at a given commit sha
* @param tag "v0.2.0"
* @param sha "ffac537e6cbbf934b08745a378932722df287a53"
*/
async createTag(tag: string, sha: string): Promise<void> {
try {
await this.octokit.git.createRef({
owner: this.owner,
repo: this.repo,
ref: `refs/tags/${tag}`,
sha
});
} catch (e) {
e.message = `Error creating tag ${tag} at ${sha}: ${e.message}`;
throw e;
}
}
/**
* Removes all Github releases that match a tag, and it's assets
* @param tag "v0.2.0"
*/
async deteleReleaseAndAssets(tag: string): Promise<void> {
const releases = await this.octokit.repos
.listReleases({ owner: this.owner, repo: this.repo })
.then(res => res.data);
const matchingReleases = releases.filter(
({ tag_name, name }) => tag_name === tag || name === tag
);
for (const matchingRelease of matchingReleases) {
for (const asset of matchingRelease.assets)
try {
await this.octokit.repos.deleteReleaseAsset({
owner: this.owner,
repo: this.repo,
asset_id: asset.id
});
} catch (e) {
e.message = `Error deleting release asset: ${e.message}`;
throw e;
}
try {
await this.octokit.repos.deleteRelease({
owner: this.owner,
repo: this.repo,
release_id: matchingRelease.id
});
} catch (e) {
e.message = `Error deleting release: ${e.message}`;
throw e;
}
}
}
/**
* Create a Github release
* - With `assetsDir`, all its files will be uploaded as release assets
* @param tag "v0.2.0"
* @param options
*/
async createReleaseAndUploadAssets(
tag: string,
options?: {
body?: string;
prerelease?: boolean;
assetsDir?: string;
ignorePattern?: RegExp;
}
): Promise<void> {
const { body, prerelease, assetsDir, ignorePattern } = options || {};
const release = await this.octokit.repos
.createRelease({
owner: this.owner,
repo: this.repo,
tag_name: tag,
name: tag,
body,
prerelease
})
.catch(e => {
e.message = `Error creating release: ${e.message}`;
throw e;
});
if (assetsDir)
for (const file of fs.readdirSync(assetsDir)) {
// Used to ignore duplicated legacy .tar.xz image
if (ignorePattern && ignorePattern.test(file)) continue;
const filepath = path.resolve(assetsDir, file);
const contentType = mime.lookup(filepath) || "application/octet-stream";
try {
// The uploadReleaseAssetApi fails sometimes, retry 3 times
await retry(
async () => {
await this.octokit.repos.uploadReleaseAsset({
url: release.data.upload_url,
data: fs.createReadStream(filepath),
headers: {
"content-type": contentType,
"content-length": fs.statSync(filepath).size
},
name: path.basename(filepath)
});
},
{ retries: 3 }
);
} catch (e) {
e.message = `Error uploading release asset: ${e.message}`;
throw e;
}
}
}
/**
* Open a Github pull request
*/
async openPR({
from,
to,
title
}: {
from: string;
to: string;
title: string;
}): Promise<void> {
await this.octokit.pulls.create({
owner: this.owner,
repo: this.repo,
title,
head: from,
base: to
});
}
}