forked from mheap/github-action-pr-heroku-review-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
160 lines (140 loc) · 4.79 KB
/
index.js
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
const { Toolkit } = require("actions-toolkit");
const Heroku = require("heroku-client");
const heroku = new Heroku({ token: process.env.HEROKU_API_TOKEN });
// Run your GitHub Action!
Toolkit.run(
async (tools) => {
const pr = tools.context.payload.pull_request;
// Required information
const event = tools.context.event;
const branch = pr.head.ref;
const version = pr.head.sha;
const fork = pr.head.repo.fork;
const pr_number = pr.number;
const repo_url = pr.head.repo.html_url;
const source_url = `${pr.head.repo.html_url}/tarball/${branch}`;
let fork_repo_id;
if (fork) {
fork_repo_id = pr.head.repo.id;
}
tools.log.debug("Deploy Info", {
branch,
version,
fork,
pr_number,
source_url,
});
let action = tools.context.payload.action;
// We can delete a review app without them being a collaborator
// as the only people that can close PRs are maintainers or the author
if (action === "closed") {
// Fetch all PRs
tools.log.pending("Listing review apps");
const reviewApps = await heroku.get(
`/pipelines/${process.env.HEROKU_PIPELINE_ID}/review-apps`
);
tools.log.complete("Fetched review app list");
// Filter to the one for this PR
const app = reviewApps.find((app) => app.pr_number == pr_number);
if (!app) {
tools.log.info(`Could not find review app for PR number ${pr_number}`);
return;
}
// Delete the PR
tools.log.pending("Deleting review app");
await heroku.delete(`/review-apps/${app.id}`);
tools.log.complete("Review app deleted");
return;
}
// Do they have the required permissions?
let requiredCollaboratorPermission = process.env.COLLABORATOR_PERMISSION;
if (requiredCollaboratorPermission) {
requiredCollaboratorPermission = requiredCollaboratorPermission.split(
","
);
} else {
requiredCollaboratorPermission = ["triage", "write", "maintain", "admin"];
}
const reviewAppLabelName =
process.env.REVIEW_APP_LABEL_NAME || "review-app";
const perms = await tools.github.repos.getCollaboratorPermissionLevel({
...tools.context.repo,
username: tools.context.actor,
});
if (!requiredCollaboratorPermission.includes(perms.data.permission)) {
tools.exit.success("User is not a collaborator. Skipping");
}
tools.log.info(`User is a collaborator: ${perms.data.permission}`);
let createReviewApp = false;
if (["opened", "reopened", "synchronize"].indexOf(action) !== -1) {
tools.log.info("PR opened by collaborator");
createReviewApp = true;
await tools.github.issues.addLabels({
...tools.context.repo,
labels: ["review-app"],
issue_number: pr_number,
});
} else if (action === "labeled") {
const labelName = tools.context.payload.label.name;
tools.log.info(`${labelName} label was added by collaborator`);
if (labelName === reviewAppLabelName) {
createReviewApp = true;
} else {
tools.log.debug(`Unexpected label, not creating app: ${labelName}`);
}
}
if (createReviewApp) {
// If it's a fork, creating the review app will fail as there are no secrets available
if (fork && event == "pull_request") {
tools.log.pending("Fork detected. Exiting");
tools.log.pending(
"If you would like to support PRs from forks, use the pull_request_target event"
);
tools.log.success("Action complete");
return;
}
// Otherwise we can complete it in this run
try {
tools.log.pending("Creating review app");
const resp = await heroku.post("/review-apps", {
body: {
branch,
pipeline: process.env.HEROKU_PIPELINE_ID,
source_blob: {
url: source_url,
version,
},
fork_repo_id,
pr_number,
environment: {
GIT_REPO_URL: repo_url,
},
},
});
tools.log.complete("Created review app");
} catch (e) {
// A 409 is a conflict, which means the app already exists
if (e.statusCode !== 409) {
throw e;
}
tools.log.complete("Review app is already created");
}
}
tools.log.success("Action complete");
},
{
event: [
"pull_request.opened",
"pull_request.reopened",
"pull_request.synchronize",
"pull_request.labeled",
"pull_request.closed",
"pull_request_target.opened",
"pull_request_target.reopened",
"pull_request_target.synchronize",
"pull_request_target.labeled",
"pull_request_target.closed",
],
secrets: ["GITHUB_TOKEN", "HEROKU_API_TOKEN", "HEROKU_PIPELINE_ID"],
}
);