-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathevaluate-dependencies.js
141 lines (124 loc) · 5.01 KB
/
evaluate-dependencies.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
const core = require('@actions/core');
const github = require('@actions/github');
var customDomains = core.getInput('custom-domains')?.split(/(\s+)/) ?? [];
const keyPhrases = 'depends on|blocked by';
const issueTypes = 'issues|pull';
const domainsList = ['github.com'].concat(customDomains); // add others from parameter
const domainsString = combineDomains(domainsList);
const quickLinkRegex = new RegExp(`(${keyPhrases}):? #(\\d+)`, 'gmi');
const partialLinkRegex = new RegExp(`(${keyPhrases}):? ([-_\\w]+)\\/([-._a-z0-9]+)(#)(\\d+)`, 'gmi');
const partialUrlRegex = new RegExp(`(${keyPhrases}):? ([-_\\w]+)\\/([-._a-z0-9]+)\\/(${issueTypes})\\/(\\d+)`, 'gmi');
const fullUrlRegex = new RegExp(`(${keyPhrases}):? https?:\\/\\/(?:${domainsString})\\/([-_\\w]+)\\/([-._a-z0-9]+)\\/(${issueTypes})\\/(\\d+)`, 'gmi');
const markdownRegex = new RegExp(`(${keyPhrases}):? \\[.*\\]\\(https?:\\/\\/(?:${domainsString})\\/([-_\\w]+)\\/([-._a-z0-9]+)\\/(${issueTypes})\\/(\\d+)\\)`, 'gmi');
function escapeDomainForRegex(domain) {
return domain.replace('.', '\\.');
}
function combineDomains(domains) {
return domains.map(x => escapeDomainForRegex(x)).join("|");
}
function extractFromMatch(match) {
return {
owner: match[2],
repo: match[3],
pull_number: parseInt(match[5], 10)
};
}
function getAllDependencies(body) {
var allMatches = [];
var quickLinkMatches = [...body.matchAll(quickLinkRegex)];
if (quickLinkMatches.length !== 0) {
quickLinkMatches.forEach(match => {
core.info(` Found number-referenced dependency in '${match}'`);
allMatches.push({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: parseInt(match[2], 10)
});
});
}
var extractableMatches = [...body.matchAll(partialLinkRegex)]
.concat([...body.matchAll(partialUrlRegex)])
.concat([...body.matchAll(fullUrlRegex)])
.concat([...body.matchAll(markdownRegex)]);
if (extractableMatches.length !== 0) {
extractableMatches.forEach(match => {
core.info(` Found number-referenced dependency in '${match}'`);
allMatches.push(extractFromMatch(match));
});
}
return allMatches;
}
async function evaluate() {
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,
});
if (!pullRequest.body){
core.info('body empty')
return;
}
core.info('\nReading PR body...');
var dependencies = getAllDependencies(pullRequest.body);
core.info('\nAnalyzing lines...');
var dependencyIssues = [];
for (var d of dependencies) {
core.info(` Fetching '${JSON.stringify(d)}'`);
var isPr = true;
var response = await octokit.pulls.get(d).catch(error => core.error(error));
if (response === undefined) {
isPr = false;
d = {
owner: d.owner,
repo: d.repo,
issue_number: d.pull_number,
};
core.info(` Fetching '${JSON.stringify(d)}'`);
response = await octokit.issues.get(d).catch(error => core.error(error));
if (response === undefined) {
core.info(' Could not locate this dependency. Will need to verify manually.');
continue;
}
}
if (isPr) {
const { data: pr } = response;
if (!pr) continue;
if (!pr.merged && !pr.closed_at) {
core.info(' PR is still open.');
dependencyIssues.push(pr);
} else {
core.info(' PR has been closed.');
}
} else {
const { data: issue } = response;
if (!issue) continue;
if (!issue.closed_at) {
core.info(' Issue is still open.');
dependencyIssues.push(issue);
} else {
core.info(' Issue has been closed.');
}
}
}
if (dependencyIssues.length !== 0) {
var msg = '\nThe following issues need to be resolved before this PR can be merged:\n';
for (var pr of dependencyIssues) {
msg += `\n#${pr.number} - ${pr.title}`;
}
core.setFailed(msg);
} else {
core.info("\nAll dependencies have been resolved!")
}
} catch (error) {
core.setFailed(error.message);
throw error;
}
}
module.exports = {
evaluate: evaluate,
getAllDependencies: getAllDependencies
}