Skip to content

Commit

Permalink
validate-tags: Support a custom tag_version_regex
Browse files Browse the repository at this point in the history
Some collections don't tag versions as v{version} or {version}. While
collections should use a standard tag name format per the Collection
Requirements, we won't impose this on existing collections.
  • Loading branch information
gotmax23 committed Nov 16, 2022
1 parent 3b215f0 commit 006f606
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/antsibull/collection_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ class CollectionMetadata:
changelog_url: t.Optional[str]
collection_directory: t.Optional[str]
repository: t.Optional[str]
tag_version_regex: t.Optional[str]

def __init__(self, source: t.Optional[t.Mapping[str, t.Any]] = None):
if source is None:
source = {}
self.changelog_url = source.get('changelog-url')
self.collection_directory = source.get('collection-directory')
self.repository = source.get('repository')
self.tag_version_regex = source.get('tag_version_regex')


class CollectionsMetadata:
Expand Down
19 changes: 15 additions & 4 deletions src/antsibull/tagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from antsibull.collection_meta import CollectionsMetadata

TAG_MATCHER: t.Pattern[str] = re.compile(r'^.*refs/tags/(.*)$')
TAG_VERSION_REGEX: t.Pattern[str] = re.compile(r'^v?(.*)$')
mlog = log.fields(mod=__name__)


Expand Down Expand Up @@ -138,8 +139,15 @@ async def _get_collection_tags(
if not repository:
flog.debug("'repository' is None. Exitting...")
return data
tag_version_regex: t.Optional[t.Pattern[str]] = None
if meta_data.tag_version_regex:
try:
tag_version_regex = re.compile(meta_data.tag_version_regex)
except re.error as err:
flog.fields(err=err).error(f'{tag_version_regex} is an invalid regex')
return data
async for tag in _get_tags(repository):
if _normalize_tag(tag) == version:
if _normalize_tag(tag, tag_version_regex) == version:
data['tag'] = tag
break
return data
Expand Down Expand Up @@ -182,7 +190,10 @@ async def _get_tags(repository) -> t.AsyncGenerator[str, None]:
flog.debug(f'git ls-remote output line skipped: {tag}')


def _normalize_tag(tag: str) -> str:
if tag.startswith('v'):
tag = tag[1:]
def _normalize_tag(
tag: str, regex: t.Optional[t.Pattern[str]]
) -> t.Optional[str]:
regex = regex or TAG_VERSION_REGEX
if match := regex.match(tag):
return match.group(1)
return tag

0 comments on commit 006f606

Please sign in to comment.