class Jekyll::Gitlab::Letsencrypt::GitlabClient

Attributes

content[RW]

Public Instance Methods

commit!(content) click to toggle source
# File lib/jekyll/gitlab/letsencrypt/gitlab_client.rb, line 12
def commit!(content)
  @content = content
  create_branch! unless branch_exists?
  commit_file!
end
commit_file!() click to toggle source
# File lib/jekyll/gitlab/letsencrypt/gitlab_client.rb, line 41
def commit_file!
  Jekyll.logger.info "Commiting challenge file as #{filename}"
  connection.run_request(request_method_for_commit, nil, nil, nil) do |req|
    req.url        "projects/#{repo_id}/repository/files/#{enc_filename}"
    req.body = {
      commit_message: commit_message,
      branch:         branch,
      content:        content
    }.to_json
  end
  Jekyll.logger.info "Done Commiting! Check #{gitlab_url}/#{gitlab_repo}/commits/#{branch}"
end
create_branch!() click to toggle source
# File lib/jekyll/gitlab/letsencrypt/gitlab_client.rb, line 30
def create_branch!
  Jekyll.logger.info "Creating branch #{branch}.."
  connection.post do |req|
    req.url  "projects/#{repo_id}/repository/branches"
    req.body = {
      branch:  branch,
      ref:     'master'
    }.to_json
  end
end
update_certificate!(certificate, key) click to toggle source
# File lib/jekyll/gitlab/letsencrypt/gitlab_client.rb, line 18
def update_certificate!(certificate, key)
  Jekyll.logger.info "Updating domain #{domain} pages setting with new certificates.."
  response = connection.put do |req|
    req.url "projects/#{repo_id}/pages/domains/#{domain}"
    req.body = {
      certificate:  certificate,
      key:          key
    }.to_json
  end
  response.success?
end

Private Instance Methods

branch_exists?() click to toggle source
# File lib/jekyll/gitlab/letsencrypt/gitlab_client.rb, line 56
def branch_exists?
  response = connection.get "projects/#{repo_id}/repository/branches"
  JSON.parse(response.body).any? { |json| json['name'] == branch }
end
connection() click to toggle source
# File lib/jekyll/gitlab/letsencrypt/gitlab_client.rb, line 81
def connection
  @connection ||= Faraday.new(url: "#{gitlab_url}/api/v4/") do |faraday|
    faraday.adapter Faraday.default_adapter
    faraday.headers['Content-Type']  = 'application/json'
    faraday.headers['PRIVATE-TOKEN'] = personal_access_token
  end
end
enc_filename() click to toggle source
# File lib/jekyll/gitlab/letsencrypt/gitlab_client.rb, line 66
def enc_filename
  filename.gsub "/", "%2f"
end
repo_id() click to toggle source
# File lib/jekyll/gitlab/letsencrypt/gitlab_client.rb, line 70
def repo_id
  @repo_id ||= begin
    repo_name = gitlab_repo.gsub "/", "%2f"
    response  = connection.get "projects/#{repo_name}"
    unless response.success?
      fail StandardError, "Failed response for projects/#{repo_name}. Please check if personal token and repo name are correct"
    end
    JSON.parse(response.body)['id']
  end
end
request_method_for_commit() click to toggle source
# File lib/jekyll/gitlab/letsencrypt/gitlab_client.rb, line 61
def request_method_for_commit
  response = connection.get "projects/#{repo_id}/repository/files/#{enc_filename}?ref=#{branch}"
  response.status == 404 ? :post : :put
end