Repository navigation
Expand file tree
/
Copy pathupdate_backport_status.py
More file actions
110 lines (90 loc) · 3.99 KB
/
Copy pathupdate_backport_status.py
File metadata and controls
110 lines (90 loc) · 3.99 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
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations
import os
import sys
import requests
def get_success_comment(branch: str, pr_url: str, pr_number: str):
shield_url = f"https://img.shields.io/badge/PR-{pr_number}-blue"
comment = f"""### Backport successfully created: {branch}\n\n<table>
<tr>
<th>Status</th>
<th>Branch</th>
<th>Result</th>
</tr>
<tr>
<td>✅</td>
<td>{branch}</td>
<td><a href="{pr_url}"><img src="{shield_url}" alt="PR Link"></a></td>
</tr>
</table>"""
return comment
def get_failure_comment(branch: str, commit_sha_url: str, commit_sha: str):
commit_shield_url = f"https://img.shields.io/badge/Commit-{commit_sha[:7]}-red"
comment = f"""### Backport failed to create: {branch}. View the failure log <a href='https://github.com/{os.getenv("REPOSITORY")}/actions/runs/{os.getenv("RUN_ID")}'> Run details </a>\n
<table>
<tr>
<th>Status</th>
<th>Branch</th>
<th>Result</th>
</tr>
<tr>
<td>❌</td>
<td>{branch}</td>
<td><a href="{commit_sha_url}"><img src='{commit_shield_url}' alt='Commit Link'></a></td>
</tr>
</table>
You can attempt to backport this manually by running:
```bash
cherry_picker {commit_sha[:7]} {branch}
```
This should apply the commit to the {branch} branch and leave the commit in conflict state marking
the files that need manual conflict resolution.
After you have resolved the conflicts, you can continue the backport process by running:
```bash
cherry_picker --continue
```
If you don't have cherry-picker installed, see the [installation guide](https://github.com/apache/airflow/blob/main/dev/README_AIRFLOW3_DEV.md#how-to-backport-pr-with-cherry-picker-cli).
"""
return comment
def add_comments(backport_url: str, target_branch: str, commit_sha: str, source_pr_number: str):
if backport_url.strip() != "EMPTY":
pr_number = backport_url.split("/")[-1]
comment = get_success_comment(branch=target_branch, pr_url=backport_url, pr_number=pr_number)
else:
commit_sha_url = f"https://github.com/{os.getenv('REPOSITORY')}/commit/{commit_sha}"
comment = get_failure_comment(
branch=target_branch, commit_sha_url=commit_sha_url, commit_sha=commit_sha
)
token = os.getenv("GH_TOKEN")
comment_url = f"https://api.github.com/repos/{os.getenv('REPOSITORY')}/issues/{source_pr_number}/comments"
headers = {"Authorization": f"Bearer {token}", "Accept": "application/vnd.github+json"}
data = {"body": comment}
try:
response = requests.post(comment_url, headers=headers, json=data)
if not (response.status_code == 201):
raise requests.HTTPError(response=response)
except requests.HTTPError as e:
print(e)
print(f"Error: Failed to add comments to pr {source_pr_number}")
sys.exit(e.response.status_code)
if __name__ == "__main__":
bp_url = sys.argv[1]
commit = sys.argv[2]
tg_branch = sys.argv[3]
source_pr = sys.argv[4]
add_comments(backport_url=bp_url, target_branch=tg_branch, commit_sha=commit, source_pr_number=source_pr)