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-triage
executable file
·103 lines (92 loc) · 3.03 KB
/
enarxbot-triage
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
#!/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"] not in {"opened", "reopened"}:
sys.exit(0)
id = event.get('pull_request', event.get('issue'))
id = id['node_id']
# Find out what projects the issue/PR is in.
try:
result = githubgql.graphql(
"""
query($id:ID!, $cursor:String) {
node(id:$id) {
... on PullRequest {
number
projectCards(first:100, archivedStates:NOT_ARCHIVED, after:$cursor) {
pageInfo { endCursor hasNextPage }
nodes {
id
column {
id
name
project {
id
name
}
}
}
}
}
... on Issue {
number
projectCards(first:100, archivedStates:NOT_ARCHIVED, after:$cursor) {
pageInfo { endCursor hasNextPage }
nodes {
id
column {
id
name
project {
id
name
}
}
}
}
}
}
}
""",
id=id,
cursors={
'cursor': ["node", "projectCards"]
},
)
except githubgql.TokenError as e:
print(e.error)
sys.exit(0)
cards = result["node"]["projectCards"]["nodes"]
projects = {card["column"]["project"]["id"] for card in cards}
# If the issue/PR isn't in the Planning project, add it to Triage.
if constants.PROJECTS["Planning"] not in projects:
# Print status.
print(f"Adding {owner}/{repo}#{result['node']['number']} to Planning board")
# Add the content to the project.
try:
githubgql.graphql(
"""
mutation($input:AddProjectCardInput!) {
addProjectCard(input:$input) {
clientMutationId
}
}
""",
input={
"projectColumnId": constants.COLUMNS["Planning"]["Triage"],
"contentId": id
}
)
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.")