-
-
Notifications
You must be signed in to change notification settings - Fork 98
119 lines (116 loc) · 4.27 KB
/
add-scope-to-bet.yml
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
name: Add related issues
on:
issues:
types:
- edited
- opened
jobs:
add_related_issues:
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- name: Parse Issue Description
id: parse
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GH_TOKEN }}
result-encoding: json
script: |
const regexSimple = /(Related\sto\s+(\S+)?#(\d+))/;
const description = context.payload.issue.body;
const match = regexSimple.exec(description);
if (!match) {
console.log("Nothing to do in this issue");
return null;
}
let result = null;
const repoFull = match[2] ? match[2] : context.payload.repository.full_name;
const owner = repoFull.split('/')[0];
const repo = repoFull.split('/')[1];
const issueNumber = Number(match[3]);
const repoUrl = `https://github.com/${repoFull}`;
const orgOrUser = context.payload.organization ? 'organization' : 'user';
const query = `
query($owner: String!, $repo: String!, $issue: Int!) {
${orgOrUser}(login: $owner) {
repository(name: $repo) {
issue(number: $issue) {
id
title
body
trackedIssues(first:100) {
nodes {
number
}
}
}
}
}
}
`;
const variables = {
owner,
repo,
issue: issueNumber
}
try {
const data = await github.graphql(query, variables)
const {body, id, trackedIssues} = data[orgOrUser].repository.issue
result = {
issueNumber,
owner,
repo,
repoUrl,
betIssueDescription: body,
issueNodeId: id,
trackedTasks : trackedIssues.nodes.map(issue => issue.number)
};
} catch (error) {
console.log(`Failed query for issue #${issueNumber}`, error);
}
return result;
- name: Add The issue to the Bet
id: add-bet
uses: actions/github-script@v6
if: ${{ steps.parse.outputs.result }}
with:
github-token: ${{ secrets.GH_TOKEN }}
script: |
const results = ${{ steps.parse.outputs.result }};
if (!results) {
console.log("Nothing to do in this issue - outputs results is null");
return;
}
const { issueNumber, repo, owner, repoUrl, betIssueDescription, issueNodeId, trackedTasks } = results;
const regex = /###?\sScope([^-]+)((-\s+\[[\sX|x]\]\s*#?.+\s*)+)/;
const match = betIssueDescription.match(regex);
const taskNumber = context.payload.issue.number;
if (!match) {
console.log("Nothing to do in this issue");
return;
}
const currentScope = match[2].trim();
const newTask = `- [ ] #${taskNumber}`;
if (trackedTasks.includes(Number(taskNumber))) {
console.log("This issue already exists");
return;
}
const newScope = `${currentScope}\n${newTask}`;
const newBetDescription = betIssueDescription.replace(currentScope, newScope);
const mutation = `mutation ($input: UpdateIssueInput!) {
updateIssue(input: $input) {
clientMutationId
}
}`;
const variables = {
input : {
id: issueNodeId,
body: newBetDescription
}
};
try {
await github.graphql(mutation, variables);
} catch (error) {
console.log(`Failed mutation for issue #${issueNodeId}`, error);
}