Skip to content

Commit

Permalink
[auth] Allow hub used in Actions by specifying GITHUB_USER
Browse files Browse the repository at this point in the history
When supplied with GITHUB_TOKEN, hub will attempt to request the `user`
API resource which isn't available in the context of Actions.

To work around this, scripts may now set GITHUB_USER to avoid hitting
the unavailable API resource.
  • Loading branch information
mislav committed Jul 19, 2019
1 parent 954c3c3 commit 2aef003
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
27 changes: 26 additions & 1 deletion features/authentication.feature
Expand Up @@ -261,9 +261,34 @@ Feature: OAuth authentication
And the stderr should contain exactly:
"""
Error getting current user: Forbidden (HTTP 403)
Resource not accessible by integration\n
Resource not accessible by integration
You must specify GITHUB_USER via environment variable.\n
"""

Scenario: Credentials from GITHUB_TOKEN and GITHUB_USER
Given I am in "git://github.com/monalisa/playground.git" git repo
Given the GitHub API server:
"""
get('/user') {
status 403
json :message => "Resource not accessible by integration",
:documentation_url => "https://developer.github.com/v3/users/#get-the-authenticated-user"
}
get('/repos/monalisa/playground/releases') {
halt 401 unless request.env["HTTP_AUTHORIZATION"] == "token OTOKEN"
json [
{ tag_name: 'v1.2.0',
}
]
}
"""
Given $GITHUB_TOKEN is "OTOKEN"
Given $GITHUB_USER is "hubot"
When I successfully run `hub release show v1.2.0`
Then the output should not contain "github.com password"
And the output should not contain "github.com username"
And the file "../home/.config/hub" should not exist

Scenario: Credentials from GITHUB_TOKEN override those from config file
Given I am "mislav" on github.com with OAuth token "OTOKEN"
Given the GitHub API server:
Expand Down
3 changes: 3 additions & 0 deletions github/client.go
Expand Up @@ -1044,6 +1044,9 @@ func FormatError(action string, err error) (ee error) {
errorMessage = strings.Join(errorSentences, "\n")
} else {
errorMessage = e.Message
if action == "getting current user" && e.Message == "Resource not accessible by integration" {
errorMessage = errorMessage + "\nYou must specify GITHUB_USER via environment variable."
}
}

if errorMessage != "" {
Expand Down
14 changes: 10 additions & 4 deletions github/config.go
Expand Up @@ -87,11 +87,17 @@ func (c *Config) PromptForHost(host string) (h *Host, err error) {
}
}

currentUser, err := client.CurrentUser()
if err != nil {
return
userFromEnv := os.Getenv("GITHUB_USER")
if tokenFromEnv && userFromEnv != "" {
h.User = userFromEnv
} else {
var currentUser *User
currentUser, err = client.CurrentUser()
if err != nil {
return
}
h.User = currentUser.Login
}
h.User = currentUser.Login

if !tokenFromEnv {
err = newConfigService().Save(configsFile(), c)
Expand Down

0 comments on commit 2aef003

Please sign in to comment.