Skip to content

Commit 2cb0ce3

Browse files
author
Anis
authored
Merge pull request #8 from lumapps/handle_multi_services
handle multi services
2 parents f4575cc + ec273cf commit 2cb0ce3

File tree

7 files changed

+109
-21
lines changed

7 files changed

+109
-21
lines changed

action.yml

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ author: 'Sébastien Boulle'
77
branding:
88
icon: 'bookmark'
99
color: 'green'
10+
inputs:
11+
github_token:
12+
description: |
13+
The github token to be used to fetch the repository
14+
15+
Notes:
16+
this will become required in the next version
17+
tag_prefix:
18+
description: if you tags follow the `prefix/version` pattern
19+
required: false
20+
path_filters:
21+
description: |
22+
A space separated list of git path blob regex to be used to filters the commits.
23+
This can be used to keep only relevant commits by filtering on relevant files.
24+
Example:
25+
path_filters: "src/ doc/ tests/*/qa/*"
1026
outputs:
1127
changelog:
1228
description: 'The generated changelog'
@@ -17,13 +33,30 @@ runs:
1733
- name: Generate
1834
id: generate
1935
run: |
36+
# install de dependencies
2037
sudo apt-get install python3-wheel -y
2138
pip3 install -r ${{ github.action_path }}/requirements.txt
39+
40+
# define the environment variables
2241
export PYTHONPATH=${{ github.action_path }}:$PYTHONPATH
23-
export CHANGELOG=$(python3 -m changelog_generator)
24-
export CHANGELOG="${CHANGELOG//'%'/'%25'}"
25-
export CHANGELOG="${CHANGELOG//$'\n'/'%0A'}"
26-
export CHANGELOG="${CHANGELOG//$'\r'/'%0D'}"
42+
43+
# for retro-compatibility purpose. once github_token is required, then no need for the if
44+
if test ! -z "${{ inputs.github_token }}"
45+
then
46+
export GITHUB_TOKEN=${{ inputs.github_token }}
47+
fi
48+
49+
# generate the change log
50+
CHANGELOG=$(python3 -m changelog_generator \
51+
--tag_prefix ${{ inputs.tag_prefix }} \
52+
--path_filters ${{ inputs.path_filters }} \
53+
| tr '%' '%25' | tr '\n' '%0A' | tr '\r' '%0D'
54+
)
55+
56+
# truncate the release note to not bloat the 65536 bytes max limit
57+
CHANGELOG=${CHANGELOG:0:65200}
58+
59+
# output the changelog
2760
echo "::set-output name=changelog::$CHANGELOG"
2861
2962
shell: bash

changelog_generator/__main__.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from argparse import ArgumentParser
23
from typing import List, NamedTuple, Sequence
34

