forked from ubiquity/ubiquibot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
181 lines (163 loc) · 4.24 KB
/
Copy pathutils.ts
File metadata and controls
181 lines (163 loc) · 4.24 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
import { expect } from "@jest/globals";
import { RequestError } from "@octokit/request-error";
import EventEmitter from "events";
import { Octokit } from "octokit";
import YAML from "yaml";
import { BotConfig } from "../types/configuration-types";
export const webhookEventEmitter = new EventEmitter();
export function waitForNWebhooks(n = 1) {
return new Promise<void>((resolve, reject) => {
let i = 0;
const timer = setTimeout(() => {
reject(new Error(`timeout, received ${i} webhooks, expected ${n}`));
}, 30000);
webhookEventEmitter.on("event", () => {
i += 1;
if (i == n) {
clearTimeout(timer);
setTimeout(resolve, 1000);
}
});
});
}
export async function createLabel({ octokit, owner, repo, label, color }: CreateLabel) {
try {
await octokit.rest.issues.createLabel({
owner,
repo,
name: label,
color,
});
} catch (err: unknown) {
if (err instanceof RequestError) {
expect(err).toBeDefined();
expect(err?.status).toBe(422);
}
}
}
export async function addLabelToIssue({ octokit, owner, repo, issueNumber, label }: LabelParams) {
await octokit.rest.issues.addLabels({
owner,
repo,
issue_number: issueNumber,
labels: [label],
});
}
export async function removeLabelFromIssue({ octokit, owner, repo, issueNumber, label }: LabelParams) {
await octokit.rest.issues.removeLabel({
owner,
repo,
issue_number: issueNumber,
name: label,
});
}
export async function createAndAddLabel({ octokit, owner, repo, issueNumber, label }: LabelParams) {
try {
await octokit.rest.issues.createLabel({
owner,
repo,
name: label,
});
} catch (err: unknown) {
if (err instanceof RequestError) {
expect(err).toBeDefined();
expect(err?.status).toBe(422);
}
} finally {
await octokit.rest.issues.addLabels({
owner,
repo,
issue_number: issueNumber,
labels: [label],
});
await waitForNWebhooks(1);
}
}
export async function updateConfig({ octokit, owner, repo, path, config }: UpdateConfig) {
let sha: string | undefined = undefined;
try {
const fileContent = await octokit.rest.repos.getContent({
owner,
repo,
path,
});
if (Array.isArray(fileContent.data)) {
throw new Error("ubiquibot-config.yml should not be directory");
}
if (fileContent.data.type !== "file") {
throw new Error("ubiquibot-config.yml is not a file");
}
sha = fileContent.data.sha;
} catch (err: unknown) {
if (err instanceof RequestError) {
expect(err).toBeDefined();
expect(err?.status).toBe(404);
}
}
await octokit.rest.repos.createOrUpdateFileContents({
owner,
repo,
path,
message: "test(e2e): automated update config for test",
content: Buffer.from(YAML.stringify(config)).toString("base64"),
sha,
});
}
export async function createComment({ octokit, owner, repo, issueNumber, body }: CreateComment) {
await octokit.rest.issues.createComment({
repo,
owner,
issue_number: issueNumber,
body: body,
});
}
interface GetLastComment {
octokit: Octokit;
owner: string;
repo: string;
issueNumber: number;
}
export async function getLastComment({ octokit, owner, repo, issueNumber }: GetLastComment) {
const { data } = await octokit.rest.issues.listComments({
repo,
owner,
issue_number: issueNumber,
per_page: 100,
});
expect(data.length).toBeGreaterThan(0);
return data[data.length - 1];
}
export default async function checkLastComment({
octokit,
owner,
repo,
issueNumber,
expectedComment,
}: CheckLastComment) {
const lastComment = await getLastComment({ octokit, owner, repo, issueNumber });
expect(lastComment.body).toBe(expectedComment);
}
interface OctokitParams {
octokit: Octokit;
owner: string;
repo: string;
}
interface IssueParams extends OctokitParams {
issueNumber: number;
}
interface LabelParams extends IssueParams {
label: string;
}
interface CreateLabel extends LabelParams {
color?: string;
}
interface UpdateConfig extends OctokitParams {
path: string;
config: BotConfig;
}
interface CreateComment extends IssueParams {
body: string;
}
interface CheckLastComment extends IssueParams {
expectedComment: string;
}