Skip to content

Commit 6d56b7e

Browse files
committed
Expand release-information to include git tags
Ticket: ENT-13778
1 parent 38f4746 commit 6d56b7e

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import re
2+
import os
3+
import subprocess
4+
from collections import OrderedDict
5+
from cfbs.utils import (
6+
write_json,
7+
)
8+
9+
CACHE_DIR = "cache/git/github.com"
10+
TARGET_DIR = "git/github.com"
11+
12+
TAG_REGEX = re.compile(r"v?\d+\.\d+\.\d+(-\d+)?")
13+
14+
REPOS = [
15+
"cfengine/core",
16+
"cfengine/enterprise",
17+
"cfengine/nova",
18+
"cfengine/mission-portal",
19+
"cfengine/buildscripts",
20+
"cfengine/masterfiles",
21+
]
22+
23+
24+
def clone_or_update_repo(repo):
25+
# Clone repo if not present, else fetch latest version
26+
repo_path = os.path.join(CACHE_DIR, repo)
27+
28+
if os.path.isdir(repo_path):
29+
print(f"Updating {repo}...")
30+
subprocess.run(["git", "fetch", "--tags"], cwd=repo_path, check=True)
31+
32+
else:
33+
print(f"Cloning {repo}...")
34+
os.makedirs(os.path.dirname(repo_path), exist_ok=True)
35+
subprocess.run(
36+
["git", "clone", f"git@github.com:{repo}.git", repo_path],
37+
check=True,
38+
)
39+
return repo_path
40+
41+
42+
def get_commit_shas_from_tags(repo_path):
43+
# Returns a mapping of git tag to commit SHA for all version tags in the repo
44+
output = (
45+
subprocess.check_output(["git", "show-ref", "--tags"], cwd=repo_path)
46+
.decode()
47+
.strip()
48+
)
49+
tag_map = {}
50+
51+
for line in output.splitlines():
52+
ref = line.split()[1]
53+
tag = ref.split("refs/tags/")[1]
54+
if re.fullmatch(TAG_REGEX, tag):
55+
sha = (
56+
subprocess.check_output(
57+
["git", "log", "-n", "1", "--format=%H", tag], cwd=repo_path
58+
)
59+
.decode()
60+
.strip()
61+
)
62+
tag_map[tag] = sha
63+
64+
return tag_map
65+
66+
67+
def build_tag_map(repo):
68+
repo_path = clone_or_update_repo(repo)
69+
tag_map = get_commit_shas_from_tags(repo_path)
70+
71+
return sort_git_tags(tag_map)
72+
73+
74+
def write_tag_map(repo, tag_map):
75+
repo_dir = os.path.join(TARGET_DIR, repo)
76+
os.makedirs(repo_dir, exist_ok=True)
77+
write_json(f"{repo_dir}/tags.json", tag_map)
78+
79+
80+
def sort_git_tags(tag_map):
81+
# Sorts git tags by version descending
82+
return OrderedDict(
83+
sorted(
84+
tag_map.items(),
85+
reverse=True,
86+
key=lambda item: tuple(
87+
int(x) for x in item[0].lstrip("v").replace("-", ".").split(".")
88+
),
89+
)
90+
)
91+
92+
93+
def generate_git_tags_map():
94+
os.makedirs(TARGET_DIR, exist_ok=True)
95+
os.makedirs(CACHE_DIR, exist_ok=True)
96+
for repo in REPOS:
97+
print(f"\nProcessing {repo}...")
98+
tag_map = build_tag_map(repo)
99+
write_tag_map(repo, tag_map)

src/cfengine_cli/masterfiles/generate_release_information.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
from cfengine_cli.masterfiles.check_download_matches_git import (
99
check_download_matches_git,
1010
)
11+
from cfengine_cli.masterfiles.generate_git_tags import (
12+
generate_git_tags_map,
13+
)
1114
from cfbs.utils import (
1215
get_json,
1316
immediate_subdirectories,
@@ -46,6 +49,8 @@ def generate_release_information_impl(
4649

4750
generate_release_history()
4851

52+
generate_git_tags_map()
53+
4954
if check:
5055
print(
5156
"Downloading releases of masterfiles from git (github.com) and generating "

0 commit comments

Comments
 (0)