-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathapi.ts
More file actions
182 lines (160 loc) · 5.6 KB
/
api.ts
File metadata and controls
182 lines (160 loc) · 5.6 KB
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
import { APIError } from "@stainless-api/gitlab-internal/core/error";
import type { APIEntitiesNote } from "@stainless-api/gitlab-internal/resources/projects/issues/notes/notes";
import {
APIEntitiesMergeRequest,
BaseMergeRequests,
} from "@stainless-api/gitlab-internal/resources/projects/merge-requests/merge-requests";
import { BaseNotes } from "@stainless-api/gitlab-internal/resources/projects/merge-requests/notes";
import { BaseCommits } from "@stainless-api/gitlab-internal/resources/projects/repository/commits";
import {
createClient,
PartialGitLab,
} from "@stainless-api/gitlab-internal/tree-shakable";
import { logger } from "../../logger";
import type { APIClient, Comment, PullRequest } from "../api";
import { getInput } from "../input";
import { getGitLabContext as ctx } from "./context";
class GitLabClient implements APIClient {
private client: PartialGitLab<{
projects: {
repository: { commits: BaseCommits };
mergeRequests: BaseMergeRequests & { notes: BaseNotes };
};
}>;
constructor(token: string) {
this.client = createClient({
apiToken: token,
baseURL: ctx().urls.api,
resources: [BaseCommits, BaseMergeRequests, BaseNotes],
logLevel: "warn",
logger,
});
}
async listComments(prNumber: number): Promise<Comment[]> {
// The OAS claims it's a single object, but the docs claim it's an array.
// Just handle both.
const comments: APIEntitiesNote[] =
await this.client.projects.mergeRequests.notes
.list(prNumber, { id: ctx().projectID })
.then((data) => (Array.isArray(data) ? data : [data]))
.catch((err) => {
if (err instanceof APIError && err.status === 404) {
return [];
}
throw err;
});
return comments.map((c) => ({ id: c.id!, body: c.body ?? "" }));
}
async createComment(
prNumber: number,
props: Omit<Comment, "id">,
): Promise<Comment> {
const data = await this.client.projects.mergeRequests.notes.create(
prNumber,
{ ...props, id: ctx().projectID },
);
return { id: data.id!, body: data.body! };
}
async updateComment(prNumber: number, props: Comment): Promise<Comment> {
const data = await this.client.projects.mergeRequests.notes.update(
props.id as number,
{ ...props, id: ctx().projectID, noteable_id: prNumber },
);
return { id: data.id!, body: data.body! };
}
async getPullRequest(number: number): Promise<PullRequest | null> {
/**
* Per GitLab docs, diff_refs is "Empty when the merge request is created,
* and populates asynchronously." So we poll until it's populated.
*/
let mergeRequest: APIEntitiesMergeRequest | null = null;
let attempts = 0;
while (attempts++ < 3) {
mergeRequest = await this.client.projects.mergeRequests.retrieve(number, {
id: ctx().projectID,
});
if (
mergeRequest?.diff_refs?.start_sha &&
mergeRequest?.diff_refs?.head_sha
) {
return {
number: mergeRequest.iid,
state:
mergeRequest.state === "opened"
? "open"
: mergeRequest.state === "locked"
? "closed"
: (mergeRequest.state as "closed" | "merged"),
title: mergeRequest.title,
base_sha: mergeRequest.diff_refs.start_sha,
base_ref: mergeRequest.target_branch,
head_sha: mergeRequest.diff_refs.head_sha,
head_ref: mergeRequest.source_branch,
merge_commit_sha:
mergeRequest.merge_commit_sha ||
mergeRequest.squash_commit_sha ||
null,
};
}
await new Promise<void>((resolve) => {
setTimeout(() => resolve(), 1000 * (2 ** attempts + Math.random()));
});
}
logger.warn(
`Failed to find get diff_refs for merge request after ${attempts} attempts`,
{ mergeRequestIID: number },
);
return null;
}
async getPullRequestForCommit(sha: string): Promise<PullRequest | null> {
const mergeRequests: APIEntitiesMergeRequest[] =
await this.client.projects.repository.commits
.retrieveMergeRequests(sha, {
id: ctx().projectID,
})
.then((data) =>
// The OAS claims it's a single object, but the docs claim it's an
// array? Just handle both.
(Array.isArray(data) ? data : [data]).filter(
(c) => c.state !== "closed" && c.state !== "locked",
),
)
.catch((err) => {
if (err instanceof APIError && err.status === 404) {
return [];
}
throw err;
});
if (mergeRequests.length === 0) {
return null;
}
if (mergeRequests.length > 1) {
logger.warn(
`Multiple merge requests found for commit; only using first.`,
{ commit: sha, mergeRequests: mergeRequests.map((c) => c.iid) },
);
}
const mergeRequestIID = mergeRequests[0]!.iid;
const mergeRequest = await this.getPullRequest(mergeRequestIID);
return mergeRequest;
}
}
let cachedClient: GitLabClient | null | undefined;
export function getGitLabClient(): GitLabClient | null {
if (cachedClient !== undefined) {
return cachedClient;
}
const token = getInput("GITLAB_TOKEN");
if (token?.startsWith("$")) {
throw new Error(
`Input GITLAB_TOKEN starts with '$'; expected token to start with 'gl'. Does the CI have access to the variable?`,
);
}
if (token) {
cachedClient = new GitLabClient(token);
} else {
logger.info("No GitLab token found in input 'GITLAB_TOKEN'.");
cachedClient = null;
}
return cachedClient;
}