forked from 030/gomod-go-version-updater-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
150 lines (125 loc) · 4.48 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import logging
import os
import re
import sys
from pathlib import Path
from typing import Tuple
import requests
DOCKERFILE = "Dockerfile"
GO_MOD_FILE = "go.mod"
GO_MOD_GO_VERSION_REGEX = r"go\s\d+.*"
GO_VERSIONS_URL = "https://go.dev/dl/?mode=json"
LOGGING_LEVEL = os.getenv(
"GOMOD_GO_VERSION_UPDATER_ACTION_LOGGING_LEVEL", logging.INFO
)
def configure_logging(level=logging.INFO):
logging.basicConfig(
level=level,
format="%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
def get_latest_go_version():
try:
response = requests.get(GO_VERSIONS_URL)
response.raise_for_status()
data = response.json()
match = re.match(r"go(\d+)\.(\d+)\.(\d+)", data[0]["version"])
return match.groups() if match else ("", "", "")
except requests.RequestException as e:
logging.error(f"Error fetching data from {GO_VERSIONS_URL}: {e}")
sys.exit(1)
def get_go_version_from_mod_file(go_mod_file: str) -> Tuple[str, bool]:
with open(go_mod_file, "r") as file:
content = file.read()
match = re.search(r"go\s(\d+)\.(\d+)\.?(\d+)?", content)
if not match:
raise ValueError(f"No Go version defined in file: {go_mod_file}")
major, minor, patch = match.groups()
version = f"{major}.{minor}" + (f".{patch}" if patch else "")
return version, bool(patch)
def update_go_version_in_mod_file(
go_mod_file: str, current_version: str, new_version: str
):
try:
with open(go_mod_file, "r") as file:
content = file.read()
content = re.sub(GO_MOD_GO_VERSION_REGEX, f"go {new_version}", content)
with open(go_mod_file, "w") as file:
file.write(content)
logging.info(
f"bump golang version from {current_version} to {new_version}"
)
except FileNotFoundError:
logging.info(f"File not found: {go_mod_file}")
def update_go_version_in_directory(
new_major: str,
new_minor: str,
new_patch: str,
dir: str = None,
):
if dir is None:
dir = os.getcwd()
for path in Path(dir).rglob(GO_MOD_FILE):
logging.debug(f"Found go.mod file: {path}")
current_version, has_patch = get_go_version_from_mod_file(path)
new_major_minor = f"{new_major}.{new_minor}"
if has_patch:
update_go_version_in_mod_file(
path,
current_version,
f"{new_major_minor}.{new_patch}",
)
continue
update_go_version_in_mod_file(
path, current_version, f"{new_major_minor}"
)
def update_dockerfile_version_in_directory(
new_major: str, new_minor: str, new_patch: str
):
for root, _, files in os.walk(os.getcwd()):
for file in files:
if file == DOCKERFILE:
dockerfile_path = os.path.join(root, file)
update_dockerfile_version(
dockerfile_path,
new_major,
new_minor,
new_patch,
)
def update_dockerfile_version(
dockerfile_path: str, new_major: str, new_minor: str, new_patch: str
):
with open(dockerfile_path, "r") as file:
lines = file.readlines()
updated_lines = []
three_digit_pattern = re.compile(r"FROM\sgolang:(\d+\.\d+\.\d+)")
two_digit_pattern = re.compile(r"FROM\sgolang:(\d+\.\d+)")
for line in lines:
match = three_digit_pattern.search(line) or two_digit_pattern.search(
line
)
if match:
version = match.group(1)
new_version = (
f"{new_major}.{new_minor}.{new_patch}"
if "." in version
else f"{new_major}.{new_minor}"
)
updated_lines.append(line.replace(version, new_version))
logging.info(f"bump golang version from {version} to {new_version}")
else:
updated_lines.append(line)
with open(dockerfile_path, "w") as file:
file.writelines(updated_lines)
logging.info(
f"Updated Dockerfile to version {new_major}.{new_minor}.{new_patch}"
)
def main():
configure_logging(LOGGING_LEVEL)
latest_major, latest_minor, latest_patch = get_latest_go_version()
update_dockerfile_version_in_directory(
latest_major, latest_minor, latest_patch
)
update_go_version_in_directory(latest_major, latest_minor, latest_patch)
if __name__ == "__main__":
main()