-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathconfig.py
More file actions
142 lines (120 loc) · 5.06 KB
/
config.py
File metadata and controls
142 lines (120 loc) · 5.06 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
130
131
132
133
134
135
136
137
138
139
140
141
142
import json
from collections.abc import Mapping
from typing import Any, NamedTuple
import github_action_utils as gha_utils # type: ignore
LATEST_RELEASE_TAG = "release-tag"
LATEST_RELEASE_COMMIT_SHA = "release-commit-sha"
DEFAULT_BRANCH_COMMIT_SHA = "default-branch-sha"
UPDATE_VERSION_WITH_LIST = [
LATEST_RELEASE_TAG,
LATEST_RELEASE_COMMIT_SHA,
DEFAULT_BRANCH_COMMIT_SHA,
]
class ActionEnvironment(NamedTuple):
repository: str
base_branch: str
event_name: str
@classmethod
def from_env(cls, env: Mapping[str, str]) -> "ActionEnvironment":
return cls(
repository=env["GITHUB_REPOSITORY"],
base_branch=env["GITHUB_REF"],
event_name=env["GITHUB_EVENT_NAME"],
)
class Configuration(NamedTuple):
"""Configuration class for GitHub Actions Version Updater"""
github_token: str | None = None
skip_pull_request: bool = False
git_committer_username: str = "github-actions[bot]"
git_committer_email: str = "github-actions[bot]@users.noreply.github.com"
pull_request_title: str = "Update GitHub Action Versions"
commit_message: str = "Update GitHub Action Versions"
ignore_actions: set[str] = set()
update_version_with: str = LATEST_RELEASE_TAG
pull_request_user_reviewers: set[str] = set()
pull_request_team_reviewers: set[str] = set()
@property
def git_commit_author(self) -> str:
"""git_commit_author option"""
return f"{self.git_committer_username} <{self.git_committer_email}>"
@classmethod
def create(cls, env: Mapping[str, str | None]) -> "Configuration":
"""
Create a Configuration object from environment variables
"""
cleaned_user_config: dict[str, Any] = cls.clean_user_config(
cls.get_user_config(env)
)
return cls(**cleaned_user_config)
@classmethod
def get_user_config(cls, env: Mapping[str, str | None]) -> dict[str, str | None]:
"""
Read user provided input and return user configuration
"""
user_config: dict[str, str | None] = {
"github_token": env.get("INPUT_TOKEN"),
"skip_pull_request": env.get("INPUT_SKIP_PULL_REQUEST"),
"git_committer_username": env.get("INPUT_COMMITTER_USERNAME"),
"git_committer_email": env.get("INPUT_COMMITTER_EMAIL"),
"pull_request_title": env.get("INPUT_PULL_REQUEST_TITLE"),
"commit_message": env.get("INPUT_COMMIT_MESSAGE"),
"ignore_actions": env.get("INPUT_IGNORE"),
"update_version_with": env.get("INPUT_UPDATE_VERSION_WITH"),
"pull_request_user_reviewers": env.get("INPUT_PULL_REQUEST_USER_REVIEWERS"),
"pull_request_team_reviewers": env.get("INPUT_PULL_REQUEST_TEAM_REVIEWERS"),
}
return user_config
@classmethod
def clean_user_config(cls, user_config: dict[str, str | None]) -> dict[str, Any]:
cleaned_user_config: dict[str, Any] = {}
for key, value in user_config.items():
if key in cls._fields:
cleaned_value = getattr(cls, f"clean_{key.lower()}", lambda x: x)(value)
if cleaned_value is not None:
cleaned_user_config[key] = cleaned_value
return cleaned_user_config
@staticmethod
def clean_ignore_actions(value: Any) -> set[str] | None:
if isinstance(value, str) and value.startswith("[") and value.endswith("]"):
ignore_actions = json.loads(value)
if isinstance(ignore_actions, list) and all(
isinstance(item, str) for item in ignore_actions
):
return set(ignore_actions)
else:
gha_utils.error(
"Invalid input for `ignore` field, "
f"expected JSON array of strings but got `{value}`"
)
raise SystemExit(1)
elif value and isinstance(value, str):
return {s.strip() for s in value.strip().split(",") if s}
else:
return None
@staticmethod
def clean_pull_request_user_reviewers(value: Any) -> set[str] | None:
if value and isinstance(value, str):
return {s.strip() for s in value.strip().split(",") if s}
return None
@staticmethod
def clean_pull_request_team_reviewers(value: Any) -> set[str] | None:
if value and isinstance(value, str):
return {s.strip() for s in value.strip().split(",") if s}
return None
@staticmethod
def clean_skip_pull_request(value: Any) -> bool | None:
if value in [1, "1", True, "true", "True"]:
return True
return None
@staticmethod
def clean_update_version_with(value: Any) -> str | None:
if value and value not in UPDATE_VERSION_WITH_LIST:
gha_utils.error(
"Invalid input for `update_version_with` field, "
f"expected one of {UPDATE_VERSION_WITH_LIST} but got `{value}`"
)
raise SystemExit(1)
elif value:
return value
else:
return None