Skip to content
Snippets Groups Projects
Commit 7eb3534d authored by Ralf Jung's avatar Ralf Jung
Browse files

gitlab-extract: add support for excluding a branch

parent a5f0e7d8
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse, pprint, sys, glob, zipfile import argparse, pprint, sys, glob, zipfile, subprocess
import requests import requests
import parse_log import parse_log
...@@ -40,6 +40,9 @@ parser.add_argument("-c", "--commits", ...@@ -40,6 +40,9 @@ parser.add_argument("-c", "--commits",
parser.add_argument("-a", "--artifacts", parser.add_argument("-a", "--artifacts",
dest="artifacts", dest="artifacts",
help="Location of the artifacts (following GitLab's folder structure). If not given (which should be the common case), the artifacts will be downloaded from GitLab.") help="Location of the artifacts (following GitLab's folder structure). If not given (which should be the common case), the artifacts will be downloaded from GitLab.")
parser.add_argument("-b", "--blacklist-branch",
dest="blacklist_branch",
help="Skip the commit if it is contained in the given branch.")
args = parser.parse_args() args = parser.parse_args()
log_file = sys.stdout if args.file == "-" else open(args.file, "a") log_file = sys.stdout if args.file == "-" else open(args.file, "a")
...@@ -60,7 +63,13 @@ BREAK = False ...@@ -60,7 +63,13 @@ BREAK = False
for commit in parse_log.parse_git_commits(args.commits): for commit in parse_log.parse_git_commits(args.commits):
if BREAK: if BREAK:
break break
print("Fetching {}...".format(commit)) # test to skip the commit
if args.blacklist_branch is not None:
branches = subprocess.check_output(["git", "branch", "-r", "--contains", commit]).decode("utf-8")
if args.blacklist_branch in map(lambda x: x.strip(), branches.split('\n')):
continue
# Find out more about the commit
print("Fetching {}...".format(commit), end='')
commit_data = req("/projects/{}/repository/commits/{}".format(project['id'], commit)) commit_data = req("/projects/{}/repository/commits/{}".format(project['id'], commit))
if commit_data.status_code != 200: if commit_data.status_code != 200:
raise Exception("Commit not found?") raise Exception("Commit not found?")
...@@ -68,10 +77,12 @@ for commit in parse_log.parse_git_commits(args.commits): ...@@ -68,10 +77,12 @@ for commit in parse_log.parse_git_commits(args.commits):
if builds.status_code != 200: if builds.status_code != 200:
raise Exception("Build not found?") raise Exception("Build not found?")
# iterate over builds by decreasing ID, and look for the artifact # iterate over builds by decreasing ID, and look for the artifact
found_build = False
for build in builds.json(): for build in builds.json():
if build['status'] in ('created', 'pending', 'running'): if build['status'] in ('created', 'pending', 'running'):
# build still not yet done, don't fetch this or any later commit # build still not yet done, don't fetch this or any later commit
BREAK = True BREAK = True
print(" build still in progress, aborting")
break break
if build['status'] != 'success': if build['status'] != 'success':
# build failed or cancelled, skip to next # build failed or cancelled, skip to next
...@@ -100,4 +111,8 @@ for commit in parse_log.parse_git_commits(args.commits): ...@@ -100,4 +111,8 @@ for commit in parse_log.parse_git_commits(args.commits):
log_file.write(build_times.text) log_file.write(build_times.text)
log_file.flush() log_file.flush()
# don't fetch another build # don't fetch another build
found_build = True
print(" success")
break break
if not found_build:
print(" found no succeessful build")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment