This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathenarxbot-assigned
executable file
·138 lines (124 loc) · 4.29 KB
/
enarxbot-assigned
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
#!/usr/bin/python3
# SPDX-License-Identifier: Apache-2.0
from githubgql import githubgql
import constants
import json
import sys
import os
if os.environ["GITHUB_EVENT_NAME"] not in ["pull_request_target", "issues"]:
sys.exit(0)
owner, repo = os.environ["GITHUB_REPOSITORY"].split("/")
with open(os.environ["GITHUB_EVENT_PATH"]) as f:
event = json.load(f)
if event["action"] != "assigned":
sys.exit(0)
id = event.get('pull_request', None)
if id is None:
id_type = "issue"
id = event['issue']['node_id']
else:
id_type = "pr"
id = id['node_id']
# Find out what projects the issue/PR is in, as well as their assignees.
try:
result = githubgql.graphql(
"""
query($id:ID!, $cardCursor:String, $assigneeCursor:String) {
node(id:$id) {
... on PullRequest {
number
assignees(first:100, after:$assigneeCursor) {
pageInfo { endCursor hasNextPage }
nodes {
login
}
}
projectCards(first:100, archivedStates:NOT_ARCHIVED, after:$cardCursor) {
pageInfo { endCursor hasNextPage }
nodes {
id
column {
id
name
project {
id
name
}
}
}
}
}
... on Issue {
number
assignees(first:100, after:$assigneeCursor) {
pageInfo { endCursor hasNextPage }
nodes {
login
}
}
projectCards(first:100, archivedStates:NOT_ARCHIVED, after:$cardCursor) {
pageInfo { endCursor hasNextPage }
nodes {
id
column {
id
name
project {
id
name
}
}
}
}
}
}
}
""",
id=id,
page={
"cardCursor": ["node", "projectCards"],
"assigneeCursor": ["node", "assignees"],
}
)
except githubgql.TokenError as e:
print(e.error)
sys.exit(0)
# Construct lists of assignees, projects, and columns.
assignees = result["node"]["assignees"]["nodes"]
cards = result["node"]["projectCards"]["nodes"]
columns = {card["column"]["project"]["id"]: card["column"]["name"] for card in cards}
input = None
if len(assignees) > 0 and constants.PROJECTS["Sprint"] not in columns.keys():
# Issues must additionally be present in the "Assigned" column of the
# Planning board to be moved.
if id_type == "issue" and columns.get(constants.PROJECTS["Planning"]) == "Assigned":
# Print status.
print(f"Moving issue {owner}/{repo}#{result['node']['number']} to Sprint board")
input = {
"projectColumnId": constants.COLUMNS["Sprint"]["Assigned"],
"contentId": id
}
if id_type == "pr":
# Print status.
print(f"Moving PR {owner}/{repo}#{result['node']['number']} to Sprint board")
input = {
"projectColumnId": constants.COLUMNS["Sprint"]["Reviewing"],
"contentId": id
}
# If this issue/PR needs to be moved, move it.
if input is not None:
try:
githubgql.graphql(
"""
mutation($input:AddProjectCardInput!) {
addProjectCard(input:$input) {
clientMutationId
}
}
""",
input=input
)
except githubgql.GraphQLError as e:
if e.errors[0]["message"] != "Project already has the associated issue":
raise
print(f"Project already has this card. Skipping addition.")