-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
autocreate.py
107 lines (93 loc) · 3.35 KB
/
autocreate.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
import contextlib
import os
from github import GithubException
from github_actions_utils.env import github_envs, get_env, get_input
from github_actions_utils.github import get_github
from github_actions_utils.log import github_log_group, summary
import github_action_utils as gha_utils
# def get_gh():
# actor = os.getenv("GITHUB_ACTOR")
# assert actor, "Must set GITHUB_ACTOR"
#
# summary(f"Retrieving {actor} token...", end="")
# token = os.getenv(actor)
# if token:
# actor_token = True
# summary(":white_check_mark:")
# else:
# actor_token = False
# summary(":x:")
# summary("Retrieving GITHUB_TOKEN")
# token = os.getenv("GITHUB_TOKEN")
# if not token:
# exit_(f"User '{actor}' is not allowed to auto create Pull Request")
#
# if actor_token:
# summary(f"Login in as {actor}...", end="")
# gh = Github(auth=Token(token))
# login = gh.get_user().login
# if login != actor:
# if actor_token:
# summary(":x:")
# exit_(f"Token is for user {login}!")
# if actor_token:
# summary(":white_check_mark:")
# return gh
def exit_(message): # TODO to github_actions_utils
gha_utils.error(f"{message}")
exit(1)
@contextlib.contextmanager
def github_log_group_context_manager(text): # TODO to github_actions_utils
print(f"::group::{text}")
yield
print("::endgroup::")
# def check_user_permission():
def main():
# check_user_permission()
actor = github_envs.actor
actor_token = get_env(actor)
gh = None
if actor_token and not actor_token.startswith("ghs"):
with gha_utils.group(f"Logging with {actor}..."):
gh = get_github(actor_token)
if gh.get_user().login != actor:
exit_(f"Token is for user {gh.get_user().login} not for {actor}!")
gha_utils.append_job_summary(f"- Successfully logged as {actor}!")
elif token := github_envs.token:
gh = get_github(token)
if not gh:
exit_(f"{actor} is not allowed to trigger autocreate PR!")
repo = gh.get_current_repo()
current_branch = github_envs.ref_name
draft = get_input("draft", type=bool, default="False")
auto_merge = get_input("auto_merge", type=bool, default="False")
merge_method = get_input("merge_method", "MERGE")
@github_log_group("Creating PR")
def _create_pull():
try:
pr_ = repo.create_pull(
repo.default_branch,
current_branch,
title=current_branch,
body="PR automatically created",
draft=draft,
)
print(f"Created PR {pr_.html_url}")
return pr_
except GithubException as e:
errors_messages = [e.get("message", str(e)) for e in e.data["errors"]]
if len(errors_messages) == 1 and (em := errors_messages[0]).startswith(
"A pull request already exists for"
):
print(em)
return list(repo.get_pulls())[-1]
raise e
pr = _create_pull()
if auto_merge:
@github_log_group("Setting to auto merge")
def _auto_merge():
pr.enable_automerge(merge_method)
print(f"Auto merge enabled for PR {pr.html_url}")
_auto_merge()
if __name__ == "__main__": # pragma: no cover
main()