forked from renovatebot/renovate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ts
246 lines (220 loc) · 7.32 KB
/
client.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { REPOSITORY_ARCHIVED } from '../../../constants/error-messages';
import { logger } from '../../../logger';
import { GerritHttp } from '../../../util/http/gerrit';
import type {
GerritAccountInfo,
GerritBranchInfo,
GerritChange,
GerritChangeMessageInfo,
GerritFindPRConfig,
GerritMergeableInfo,
GerritProjectInfo,
} from './types';
import { mapPrStateToGerritFilter } from './utils';
class GerritClient {
private requestDetails = [
'SUBMITTABLE', //include the submittable field in ChangeInfo, which can be used to tell if the change is reviewed and ready for submit.
'CHECK', // include potential problems with the change.
'MESSAGES',
'DETAILED_ACCOUNTS',
'LABELS',
'CURRENT_ACTIONS', //to check if current_revision can be "rebased"
'CURRENT_REVISION', //get RevisionInfo::ref to fetch
] as const;
private gerritHttp = new GerritHttp();
async getRepos(): Promise<string[]> {
const res = await this.gerritHttp.getJson<string[]>(
'a/projects/?type=CODE&state=ACTIVE',
{},
);
return Object.keys(res.body);
}
async getProjectInfo(repository: string): Promise<GerritProjectInfo> {
const projectInfo = await this.gerritHttp.getJson<GerritProjectInfo>(
`a/projects/${encodeURIComponent(repository)}`,
);
if (projectInfo.body.state !== 'ACTIVE') {
throw new Error(REPOSITORY_ARCHIVED);
}
return projectInfo.body;
}
async getBranchInfo(repository: string): Promise<GerritBranchInfo> {
const branchInfo = await this.gerritHttp.getJson<GerritBranchInfo>(
`a/projects/${encodeURIComponent(repository)}/branches/HEAD`,
);
return branchInfo.body;
}
async findChanges(
repository: string,
findPRConfig: GerritFindPRConfig,
refreshCache?: boolean,
): Promise<GerritChange[]> {
const filters = GerritClient.buildSearchFilters(repository, findPRConfig);
const changes = await this.gerritHttp.getJson<GerritChange[]>(
`a/changes/?q=` +
filters.join('+') +
this.requestDetails.map((det) => `&o=${det}`).join(''),
{ memCache: !refreshCache },
);
logger.trace(
`findChanges(${filters.join(', ')}) => ${changes.body.length}`,
);
return changes.body;
}
async getChange(changeNumber: number): Promise<GerritChange> {
const changes = await this.gerritHttp.getJson<GerritChange>(
`a/changes/${changeNumber}?` +
this.requestDetails.map((det) => `o=${det}`).join('&'),
);
return changes.body;
}
async getMergeableInfo(change: GerritChange): Promise<GerritMergeableInfo> {
const mergeable = await this.gerritHttp.getJson<GerritMergeableInfo>(
`a/changes/${change._number}/revisions/current/mergeable`,
);
return mergeable.body;
}
async abandonChange(changeNumber: number): Promise<void> {
await this.gerritHttp.postJson(`a/changes/${changeNumber}/abandon`);
}
async submitChange(changeNumber: number): Promise<GerritChange> {
const change = await this.gerritHttp.postJson<GerritChange>(
`a/changes/${changeNumber}/submit`,
);
return change.body;
}
async setCommitMessage(changeNumber: number, message: string): Promise<void> {
await this.gerritHttp.putJson(`a/changes/${changeNumber}/message`, {
body: { message },
});
}
async updateCommitMessage(
number: number,
gerritChangeID: string,
prTitle: string,
): Promise<void> {
await this.setCommitMessage(
number,
`${prTitle}\n\nChange-Id: ${gerritChangeID}\n`,
);
}
async getMessages(changeNumber: number): Promise<GerritChangeMessageInfo[]> {
const messages = await this.gerritHttp.getJson<GerritChangeMessageInfo[]>(
`a/changes/${changeNumber}/messages`,
{ memCache: false },
);
return messages.body;
}
async addMessage(
changeNumber: number,
fullMessage: string,
tag?: string,
): Promise<void> {
const message = this.normalizeMessage(fullMessage);
await this.gerritHttp.postJson(
`a/changes/${changeNumber}/revisions/current/review`,
{ body: { message, tag } },
);
}
async checkForExistingMessage(
changeNumber: number,
newMessage: string,
msgType?: string,
): Promise<boolean> {
const messages = await this.getMessages(changeNumber);
return messages.some(
(existingMsg) =>
(msgType === undefined || msgType === existingMsg.tag) &&
existingMsg.message.includes(newMessage),
);
}
async addMessageIfNotAlreadyExists(
changeNumber: number,
message: string,
tag?: string,
): Promise<void> {
const newMsg = this.normalizeMessage(message);
if (!(await this.checkForExistingMessage(changeNumber, newMsg, tag))) {
await this.addMessage(changeNumber, newMsg, tag);
}
}
async setLabel(
changeNumber: number,
label: string,
value: number,
): Promise<void> {
await this.gerritHttp.postJson(
`a/changes/${changeNumber}/revisions/current/review`,
{ body: { labels: { [label]: value } } },
);
}
async addReviewer(changeNumber: number, reviewer: string): Promise<void> {
await this.gerritHttp.postJson(`a/changes/${changeNumber}/reviewers`, {
body: { reviewer },
});
}
async addAssignee(changeNumber: number, assignee: string): Promise<void> {
await this.gerritHttp.putJson<GerritAccountInfo>(
`a/changes/${changeNumber}/assignee`,
{
body: { assignee },
},
);
}
async getFile(
repo: string,
branch: string,
fileName: string,
): Promise<string> {
const base64Content = await this.gerritHttp.get(
`a/projects/${encodeURIComponent(
repo,
)}/branches/${branch}/files/${encodeURIComponent(fileName)}/content`,
);
return Buffer.from(base64Content.body, 'base64').toString();
}
async approveChange(changeId: number): Promise<void> {
const isApproved = await this.checkIfApproved(changeId);
if (!isApproved) {
await this.setLabel(changeId, 'Code-Review', +2);
}
}
async checkIfApproved(changeId: number): Promise<boolean> {
const change = await client.getChange(changeId);
const reviewLabels = change?.labels?.['Code-Review'];
return reviewLabels === undefined || reviewLabels.approved !== undefined;
}
wasApprovedBy(change: GerritChange, username: string): boolean | undefined {
return (
change.labels?.['Code-Review'].approved &&
change.labels['Code-Review'].approved.username === username
);
}
normalizeMessage(message: string): string {
//the last \n was removed from gerrit after the comment was added...
return message.substring(0, 0x4000).trim();
}
private static buildSearchFilters(
repository: string,
searchConfig: GerritFindPRConfig,
): string[] {
const filterState = mapPrStateToGerritFilter(searchConfig.state);
const filters = ['owner:self', 'project:' + repository, filterState];
if (searchConfig.branchName !== '') {
filters.push(`hashtag:sourceBranch-${searchConfig.branchName}`);
}
if (searchConfig.targetBranch) {
filters.push(`branch:${searchConfig.targetBranch}`);
}
if (searchConfig.label) {
filters.push(`label:Code-Review=${searchConfig.label}`);
}
if (searchConfig.prTitle) {
filters.push(
`message:${encodeURIComponent('"' + searchConfig.prTitle + '"')}`,
);
}
return filters;
}
}
export const client = new GerritClient();