forked from SonarSource/sonar-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
114 lines (111 loc) · 4.85 KB
/
SubmitReview.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
name: Submit Review
on:
pull_request_review:
types: ["submitted"]
jobs:
assign_and_move_card:
name: Assign issue to PR author and move Kanban card
runs-on: ubuntu-latest
# Single quotes must be used here https://docs.github.com/en/free-pro-team@latest/actions/reference/context-and-expression-syntax-for-github-actions#literals
# PRs from forks don't have required token authorization
if: |
github.event.pull_request.head.repo.full_name == github.repository
&& (github.event.review.state == 'approved' || github.event.review.state == 'changes_requested')
steps:
# https://github.com/actions/github-script
- uses: actions/github-script@v4.0.2
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const TODO_COLUMN = 4971951;
const IN_PROGRESS_COLUMN = 4971952;
const REVIEW_IN_PROGRESS_COLUMN = 4971953;
const REVIEW_APPROVED_COLUMN = 4971954;
const VALIDATE_PEACH_COLUMN = 7838551;
const DONE_COLUMN = 4971955;
//
async function getIssue(issue_number) {
try {
return (await github.issues.get({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number
})).data;
}
catch (error) {
console.log(`Issue #${issue_number} not found: ${error}`);
return null;
}
}
//
async function findCard(content_url) {
// Columns are searched from the most probable one
const allColumns = [REVIEW_IN_PROGRESS_COLUMN, REVIEW_APPROVED_COLUMN, IN_PROGRESS_COLUMN, TODO_COLUMN, VALIDATE_PEACH_COLUMN, DONE_COLUMN];
for (let i = 0; i < allColumns.length; i++) {
let cards = await github.projects.listCards({ column_id: allColumns[i] });
let card = cards.data.find(x => x.content_url == content_url);
if (card) {
return card;
}
}
console.log("Card not found for: " + content_url);
return null;
}
//
async function removeAssignees(issue){
const oldAssignees = issue.assignees.map(x => x.login);
if (oldAssignees.length !== 0) {
console.log("Removing assignees: " + oldAssignees.join(", "));
await github.issues.removeAssignees({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
assignees: oldAssignees
});
}
}
//
async function addAssignee(issue, login) {
console.log("Assigning to: " + login);
await github.issues.addAssignees({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
assignees: [login]
});
}
//
async function processIssue(issue) {
await removeAssignees(issue);
addAssignee(issue, context.payload.pull_request.user.login);
if (issue.state == "open") {
const card = await findCard(issue.url);
const newColumn = context.payload.review.state == "approved" ? REVIEW_APPROVED_COLUMN : IN_PROGRESS_COLUMN;
if (card && card.column_id != newColumn) {
console.log("Moving card");
github.projects.moveCard({ card_id: card.id, position: "bottom", column_id: newColumn });
}
}
}
//
let processPR = true;
const pr = context.payload.pull_request;
const matches = pr.body == null ? null : pr.body.match(/(close|closes|closed|fix|fixes|fixed|resolve|resolves|resolved)\s*#\d+/gi);
if (matches) {
for (let i = 0; i < matches.length; i++) {
console.log("Processing linked issue: " + matches[i]);
let linkedIssue = await getIssue(matches[i].split("#")[1]);
if (linkedIssue) {
processPR = false;
processIssue(linkedIssue);
}
}
}
if (processPR) {
console.log("Processing PR: #" + pr.number);
const issue = await getIssue(pr.number);
if (issue) {
processIssue(issue);
}
}
console.log("Done");