renovate/lib/platform/bitbucket-server/utils.ts

87 lines
2.2 KiB
TypeScript

// SEE for the reference https://github.com/renovatebot/renovate/blob/c3e9e572b225085448d94aa121c7ec81c14d3955/lib/platform/bitbucket/utils.js
import url from 'url';
import { api } from './bb-got-wrapper';
import { Pr } from '../common';
import {
PR_STATE_CLOSED,
PR_STATE_MERGED,
PR_STATE_OPEN,
} from '../../constants/pull-requests';
// 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: any): Pr {
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;
};
export async function accumulateValues<T = any>(
reqUrl: string,
method = 'get',
options?: any,
limit?: number
): Promise<T[]> {
let accumulator: T[] = [];
let nextUrl = addMaxLength(reqUrl, limit);
const lowerCaseMethod = method.toLocaleLowerCase();
while (typeof nextUrl !== 'undefined') {
// TODO: fix typing
const { body } = await (api as any)[lowerCaseMethod](nextUrl, 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;
}