-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgithub_reporter.py
More file actions
129 lines (97 loc) · 3.3 KB
/
Copy pathgithub_reporter.py
File metadata and controls
129 lines (97 loc) · 3.3 KB
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
import logging
_logger = logging.getLogger(__name__)
COMMENT_SIGNATURE = """
<sub><sup>
--
🤖 @patchback
I'm built with [octomachinery](https://octomachinery.dev) and
my source is open — https://github.com/sanitizers/patchback-github-app.
</sup></sub>
"""
PROCESS_START_COMMENT = f"""
Beep beep boop!
🤖 Hi, I'm @patchback — your friendly neighborhood robot!
Somebody's just asked me to create a backport of this pull request
into the `{{branch_name}}` branch. So in this comment I report that
I'm on it!
*Please, expect to see further updates regarding my progress in updates
to this comment. Stay tuned!*
If you think I'm experiencing problems, ping `@webknjaz` — my creator.
{COMMENT_SIGNATURE}
"""
PROGRESS_COMMENT = f"""
### {{title!s}}
{{summary}}
{{text}}
{COMMENT_SIGNATURE}
"""
class PullRequestReporter:
def __init__(self, *, checks_api, comments_api, locking_api, branch_name):
self._branch_name = branch_name
self._checks_api = checks_api
self._comments_api = comments_api
self._locking_api = locking_api
self._use_checks_api = False
async def start_reporting(self, pr_head_sha, pr_number, pr_merge_commit):
await self._locking_api.unlock_pr()
await self._comments_api.create_comment(
PROCESS_START_COMMENT.format(branch_name=self._branch_name),
)
try:
await self._checks_api.create_check(pr_head_sha)
except PermissionError as perm_err:
_logger.info(
'Failed to report PR #%d (commit `%s`) backport status '
'updates via Checks API because '
'of insufficient GitHub App Installation privileges to '
'create pull requests: %s',
pr_number, pr_merge_commit, perm_err,
)
else:
self._use_checks_api = True
_logger.info('Checks API is available')
await self._checks_api.update_check()
async def update_progress(
self,
*,
subtitle, text, summary,
):
checks_output = await self._make_comment_from_details(
subtitle, text, summary,
)
if not self._use_checks_api:
return
await self._checks_api.update_check(
status='in_progress',
output=checks_output,
)
async def finish_reporting(
self,
*,
subtitle=None, text=None, summary=None,
conclusion='neutral',
):
checks_output = await self._make_comment_from_details(
subtitle, text, summary,
)
await self._locking_api.lock_pr()
if not self._use_checks_api:
return
await self._checks_api.update_check(
status='completed',
conclusion=conclusion,
output=checks_output,
)
async def _make_comment_from_details(self, subtitle, text, summary):
title = self._checks_api.check_run_name
if subtitle:
title = f'{title}: {subtitle!s}'
checks_output = {
'title': title,
'text': text or '',
'summary': summary or '',
}
await self._comments_api.update_comment(
PROGRESS_COMMENT.format_map(checks_output),
)
return checks_output