-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
153 lines (119 loc) · 4.1 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
const core = require("@actions/core");
const github = require("@actions/github");
const marked = require("marked");
const semverParse = require("semver/functions/parse");
const versionLabel = "Version: ";
const versionTitlesInMarkdown = ["Version", "New Version"];
const rnInfoTitleInMarkdown = "Output of `react-native info`";
const labelForNoVersion = "Version: unspecified";
const getVersionFromIssueBody = (issueBody) => {
let versionFromIssueBody;
// Parse the markdown from the issue body
const markdownSection = marked.lexer(issueBody);
// Loop through all sections
for (const markdownSectionIndex in markdownSection) {
// If this section matches `versionTitleInMarkdown`
if (
versionTitlesInMarkdown.includes(
markdownSection[markdownSectionIndex].text
)
) {
// Then the version can be found in the next section
const specifiedVersion =
markdownSection[Number(markdownSectionIndex) + 1];
if (!specifiedVersion.text) {
continue;
}
const parsedVersion = semverParse(specifiedVersion.text);
if (!parsedVersion) {
continue;
}
versionFromIssueBody = parsedVersion.version;
break;
}
// If this section matches `rnInfoTitleInMarkdown`
if (markdownSection[markdownSectionIndex].text === rnInfoTitleInMarkdown) {
// Then the version can be found in the next section
const rnInfoOutput = markdownSection[Number(markdownSectionIndex) + 1];
if (!rnInfoOutput.text) {
continue;
}
const rnInfoRNPart = rnInfoOutput.text.match(/react-native:(.+?)=>/);
if (!rnInfoRNPart || rnInfoRNPart.length === 0) {
continue;
}
const versionFromRnInfoPart = semverParse(rnInfoRNPart[1].trim());
if (!versionFromRnInfoPart) {
continue;
}
versionFromIssueBody = versionFromRnInfoPart;
break;
}
}
return versionFromIssueBody;
};
const getLabelToBeApplied = (version) =>
version ? `${versionLabel}${version}` : labelForNoVersion;
// Look for a version on the issue body
const getIsIssueLabelAVersion = (label) => label.startsWith(versionLabel);
(async () => {
const githubToken = core.getInput("github-token", { required: true });
const octokit = github.getOctokit(githubToken);
const { issue } = github.context;
// This fetches the issue again as it can have different data after running the other actions
const { data: updatedIssue } = await octokit.rest.issues.get({
owner: issue.owner,
repo: issue.repo,
issue_number: issue.number,
});
if (updatedIssue.state === "closed") {
// Do nothing if the issue has been closed
core.debug("Issue already closed");
return;
}
const versionFromIssueBody = getVersionFromIssueBody(updatedIssue.body);
const labelToBeApplied = getLabelToBeApplied(versionFromIssueBody);
// Get all the labels in the issue
const { data: labels } = await octokit.rest.issues.listLabelsOnIssue({
owner: issue.owner,
repo: issue.repo,
issue_number: issue.number,
});
// Loop through all labels and remove the version label if it exists
// and is not the same as the version from the issue body
try {
await Promise.all(labels.map(({ name }) => {
const isLabelAVersion = getIsIssueLabelAVersion(name);
if (!isLabelAVersion || name === labelToBeApplied) {
return;
}
return octokit.rest.issues.removeLabel({
owner: issue.owner,
repo: issue.repo,
issue_number: issue.number,
name,
});
}));
} catch (error) {
core.error(error);
core.setFailed("Failed to remove version labels")
}
try {
// Make sure that the label to be added exists
await octokit.rest.issues.getLabel({
owner: issue.owner,
repo: issue.repo,
name: labelToBeApplied,
});
// Then add it
await octokit.rest.issues.addLabels({
owner: issue.owner,
repo: issue.repo,
issue_number: issue.number,
labels: [labelToBeApplied],
});
} catch (error) {
core.error(error);
core.setFailed(`Label ${labelToBeApplied} doesn't seem to exist`)
}
})();