Skip to content

Commit

Permalink
cmd/ghreleases2appcast: Use GITHUB_API_KEY envvar so we dont exceed r…
Browse files Browse the repository at this point in the history
…ate limit of github api when fetching releases
  • Loading branch information
Jerry Jacobs committed Apr 6, 2021
1 parent 809a0f9 commit 9a5cebb
Show file tree
Hide file tree
Showing 3 changed files with 378 additions and 1 deletion.
21 changes: 20 additions & 1 deletion cmd/ghreleases2appcast/ghreleases2appcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package main

import (
"os"
"bytes"
"context"
"encoding/xml"
Expand All @@ -12,7 +13,9 @@ import (
"strconv"
"strings"
"time"
"net/http"

"golang.org/x/oauth2"
"github.com/google/go-github/github"
"github.com/hashicorp/go-version"
"github.com/russross/blackfriday/v2"
Expand All @@ -33,11 +36,27 @@ func init() {
flag.Parse()
}

func getGHOAuth2ClientFromEnv() *http.Client {
ghAPIKey := os.Getenv("GITHUB_API_KEY")
if ghAPIKey == "" {
return nil
}

log.Printf("Using github API key from envvar (GITHUB_API_KEY)")

ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: ghAPIKey},
)

return oauth2.NewClient(ctx, ts)
}

func main() {
// Load github releases
log.Printf("loading github releases from %s/%s", githubOrg, githubRepo)

ghclient := github.NewClient(nil)
ghclient := github.NewClient(getGHOAuth2ClientFromEnv())
releases, _, err := ghclient.Repositories.ListReleases(context.Background(), githubOrg, githubRepo, nil)
if err != nil {
log.Fatal(err)
Expand Down
1 change: 1 addition & 0 deletions cmd/ghreleases2appcast/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ require (
github.com/google/go-querystring v1.0.0 // indirect
github.com/hashicorp/go-version v1.2.1
github.com/russross/blackfriday/v2 v2.1.0
golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602
)
Loading

0 comments on commit 9a5cebb

Please sign in to comment.