Skip to content

Commit

Permalink
consolidate github request/authenticated request
Browse files Browse the repository at this point in the history
  • Loading branch information
dpb587 committed Sep 18, 2018
1 parent c947382 commit 4717ac2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 65 deletions.
2 changes: 1 addition & 1 deletion examples/cfbosh.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func main() {
message.Join(
" ",
boshio.Release{Alias: "bosh", Repository: "github.com/cloudfoundry/bosh"},
github.AuthenticatedRelease{Token: os.Getenv("GITHUB_TOKEN"), Alias: "bosh-cli", Owner: "cloudfoundry", Name: "bosh-cli"},
github.Release{Token: os.Getenv("GITHUB_TOKEN"), Alias: "bosh-cli", Owner: "cloudfoundry", Name: "bosh-cli"},
boshio.Stemcell{Alias: "ubuntu-xenial", Name: "bosh-aws-xen-hvm-ubuntu-xenial-go_agent"},
),
),
Expand Down
43 changes: 0 additions & 43 deletions message/github/authenticated_release.go

This file was deleted.

43 changes: 22 additions & 21 deletions message/github/release.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
package github

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"context"
"fmt"

"github.com/dpb587/go-slack-topic-bot/message"
"github.com/pkg/errors"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)

type Release struct {
Token string
Alias string
Owner string
Name string
}

var _ message.Messager = &Release{}

type gitHubReleaseApiV2 []struct {
Name string `json:"name"`
}

func (m Release) Message() (string, error) {
res, err := http.DefaultClient.Get(fmt.Sprintf("https://api.github.com/repos/%s/%s/releases", m.Owner, m.Name))
if err != nil {
return "", err
}
ctx := context.Background()

resBodyBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", errors.Wrap(err, "reading response")
var client = http.DefaultClient

if m.Token != "" {
client = oauth2.NewClient(
ctx,
oauth2.StaticTokenSource(&oauth2.Token{AccessToken: m.Token}),
)
}

var data gitHubReleaseApiV2
gh := github.NewClient(client)

err = json.Unmarshal(resBodyBytes, &data)
// list all repositories for the authenticated user

rels, _, err := gh.Repositories.ListReleases(ctx, m.Owner, m.Name, nil)
if err != nil {
return "", errors.Wrap(err, "unmarshalling")
return "", errors.Wrap(err, "listing releases")
}

if len(data) == 0 {
return "", nil
}
for _, rel := range rels {
return fmt.Sprintf("%s/%s", m.Alias, *rel.Name), nil
}

return fmt.Sprintf("%s/%s", m.Alias, data[0].Name), nil
return "", nil
}

0 comments on commit 4717ac2

Please sign in to comment.