Class: PdkSync::GitlabClient

Inherits:
Object
  • Object
show all
Defined in:
lib/pdksync/gitlabclient.rb

Instance Method Summary collapse

Constructor Details

#initialize(access_token, gitlab_api_endpoint) ⇒ GitlabClient

Returns a new instance of GitlabClient.

Parameters:

  • access_token (String)

    The Gitlab private access token, required to access the Gitlab API

  • gitlab_api_endpoint (String)

    URL to the Gitlab API endpoint against which to work



15
16
17
# File 'lib/pdksync/gitlabclient.rb', line 15

def initialize(access_token, gitlab_api_endpoint)
  @client = Gitlab.client(endpoint: gitlab_api_endpoint, private_token: access_token)
end

Instance Method Details

#create_pull_request(project, target_branch, source_branch, title, message) ⇒ Object

Returns A Gitlab merge request object for the newly created merge request.

Parameters:

  • project (String)

    The full project name, i.e. “namespace/project” in which to create the merge request

  • target_branch (String)

    The target branch against which to create the merge request

  • source_branch (String)

    The source branch from which to create the merge request

  • title (String)

    The title/name of the merge request to create

  • message (String)

    The pull request message/body

Returns:

  • A Gitlab merge request object for the newly created merge request



47
48
49
50
51
52
53
54
# File 'lib/pdksync/gitlabclient.rb', line 47

def create_pull_request(project, target_branch, source_branch, title, message)
  mr_options = {
    source_branch: source_branch,
    target_branch: target_branch,
    description: message
  }
  @client.create_merge_request(project, title, mr_options)
end

#delete_branch(project, branch_name) ⇒ Boolean

Returns true on success, false on failure.

Parameters:

  • project (String)

    The full project name, i.e. “namespace/project” in which to delete the branch

  • branch_name (String)

    The name of the branch to delete

Returns:

  • (Boolean)

    true on success, false on failure



88
89
90
# File 'lib/pdksync/gitlabclient.rb', line 88

def delete_branch(project, branch_name)
  @client.delete_branch(project, branch_name)
end

#labels(project) ⇒ Array

Returns List of available labels in the project.

Parameters:

  • project (String)

    The full project name, i.e. “namespace/project”, from which to get the available labels

Returns:

  • (Array)

    List of available labels in the project



61
62
63
# File 'lib/pdksync/gitlabclient.rb', line 61

def labels(project)
  @client.labels(project)
end

#repository?(project) ⇒ Boolean

Returns true if the project exists, false otherwise.

Parameters:

  • project (String)

    The full repository name, i.e. “namespace/project”

Returns:

  • (Boolean)

    true if the project exists, false otherwise



23
24
25
26
27
28
29
# File 'lib/pdksync/gitlabclient.rb', line 23

def repository?(project)
  @client.project(project)

  true
rescue Gitlab::Error::NotFound
  false
end

#update_issue(project, id, options) ⇒ Object

Note:

This method is specifically used to set labels for a merge request

Returns A Gitlab merge request object of the updated merge request.

Parameters:

  • project (String)

    The full project name, i.e. “namespace/project” in which to update the issue

  • id (Integer)

    The id number of the merge request to update

  • options (Hash)

    A hash of options defining the changes to the merge request

Returns:

  • A Gitlab merge request object of the updated merge request



75
76
77
78
79
# File 'lib/pdksync/gitlabclient.rb', line 75

def update_issue(project, id, options)
  # Gitlab requires labels to be supplied as a comma-separated string
  labels = options[:labels].join(',')
  @client.update_merge_request(project, id, labels: labels)
end