forked from renovatebot/renovate
/
source-github.ts
213 lines (201 loc) · 6.65 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
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
213
// TODO #7154
import URL from 'node:url';
import { GlobalConfig } from '../../../../../config/global';
import { logger } from '../../../../../logger';
import type { Release } from '../../../../../modules/datasource/types';
import * as allVersioning from '../../../../../modules/versioning';
import * as memCache from '../../../../../util/cache/memory';
import * as packageCache from '../../../../../util/cache/package';
import * as hostRules from '../../../../../util/host-rules';
import { regEx } from '../../../../../util/regex';
import type { BranchUpgradeConfig } from '../../../../types';
import { slugifyUrl } from './common';
import { getTags } from './github';
import { addReleaseNotes } from './release-notes';
import { getInRangeReleases } from './releases';
import type { ChangeLogRelease, ChangeLogResult } from './types';
function getCachedTags(
endpoint: string,
repository: string
): Promise<string[]> {
const cacheKey = `getTags-${endpoint}-${repository}`;
const cachedResult = memCache.get<Promise<string[]>>(cacheKey);
// istanbul ignore if
if (cachedResult !== undefined) {
return cachedResult;
}
const promisedRes = getTags(endpoint, repository);
memCache.set(cacheKey, promisedRes);
return promisedRes;
}
export async function getChangeLogJSON(
config: BranchUpgradeConfig
): Promise<ChangeLogResult | null> {
const versioning = config.versioning!;
const currentVersion = config.currentVersion!;
const newVersion = config.newVersion!;
const sourceUrl = config.sourceUrl!;
const sourceDirectory = config.sourceDirectory!;
const packageName = config.packageName!;
const manager = config.manager;
if (sourceUrl === 'https://github.com/DefinitelyTyped/DefinitelyTyped') {
logger.trace('No release notes for @types');
return null;
}
const version = allVersioning.get(versioning);
const { protocol, host, pathname } = URL.parse(sourceUrl);
// TODO: types (#7154)
const baseUrl = `${protocol!}//${host!}/`;
const url = sourceUrl.startsWith('https://github.com/')
? 'https://api.github.com/'
: sourceUrl;
const { token } = hostRules.find({
hostType: 'github',
url,
});
// istanbul ignore if
if (!token) {
if (host!.endsWith('.github.com') || host === 'github.com') {
if (!GlobalConfig.get('githubTokenWarn')) {
logger.debug(
{ manager, packageName, sourceUrl },
'GitHub token warning has been suppressed. Skipping release notes retrieval'
);
return null;
}
logger.warn(
{ manager, packageName, sourceUrl },
'No github.com token has been configured. Skipping release notes retrieval'
);
return { error: 'MissingGithubToken' };
}
logger.debug(
{ manager, packageName, sourceUrl },
'Repository URL does not match any known github 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(regEx(/\/$/), '')
.replace(regEx(/\.git$/), '');
if (repository.split('/').length !== 2) {
logger.debug(`Invalid github URL found: ${sourceUrl}`);
return null;
}
const releases = config.releases ?? (await getInRangeReleases(config));
if (!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 for dep ${packageName}`);
return null;
}
let tags: string[];
async function getRef(release: Release): Promise<string | null> {
if (!tags) {
tags = await getCachedTags(apiBaseUrl, repository);
}
const tagName = findTagOfRelease(
version,
packageName,
release.version,
tags
);
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 `${slugifyUrl(sourceUrl)}:${packageName}:${prev}:${next}`;
}
const changelogReleases: ChangeLogRelease[] = [];
// compare versions
const include = (v: string): boolean =>
version.isGreaterThan(v, currentVersion) &&
!version.isGreaterThan(v, newVersion);
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 packageCache.get(
cacheNamespace,
getCacheKey(prev.version, next.version)
);
// istanbul ignore else
if (!release) {
release = {
version: next.version,
gitRef: next.gitRef,
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 packageCache.set(
cacheNamespace,
getCacheKey(prev.version, next.version),
release,
cacheMinutes
);
}
changelogReleases.unshift(release);
}
}
let res: ChangeLogResult | null = {
project: {
apiBaseUrl,
baseUrl,
type: 'github',
repository,
sourceUrl,
sourceDirectory,
packageName,
},
versions: changelogReleases,
};
res = await addReleaseNotes(res, config);
return res;
}
function findTagOfRelease(
version: allVersioning.VersioningApi,
packageName: string,
depNewVersion: string,
tags: string[]
): string | undefined {
const regex = regEx(`(?:${packageName}|release)[@-]`, undefined, false);
const excactReleaseRegex = regEx(`${packageName}[@\\-_]v?${depNewVersion}`);
const exactTagsList = tags.filter((tag) => {
return excactReleaseRegex.test(tag);
});
let tagName: string | undefined;
if (exactTagsList.length) {
tagName = exactTagsList
.filter((tag) => version.isVersion(tag.replace(regex, '')))
.find((tag) => version.equals(tag.replace(regex, ''), depNewVersion));
} else {
tagName = tags
.filter((tag) => version.isVersion(tag.replace(regex, '')))
.find((tag) => version.equals(tag.replace(regex, ''), depNewVersion));
}
return tagName;
}