Class: GithubFlowCli::API

Inherits:
Object
  • Object
show all
Defined in:
lib/github_flow_cli/api.rb

Class Method Summary collapse

Class Method Details

.authorize(username, password, two_factor_token: nil) ⇒ String

Returns OAuth token.

Returns:

  • (String)

    OAuth token



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/github_flow_cli/api.rb', line 8

def authorize(username, password, two_factor_token: nil)
  client = Octokit::Client.new(login: username, password: password)
  auth_config = {
    scopes: ['user', 'repo', 'admin:repo_hook'],
    note: app_name,
  }
  if two_factor_token
    auth_config.merge!(:headers => { "X-GitHub-OTP" => two_factor_token })
  end
  client.create_authorization(auth_config).token
rescue Octokit::UnprocessableEntity => ex
  if ex.message =~ /already_exists/
    id = client.authorizations.find { |auth| auth[:note] == app_name }.id
    client.delete_authorization(id)
    retry
  else
    raise
  end
end

.create_issue(title, body = nil, options = {}) ⇒ Object

TODO: explicify options :assignee (String) — User login. :milestone (Integer) — Milestone number. :labels TODO: keywordify parameters



54
55
56
# File 'lib/github_flow_cli/api.rb', line 54

def create_issue(title, body = nil, options = {})
  @client.create_issue(Local.repo, title, body, options)
end

.create_pr(base: "master", title: nil, body: nil) ⇒ Object

TODO: other options



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/github_flow_cli/api.rb', line 59

def create_pr(base: "master", title: nil, body: nil)
  branch_name = Local.git.current_branch
  issue_number = Config.branch_issue_map[branch_name]
  if issue_number
    issue = API.issue(Local.repo, issue_number)
    title ||= issue.title
    body ||= issue.body
  end
  unless Local.git.branches.remote.find { |b| b.name == branch_name }
    # TODO: custom default remote
    puts "no remote branch found, creating remote branch..."
    Local.git.config("branch.#{branch_name}.remote", 'origin')
    Local.git.config("branch.#{branch_name}.merge", "refs/heads/#{branch_name}")
  end
  # TODO: custom default remote
  puts "git push..."
  Local.git.push('origin', branch_name)
  @client.create_pull_request(Local.repo, base, branch_name, title, body).tap do |pr|
    Config.link_pr_to_branch(pr, branch_name)
  end
end

.method_missing(method, *args, &block) ⇒ Object

delegate API calls to Octokit::Client



39
40
41
42
43
44
45
46
47
# File 'lib/github_flow_cli/api.rb', line 39

def method_missing(method, *args, &block)
  if @client.respond_to?(method)
    define_singleton_method(method) do |*args, &block|
      @client.send(method, *args, &block)
    end
    return send(method, *args, &block)
  end
  super
end

.use_oauth_token(token) ⇒ Object



28
29
30
# File 'lib/github_flow_cli/api.rb', line 28

def use_oauth_token(token)
  @client = Octokit::Client.new(access_token: token)
end

.valid?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/github_flow_cli/api.rb', line 32

def valid?
  !user[:login].nil?
rescue Octokit::Unauthorized
  false
end