-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
utils.ts
116 lines (104 loc) · 3.03 KB
/
utils.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
// SEE for the reference https://github.com/renovatebot/renovate/blob/c3e9e572b225085448d94aa121c7ec81c14d3955/lib/platform/bitbucket/utils.js
import url from 'url';
import {
PR_STATE_CLOSED,
PR_STATE_MERGED,
PR_STATE_OPEN,
} from '../../constants/pull-requests';
import { HttpResponse } from '../../util/http';
import { BitbucketServerHttp } from '../../util/http/bitbucket-server';
import { BbbsRestPr, BbsPr } from './types';
const bitbucketServerHttp = new BitbucketServerHttp();
// https://docs.atlassian.com/bitbucket-server/rest/6.0.0/bitbucket-rest.html#idp250
const prStateMapping: any = {
MERGED: PR_STATE_MERGED,
DECLINED: PR_STATE_CLOSED,
OPEN: PR_STATE_OPEN,
};
export function prInfo(pr: BbbsRestPr): BbsPr {
return {
version: pr.version,
number: pr.id,
body: pr.description,
branchName: pr.fromRef.displayId,
targetBranch: pr.toRef.displayId,
title: pr.title,
state: prStateMapping[pr.state],
createdAt: pr.createdDate,
isModified: true,
};
}
const addMaxLength = (inputUrl: string, limit = 100): string => {
const { search, ...parsedUrl } = url.parse(inputUrl, true); // eslint-disable-line @typescript-eslint/no-unused-vars
const maxedUrl = url.format({
...parsedUrl,
query: { ...parsedUrl.query, limit },
});
return maxedUrl;
};
async function callApi<T>(
apiUrl: string,
method: string,
options?: any
): Promise<HttpResponse<T>> {
/* istanbul ignore next */
switch (method.toLowerCase()) {
case 'post':
return bitbucketServerHttp.postJson<T>(apiUrl, options);
case 'put':
return bitbucketServerHttp.putJson<T>(apiUrl, options);
case 'patch':
return bitbucketServerHttp.patchJson<T>(apiUrl, options);
case 'head':
return bitbucketServerHttp.headJson<T>(apiUrl, options);
case 'delete':
return bitbucketServerHttp.deleteJson<T>(apiUrl, options);
case 'get':
default:
return bitbucketServerHttp.getJson<T>(apiUrl, options);
}
}
export async function accumulateValues<T = any>(
reqUrl: string,
method = 'get',
options?: any,
limit?: number
): Promise<T[]> {
let accumulator: T[] = [];
let nextUrl = addMaxLength(reqUrl, limit);
while (typeof nextUrl !== 'undefined') {
// TODO: fix typing
const { body } = await callApi<{
values: T[];
isLastPage: boolean;
nextPageStart: string;
}>(nextUrl, method, options);
accumulator = [...accumulator, ...body.values];
if (body.isLastPage !== false) {
break;
}
const { search, ...parsedUrl } = url.parse(nextUrl, true); // eslint-disable-line @typescript-eslint/no-unused-vars
nextUrl = url.format({
...parsedUrl,
query: {
...parsedUrl.query,
start: body.nextPageStart,
},
});
}
return accumulator;
}
export interface BitbucketCommitStatus {
failed: number;
inProgress: number;
successful: number;
}
export type BitbucketBranchState =
| 'SUCCESSFUL'
| 'FAILED'
| 'INPROGRESS'
| 'STOPPED';
export interface BitbucketStatus {
key: string;
state: BitbucketBranchState;
}