-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
67 lines (60 loc) · 2.3 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
const core = require('@actions/core');
const github = require('@actions/github');
const Octokit = require("@octokit/rest");
const octokit = Octokit({ auth: core.getInput("github-token"), baseUrl: 'https://api.github.com' });
const release = github.context.payload.release;
const owner = github.context.payload.repository.owner.login;
const repo = github.context.payload.repository.name;
async function updateMilestoneWithPreRelease(milestone) {
const resp = await octokit.issues.updateMilestone({
owner: owner,
repo: repo,
milestone_number: milestone.number,
description: 'Pre-release: [' + release.tag_name + '](' + release.html_url + ')'
});
if (resp.status === 200) {
console.log('Updated Milestone ' + milestone.title + '!');
} else {
console.error('Failed to Update Milestone ' + milestone.title + '. GitHub API returned Status Code: ' + resp.status);
process.exit(1);
}
}
async function updateMilestoneWithRelease(milestone) {
const resp = await octokit.issues.updateMilestone({
owner: owner,
repo: repo,
milestone_number: milestone.number,
description: 'Release: [' + release.tag_name + '](' + release.html_url + ')',
state: 'closed'
});
if (resp.status === 200) {
console.log('Updated & Closed Milestone ' + milestone.title + '!');
} else {
console.error('Failed to Update & Close Milestone ' + milestone.title + '. GitHub API returned Status Code: ' + resp.status);
process.exit(1);
}
}
function doesVersionMatch(milestone, release) {
const milestoneVersion = milestone.title.match(/\d+\.\d+\.\d+/);
const releaseVersion = release.tag_name.match(/\d+\.\d+\.\d+/);
return milestoneVersion !== null && releaseVersion !== null && milestoneVersion[0] === releaseVersion[0];
}
octokit.issues.listMilestonesForRepo({ owner: owner, repo: repo }).then((payload) => {
const milestones = payload.data.filter((milestone) => {
return milestone.state === 'open' && doesVersionMatch(milestone, release);
});
if (milestones.length > 0) {
milestones.forEach((milestone) => {
if (release.prerelease) {
updateMilestoneWithPreRelease(milestone);
} else {
updateMilestoneWithRelease(milestone);
}
});
} else {
console.log('No Milestones matched the Release\'s Tag');
}
}, () => {
console.error('Unable to get Milestones for repository ' + owner + '/' + repo);
process.exit(1);
});