Skip to content

Commit

Permalink
All Tests passing
Browse files Browse the repository at this point in the history
- Fixed rate limit issue getLatestReleasesFromGithubRepo()
  - github api tests Test_getLatestReleasesFromGithubRepo()
  • Loading branch information
Phillip Miller committed Dec 29, 2021
1 parent daab593 commit 7124b9a
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ jobs:
- name: Tests
run: |
make -f MakeFile test || true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# Upload the original go test log as an artifact for later review.
- name: Upload test log
Expand Down
47 changes: 40 additions & 7 deletions githubapi/githubapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@ package githubapi
import (
"context"
"fmt"
"log"
"os"
"path"
"strings"

"github.com/mr-pmillz/pimp-my-shell/localio"

"github.com/google/go-github/v39/github"
"golang.org/x/oauth2"
)

const (
darwin = "darwin"
linux = "linux"
amd64 = "amd64"
arm64 = "arm64"
musl = "musl"
)

// ReleaseAssets ...
Expand All @@ -24,36 +35,58 @@ type ReleaseAssets struct {
}

func getLatestReleasesFromGithubRepo(owner, repo string) (*ReleaseAssets, error) {
client := github.NewClient(nil)
client := &github.Client{}
ctx := context.Background()
latestRelease, _, err := client.Repositories.GetLatestRelease(ctx, owner, repo)
githubToken, ok := os.LookupEnv("GITHUB_TOKEN")
if ok {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubToken},
)
tc := oauth2.NewClient(ctx, ts)
client = github.NewClient(tc)
} else {
client = github.NewClient(nil)
}

latestRelease, resp, err := client.Repositories.GetLatestRelease(ctx, owner, repo)
if err != nil {
return nil, err
}
// Rate.Limit should most likely be 5000 when authorized.
log.Printf("Github API Rate: %#v\n", resp.Rate)

// If a Token Expiration has been set, it will be displayed.
if !resp.TokenExpiration.IsZero() {
log.Printf("Github Token Expiration: %v\n", resp.TokenExpiration)
}
var releaseAssetsDownloadURLS []string
r := ReleaseAssets{}

for _, release := range latestRelease.Assets {
releaseAssetsDownloadURLS = append(releaseAssetsDownloadURLS, *release.BrowserDownloadURL)
}
for _, releaseTypeURL := range releaseAssetsDownloadURLS {
if strings.Contains(releaseTypeURL, "amd64") && strings.HasSuffix(releaseTypeURL, ".deb") && !strings.Contains(releaseTypeURL, "musl") {
if strings.Contains(releaseTypeURL, amd64) && strings.HasSuffix(releaseTypeURL, ".deb") && !strings.Contains(releaseTypeURL, musl) {
r.LinuxAMDURL = releaseTypeURL
r.LinuxAMDFileName = path.Base(releaseTypeURL)
}
if strings.Contains(releaseTypeURL, "amd64") && strings.HasSuffix(releaseTypeURL, ".gz") && !strings.Contains(releaseTypeURL, "musl") && strings.Contains(releaseTypeURL, "linux") {
if strings.Contains(releaseTypeURL, amd64) && strings.HasSuffix(releaseTypeURL, ".gz") && !strings.Contains(releaseTypeURL, musl) && strings.Contains(releaseTypeURL, linux) {
r.LinuxAMDURL = releaseTypeURL
r.LinuxAMDFileName = path.Base(releaseTypeURL)
}
if strings.Contains(releaseTypeURL, "arm64") && strings.HasSuffix(releaseTypeURL, ".deb") && !strings.Contains(releaseTypeURL, "musl") {
if strings.Contains(releaseTypeURL, arm64) && strings.HasSuffix(releaseTypeURL, ".deb") && !strings.Contains(releaseTypeURL, musl) {
r.LinuxARMURL = releaseTypeURL
r.LinuxARMFileName = path.Base(releaseTypeURL)
}
if strings.Contains(releaseTypeURL, arm64) && strings.HasSuffix(releaseTypeURL, ".gz") && !strings.Contains(releaseTypeURL, musl) && strings.Contains(releaseTypeURL, linux) {
r.LinuxARMURL = releaseTypeURL
r.LinuxARMFileName = path.Base(releaseTypeURL)
}
if strings.Contains(releaseTypeURL, "x86_64") && strings.HasSuffix(releaseTypeURL, ".gz") && strings.Contains(releaseTypeURL, "darwin") && !strings.Contains(releaseTypeURL, "arm64") {
if strings.Contains(releaseTypeURL, "x86_64") && strings.HasSuffix(releaseTypeURL, ".gz") && strings.Contains(releaseTypeURL, darwin) && !strings.Contains(releaseTypeURL, arm64) {
r.DarwinAMDURL = releaseTypeURL
r.DarwinAMDFileName = path.Base(releaseTypeURL)
}
if strings.Contains(releaseTypeURL, "arm64") && strings.HasSuffix(releaseTypeURL, ".gz") && strings.Contains(releaseTypeURL, "darwin") && !strings.Contains(releaseTypeURL, "amd64") {
if strings.Contains(releaseTypeURL, arm64) && strings.HasSuffix(releaseTypeURL, ".gz") && strings.Contains(releaseTypeURL, darwin) && !strings.Contains(releaseTypeURL, amd64) {
r.DarwinARMURL = releaseTypeURL
r.DarwinARMFileName = path.Base(releaseTypeURL)
}
Expand Down
57 changes: 41 additions & 16 deletions githubapi/githubapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ import (
"testing"

"github.com/mr-pmillz/pimp-my-shell/localio"
"github.com/stretchr/testify/assert"
)

const (
owner = "mr-pmillz"
repo = "pimp-my-shell"
)

func TestDownloadLatestRelease(t *testing.T) {
dirs, err := localio.NewDirectories()
if err != nil {
log.Panic(err)
}
releaseAssets, err := getLatestReleasesFromGithubRepo("mr-pmillz", "pimp-my-shell")
releaseAssets, err := getLatestReleasesFromGithubRepo(owner, repo)
if err != nil {
t.Errorf("couldn't get release assets: %v\n", err)
}
Expand All @@ -35,8 +41,8 @@ func TestDownloadLatestRelease(t *testing.T) {
args{
osType: "linux",
dirs: dirs,
owner: "mr-pmillz",
repo: "pimp-my-shell",
owner: owner,
repo: repo,
},
fmt.Sprintf("%s/%s", dirs.HomeDir, releaseAssets.LinuxAMDFileName),
false,
Expand All @@ -46,23 +52,12 @@ func TestDownloadLatestRelease(t *testing.T) {
args{
osType: "darwin",
dirs: dirs,
owner: "mr-pmillz",
repo: "pimp-my-shell",
owner: owner,
repo: repo,
},
fmt.Sprintf("%s/%s", dirs.HomeDir, releaseAssets.DarwinAMDFileName),
false,
},
{
"Test DownloadLatestRelease From This Repo darwin 3 Should fail",
args{
osType: "darwin",
dirs: dirs,
owner: "mr-pmillzaasdfsadf",
repo: "pimp-my-shellasdfasdf",
},
fmt.Sprintf("%s/%s", dirs.HomeDir, releaseAssets.DarwinAMDFileName),
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -77,3 +72,33 @@ func TestDownloadLatestRelease(t *testing.T) {
})
}
}

func Test_getLatestReleasesFromGithubRepo(t *testing.T) {
type args struct {
owner string
repo string
}
tests := []struct {
name string
args args
want *ReleaseAssets
wantErr bool
}{
{name: "PimpMyShell Releases", args: args{
owner: owner,
repo: repo,
}, want: &ReleaseAssets{}, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getLatestReleasesFromGithubRepo(tt.args.owner, tt.args.repo)
if (err != nil) != tt.wantErr {
t.Errorf("getLatestReleasesFromGithubRepo() error = %v, wantErr %v", err, tt.wantErr)
return
}
if ok := assert.IsType(t, &ReleaseAssets{}, got); !ok {
t.Errorf("getLatestReleasesFromGithubRepo() = %v, want %v", got, tt.want)
}
})
}
}
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,29 @@ require (
github.com/jubnzv/go-tmux v0.0.0-20210107170159-c6ae3ccbe820
github.com/klauspost/cpuid/v2 v2.0.9
github.com/schollz/progressbar/v3 v3.8.5
github.com/stretchr/testify v1.7.0
github.com/tidwall/gjson v1.12.1
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
gopkg.in/ini.v1 v1.66.2
)

require (
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/golang/protobuf v1.3.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
Expand All @@ -37,6 +42,8 @@ require (
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
periph.io/x/periph v3.6.8+incompatible // indirect
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ github.com/go-git/go-git-fixtures/v4 v4.2.1/go.mod h1:K8zd3kDUAykwTdDCr+I0per6Y6
github.com/go-git/go-git/v5 v5.4.2 h1:BXyZu9t0VkbiHtqrsvdq39UDhGJTl1h55VW6CSC4aY4=
github.com/go-git/go-git/v5 v5.4.2/go.mod h1:gQ1kArt6d+n+BGd+/B/I74HwRTLhth2+zti4ihgckDc=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand Down Expand Up @@ -112,6 +113,7 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -137,6 +139,7 @@ golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down

0 comments on commit 7124b9a

Please sign in to comment.