From 21f676dab64c7f3d24241febd82059c45c12222d Mon Sep 17 00:00:00 2001 From: Shuhei Kitagawa Date: Wed, 29 Apr 2020 14:36:52 +0900 Subject: [PATCH] Remove Authentication.UsingGithubToken method --- README.md | 32 +------------- authentication.go | 69 ----------------------------- authentication_integration_test.go | 29 ------------- authentication_test.go | 70 ------------------------------ travis.go | 10 +++-- travis_test.go | 12 +++++ 6 files changed, 21 insertions(+), 201 deletions(-) delete mode 100644 authentication.go delete mode 100644 authentication_integration_test.go delete mode 100644 authentication_test.go diff --git a/README.md b/README.md index f365ea4..19ed08e 100644 --- a/README.md +++ b/README.md @@ -40,13 +40,6 @@ Travis CI is migrating projects in `https://api.travis-ci.org/` to `https://api. ### Authentication -There two ways to authenticator your Travis CI client. - -- Authentication with Travis API token -- Authentication with GitHub personal access token - -##### Authentication with Travis API token - ```go client := travis.NewClient(travis.ApiOrgUrl, "TravisApiToken") @@ -58,23 +51,9 @@ You can issue Travis API token and hand it in to `NewClient` method directly. Yo For more information on how to issue Travis CI API token, please visit [their documentation](https://docs.travis-ci.com/user/triggering-builds/). +### Unauthenticated client - -##### Authentication with GitHub personal access token -Authentication with a Github personal access token will require some extra steps. [This GitHub help page](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/) guides you thorough how to create one. - -```go -client := travis.NewClient(travis.ApiOrgUrl, "") - -_, _, err := client.Authentication.UsingGithubToken(context.Background(), "GitHubToken") - -// Jobs.Cancel will success -_, _, err = client.Jobs.Cancel(context.Background(), 12345) -``` - -#### Unauthenticated client - -It is possible to interact with the API without authentication. However some resources are not accessible. +It is possible to interact with the API without authentication. However, most resources are not accessible. ```go client := travis.NewClient(travis.ApiOrgUrl, "") @@ -100,13 +79,6 @@ opt := BuildOption{Include: []string{"build.repository", "build.commits"}} build, _, err := client.Builds.Find(context.Background(), 123, &opt) ``` -## Important Note - -There are several breaking changes between v0.1.9 and v0.2.0 mainly to support eager loading. If you have never used go-travis, please select v0.2.0 or above. If you have used go-travis v0.1.9 or below, and consider updating it, please be aware those two changes: - -- Almost all the struct fields are pointer. -- Almost all the methods interacting with the API takes an option parameter to support eager loading. - ## Contribution Contributions are of course always welcome! diff --git a/authentication.go b/authentication.go deleted file mode 100644 index 953287b..0000000 --- a/authentication.go +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright (c) 2015 Ableton AG, Berlin. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package travis - -import ( - "context" - "fmt" - "net/http" -) - -// BuildsService handles communication with the builds -// related methods of the Travis CI API. -type AuthenticationService struct { - client *Client -} - -// AccessToken is a token to access Travis CI API -type AccessToken string - -type accessTokenResponse struct { - Token AccessToken `json:"access_token"` -} - -// UsingGithubToken will generate a Travis CI API authentication -// token and call the UsingTravisToken method with it, leaving your -// client authenticated and ready to use. -func (as *AuthenticationService) UsingGithubToken(ctx context.Context, githubToken string) (AccessToken, *http.Response, error) { - if githubToken == "" { - return "", nil, fmt.Errorf("unable to authenticate client; empty github token provided") - } - - b := map[string]string{"github_token": githubToken} - h := map[string]string{"Accept": mediaTypeV2} - - req, err := as.client.NewRequest(http.MethodPost, "/auth/github", b, h) - if err != nil { - return "", nil, err - } - - // This is the only place you need to access Travis API v2.1 - // See https://github.com/travis-ci/travis-ci/issues/9273 - // FIXME Use API V3 once compatible API is implemented - req.Header.Del("Travis-API-Version") - - atr := &accessTokenResponse{} - resp, err := as.client.Do(ctx, req, atr) - if err != nil { - return "", nil, err - } - - as.UsingTravisToken(string(atr.Token)) - - return atr.Token, resp, err -} - -// UsingTravisToken will format and write provided -// travisToken in the AuthenticationService client's headers. -func (as *AuthenticationService) UsingTravisToken(travisToken string) error { - if travisToken == "" { - return fmt.Errorf("unable to authenticate client; empty travis token provided") - } - - as.client.Headers["Authorization"] = "token " + travisToken - - return nil -} diff --git a/authentication_integration_test.go b/authentication_integration_test.go deleted file mode 100644 index 6a2c178..0000000 --- a/authentication_integration_test.go +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) 2015 Ableton AG, Berlin. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// +build integration - -package travis - -import ( - "context" - "testing" -) - -func TestAuthenticationService_UsingGithubToken(t *testing.T) { - token, res, err := integrationClient.Authentication.UsingGithubToken(context.TODO(), integrationGitHubToken) - - if err != nil { - t.Fatalf("UsingGithubToken fails: %s", err) - } - - if got, want := res.StatusCode, 200; got != want { - t.Fatalf("UsingGithubToken fails: invalid http response %s", res.Status) - } - - if token == "" { - t.Fatalf("UsingGithubToken fails: token is empty") - } -} diff --git a/authentication_test.go b/authentication_test.go deleted file mode 100644 index 0eea659..0000000 --- a/authentication_test.go +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright (c) 2015 Ableton AG, Berlin. All rights reserved. -// -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package travis - -import ( - "context" - "fmt" - "net/http" - "reflect" - "testing" -) - -func TestAuthenticationService_UsingGithubToken_without_token(t *testing.T) { - as := &AuthenticationService{client: NewClient(ApiOrgUrl, "")} - - _, _, err := as.UsingGithubToken(context.TODO(), "") - - if err == nil { - t.Fatal("AuthenticationService.UsingGithubToken with empty token: error is not supposed to be nil") - } -} - -func TestAuthenticationService_UsingGithubToken_with_token(t *testing.T) { - client, mux, _, teardown := setup() - defer teardown() - - mux.HandleFunc("/auth/github", func(w http.ResponseWriter, r *http.Request) { - testMethod(t, r, http.MethodPost) - testHeader(t, r, "Accept", "application/vnd.travis-ci.2.1+json") - fmt.Fprint(w, `{"access_token":"test_access_token"}`) - }) - - repo, _, err := client.Authentication.UsingGithubToken(context.Background(), "test_github_token") - - if err != nil { - t.Errorf("AuthenticationService.UsingGithubToken returned error: %v", err) - } - - want := AccessToken("test_access_token") - if !reflect.DeepEqual(repo, want) { - t.Errorf("AuthenticationService.UsingGithubToken returned %+v, want %+v", repo, want) - } -} - -func TestAuthenticationService_UsingTravisToken_without_token(t *testing.T) { - as := &AuthenticationService{client: NewClient(ApiOrgUrl, "")} - - err := as.UsingTravisToken("") - if err == nil { - t.Fatal("AuthenticationService.UsingTravisToken with empty token: error is not supposed to be nil") - } -} - -func TestAuthenticationService_UsingTravisToken_with_token(t *testing.T) { - token := "abc123easyasdoremi" - as := &AuthenticationService{client: NewClient(ApiOrgUrl, token)} - - err := as.UsingTravisToken(token) - if err != nil { - t.Fatalf("AuthenticationService.UsingTravisToken: unexpected error occurred: %s", err) - } - - authHeader := as.client.Headers["Authorization"] - if authHeader != fmt.Sprintf("token %s", token) { - t.Fatalf("AuthenticationService.Headers: unexpected Authorization %s; expected %s", authHeader, token) - } -} diff --git a/travis.go b/travis.go index d7797be..df4f58d 100644 --- a/travis.go +++ b/travis.go @@ -52,7 +52,6 @@ type Client struct { // Services used to manipulate API entities Active *ActiveService - Authentication *AuthenticationService BetaFeatures *BetaFeaturesService BetaMigrationRequests *BetaMigrationRequestsService Branches *BranchesService @@ -99,7 +98,6 @@ func NewClient(baseUrl string, travisToken string) *Client { } c.Active = &ActiveService{client: c} - c.Authentication = &AuthenticationService{client: c} c.BetaFeatures = &BetaFeaturesService{client: c} c.BetaMigrationRequests = &BetaMigrationRequestsService{client: c} c.Branches = &BranchesService{client: c} @@ -126,7 +124,7 @@ func NewClient(baseUrl string, travisToken string) *Client { c.User = &UserService{client: c} if travisToken != "" { - c.Authentication.UsingTravisToken(travisToken) + c.SetToken(travisToken) } return c @@ -226,6 +224,12 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*htt return resp, err } +// SetToken formats and writes provided +// Travis API token in the client's headers. +func (c *Client) SetToken(token string) { + c.Headers["Authorization"] = "token " + token +} + // IsAuthenticated indicates if Authorization headers were // found in Client.Headers mapping. func (c *Client) IsAuthenticated() bool { diff --git a/travis_test.go b/travis_test.go index 2f7eb15..188accf 100644 --- a/travis_test.go +++ b/travis_test.go @@ -6,6 +6,7 @@ package travis import ( + "fmt" "io/ioutil" "net/http" "net/http/httptest" @@ -212,3 +213,14 @@ func TestClient_NewRequest_with_overriding_userAgent(t *testing.T) { t.Fatalf("Wrong User-Agent: got: %s, want: %s", got, want) } } + +func TestClient_SetToken(t *testing.T) { + token := "abc123" + + c := NewClient(ApiOrgUrl, "") + c.SetToken(token) + + if h := c.Headers["Authorization"]; h != fmt.Sprintf("token %s", token) { + t.Fatalf("Client.SetToken: unexpected Authorization %s; expected %s", h, token) + } +}