mirror of
https://github.com/renovatebot/renovate.git
synced 2026-11-01 18:19:51 +00:00
914c64afbd
* ci: add linting rule for schema and infered types * refactor: remove usages of Schema and differently named types
171 lines
4.3 KiB
TypeScript
171 lines
4.3 KiB
TypeScript
import { ScmManagerHttp } from '../../../util/http/scm-manager.ts';
|
|
import type { Link } from './schema.ts';
|
|
import {
|
|
DefaultBranch,
|
|
PagedPullRequest,
|
|
PagedRepo,
|
|
PullRequest,
|
|
Repo,
|
|
User,
|
|
} from './schema.ts';
|
|
import type {
|
|
PullRequestCreateParams,
|
|
PullRequestUpdateParams,
|
|
} from './types.ts';
|
|
|
|
export const scmManagerHttp = new ScmManagerHttp();
|
|
|
|
const URLS = {
|
|
ME: 'me',
|
|
ALL_REPOS: 'repositories?pageSize=1000000',
|
|
REPO: (repoPath: string) => `repositories/${repoPath}`,
|
|
PULLREQUESTS: (repoPath: string) => `pull-requests/${repoPath}`,
|
|
ALL_PULLREQUESTS_WITH_PAGINATION: (repoPath: string) =>
|
|
`pull-requests/${repoPath}?status=ALL&pageSize=1000000`,
|
|
MY_PULLREQUESTS_WITH_PAGINATION: (repoPath: string) =>
|
|
`pull-requests/${repoPath}?status=MINE&pageSize=1000000`,
|
|
PULLREQUEST_BY_ID: (repoPath: string, id: number) =>
|
|
`pull-requests/${repoPath}/${id}`,
|
|
};
|
|
|
|
const CONTENT_TYPES = {
|
|
ME: 'application/vnd.scmm-me+json;v=2',
|
|
REPOSITORY: 'application/vnd.scmm-repository+json;v=2',
|
|
REPOSITORIES: 'application/vnd.scmm-repositoryCollection+json;v=2',
|
|
GIT_CONFIG: 'application/vnd.scmm-gitDefaultBranch+json;v=2',
|
|
PULLREQUEST: 'application/vnd.scmm-pullRequest+json;v=2',
|
|
PULLREQUESTS: 'application/vnd.scmm-pullRequestCollection+json;v=2',
|
|
};
|
|
|
|
export async function getCurrentUser(token: string): Promise<User> {
|
|
const response = await scmManagerHttp.getJson(
|
|
URLS.ME,
|
|
{
|
|
headers: { accept: CONTENT_TYPES.ME },
|
|
token,
|
|
},
|
|
User,
|
|
);
|
|
return response.body;
|
|
}
|
|
|
|
export async function getRepo(repoPath: string): Promise<Repo> {
|
|
const response = await scmManagerHttp.getJson(
|
|
URLS.REPO(repoPath),
|
|
{
|
|
headers: { accept: CONTENT_TYPES.REPOSITORY },
|
|
},
|
|
Repo,
|
|
);
|
|
return response.body;
|
|
}
|
|
|
|
export async function getAllRepos(): Promise<Repo[]> {
|
|
const response = await scmManagerHttp.getJson(
|
|
URLS.ALL_REPOS,
|
|
{
|
|
headers: { accept: CONTENT_TYPES.REPOSITORIES },
|
|
},
|
|
PagedRepo,
|
|
);
|
|
|
|
return response.body._embedded.repositories;
|
|
}
|
|
|
|
export async function getDefaultBranch(repo: Repo): Promise<string> {
|
|
const defaultBranchUrl = repo._links.defaultBranch as Link;
|
|
const response = await scmManagerHttp.getJson(
|
|
defaultBranchUrl.href,
|
|
{
|
|
headers: { accept: CONTENT_TYPES.GIT_CONFIG },
|
|
},
|
|
DefaultBranch,
|
|
);
|
|
|
|
return response.body.defaultBranch;
|
|
}
|
|
|
|
export async function getAllRepoPrs(
|
|
repoPath: string,
|
|
ignorePrAuthor: boolean,
|
|
): Promise<PullRequest[]> {
|
|
const response = await scmManagerHttp.getJson(
|
|
ignorePrAuthor
|
|
? URLS.ALL_PULLREQUESTS_WITH_PAGINATION(repoPath)
|
|
: URLS.MY_PULLREQUESTS_WITH_PAGINATION(repoPath),
|
|
{
|
|
headers: { accept: CONTENT_TYPES.PULLREQUESTS },
|
|
},
|
|
PagedPullRequest,
|
|
);
|
|
return response.body._embedded.pullRequests;
|
|
}
|
|
|
|
export async function getRepoPr(
|
|
repoPath: string,
|
|
id: number,
|
|
): Promise<PullRequest> {
|
|
const response = await scmManagerHttp.getJson(
|
|
URLS.PULLREQUEST_BY_ID(repoPath, id),
|
|
{
|
|
headers: { accept: CONTENT_TYPES.PULLREQUEST },
|
|
},
|
|
PullRequest,
|
|
);
|
|
|
|
return response.body;
|
|
}
|
|
|
|
export async function createScmPr(
|
|
repoPath: string,
|
|
params: PullRequestCreateParams,
|
|
): Promise<PullRequest> {
|
|
const createPrResponse = await scmManagerHttp.postJson(
|
|
URLS.PULLREQUESTS(repoPath),
|
|
{
|
|
body: params,
|
|
headers: {
|
|
'Content-Type': CONTENT_TYPES.PULLREQUEST,
|
|
accept: CONTENT_TYPES.PULLREQUEST,
|
|
},
|
|
},
|
|
);
|
|
|
|
const getCreatedPrResponse = await scmManagerHttp.getJson(
|
|
createPrResponse.headers.location!,
|
|
{
|
|
headers: { accept: CONTENT_TYPES.PULLREQUEST },
|
|
},
|
|
PullRequest,
|
|
);
|
|
|
|
return getCreatedPrResponse.body;
|
|
}
|
|
|
|
export async function updateScmPr(
|
|
repoPath: string,
|
|
id: number,
|
|
params: PullRequestUpdateParams,
|
|
): Promise<void> {
|
|
const currentPr = await getRepoPr(repoPath, id);
|
|
await scmManagerHttp.putJson(URLS.PULLREQUEST_BY_ID(repoPath, id), {
|
|
body: mergePullRequestWithUpdate(currentPr, params),
|
|
headers: {
|
|
'Content-Type': CONTENT_TYPES.PULLREQUEST,
|
|
},
|
|
});
|
|
}
|
|
|
|
function mergePullRequestWithUpdate(
|
|
pr: PullRequest,
|
|
updateParams: PullRequestUpdateParams,
|
|
): PullRequest {
|
|
return {
|
|
...pr,
|
|
title: updateParams.title,
|
|
description: updateParams.description ?? pr.description,
|
|
status: updateParams.status ?? pr.status,
|
|
target: updateParams.target ?? pr.target,
|
|
};
|
|
}
|