-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargocd-diff
executable file
·143 lines (123 loc) · 4.05 KB
/
argocd-diff
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
#!/usr/bin/env python3
import argparse
import json
import os
import re
import subprocess
import time
from githubkit import GitHub
import asyncio
import yaml
def _get_pr_files() -> list[str]:
# Create GitHub client with your token
gh = GitHub(os.environ["GITHUB_TOKEN"])
# Get the list of files from a specific PR
owner, repo = os.environ["GITHUB_REPOSITORY"].split("/")
pr_number = int(os.environ["GITHUB_REF_NAME"].split("/")[0])
# Get files from the PR
files = gh.rest.pulls.list_files(
owner=owner,
repo=repo,
pull_number=pr_number
)
result = []
# Print the files
for file in files.parsed_data:
result.append(file.filename)
return result
def main() -> None:
"""
Display application diff for GitHub.
"""
parser = argparse.ArgumentParser(description="Display application diff for GitHub.")
parser.add_argument("--revision", help="Diff on the revision")
args = parser.parse_args()
# Get the list of modified files in the current pull request
files = _get_pr_files()
all_app = False
app_prefix = set()
for file in files:
if file.startswith("apps/int/") or file.startswith("apps/prod/"):
file_split = file.split("/")
if len(file_split) >= 4:
app_prefix.add(f"gmf-{file_split[2]}-{file_split[1]}-{file_split[3]}")
else:
app_prefix.add(f"gmf-{file_split[2]}-{file_split[1]}-")
elif file.startswith("apps/values/"):
file_split = file.split("/")
app_prefix.add(f"gmf-{file_split[2]}-")
elif file.startswith("values/"):
all_app = True
break
if all_app:
print("All applications will be checked")
else:
if app_prefix:
print(f"Applications to be checked:")
for prefix in app_prefix:
print(f" - {prefix}")
else:
print("No application to be checked")
return
print()
app_list = json.loads(
subprocess.run(
["argocd", "app", "list", "--output=json"], stdout=subprocess.PIPE, check=True
).stdout.decode("utf-8")
)
apps = {
app["metadata"]["name"]
for app in app_list
if app["spec"]["source"]["repoURL"] == f"[email protected]:{os.environ['GITHUB_REPOSITORY']}.git"
}
with open("data/argocd.yaml", encoding="utf-8") as no_sync_file:
apps_re = [
re.compile(pattern) for pattern in yaml.load(no_sync_file, Loader=yaml.SafeLoader)["apps_re"]
]
for app in apps:
if not all_app:
accept = False
for prefix in app_prefix:
if app.startswith(prefix):
accept = True
break
if not accept:
#print(f"The application {app} is skipped")
continue
accept = False
for app_re in apps_re:
if app_re.match(app):
accept = True
break
if not accept:
continue
start = time.perf_counter()
app_diff_proc = subprocess.run( # pylint: disable=subprocess-run-check
[
"argocd",
"app",
"diff",
"--exit-code=false",
app,
"--loglevel=warn",
f"--revision={args.revision}",
],
stdout=subprocess.PIPE,
encoding="utf-8",
)
end = time.perf_counter()
if app_diff_proc.returncode == 0:
app_diff = app_diff_proc.stdout
if app_diff:
print(f"::group::Diff on application {app}")
print(app_diff)
print("::endgroup::")
else:
print(f"No diff on application {app}")
else:
print(f"Error on application {app}")
print(app_diff_proc.stderr)
print(app_diff_proc.stdout)
print(f"Time to diff application {app}: {end - start:.2f}s")
if __name__ == "__main__":
main()