-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
source-github.ts
181 lines (167 loc) · 5.36 KB
/
source-github.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
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
import URL from 'url';
import { PLATFORM_TYPE_GITHUB } from '../../../constants/platforms';
import { Release } from '../../../datasource';
import { logger } from '../../../logger';
import * as globalCache from '../../../util/cache/global';
import * as hostRules from '../../../util/host-rules';
import { GithubHttp } from '../../../util/http/github';
import * as allVersioning from '../../../versioning';
import { BranchUpgradeConfig } from '../../common';
import { ChangeLogError, ChangeLogRelease, ChangeLogResult } from './common';
import { addReleaseNotes } from './release-notes';
const http = new GithubHttp();
async function getTags(
endpoint: string,
repository: string
): Promise<string[]> {
const url = `${endpoint}repos/${repository}/tags?per_page=100`;
try {
const res = await http.getJson<{ name: string }[]>(url, {
paginate: true,
});
const tags = (res && 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 && err.message.includes('Bad credentials')) {
logger.warn('Bad credentials triggering tag fail lookup in changelog');
throw err;
}
return [];
}
}
export async function getChangeLogJSON({
versioning,
fromVersion,
toVersion,
sourceUrl,
releases,
depName,
manager,
}: BranchUpgradeConfig): Promise<ChangeLogResult | null> {
if (sourceUrl === 'https://github.com/DefinitelyTyped/DefinitelyTyped') {
logger.debug('No release notes for @types');
return null;
}
const version = allVersioning.get(versioning);
const { protocol, host, pathname } = URL.parse(sourceUrl);
const baseUrl = `${protocol}//${host}/`;
const url = sourceUrl.startsWith('https://github.com/')
? 'https://api.github.com/'
: sourceUrl;
const config = hostRules.find({
hostType: PLATFORM_TYPE_GITHUB,
url,
});
// istanbul ignore if
if (!config.token) {
if (host.endsWith('github.com')) {
logger.warn(
{ manager, depName, sourceUrl },
'No github.com token has been configured. Skipping release notes retrieval'
);
return { error: ChangeLogError.MissingGithubToken };
}
logger.debug(
{ manager, depName, sourceUrl },
'Repository URL does not match any known hosts - skipping changelog retrieval'
);
return null;
}
const apiBaseUrl = sourceUrl.startsWith('https://github.com/')
? 'https://api.github.com/'
: baseUrl + 'api/v3/';
const repository = pathname.slice(1).replace(/\/$/, '');
if (repository.split('/').length !== 2) {
logger.debug({ sourceUrl }, 'Invalid github URL found');
return null;
}
if (!(releases && releases.length)) {
logger.debug('No releases');
return null;
}
// This extra filter/sort should not be necessary, but better safe than sorry
const validReleases = [...releases]
.filter((release) => version.isVersion(release.version))
.sort((a, b) => version.sortVersions(a.version, b.version));
if (validReleases.length < 2) {
logger.debug('Not enough valid releases');
return null;
}
let tags: string[];
async function getRef(release: Release): Promise<string | null> {
if (!tags) {
tags = await getTags(apiBaseUrl, repository);
}
const regex = new RegExp(`${depName}[@-]`);
const tagName = tags
.filter((tag) => version.isVersion(tag.replace(regex, '')))
.find((tag) => version.equals(tag.replace(regex, ''), release.version));
if (tagName) {
return tagName;
}
if (release.gitRef) {
return release.gitRef;
}
return null;
}
const cacheNamespace = 'changelog-github-release';
function getCacheKey(prev: string, next: string): string {
return `${manager}:${depName}:${prev}:${next}`;
}
const changelogReleases: ChangeLogRelease[] = [];
// compare versions
const include = (v: string): boolean =>
version.isGreaterThan(v, fromVersion) &&
!version.isGreaterThan(v, toVersion);
for (let i = 1; i < validReleases.length; i += 1) {
const prev = validReleases[i - 1];
const next = validReleases[i];
if (include(next.version)) {
let release = await globalCache.get(
cacheNamespace,
getCacheKey(prev.version, next.version)
);
// istanbul ignore else
if (!release) {
release = {
version: next.version,
date: next.releaseTimestamp,
// put empty changes so that existing templates won't break
changes: [],
compare: {},
};
const prevHead = await getRef(prev);
const nextHead = await getRef(next);
if (prevHead && nextHead) {
release.compare.url = `${baseUrl}${repository}/compare/${prevHead}...${nextHead}`;
}
const cacheMinutes = 55;
await globalCache.set(
cacheNamespace,
getCacheKey(prev.version, next.version),
release,
cacheMinutes
);
}
changelogReleases.unshift(release);
}
}
let res: ChangeLogResult = {
project: {
apiBaseUrl,
baseUrl,
github: repository,
repository: sourceUrl,
depName,
},
versions: changelogReleases,
};
res = await addReleaseNotes(res);
return res;
}