Skip to content
Snippets Groups Projects

Refactor 'check_pipeline_status' and add 'trigger_pipeline'

Merged Tobias Kahlki requested to merge add-trigger-pipeline into master
3 files
+ 162
40
Compare changes
  • Side-by-side
  • Inline
Files
3
  • Rename 'check_pipeline_status' to 'mirror_pipeline_result' in order to
    better reflect what the function does.
    
    Move determination of the pipeline out of this function to a new
    function 'get_pipelines' so that this can be tested separately and we
    can do different things depending on whether a pipeline exists or not.
    
    Add function 'trigger_pipeline' to trigger a pipeline on a branch.
    
    BCS 746-000635
get_pipelines.py 0 → 100755
+ 91
0
#!/usr/bin/env python3
import common
import argparse
import sys
from gitlab import Gitlab
from gitlab.v4.objects import Project
def get_pipelines(project: Project, commit, ref: str):
"""
Get all pipelines for a given commit and ref.
The ref can be negated with a preceding '^', e.g. '^master' finds pipelines on any
ref different from 'master'.
"""
# Find pipelines for commit
pipelines = project.pipelines.list(sha=commit, retry_transient_errors=True)
# Remove pipelines not matching the ref condition
if ref:
for p in pipelines[:]:
if (
ref.startswith("^")
and p.ref == ref[1:]
or not ref.startswith("^")
and p.ref != ref
):
pipelines.remove(p)
if not pipelines:
raise LookupError(
"No pipeline for commit '%s'%s found"
% (commit, " on ref '%s'" % ref if ref else "")
)
return pipelines
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--gitlab-url",
help="""URL to the GitLab instance""",
dest="gitlab_url",
required=True,
)
parser.add_argument(
"--token",
help="""GitLab REST API private access token""",
dest="token",
required=True,
)
parser.add_argument(
"--project",
help="""name of the GitLab project""",
dest="project",
required=True,
)
parser.add_argument(
"--commit",
help="""sha of the project commit to check""",
dest="commit",
required=True,
)
parser.add_argument(
"--ref",
help="""ref the pipeline ran on (can be negated with preceding '^')""",
dest="ref",
default="",
required=False,
)
args, _ = parser.parse_known_args()
gitlab = Gitlab(args.gitlab_url, private_token=args.token)
project = common.get_project(gitlab, args.project)
try:
pipelines = get_pipelines(project, args.commit, args.ref)
except LookupError as e:
sys.exit(e)
for p in pipelines:
print(p.id)
sys.exit(0)
if __name__ == "__main__":
main()
Loading