Skip to content

Commit cb56777

Browse files
committed
Switch to bearer token
1 parent 9b0da8a commit cb56777

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

bin/compile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ File.open "#{ENV['HOME']}/.curlrc", "w+", 0600 do |f|
3030
f.puts "--netrc-optional"
3131
end
3232

33-
if valid_login?
33+
if valid_login? github_token
3434
puts " Generated .netrc & .curlrc files (available only at build-time)"
3535

3636
##
@@ -42,7 +42,7 @@ if valid_login?
4242

4343
##
4444
# Print out user information
45-
puts user_block(ENV['GITHUB_AUTH_TOKEN'])
45+
puts user_block(github_token)
4646
else
4747
puts " !!!! No valid Github user found with GITHUB_AUTH_TOKEN: '#{ENV['GITHUB_AUTH_TOKEN']}'"
4848
exit 1

lib/github.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
require 'json'
22

3-
def valid_login?
4-
login != "error"
3+
def valid_login? token
4+
login(token) != "error"
55
end
66

7-
def login
8-
@login ||= github_user_login
7+
def login token
8+
@login ||= github_user_login(token)
99
end
1010

11-
def github_user_login
12-
json = `curl -s https://api.github.com/user` rescue "{}"
11+
def github_user_login token
12+
json = `curl -H "Authorization: Bearer #{token}" -s https://api.github.com/user` rescue "{}"
1313
user = JSON.parse json rescue {}
1414
user["login"] || "error"
1515
end
1616

1717
def github_user_auth token
18+
# This call must use .netrc user/pass combination
1819
json = `curl -s https://api.github.com/authorizations` rescue "[]"
1920
all = JSON.parse json rescue []
20-
authz = all.select {|a| a["token"] =~ /#{token}/}.first
21+
authz = all.select {|a| a["token"] =~ /#{token}/}.first || []
2122
"#{authz["note"]} (#{authz["scopes"].include?('repo') ? 'private repo access' : 'insufficient access'})"
2223
end
2324

2425
def github_user_orgs
25-
json = `curl -s https://api.github.com/user/orgs` rescue "[]"
26+
json = `curl -H "Authorization: Bearer #{token}" -s https://api.github.com/user/orgs` rescue "[]"
2627
orgs = JSON.parse json rescue []
2728
orgs.map {|o| o["login"].downcase }.sort
2829
end
2930

3031
def user_block token
3132
<<-USER
32-
Github User: #{login}
33+
Github User: #{login(token)}
3334
Authorization: #{github_user_auth(token)}
34-
Organizations: #{github_user_orgs.join(", ")}
35+
Organizations: #{github_user_orgs(token).join(", ")}
3536
USER
3637
end

support/netrc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
machine api.github.com login <%= github_token %> password x-oauth-basic
2-
machine code.github.com login <%= github_token %> password x-oauth-basic
3-
machine raw.github.com login <%= github_token %> password x-oauth-basic
41
machine github.com login <%= github_token %> password x-oauth-basic

0 commit comments

Comments
 (0)