-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_api.rb
More file actions
87 lines (65 loc) · 2.02 KB
/
github_api.rb
File metadata and controls
87 lines (65 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require "code/repository"
require "code/system"
require "octokit"
require "securerandom"
module Code
class GitHubAPI
def initialize(repository: Repository.current)
@repository = repository
end
def ensure_authorized
if not authorized?
username = System.prompt 'GitHub username'
password = System.prompt_hidden 'GitHub password'
authorize(username: username, password: password)
end
end
def pull_requests_for_branch(branch)
client = octokit_client_instance_from_token
client.pull_requests(current_repo_slug, head: "#{current_organization}:#{branch.name}")
end
def label_pr(pull_request, label)
add_label_to_pr(pull_request.number, label)
end
private
def octokit_client_instance_from_token
client = Octokit::Client.new access_token: authorization_token
end
def octokit_client_instance_from_basic_auth(username:, password:)
client = Octokit::Client.new login: username, password: password
end
def create_token(client)
client.create_authorization(scopes: ["repo"], note: authorization_note)[:token]
end
def current_organization
@repository.organization
end
def current_repo_slug
@repository.slug
end
def current_branch
Branch.current
end
def add_label_to_pr(number, label)
octokit_client_instance_from_token.add_labels_to_an_issue(current_repo_slug, number, [label])
end
def authorize(username:, password:)
client = octokit_client_instance_from_basic_auth(username: username, password: password)
token = create_token(client)
self.authorization_token = token
end
def authorized?
!authorization_token.empty?
end
def authorization_token
token = System.result('git config --get oauth.token')
token
end
def authorization_token=(token)
System.result("git config --global oauth.token #{token}")
end
def authorization_note
"CoderlyCode-" + SecureRandom.uuid
end
end
end