ci: testbed-cdrouter.py: add gitlab-branch and gitlab-job tags
When looking for regressions, it's handy to know on which branch/job the
test was run, so let's add support for branch/job tagging.
This commit introduces the `GitLabEnvironment` class, which extracts
GitLab CI environment variables related to branch names and job IDs.
It generates CDRouter-compatible tags that denote both the branch and
the CI job ID for the test execution. The priority for determining the
branch name is as follows:
1. CI_MERGE_REQUEST_SOURCE_BRANCH_NAME (for merge request pipelines)
2. CI_COMMIT_BRANCH (for regular branch pipelines)
The generated tags are formatted as:
- `gitlab-branch.{sanitized-branch}` for branch information
- `gitlab-job.{job_id}` for CI job identification
These tags are added to the existing tags generated by the
OpenWrtSystemInfo class and any user-defined tags. This addition aims
to simplify regression analysis by making it clear on which branch and
in which CI job a particular test execution occurred.
Signed-off-by: Petr Štetiar <petr.stetiar@prplfoundation.org>
This commit is contained in:
parent
7fc53bff8d
commit
addc70be89
@ -9,6 +9,8 @@ import time
|
||||
import logging
|
||||
import humanize
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
from types import SimpleNamespace
|
||||
from http.client import RemoteDisconnected
|
||||
from requests.exceptions import ConnectionError
|
||||
@ -67,6 +69,68 @@ class OpenWrtSystemInfo:
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class GitLabEnvironment:
|
||||
"""GitLab CI environment information handler for CDRouter tagging.
|
||||
|
||||
This class extracts environment information from GitLab CI environment variables
|
||||
and generates CDRouter-compatible tags. It prioritizes merge request source
|
||||
branches over regular commit branches and includes CI job ID information.
|
||||
"""
|
||||
|
||||
branch_name: Optional[str] = None
|
||||
job_id: Optional[str] = None
|
||||
|
||||
def __post_init__(self):
|
||||
"""Initialize GitLab CI info by reading environment variables.
|
||||
|
||||
Priority order:
|
||||
1. CI_MERGE_REQUEST_SOURCE_BRANCH_NAME (for merge request pipelines)
|
||||
2. CI_COMMIT_BRANCH (for branch pipelines)
|
||||
"""
|
||||
if self.branch_name is None:
|
||||
self.branch_name = os.getenv(
|
||||
"CI_MERGE_REQUEST_SOURCE_BRANCH_NAME"
|
||||
) or os.getenv("CI_COMMIT_BRANCH")
|
||||
if self.job_id is None:
|
||||
self.job_id = os.getenv("CI_JOB_ID")
|
||||
|
||||
@property
|
||||
def branch(self):
|
||||
"""Get the current branch name from GitLab CI environment.
|
||||
|
||||
Returns:
|
||||
str or None: The branch name if available, None otherwise.
|
||||
"""
|
||||
return self.branch_name
|
||||
|
||||
@property
|
||||
def job(self):
|
||||
"""Get the current CI job ID from GitLab CI environment.
|
||||
|
||||
Returns:
|
||||
str or None: The job ID if available, None otherwise.
|
||||
"""
|
||||
return self.job_id
|
||||
|
||||
def as_tags(self):
|
||||
"""Generate CDRouter tags from GitLab CI information.
|
||||
|
||||
Returns:
|
||||
str or None: Comma-separated tags in format 'gitlab-branch.{branch},gitlab-job.{job_id}'
|
||||
if information is available, None otherwise.
|
||||
"""
|
||||
tag_mapping = {"gitlab-branch": self.branch, "gitlab-job": self.job}
|
||||
|
||||
tags = [
|
||||
f"{prefix}.{TestbedCDRouter.sanitize_tag(value)}"
|
||||
for prefix, value in tag_mapping.items()
|
||||
if value
|
||||
]
|
||||
|
||||
return ",".join(tags) if tags else None
|
||||
|
||||
|
||||
class TestbedCDRouter:
|
||||
def __init__(self, args):
|
||||
self.args = args
|
||||
@ -133,12 +197,18 @@ class TestbedCDRouter:
|
||||
)
|
||||
)
|
||||
|
||||
tags = None
|
||||
tags = []
|
||||
if self.args.system_info:
|
||||
tags = OpenWrtSystemInfo(self.args.system_info).as_tags().split(",")
|
||||
tags.extend(OpenWrtSystemInfo(self.args.system_info).as_tags().split(","))
|
||||
|
||||
gitlab_tags = GitLabEnvironment().as_tags()
|
||||
if gitlab_tags:
|
||||
tags.extend(gitlab_tags.split(","))
|
||||
|
||||
if self.args.tags:
|
||||
tags += self.args.tags.split(",")
|
||||
tags.extend(self.args.tags.split(","))
|
||||
|
||||
logging.debug("Final tags being sent to CDRouter: {}".format(tags))
|
||||
|
||||
options = Options(tags=tags)
|
||||
job = Job(package_id=p.id, options=options)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user