-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathutils.ts
More file actions
132 lines (122 loc) · 3.65 KB
/
utils.ts
File metadata and controls
132 lines (122 loc) · 3.65 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
import { CONFIG_GIT_URL_UNAVAILABLE } from '../../../constants/error-messages';
import { logger } from '../../../logger';
import type { BranchStatus, PrState } from '../../../types';
import * as hostRules from '../../../util/host-rules';
import { regEx } from '../../../util/regex';
import { joinUrlParts, parseUrl } from '../../../util/url';
import { hashBody } from '../pr-body';
import type { Pr } from '../types';
import type {
GerritChange,
GerritChangeStatus,
GerritLabelTypeInfo,
} from './types';
export const TAG_PULL_REQUEST_BODY = 'pull-request';
export function getGerritRepoUrl(repository: string, endpoint: string): string {
// Find options for current host and determine Git endpoint
const opts = hostRules.find({
hostType: 'gerrit',
url: endpoint,
});
const url = parseUrl(endpoint);
if (!url) {
throw new Error(CONFIG_GIT_URL_UNAVAILABLE);
}
if (!(opts.username && opts.password)) {
throw new Error(
'Init: You must configure a Gerrit Server username/password',
);
}
url.username = opts.username;
url.password = opts.password;
url.pathname = joinUrlParts(
url.pathname,
'a',
encodeURIComponent(repository),
);
logger.trace(
{ url: url.toString() },
'using URL based on configured endpoint',
);
return url.toString();
}
export function mapPrStateToGerritFilter(state?: PrState): string {
switch (state) {
case 'closed':
return 'status:closed';
case 'merged':
return 'status:merged';
case '!open':
return '-status:open';
case 'open':
return 'status:open';
case 'all':
default:
return '-is:wip';
}
}
export function mapGerritChangeToPr(change: GerritChange): Pr {
return {
number: change._number,
state: mapGerritChangeStateToPrState(change.status),
sourceBranch: extractSourceBranch(change) ?? change.branch,
targetBranch: change.branch,
title: change.subject,
createdAt: change.created?.replace(' ', 'T'),
reviewers:
change.reviewers?.REVIEWER?.filter(
(reviewer) => typeof reviewer.username === 'string',
).map((reviewer) => reviewer.username!) ?? [],
bodyStruct: {
hash: hashBody(findPullRequestBody(change)),
},
};
}
export function mapGerritChangeStateToPrState(
state: GerritChangeStatus | 'UNKNOWN', // suppress default path code removal
): PrState {
switch (state) {
case 'NEW':
return 'open';
case 'MERGED':
return 'merged';
case 'ABANDONED':
return 'closed';
}
return 'all';
}
export function extractSourceBranch(change: GerritChange): string | undefined {
let sourceBranch: string | undefined = undefined;
if (change.current_revision) {
const re = regEx(/^Renovate-Branch: (.+)$/m);
const message = change.revisions[change.current_revision]?.commit?.message;
if (message) {
sourceBranch = re.exec(message)?.[1];
}
}
return sourceBranch ?? undefined;
}
export function findPullRequestBody(change: GerritChange): string | undefined {
const msg = Array.from(change.messages ?? [])
.reverse()
.find((msg) => msg.tag === TAG_PULL_REQUEST_BODY);
if (msg) {
return msg.message.replace(/^Patch Set \d+:\n\n/, ''); //TODO: check how to get rid of the auto-added prefix?
}
return undefined;
}
export function mapBranchStatusToLabel(
state: BranchStatus | 'UNKNOWN', // suppress default path code removal
label: GerritLabelTypeInfo,
): number {
const numbers = Object.keys(label.values).map((x) => parseInt(x, 10));
switch (state) {
case 'green':
return Math.max(...numbers);
case 'yellow':
case 'red':
return Math.min(...numbers);
}
/* v8 ignore next */
return label.default_value;
}