-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
index.ts
113 lines (100 loc) · 3.12 KB
/
index.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
import changelogFilenameRegex from 'changelog-filename-regex';
import { logger } from '../../../../logger';
import type {
GithubGitBlob,
GithubGitTree,
} from '../../../../types/platform/github';
import { GithubHttp } from '../../../../util/http/github';
import { ensureTrailingSlash } from '../../../../util/url';
import type { ChangeLogFile, ChangeLogNotes } from '../types';
const http = new GithubHttp();
export async function getTags(
endpoint: string,
repository: string
): Promise<string[]> {
logger.trace('github.getTags()');
const url = `${endpoint}repos/${repository}/tags?per_page=100`;
try {
const res = await http.getJson<{ name: string }[]>(url, {
paginate: true,
});
const tags = res.body;
if (!tags.length) {
logger.debug({ repository }, 'repository has no Github tags');
}
return tags.map((tag) => tag.name).filter(Boolean);
} catch (err) {
logger.debug({ sourceRepo: repository }, 'Failed to fetch Github tags');
logger.debug({ err });
// istanbul ignore if
if (err.message?.includes('Bad credentials')) {
logger.warn('Bad credentials triggering tag fail lookup in changelog');
throw err;
}
return [];
}
}
export async function getReleaseNotesMd(
repository: string,
apiBaseUrl: string
): Promise<ChangeLogFile> | null {
logger.trace('github.getReleaseNotesMd()');
const apiPrefix = `${ensureTrailingSlash(apiBaseUrl)}repos/${repository}`;
const { default_branch: defaultBranch = 'master' } = (
await http.getJson<{ default_branch: string }>(apiPrefix)
).body;
// https://docs.github.com/en/rest/reference/git#get-a-tree
const res = await http.getJson<GithubGitTree>(
`${apiPrefix}/git/trees/${defaultBranch}`
);
// istanbul ignore if
if (res.body.truncated) {
logger.debug({ repository }, 'Git tree truncated');
}
const files = res.body.tree
.filter((f) => f.type === 'blob')
.filter((f) => changelogFilenameRegex.test(f.path));
if (!files.length) {
logger.trace('no changelog file found');
return null;
}
const { path: changelogFile, sha } = files.shift();
/* istanbul ignore if */
if (files.length !== 0) {
logger.debug(
`Multiple candidates for changelog file, using ${changelogFile}`
);
}
// https://docs.github.com/en/rest/reference/git#get-a-blob
const fileRes = await http.getJson<GithubGitBlob>(
`${apiPrefix}/git/blobs/${sha}`
);
const changelogMd =
Buffer.from(fileRes.body.content, 'base64').toString() + '\n#\n##';
return { changelogFile, changelogMd };
}
export async function getReleaseList(
apiBaseUrl: string,
repository: string
): Promise<ChangeLogNotes[]> {
logger.trace('github.getReleaseList()');
const url = `${ensureTrailingSlash(
apiBaseUrl
)}repos/${repository}/releases?per_page=100`;
const res = await http.getJson<
{
html_url: string;
id: number;
tag_name: string;
name: string;
body: string;
}[]
>(url, { paginate: true });
return res.body.map((release) => ({
url: release.html_url,
id: release.id,
tag: release.tag_name,
name: release.name,
body: release.body,
}));
}