Skip to content

Commit

Permalink
access token is now optional
Browse files Browse the repository at this point in the history
* fix some err checks
* github api url can be configured (e.g. point it at an enterprise instance)

[finishes #89638752 #89633744]

Signed-off-by: Chris Brown <[email protected]>
  • Loading branch information
vito authored and Chris Brown committed Mar 4, 2015
1 parent 9e289b6 commit 4dbe94c
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 17 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ Fetches and creates versioned GitHub resources.

## Source Configuration

* `access_token`: *Required.* The GitHub access token that should be used to
access the API.

* `user`: *Required.* The GitHub username or organization name for the
repository that the releases are in.

* `repository`: *Required.* The repository name that contains the releases.

* `access_token`: *Optional.* The GitHub access token that should be used to
access the API. Only required for publishing releases.

* `github_api_url`: *Optional.* If you use a non-public GitHub deployment then
you can set your API URL here.

## Behavior

### `check`: Check for released versions.
Expand Down
6 changes: 5 additions & 1 deletion cmd/check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ func main() {
var request resource.CheckRequest
inputRequest(&request)

github := resource.NewGitHubClient(request.Source)
github, err := resource.NewGitHubClient(request.Source)
if err != nil {
resource.Fatal("constructing github client", err)
}

command := resource.NewCheckCommand(github)
response, err := command.Run(request)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion cmd/in/in.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ func main() {

destDir := os.Args[1]

github := resource.NewGitHubClient(request.Source)
github, err := resource.NewGitHubClient(request.Source)
if err != nil {
resource.Fatal("constructing github client", err)
}

command := resource.NewInCommand(github, os.Stderr)
response, err := command.Run(destDir, request)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion cmd/out/out.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ func main() {

sourceDir := os.Args[1]

github := resource.NewGitHubClient(request.Source)
github, err := resource.NewGitHubClient(request.Source)
if err != nil {
resource.Fatal("constructing github client", err)
}

command := resource.NewOutCommand(github, os.Stderr)
response, err := command.Run(sourceDir, request)
if err != nil {
Expand Down
21 changes: 18 additions & 3 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resource

import (
"errors"
"net/url"
"os"

"code.google.com/p/goauth2/oauth"
Expand All @@ -28,20 +29,34 @@ type GitHubClient struct {
repository string
}

func NewGitHubClient(source Source) *GitHubClient {
func NewGitHubClient(source Source) (*GitHubClient, error) {
transport := &oauth.Transport{
Token: &oauth.Token{
AccessToken: source.AccessToken,
},
}

client := github.NewClient(transport.Client())
var client *github.Client

if transport.Token.AccessToken == "" {
client = github.NewClient(nil)
} else {
client = github.NewClient(transport.Client())
}

if source.GitHubAPIURL != "" {
var err error
client.BaseURL, err = url.Parse(source.GitHubAPIURL)
if err != nil {
return nil, err
}
}

return &GitHubClient{
client: client,
user: source.User,
repository: source.Repository,
}
}, nil
}

func (g *GitHubClient) ListReleases() ([]github.RepositoryRelease, error) {
Expand Down
79 changes: 79 additions & 0 deletions github_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package resource_test

import (
"net/http"

. "github.com/concourse/github-release-resource"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/onsi/gomega/ghttp"
)

var _ = Describe("GitHub Client", func() {
var server *ghttp.Server
var client *GitHubClient
var source Source

BeforeEach(func() {
server = ghttp.NewServer()
})

JustBeforeEach(func() {
source.GitHubAPIURL = server.URL()

var err error
client, err = NewGitHubClient(source)
Ω(err).ShouldNot(HaveOccurred())
})

AfterEach(func() {
server.Close()
})

Context("with an OAuth Token", func() {
BeforeEach(func() {
source = Source{
User: "concourse",
Repository: "concourse",
AccessToken: "abc123",
}

server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/repos/concourse/concourse/releases"),
ghttp.RespondWith(200, "[]"),
ghttp.VerifyHeaderKV("Authorization", "Bearer abc123"),
),
)
})

It("sends one", func() {
_, err := client.ListReleases()
Ω(err).ShouldNot(HaveOccurred())
})
})

Context("without an OAuth Token", func() {
BeforeEach(func() {
source = Source{
User: "concourse",
Repository: "concourse",
}

server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/repos/concourse/concourse/releases"),
ghttp.RespondWith(200, "[]"),
ghttp.VerifyHeader(http.Header{"Authorization": nil}),
),
)
})

It("sends one", func() {
_, err := client.ListReleases()
Ω(err).ShouldNot(HaveOccurred())
})
})
})
11 changes: 8 additions & 3 deletions in_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ func NewInCommand(github GitHub, writer io.Writer) *InCommand {
}

func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
err := os.MkdirAll(destDir, 0755)
if err != nil {
return InResponse{}, err
}

releases, err := c.github.ListReleases()
if err != nil {
return InResponse{}, nil
return InResponse{}, err
}

sort.Sort(byVersion(releases))
Expand Down Expand Up @@ -55,7 +60,7 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {

assets, err := c.github.ListReleaseAssets(foundRelease)
if err != nil {
return InResponse{}, nil
return InResponse{}, err
}

for _, asset := range assets {
Expand Down Expand Up @@ -87,7 +92,7 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {

err := c.downloadFile(url, path)
if err != nil {
return InResponse{}, nil
return InResponse{}, err
}
}

Expand Down
48 changes: 45 additions & 3 deletions in_command_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resource_test

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -25,6 +26,7 @@ var _ = Describe("In Command", func() {
inResponse resource.InResponse
inErr error

tmpDir string
destDir string
)

Expand All @@ -34,9 +36,11 @@ var _ = Describe("In Command", func() {
githubClient = &fakes.FakeGitHub{}
command = resource.NewInCommand(githubClient, ioutil.Discard)

destDir, err = ioutil.TempDir("", "github-release")
tmpDir, err = ioutil.TempDir("", "github-release")
Ω(err).ShouldNot(HaveOccurred())

destDir = filepath.Join(tmpDir, "destination")

server = ghttp.NewServer()
server.RouteToHandler("GET", "/example.txt", ghttp.RespondWith(200, "example.txt"))
server.RouteToHandler("GET", "/example.rtf", ghttp.RespondWith(200, "example.rtf"))
Expand All @@ -50,8 +54,11 @@ var _ = Describe("In Command", func() {
})

AfterEach(func() {
server.Close()
Ω(os.RemoveAll(destDir)).Should(Succeed())
if server != nil {
server.Close()
}

Ω(os.RemoveAll(tmpDir)).Should(Succeed())
})

buildRelease := func(id int, tag string) github.RepositoryRelease {
Expand Down Expand Up @@ -171,6 +178,29 @@ var _ = Describe("In Command", func() {
Ω(err).ShouldNot(HaveOccurred())
})
})

Context("when downloading an asset fails", func() {
BeforeEach(func() {
server.Close()
server = nil
})

It("returns an error", func() {
Ω(inErr).Should(HaveOccurred())
})
})

Context("when listing release assets fails", func() {
disaster := errors.New("nope")

BeforeEach(func() {
githubClient.ListReleaseAssetsReturns(nil, disaster)
})

It("returns the error", func() {
Ω(inErr).Should(Equal(disaster))
})
})
})

Context("when the specified version is not available", func() {
Expand Down Expand Up @@ -228,4 +258,16 @@ var _ = Describe("In Command", func() {
Ω(inErr).Should(HaveOccurred())
})
})

Context("when listing releases fails", func() {
disaster := errors.New("nope")

BeforeEach(func() {
githubClient.ListReleasesReturns(nil, disaster)
})

It("returns the error", func() {
Ω(inErr).Should(Equal(disaster))
})
})
})
5 changes: 3 additions & 2 deletions resources.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package resource

type Source struct {
AccessToken string `json:"access_token"`

User string `json:"user"`
Repository string `json:"repository"`

GitHubAPIURL string `json:"github_api_url"`
AccessToken string `json:"access_token"`
}

type CheckRequest struct {
Expand Down

0 comments on commit 4dbe94c

Please sign in to comment.