Sha256: 50f88195fbba1724606c4750c8c2b9cb2dd9c094ed24b2625c7f1529039f2055

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

class GhCliClient

  def get_issue(id, owner: nil, repo: nil)
    request("'#{path_base(owner, repo)}/issues/#{id}'")
  end

  def get_pr(id, owner: nil, repo: nil)
    request("'#{path_base(owner, repo)}/pulls/#{id}'")
  end

  def get_prs_list(owner: nil, repo: nil)
    request("'#{path_base(owner, repo)}/pulls?per_page=100'")
  end

  def create_issue(title:, body: '', owner: nil, repo: nil)
    request("'#{path_base(owner, repo)}/issues'", args: {title: title, body: body})
  end

  def convert_issue_to_pr(id, head:, base:, owner: nil, repo: nil, draft: true)
    request("'#{path_base(owner, repo)}/pulls'", args: {issue: id.to_i, head: head, base: base, draft: draft})
  end

  def commits_for_range(commit_range, owner: nil, repo: nil)
    # https://docs.github.com/en/rest/commits/commits#compare-two-commits
    request("'#{path_base(owner, repo)}/compare/#{commit_range}'")
  end

  def get_statuses(branch: '{branch}', owner: nil, repo: nil)
    request("'#{path_base(owner, repo)}/statuses/#{branch}'")
  end

  def get_repo_info
    @repo_info ||= begin
      result = `gh repo view --json owner,name -q ".owner.login,.name"`.split("\n").compact
      {
        owner: result[0],
        repo: result[1]
      }
    end
  end

  def latest_remote_commit(owner:, repo:)
    result = `gh browse -R '#{owner}/#{repo}' -n -c`
    result.split("/").last.strip
  end

  protected

  def path_base(owner, repo)
    "repos/#{owner || "{owner}"}/#{repo || "{repo}"}"
  end

  def request(url, args: {})
    arg_params = args.map{ |k,v| "-F '#{k}'='#{v}'" }.join(' ')
    response = `gh api #{url} #{arg_params}`
    parsed = JSON.parse(response)
    parsed
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
github_workflow-0.4.3 lib/github_workflow/gh_cli_client.rb