Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Remove Authentication.UsingGithubToken method #75

Merged
merged 1 commit into from
Apr 29, 2020
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
32 changes: 2 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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, "")
Expand All @@ -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!

Expand Down
69 changes: 0 additions & 69 deletions authentication.go

This file was deleted.

29 changes: 0 additions & 29 deletions authentication_integration_test.go

This file was deleted.

70 changes: 0 additions & 70 deletions authentication_test.go

This file was deleted.

10 changes: 7 additions & 3 deletions travis.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ type Client struct {

// Services used to manipulate API entities
Active *ActiveService
Authentication *AuthenticationService
BetaFeatures *BetaFeaturesService
BetaMigrationRequests *BetaMigrationRequestsService
Branches *BranchesService
Expand Down Expand Up @@ -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}
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions travis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package travis

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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)
}
}