-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
168 lines (149 loc) · 4.65 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
161
162
163
164
165
166
167
168
import * as core from "@actions/core";
import * as github from "@actions/github";
const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
const issue_number = github.context.issue.number;
const configPath = process.env.INPUT_CONFIGURATION_PATH;
const passOnOctokitError = process.env.INPUT_PASS_ON_OCTOKIT_ERROR === "true";
const { Octokit } = require("@octokit/action");
var config = require("./pr-title-checker-config.json")
let octokit;
// most @actions toolkit packages have async methods
async function run() {
try {
const title = github.context.payload.pull_request.title;
const labels = github.context.payload.pull_request.labels;
let { CHECKS, LABEL, MESSAGES } = JSON.parse(JSON.stringify(config));
LABEL.name = LABEL.name || "Gitemoji missing";
LABEL.color = LABEL.color || "eee";
CHECKS.ignoreLabels = CHECKS.ignoreLabels || [];
MESSAGES = MESSAGES || {};
MESSAGES.success = MESSAGES.success || "All OK";
MESSAGES.failure = MESSAGES.failure || "Failing CI test";
MESSAGES.notice = MESSAGES.notice || "";
//TODO
// if(title.toLowerCase().includes("feature")){
// pullRequest.title = '🎉' + pullRequest.title
// }
// if(title.toLowerCase().includes("fix")){
// pullRequest.title = '🐛' + pullRequest.title
// }
for (let i = 0; i < labels.length; i++) {
for (let j = 0; j < CHECKS.ignoreLabels.length; j++) {
if (labels[i].name == CHECKS.ignoreLabels[j]) {
core.info(`Ignoring Title Check for label - ${labels[i].name}`);
removeLabel(labels, LABEL.name);
return;
}
}
}
try {
core.info(`Creating label (${LABEL.name})...`);
let createResponse = await octokit.issues.createLabel({
owner,
repo,
name: LABEL.name,
color: LABEL.color,
});
core.info(`Created label (${LABEL.name}) - ${createResponse.status}`);
} catch (error) {
// Might not always be due to label's existence
core.info(`Label (${LABEL.name}) already created.`);
}
if (CHECKS.prefixes && CHECKS.prefixes.length) {
for (let i = 0; i < CHECKS.prefixes.length; i++) {
if (title.startsWith(CHECKS.prefixes[i])) {
removeLabel(labels, LABEL.name);
core.info(MESSAGES.success);
return;
}
}
}
if (CHECKS.regexp) {
let re = new RegExp(CHECKS.regexp, CHECKS.regexpFlags || "");
if (re.test(title)) {
removeLabel(labels, LABEL.name);
core.info(MESSAGES.success);
return;
}
}
await titleCheckFailed(CHECKS, LABEL, MESSAGES);
} catch (error) {
core.info(error);
}
}
async function titleCheckFailed(CHECKS, LABEL, MESSAGES) {
try {
if (MESSAGES.notice.length) {
core.notice(MESSAGES.notice);
}
await addLabel(LABEL.name);
if (CHECKS.alwaysPassCI) {
core.info(MESSAGES.failure);
} else {
core.setFailed(MESSAGES.failure);
}
} catch (error) {
core.info(error);
if (CHECKS.alwaysPassCI) {
core.info(`Failed to add label (${LABEL.name}) to PR`);
} else {
core.setFailed(`Failed to add label (${LABEL.name}) to PR`);
}
}
}
async function addLabel(name) {
core.info(`Adding label (${name}) to PR...`);
let addLabelResponse = await octokit.issues.addLabels({
owner,
repo,
issue_number,
labels: [name],
});
core.info(`Added label (${name}) to PR - ${addLabelResponse.status}`);
}
async function removeLabel(labels, name) {
try {
if (
!labels
.map((label) => label.name.toLowerCase())
.includes(name.toLowerCase())
) {
return;
}
core.info("No formatting necessary. Removing label...");
let removeLabelResponse = await octokit.issues.removeLabel({
owner,
repo,
issue_number,
name: name,
});
core.info(`Removed label - ${removeLabelResponse.status}`);
} catch (error) {
core.info(`Failed to remove label (${name}) from PR: ${error}`);
}
}
async function getJSON(repoPath) {
const response = await octokit.repos.getContent({
owner,
repo,
path: repoPath,
ref: github.context.sha,
});
return Buffer.from(response.data.content, response.data.encoding).toString();
}
async function handleOctokitError(e) {
core.info(`Octokit Error - ${e}`);
if (passOnOctokitError) {
core.info("Passing CI regardless");
} else {
core.setFailed("Failing CI test");
}
}
try {
octokit = new Octokit();
} catch (e) {
handleOctokitError(e);
}
if (octokit) {
run();
}