-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
85 lines (70 loc) · 2.5 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
const core = require('@actions/core');
const github = require('@actions/github');;
async function start(){
try {
const label = core.getInput('label');
const targetRepo = core.getInput('targetRepo', {required: true});
const ghToken = core.getInput('token', {required: true});
const octokit = new github.getOctokit(ghToken);
const originalIssue = await getOriginalIssue(octokit);
if (!hasLabel(label, originalIssue)){
console.log(`Label ${label} not present. Will not copy issue`)
return;
}
const clonedIssue = await cloneIssue(octokit, targetRepo, originalIssue)
await addComment(octokit, originalIssue, clonedIssue)
console.log(`Issue cloned successfully`);
} catch (error) {
core.setFailed(error.message);
}
}
start();
async function getOriginalIssue(octokit) {
const payloadIssue = github.context.payload.issue;
if (!payloadIssue){
throw new Error("No issue in context");
}
const issue = await octokit.rest.issues.get({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: payloadIssue.number
})
return issue;
}
async function cloneIssue(octokit, targetRepo, original){
const splitted = targetRepo.split('/');
const owner = splitted[0];
const repoName = splitted[1];
const issueRegex = /(?<=^|\s)#\d+(?=\s|$)/g; // #12 as a word in the text
let body = original.data.body.replace(issueRegex, (match) => {
const issueNumber = match.substr(1);
return `https://github.com/${github.context.repo.owner}/${github.context.repo.repo}/issues/${issueNumber}`;
});
body = `Issue cloned from ${original.data.html_url}\n\n${body}`;
const title = original.data.title;
const result = await octokit.rest.issues.create({
owner: owner,
repo: repoName,
body: body,
title: title
});
return result;
}
async function addComment(octokit, originalIssue, clonedIssue){
const result = await octokit.rest.issues.createComment({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: originalIssue.data.number,
body: `Issue cloned to ${clonedIssue.data.html_url}`
})
return result;
}
function hasLabel(label, issue){
const labels = issue.data.labels;
for(let l of labels){
if(label === l.name){
return true;
}
}
return false;
}