141 lines
3.5 KiB
TypeScript

import environment from './environment';
const API_URL = `https://gitlab.com/api/v4/projects/${environment.gitlab.projectId}`;
export async function createIssue(params: CreateIssueParams): Promise<Issue> {
const { description, labels, title } = params;
const body: { [key: string]: string } = {
description,
title,
};
const headers = new Headers({
'Content-Type': 'application/json',
'PRIVATE-TOKEN': environment.gitlab.token,
});
if (labels) {
body['labels'] = `${labels}`;
}
const response = await fetch(`${API_URL}/issues`, {
body: JSON.stringify(body),
headers,
method: 'POST',
});
const issue = await response.json();
return issue as unknown as Issue;
}
export async function createIssueComment(params: CreateIssueCommentParams): Promise<Issue | null> {
const { description, iid } = params;
const headers = new Headers({
'Content-Type': 'application/json',
'PRIVATE-TOKEN': environment.gitlab.token,
});
const response = await fetch(`${API_URL}/issues/${iid}/discussions`, {
body: JSON.stringify({ body: description }),
headers,
method: 'POST',
});
const issue = await response.json();
return issue as unknown as Issue | null;
}
export async function getIssue(params: GetIssueParams): Promise<Issue | null> {
const { labels, not, state, title } = params;
const headers = new Headers({
'Content-Type': 'application/json',
'PRIVATE-TOKEN': environment.gitlab.token,
});
const search = new URLSearchParams({ in: 'title', title });
if (labels) {
search.append('labels', `${labels}`);
}
if (not) {
if (not.labels) {
search.append('not[labels]', `${not.labels}`);
}
if (not.state) {
search.append('not[state]', not.state);
}
}
if (state) {
search.append('state', state);
}
const response = await fetch(`${API_URL}/issues?${search}`, { headers });
const issues: readonly Issue[] = (await response.json()) as unknown as readonly Issue[];
return issues.find((issue) => issue.title === title) || null;
}
export async function updateIssue(params: UpdateIssueParams): Promise<Issue | null> {
const { event, iid, labels, labelsToRemove } = params;
const body: { [key: string]: string } = {};
const headers = new Headers({
'Content-Type': 'application/json',
'PRIVATE-TOKEN': environment.gitlab.token,
});
if (event) {
body['state_event'] = event;
}
if (labels) {
body['labels'] = `${labels}`;
}
if (labelsToRemove) {
body['remove_labels'] = `${labelsToRemove}`;
}
const response = await fetch(`${API_URL}/issues/${iid}`, {
body: JSON.stringify(body),
headers,
method: 'PUT',
});
const issue = await response.json();
return issue as unknown as Issue | null;
}
export interface CreateIssueParams {
readonly description: string;
readonly labels?: readonly string[];
readonly title: string;
}
export interface CreateIssueCommentParams {
readonly description: string;
readonly iid: number;
}
export interface GetIssueParams {
readonly labels?: readonly string[];
readonly not?: Omit<GetIssueParams, 'not' | 'title'>;
readonly state?: string;
readonly title: string;
}
export interface Issue {
readonly iid: number;
readonly labels: readonly string[];
readonly state: string;
readonly title: string;
readonly web_url: string;
}
export interface UpdateIssueParams {
readonly event?: 'close' | 'reopen';
readonly iid: number;
readonly labels?: readonly string[];
readonly labelsToRemove?: readonly string[];
}