forked from ubiquity/ubiquibot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbefore-all-handler.ts
More file actions
105 lines (91 loc) · 3.22 KB
/
Copy pathbefore-all-handler.ts
File metadata and controls
105 lines (91 loc) · 3.22 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
import { Probot, run } from "probot";
import { bindEvents } from "../bindings/event";
import { GitHubEvent } from "../types/github-events";
import {
customOctokit,
getAdminUser,
getAdminUsername,
getCollaboratorUser,
getCollaboratorUsername,
owner,
repo,
setAdminUser,
setAdminUsername,
setCollaboratorUser,
setCollaboratorUsername,
setServer,
} from "./commands-test";
import { waitForNWebhooks, webhookEventEmitter } from "./utils";
export function beforeAllHandler(): jest.ProvidesHookCallback {
return async () => {
const adminPAT = process.env.TEST_ADMIN_PAT;
if (!adminPAT) {
throw new Error("missing TEST_ADMIN_PAT");
}
setAdminUser(new customOctokit({ auth: adminPAT }));
const { data } = await getAdminUser().rest.users.getAuthenticated();
setAdminUsername(data.login);
// check if the user is admin
const adminUsername = getAdminUsername();
if (!adminUsername) {
throw new Error("TEST_ADMIN_PAT is not admin");
}
const { data: data1 } = await getAdminUser().rest.repos.getCollaboratorPermissionLevel({
repo,
owner,
username: adminUsername,
});
if (data1.permission !== "admin") {
throw new Error("TEST_ADMIN_PAT is not admin");
}
const outsideCollaboratorPAT = process.env.TEST_OUTSIDE_COLLABORATOR_PAT;
if (!outsideCollaboratorPAT) {
throw new Error("missing TEST_OUTSIDE_COLLABORATOR_PAT");
}
setCollaboratorUser(new customOctokit({ auth: outsideCollaboratorPAT }));
const { data: data2 } = await getCollaboratorUser().rest.users.getAuthenticated();
setCollaboratorUsername(data2.login);
// check if the user is outside collaborator
const collaboratorUsername = getCollaboratorUsername();
if (!collaboratorUsername) {
throw new Error("TEST_OUTSIDE_COLLABORATOR_PAT is not outside collaborator");
}
const { data: data3 } = await getAdminUser().rest.repos.getCollaboratorPermissionLevel({
repo,
owner,
username: collaboratorUsername,
});
if (data3.permission === "admin" || data3.permission === "write") {
throw new Error("TEST_OUTSIDE_COLLABORATOR_PAT is not outside collaborator");
}
if (data3.permission !== "read") {
throw new Error("TEST_OUTSIDE_COLLABORATOR_PAT does not have read access");
}
const server = await run((app: Probot) => {
const allowedEvents = Object.values(GitHubEvent) as GitHubEvent[];
app.on(allowedEvents, async (context) => {
await bindEvents(context);
webhookEventEmitter.emit("event", context.payload);
});
});
setServer(server);
// FIXME: this is a hack to get around the fact that the config is not being updated
// build errors were happening because the config was not being updated
// await updateConfig({
// octokit: getAdminUser(),
// owner,
// repo: "ubiquibot-config",
// path: ".github/ubiquibot-config.yml",
// config: {}, //orgConfig,
// });
// await waitForNWebhooks(1);
// await updateConfig({
// octokit: getAdminUser(),
// owner,
// repo: "ubiquibot-config",
// path: ".github/ubiquibot-config.yml",
// config: {}, //repoConfig,
// });
await waitForNWebhooks(1);
};
}