GitLab Tools

Tools to work with the Gitlab v3 API.

class singularity_autobuild.gitlab_tools.GitLabPushEventInfo(git_lab_response, local_repo)[source]

Toolset and object to work with the GitLab API V3

Methods expect an already parsed API response containing the events of a project. The API used, should deliver a json list, filled with json objects.

The __init__ takes a parsed Gitlab response and the returned object is able to determine if a given file was modified during the commits of the last push.

The is_modified() method returns True, if the file path, given as input, leads to a file, that has been changed.

Example API-call that delivers expected response:
  • GET {base_url}/api/v3/projects/{project_id}/events
Variables:
  • PUSH_DATE_KEY (str) – Key value of a GitLab API response for the creation date of a push.
  • repo (git.Repo) – A git.Repo used to handle the repository containing the recipes.
  • from_commit (git.Commit) – The first commit of the newest push to the GitLab remote repository. Determined through the get_latest_push() method using the git_lab_api_response response.
  • to_commit (git.Commit) – The last commit of the newest push to the GitLab remote repository. Determined through the get_latest_push() method using the git_lab_api_response response.
  • changed_files (dict) – Dictionary with full paths to all changed files as keys and their change types as values. Determined by the get_changed_files() method.
Parameters:
  • git_lab_response (list) – A list of gitlab events obtained from a GitLab v3 API.
  • local_repo (str) – The path to the local git repo to work with.
classmethod extract_push_data(cls, git_lab_response)[source]

Filter gitlab response for push events.

Return type:list
Returns:A list of push event dictionaries.
classmethod get_changed_files(cls, repo, begin_sha, end_sha)[source]

Return the files changed in a range of commits and their change type.

The dictionary of changed files is supposed to contain all files changed in all commits between begin and end. This includes begin and end commit. To get the files changed in the begin commit a diff between the commit before the begin commit and the end commit has to be performed.

Parameters:
  • repo (Repo) – Repository object to work with.
  • begin_sha (str) – The hexsha of the first commit of the intervall. Has to be an earlier commit than end_sha.
  • end_sha (str) – The hexsha of the last commit of the intervall. Has to be a later commit than begin_sha.
Return type:

dict

Returns:

Dictionary with all changed files as keys and their change type as values.

classmethod get_latest_push(cls, git_lab_response)[source]

Get the latest push data from a GitLab response.

Uses extract_push_data to make sure to work only with push data.

Parameters:git_lab_response (list) – A list of dictionaries. The dictionary is expected to conform to a GitLab API V3 response containing the events of a project.
Return type:Optional[dict]
Returns:A dictionary corresponding to the json object
is_modified_file(file_path)[source]

Gives truth value for a files modification status.

Cross references objects dictionary of files, modified within commits pushed during the latest push, with the input file path.

Parameters:file_path (str) – Full path to the file, whose modification status is to be identified.
Return type:bool
Returns:Truth value for for a files modification status.
singularity_autobuild.gitlab_tools.call_gitlab_events_api(api_url, api_key)[source]

Call a gitlab API and return its response as list.

Params api_url:The full url for the api request.
Params api_key:The key for the API.
Return type:list
Returns:The API response.