-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.js
131 lines (112 loc) · 3.64 KB
/
runner.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
const core = require('@actions/core');
const github = require('@actions/github');
function check(requirement) {
var now = new Date();
return now >= requirement;
}
const durationPattern = /^((?<year>\d+)y)?(((?<month>\d+)M)?|((?<week>\d+)w)?)((?<day>\d+)d)?((?<hour>\d+)h)?((?<minute>\d+)m)?((?<second>\d+)s)?$/
function parseDuration(text) {
var match = durationPattern.exec(text);
return {
years: parseInt(match.groups.year),
months: parseInt(match.groups.month),
weeks: parseInt(match.groups.week),
days: parseInt(match.groups.day),
hours: parseInt(match.groups.hour),
minutes: parseInt(match.groups.minute),
seconds: parseInt(match.groups.second)
}
}
// source: https://stackoverflow.com/a/2706169/878701
function addMonths(date, months) {
var d = date.getDate();
date.setMonth(date.getMonth() + +months);
if (date.getDate() != d) {
date.setDate(0);
}
return date;
}
function addDuration(date, duration) {
if (!isNaN(duration.years)){
date.setFullYear(date.getFullYear() + duration.years);
}
if (!isNaN(duration.months)){
date = addMonths(date, duration.months);
}
if (!isNaN(duration.weeks)){
date.setDate(date.getDate() + duration.weeks*7);
}
if (!isNaN(duration.days)){
date.setDate(date.getDate() + duration.days);
}
if (!isNaN(duration.hours)){
date.setHours(date.getHours() + duration.hours);
}
if (!isNaN(duration.minutes)){
date.setMinutes(date.getMinutes() + duration.minutes);
}
if (!isNaN(duration.seconds)){
date.setSeconds(date.getSeconds() + duration.seconds);
}
return date;
}
function getDurationInput() {
var time = core.getInput('time');
if (time !== undefined){
return parseDuration(time);
}
var years = parseInt(core.getInput('years'));
var months = parseInt(core.getInput('months'));
var weeks = parseInt(core.getInput('weeks'));
var days = parseInt(core.getInput('days'));
var hours = parseInt(core.getInput('hours'));
var minutes = parseInt(core.getInput('minutes'));
var seconds = parseInt(core.getInput('seconds'));
if (weeks !== NaN && months !== NaN) {
core.setFailed('Weeks are incompatible with months');
return undefined;
}
return {
years: years,
months: months,
weeks: weeks,
days: days,
hours: hours,
minutes: minutes,
seconds: seconds
};
}
async function run() {
try {
core.info('Initializing...');
const myToken = process.env.GITHUB_TOKEN;
const octokit = github.getOctokit(myToken);
const { data: pullRequest } = await octokit.pulls.get({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: github.context.issue.number,
});
// get parameters
const openTime = getDurationInput();
if (openTime === undefined){
return;
}
core.info(`Required duration: ${JSON.stringify(openTime)}`);
const created = new Date(pullRequest.created_at);
core.info(`PR created on ${created}`);
const earliestAllowedMerge = addDuration(created, openTime);
core.info(`PR can be merged after ${earliestAllowedMerge}`);
if (!check(earliestAllowedMerge)){
core.setFailed('PR has not been open long enough.');
}
} catch (error) {
core.setFailed(error.message);
throw error;
}
}
module.exports = {
run: run,
parseDuration: parseDuration,
addDuration: addDuration,
check: check
};