Class: Txgh::GitlabApi

Inherits:
GitApi
  • Object
show all
Defined in:
lib/txgh/gitlab_api.rb

Instance Attribute Summary

Attributes inherited from GitApi

#client, #repo_name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GitApi

create_from_client, #initialize

Constructor Details

This class inherits a constructor from Txgh::GitApi

Class Method Details

.create_from_credentials(_login, access_token, repo_name) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/txgh/gitlab_api.rb', line 6

def create_from_credentials(, access_token, repo_name)
  Gitlab.endpoint = 'https://gitlab.com/api/v4'
  create_from_client(
    Gitlab.client(private_token: access_token),
    repo_name
  )
end

Instance Method Details

#create_status(sha, state, options = {}) ⇒ Object



60
61
62
63
64
65
# File 'lib/txgh/gitlab_api.rb', line 60

def create_status(sha, state, options = {})
  client.update_commit_status(repo_name, Utils.url_safe_relative_branch(sha), state, options)
rescue ::Gitlab::Error::BadRequest => error
  # Gitlab pipeline may have several jobs and txgh should not override commit statuses set by others
  raise error unless error.message.include?('Cannot transition status')
end

#download(path, branch) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/txgh/gitlab_api.rb', line 50

def download(path, branch)
  file = client.get_file(repo_name, path, Utils.relative_branch(branch))

  # mock github response
  {
    content: file.encoding == 'base64' ? Base64.decode64(file.content) : file.content.force_encoding(file.encoding),
    path: path
  }
end

#get_ref(ref) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/txgh/gitlab_api.rb', line 41

def get_ref(ref)
  # mock github response
  {
    object: {
      sha: client.commit(repo_name, Utils.url_safe_relative_branch(ref)).short_id
    }
  }
end

#update_contents(branch, content_list, message) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/txgh/gitlab_api.rb', line 15

def update_contents(branch, content_list, message)
  content_list.each do |file_params|
    path = file_params.fetch(:path)
    new_contents = file_params.fetch(:contents)
    branch = Utils.relative_branch(branch)

    file_sha = file_params.fetch(:sha) do
      begin
        client.get_file(repo_name, path, branch).blob_id
      rescue ::Gitlab::Error::NotFound
        nil
      end
    end

    # If the file doesnt exist, then it isn't tracked by git and file_sha
    # will be nil. In git land, a SHA of all zeroes means create a new file
    # instead of updating an existing one.
    current_sha = file_sha || '0' * 40
    new_sha = Utils.git_hash_blob(new_contents)

    if current_sha != new_sha
      client.edit_file(repo_name, path, branch, new_contents, message)
    end
  end
end