45
from jinja2 import Environment, FileSystemLoader
@@ -55,7 +56,24 @@ def get_commit_but_types(
5556

5657

5758
def main() -> None:
58-
repository = RepositoryManager("./", os.environ.get("TAG_PREFIX"))
59+
parser = ArgumentParser()
60+
parser.add_argument(
61+
"-t",
62+
"--tag_prefix",
63+
nargs="?", # optional argument
64+
help="Filter tag with this prefix. Note: also available as TAG_PREFIX env var",
65+
)
66+
parser.add_argument(
67+
"-p",
68+
"--path_filters",
69+
nargs="*", # optional list
70+
help="Filter commits with this semicolon separated git path regex",
71+
)
72+
args = parser.parse_args()
73+
#
74+
prefix = args.tag_prefix or os.environ.get("TAG_PREFIX")
75+
filter_paths = args.path_filters
76+
repository = RepositoryManager(uri="./", prefix=prefix, filter_paths=filter_paths)
5977

6078
commits = repository.commits_since_last_tag
6179
trees: List[CommitTree] = []

changelog_generator/repository_manager.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ class RepositoryManager:
1818
organization: str = ""
1919
name: str = ""
2020

21-
def __init__(self, uri: str, prefix: str = "") -> None:
21+
def __init__(
22+
self, uri: str, prefix: str = None, filter_paths: Sequence[str] = None
23+
) -> None:
24+
self.filter_paths = filter_paths or []
25+
self.prefix = prefix
2226
self.repository = Repo(uri)
2327
self.tag_names: List[str] = []
2428
if self.repository.bare:
@@ -28,7 +32,6 @@ def __init__(self, uri: str, prefix: str = "") -> None:
2832
if res:
2933
self.organization = res.group("organization")
3034
self.name = res.group("repository")
31-
3235
self.tag_manager = (
3336
PrefixedTagManager(self.repository, prefix)
3437
if prefix
@@ -58,7 +61,13 @@ def commits_since_last_tag(self) -> Sequence[Commit]:
5861
else:
5962
revision = self.current_tag
6063

64+
options = {"no_merges": True}
65+
if self.filter_paths:
66+
options["paths"] = self.filter_paths
67+
68+
commits = self.repository.iter_commits(revision, **options)
69+
6170
return tuple(
6271
Commit(hexsha=commit.hexsha, summary=commit.summary, message=commit.message)
63-
for commit in self.repository.iter_commits(revision, no_merges=True)
72+
for commit in commits
6473
)

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import pytest
66
from git import Repo
77

8+
from changelog_generator.repository_manager import RepositoryManager
9+
810

911
@pytest.fixture(scope="session", autouse=True)
1012
def core_repo() -> Repo:

tests/test_path_filtering.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from pathlib import Path
2+
3+
from git import Repo
4+
5+
from changelog_generator.repository_manager import RepositoryManager
6+
7+
8+
def generate_service_related_paths(service: str, include_common: bool = True):
9+
common_paths = [f"core/common"]
10+
service_paths = [f"core/{service}"]
11+
return service_paths if not include_common else common_paths + service_paths
12+
13+
14+
def test_get_cms_changelog(core_repo: Repo):
15+
# GIVEN
16+
path = str(Path(core_repo.git_dir).parent)
17+
service = "cms"
18+
19+
# WHEN
20+
repository = RepositoryManager(
21+
path, service, generate_service_related_paths(service)
22+
)
23+
24+
# THEN
25+
current_tag = repository.current_tag
26+
assert current_tag
27+
previous_tag = repository.previous_tag
28+
assert previous_tag
29+
commits = repository.commits_since_last_tag
30+
assert commits

tests/test_prefixed_tag.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,16 @@
66

77

88
def test_get_cms_changelog(core_repo: Repo):
9+
# GIVEN
910
path = str(Path(core_repo.git_dir).parent)
10-
repository = RepositoryManager(path, "cms")
11-
current_tag = repository.current_tag
12-
assert current_tag
13-
previous_tag = repository.previous_tag
14-
assert previous_tag
15-
16-
commits = repository.commits_since_last_tag
17-
assert commits
1811

12+
# WHEN
13+
repository = RepositoryManager(path, "cms")
1914

20-
def test_get_rbac_changelog(core_repo: Repo):
21-
path = str(Path(core_repo.git_dir).parent)
22-
repository = RepositoryManager(path, "rbac")
15+
# THEN
2316
current_tag = repository.current_tag
2417
assert current_tag
2518
previous_tag = repository.previous_tag
2619
assert previous_tag
27-
2820
commits = repository.commits_since_last_tag
2921
assert commits

tests/test_simple_tag.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66

77

88
def test_get_organization_changelog(organization_repo: Repo):
9+
# GIVEN
910
path = str(Path(organization_repo.git_dir).parent)
11+
12+
# WHEN
1013
repository = RepositoryManager(path)
14+
15+
# THEN
1116
current_tag = repository.current_tag
1217
assert current_tag
1318
previous_tag = repository.previous_tag
1419
assert previous_tag
15-
1620
commits = repository.commits_since_last_tag
1721
assert commits

0 commit comments

Comments
 (0)