-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtrack_issues_and_pull_requests.py
executable file
·169 lines (149 loc) · 5.53 KB
/
track_issues_and_pull_requests.py
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python3
"""
Ensure all open issues are tracked in the Backlog project in the Pending Review
column and all open pull requests are tracked in the Active Sprint project in
the Code Review column.
"""
# Standard library
import argparse
import sys
import traceback
# First-party/Local
import ccos.log
from ccos import gh_utils
ISSUES_COLUMN = "Pending Review"
ISSUES_PROJECT = "Backlog"
LOG = ccos.log.setup_logger()
PULL_REQUESTS_COLUMN = "Code Review"
PULL_REQUESTS_PROJECT = "Active Sprint"
class ScriptError(Exception):
def __init__(self, message, code=None):
self.code = code if code else 1
message = "({}) {}".format(self.code, message)
super(ScriptError, self).__init__(message)
def setup():
"""Instantiate and configure argparse and logging.
Return argsparse namespace.
"""
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument(
"-n",
"--dryrun",
action="store_true",
help="dry run: do not make any changes",
)
args = ap.parse_args()
return args
def get_untracked_issues(github_client):
LOG.info("Searching for untracked open issues")
# https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests
query = (
"org:creativecommons state:open -project:creativecommons/7"
" -project:creativecommons/10 type:issue"
)
LOG.debug(f"issues query: {query}")
untracked_issues = list(github_client.search_issues(query=query))
untracked_issues.sort(key=lambda x: f"{x.repository.name}{x.number:09}")
return untracked_issues
def track_issues(args, gh_org_cc, untracked_issues):
if not untracked_issues:
LOG.info(
f"No untracked issues to add to {ISSUES_PROJECT}: {ISSUES_COLUMN}"
)
return
LOG.info(
f"Adding {len(untracked_issues)} issues to {ISSUES_PROJECT}:"
f" {ISSUES_COLUMN}"
)
for project in gh_org_cc.get_projects():
if project.name != ISSUES_PROJECT:
continue
for column in project.get_columns():
if column.name != ISSUES_COLUMN:
continue
for issue in untracked_issues:
if not args.dryrun:
no_op = ""
column.create_card(
content_id=issue.id,
content_type="Issue",
)
else:
no_op = "(no-op) "
LOG.change_indent(+1)
LOG.success(
f"{no_op}{issue.repository.name}#{issue.number}"
f" {issue.title}"
)
LOG.change_indent(-1)
def get_untracked_pull_requests(github_client):
LOG.info("Searching for untracked open pull requests")
# https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests
query = (
"org:creativecommons state:open -project:creativecommons/7"
" -project:creativecommons/10 type:pr"
)
LOG.debug(f"pull request query: {query}")
untracked_pull_requests = list(github_client.search_issues(query=query))
untracked_pull_requests.sort(
key=lambda x: f"{x.repository.name}{x.number:09}"
)
return untracked_pull_requests
def track_pull_requests(args, gh_org_cc, untracked_pull_requests):
if not untracked_pull_requests:
LOG.info(
f"No untracked pull requests to add to {PULL_REQUESTS_PROJECT}:"
f" {PULL_REQUESTS_COLUMN}"
)
return
LOG.info(
f"Adding {len(untracked_pull_requests)} pull requests to"
f" {PULL_REQUESTS_PROJECT}: {PULL_REQUESTS_COLUMN}"
)
for project in gh_org_cc.get_projects():
if project.name != PULL_REQUESTS_PROJECT:
continue
for column in project.get_columns():
if column.name != PULL_REQUESTS_COLUMN:
continue
for pull_request in untracked_pull_requests:
if not args.dryrun:
no_op = ""
column.create_card(
content_id=pull_request.id,
# Based on the code samples I found elsewhere, I
# expect this to be "PullRequest", but that doesn't
# work and "Issue" does ¯\_(ツ)_/¯
content_type="Issue",
)
else:
no_op = "(no-op) "
LOG.change_indent(+1)
LOG.success(
f"{no_op}{pull_request.repository.name}"
f"#{pull_request.number} {pull_request.title}"
)
LOG.change_indent(-1)
def main():
args = setup()
github_client = gh_utils.set_up_github_client()
gh_org_cc = gh_utils.get_cc_organization(github_client)
untracked_issues = get_untracked_issues(github_client)
track_issues(args, gh_org_cc, untracked_issues)
untracked_pull_requests = get_untracked_pull_requests(github_client)
track_pull_requests(args, gh_org_cc, untracked_pull_requests)
if __name__ == "__main__":
try:
main()
except SystemExit as e:
sys.exit(e.code)
except KeyboardInterrupt:
LOG.info("Halted via KeyboardInterrupt.")
sys.exit(130)
except ScriptError:
error_type, error_value, error_traceback = sys.exc_info()
LOG.critical(f"{error_value}")
sys.exit(error_value.code)
except Exception:
LOG.error(f"Unhandled exception: {traceback.format_exc()}")
sys.exit(1)