forked from 030/gomod-go-version-updater-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
118 lines (104 loc) · 3.71 KB
/
main.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
import logging
import re
import requests
import sys
from typing import Tuple
GO_MOD_FILE = "go.mod"
GO_MOD_GO_VERSION_REGEX = r"go\s\d+.*"
GO_VERSIONS_URL = "https://go.dev/dl/?mode=json"
def determine_whether_go_version_in_go_mod_file_contains_patch_version() -> (
Tuple[str, bool]
):
patch = False
version = ""
try:
with open(GO_MOD_FILE, "r") as file:
major, minor, patch = "", "", ""
content = file.read()
go_version = re.search(r"go\s(\d+)\.(\d+)\.?(\d+)?", content)
if go_version is None:
raise ValueError(
f"no golang version defined in file: {GO_MOD_FILE}"
)
major, minor, patch = (
go_version.group(1),
go_version.group(2),
go_version.group(3),
)
logging.debug(
f"major: {major}, minor: {minor} and patch: {patch} found in go.mod"
)
version = f"{major}.{minor}"
if patch:
version = f"{major}.{minor}.{patch}"
patch = True
logging.debug(f"go version: {version}. Patch: {patch}")
logging.debug(
f"current golang version that is defined in the go.mod: {version}"
)
return version, patch
except FileNotFoundError:
raise ValueError(f"file not found: {GO_MOD_FILE}")
except Exception as e:
raise ValueError(f"an error occurred: {e}")
def get_latest_go_version():
major, minor, patch = "", "", ""
try:
response = requests.get(GO_VERSIONS_URL)
response.raise_for_status()
data = response.json()
latest_version = data[0]["version"]
logging.debug(
f"latest go version: {latest_version} according to: {GO_VERSIONS_URL}"
)
version_regex = re.compile(r"go(\d+)\.(\d+)\.(\d+)")
match_data = version_regex.match(latest_version)
if match_data:
major, minor, patch = match_data.groups()
else:
logging.info("No version found in the input string.")
except requests.exceptions.RequestException as e:
logging.info(f"Error fetching data: {GO_VERSIONS_URL} {e}")
sys.exit(1)
logging.debug(f"latest go major: {major}, minor: {minor} and {patch}")
return major, minor, patch
def regex_replace_go_version_in_go_mod_file(
current_version: str, replacement: str
):
try:
with open(GO_MOD_FILE, "r") as file:
content = file.read()
modified_content = re.sub(
GO_MOD_GO_VERSION_REGEX, f"go {replacement}", content
)
with open(GO_MOD_FILE, "w") as file:
file.write(modified_content)
logging.info(
f"bump golang version in go.mod file from {current_version} to {replacement}"
)
except FileNotFoundError:
logging.info(f"file not found: {GO_MOD_FILE}")
except Exception as e:
logging.info(f"an error occurred: {e}")
def configure_logging():
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
def main():
configure_logging()
latest_major, latest_minor, latest_patch = get_latest_go_version()
current_version, patch = (
determine_whether_go_version_in_go_mod_file_contains_patch_version()
)
if patch:
regex_replace_go_version_in_go_mod_file(
current_version, f"{latest_major}.{latest_minor}.{latest_patch}"
)
return
regex_replace_go_version_in_go_mod_file(
current_version, f"{latest_major}.{latest_minor}"
)
if __name__ == "__main__":
main()