Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
172 lines (159 loc) · 4.71 KB
/
Copy pathindex.js
File metadata and controls
172 lines (159 loc) · 4.71 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
const core = require('@actions/core');
const github = require('@actions/github');
const githubToken = core.getInput('repo-token');
const ghClient = new github.getOctokit(githubToken);
async function createLabels(repo, labels) {
let createdLabelIds = [];
for (let label of labels) {
let result = await ghClient.graphql(`
mutation CreateLabels {
createLabel(
input: {repositoryId: "${repo}", name: "${label}", color: "1D76DB"})
{
clientMutationId,
label {
id
}
}
}`,
{
headers: {
accept: `application/vnd.github.bane-preview+json`
}
});
createdLabelIds.push(result.createLabel.label.id);
}
return createdLabelIds;
}
async function getRepoAndLabels(owner, repo, labelsSought) {
let result = await ghClient.graphql(`{
repository(name: "${repo}", owner: "${owner}") {
id
labels(first: 100) {
nodes {
id
name
}
}
}
}`, {});
let repoId = result.repository.id;
let labels = result.repository.labels.nodes;
labelIds = [];
missingLabels = [];
for (const labelSought of labelsSought) {
var labelFound = labels.find(l => l.name.toUpperCase() === labelSought.toUpperCase());
if (labelFound !== undefined)
labelIds.push(labelFound.id);
else
missingLabels.push(labelSought);
}
if (missingLabels.length > 0) {
let labelsCreated = await createLabels(repoId, missingLabels);
labelsCreated.forEach(x => labelIds.push(x));
}
return { repoId, labelIds };
}
async function getMembers(owner, teams, users) {
let userIds = [];
for (let team of teams) {
let result = await ghClient.graphql(`{
organization(login: "${owner}") {
team(slug: "${team}") {
members {
nodes {
id,
login
}
}
}
}
}`, {});
result.organization.team.members.nodes.forEach(member => userIds.push(member.id));
}
let members = await ghClient.graphql(`{
organization(login: "${owner}") {
membersWithRole(first: 100) {
nodes {
id,
login
}
}
}
}`, {});
for (const member of members.organization.membersWithRole.nodes) {
var login = member.login;
if (users.some(u => login.toUpperCase() == u.toUpperCase())) {
userIds.push(member.id);
}
}
return userIds;
}
async function markReadyToReview(currentPullRequestId) {
await ghClient.graphql(`
mutation MarkReadyToReview {
markPullRequestReadyForReview(input: {pullRequestId: "${currentPullRequestId}"}) {
clientMutationId
}
}`, {});
}
async function personnalizePullRequest(currentPullRequestId, labelIds, userIds) {
await ghClient.graphql(`
mutation PersonnalizePullRequest {
addLabelsToLabelable(
input: {labelableId: "${currentPullRequestId}", labelIds: ${JSON.stringify(labelIds)}})
{
clientMutationId
}
requestReviews(input: {pullRequestId: "${currentPullRequestId}", userIds: ${JSON.stringify(userIds)}})
{
clientMutationId
}
}`, {});
}
async function createPullRequest(repoId, base, branch, title, reviewers, labels) {
let continueFlow = true;
let result = await ghClient.graphql(`
mutation CreatePullRequest {
createPullRequest(
input: {repositoryId: "${repoId}", baseRefName: "${base}", headRefName: "${branch}", title: "${title}", draft: true})
{
pullRequest {
id
}
clientMutationId
}
}`, {})
.catch((error) => {
if (error.errors[0].message.startsWith("A pull request already exists")) {
continueFlow = false;
console.warn("Pull requests already exists. Do nothing.")
}
else {
throw error;
}
});
if (continueFlow) {
await personnalizePullRequest(result.createPullRequest.pullRequest.id, labels, reviewers);
await markReadyToReview(result.createPullRequest.pullRequest.id);
}
}
async function run() {
let repoInfo = core.getInput('repo');
let owner = repoInfo.split('/')[0];
let repo = repoInfo.split('/')[1];
let title = core.getInput('title');
let teamReviewers = core.getInput('team-reviewers').split(',');
let userReviewers = core.getInput('user-reviewers').split(',');
let baseBranch = core.getInput('destination');
let sourceBranch = core.getInput('source');
let labels = core.getInput('labels').split(',');
var result = await getRepoAndLabels(owner, repo, labels);
var reviewerIds = await getMembers(owner, teamReviewers, userReviewers);;
await createPullRequest(result.repoId, baseBranch, sourceBranch, title, reviewerIds, result.labelIds);
}
try {
run();
} catch (error) {
core.setFailed(error.message);
}