Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make hub easier to use from GitHub Actions #2256

Merged
merged 3 commits into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions features/authentication.feature
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,31 @@ Feature: OAuth authentication
And the output should not contain "github.com username"
And the file "../home/.config/hub" should not exist

Scenario: Credentials from GITHUB_TOKEN and GITHUB_REPOSITORY
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_REPOSITORY is "mona-lisa/play-ground"
Given $GITHUB_USER is ""
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
7 changes: 7 additions & 0 deletions github/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ func (c *Config) PromptForHost(host string) (h *Host, err error) {
}

userFromEnv := os.Getenv("GITHUB_USER")
repoFromEnv := os.Getenv("GITHUB_REPOSITORY")
if userFromEnv == "" && repoFromEnv != "" {
repoParts := strings.SplitN(repoFromEnv, "/", 2)
if len(repoParts) > 0 {
userFromEnv = repoParts[0]
}
}
if tokenFromEnv && userFromEnv != "" {
h.User = userFromEnv
} else {
Expand Down
18 changes: 17 additions & 1 deletion share/man/man1/hub.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,27 @@ this can be affected with the `GITHUB_HOST` environment variable:
searched for in `XDG_CONFIG_DIRS` per XDG Base Directory Specification.

`HUB_PROTOCOL`
: Use one of "https|ssh|git" as preferred protocol for git clone/push.
: One of "https", "ssh", or "git" as preferred protocol for git clone/push.

`GITHUB_HOST`
: The GitHub hostname to default to instead of "github.com".

`GITHUB_TOKEN`
: OAuth token to use for GitHub API requests.

`GITHUB_USER`
: The GitHub username of the actor of GitHub API operations.

`GITHUB_PASSWORD`
: The GitHub password used to exchange user credentials for an OAuth token
that gets stored in hub configuration. If not set, it may be interactively
prompted for on first run.

`GITHUB_REPOSITORY`
: A value in "OWNER/REPO" format that specifies the repository that API
operations should be performed against. Currently only used to infer the
default value of `GITHUB_USER` for API requests.

## Bugs

<https://github.com/github/hub/issues>
Expand Down