diff --git a/cbutil/gitlab_api.py b/cbutil/gitlab_api.py index abb39dc637f0e2694ebb34df7d959e7d8a526219..32fd653f1c57508c5714c39403e5046b24eded1e 100644 --- a/cbutil/gitlab_api.py +++ b/cbutil/gitlab_api.py @@ -30,15 +30,20 @@ def get_api_url_commits(base_api_url: str) -> str: return f"{base_api_url}/repository/commits" -def get_last_commit(instance_url: str, project_id: Project_id, branch: str = ''): +def get_last_commit(instance_url: str, project_id: Project_id, branch: str = '', commit=''): url = get_api_url_commits(get_api_url(instance_url, project_id)) + if commit: + url = f"{url}/{commit}" url = add_pagination(url, page=1, per_page=1) params = {'ref_name': branch if branch else 'master'} commit = requests.get(url, params=params) logger.info(f"requesting {url} with params") if commit.status_code != 200: commit.raise_for_status() - return commit.json()[0] + try: + return commit.json()[0] + except KeyError: + return commit.json() def get_commit_infos_from_api(commit: dict) -> dict: @@ -51,12 +56,12 @@ def get_git_infos_from_api(instance_url, project_id: Project_id, commit_key="commit", commit_msg_key="commit_message", commit_env_key=None): - last_commit = "" + commit = "" if commit_env_key: - last_commit = os.getenv(commit_env_key, None) + commit = os.getenv(commit_env_key, None) + + last_commit = get_last_commit(instance_url, project_id, branch=branch, commit=commit) - if not last_commit: - last_commit = get_last_commit(instance_url, project_id, branch=branch) commit, commit_msg = get_commit_infos_from_api(last_commit) commit_msg = remove_newline(commit_msg) return {commit_key: commit, commit_msg_key: commit_msg}