Skip to content

Commit

Permalink
ver upgrade (#1)
Browse files Browse the repository at this point in the history
* Add a common version upgrade command

* Fix the wrong position args

* Fix the wrong position args
  • Loading branch information
LinuxSuRen authored Dec 7, 2020
1 parent db1facd commit 1bde2e3
Show file tree
Hide file tree
Showing 12 changed files with 792 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea
test.xml

*/***/*.xml
2 changes: 1 addition & 1 deletion completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Get more details about onmyzsh from https://github.com/ohmyzsh/ohmyzsh
Load the %s completion code for zsh[1] into the current shell
source <(%s completion --type zsh)
Set the %s completion code for zsh[1] to autoload on startup
%s completion --type zsh > "${fpath[1]}/_%s"`, rootName, rootName, rootName, rootName, rootName, rootName),
%s completion --type zsh > "${fpath[1]}/_%s"`, rootName, rootName, rootName, rootName, rootName, rootName, rootName),
RunE: func(cmd *cobra.Command, _ []string) (err error) {
shellType := completionOptions.Type
switch shellType {
Expand Down
71 changes: 71 additions & 0 deletions github/release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package github

import (
"context"
"github.com/google/go-github/v29/github"
)

// GitHubReleaseClient is the client of jcli github
type GitHubReleaseClient struct {
Client *github.Client
Org string
Repo string
}

// ReleaseAsset is the asset from GitHub release
type ReleaseAsset struct {
TagName string
Body string
}

// Init init the GitHub client
func (g *GitHubReleaseClient) Init() {
g.Client = github.NewClient(nil)
}

// GetLatestJCLIAsset returns the latest jcli asset
func (g *GitHubReleaseClient) GetLatestJCLIAsset() (*ReleaseAsset, error) {
return g.GetLatestReleaseAsset(g.Org, g.Repo)
}

// GetLatestReleaseAsset returns the latest release asset
func (g *GitHubReleaseClient) GetLatestReleaseAsset(owner, repo string) (ra *ReleaseAsset, err error) {
ctx := context.Background()

var release *github.RepositoryRelease
if release, _, err = g.Client.Repositories.GetLatestRelease(ctx, owner, repo); err == nil {
ra = &ReleaseAsset{
TagName: release.GetTagName(),
Body: release.GetBody(),
}
}
return
}

// GetJCLIAsset returns the asset from a tag name
func (g *GitHubReleaseClient) GetJCLIAsset(tagName string) (*ReleaseAsset, error) {
return g.GetReleaseAssetByTagName(g.Org, g.Repo, tagName)
}

// GetReleaseAssetByTagName returns the release asset by tag name
func (g *GitHubReleaseClient) GetReleaseAssetByTagName(owner, repo, tagName string) (ra *ReleaseAsset, err error) {
ctx := context.Background()

opt := &github.ListOptions{
PerPage: 99999,
}

var releaseList []*github.RepositoryRelease
if releaseList, _, err = g.Client.Repositories.ListReleases(ctx, owner, repo, opt); err == nil {
for _, item := range releaseList {
if item.GetTagName() == tagName {
ra = &ReleaseAsset{
TagName: item.GetTagName(),
Body: item.GetBody(),
}
break
}
}
}
return
}
75 changes: 75 additions & 0 deletions github/release_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package github_test

import (
jClient "github.com/linuxsuren/cobra-extension/github"
"github.com/stretchr/testify/assert"
"testing"
)

func TestInit(t *testing.T) {
ghClient := jClient.GitHubReleaseClient{}

assert.Nil(t, ghClient.Client)
ghClient.Init()
assert.NotNil(t, ghClient.Client)
}

func TestGetLatestReleaseAsset(t *testing.T) {
client, teardown := jClient.PrepareForGetLatestReleaseAsset() //setup()
defer teardown()

ghClient := jClient.GitHubReleaseClient{
Client: client,
}
asset, err := ghClient.GetLatestReleaseAsset("o", "r")

assert.Nil(t, err)
assert.NotNil(t, asset)
assert.Equal(t, "tagName", asset.TagName)
assert.Equal(t, "body", asset.Body)
}

func TestGetLatestJCLIAsset(t *testing.T) {
client, teardown := jClient.PrepareForGetLatestJCLIAsset() //setup()
defer teardown()

ghClient := jClient.GitHubReleaseClient{
Client: client,
}
asset, err := ghClient.GetLatestJCLIAsset()

assert.Nil(t, err)
assert.NotNil(t, asset)
assert.Equal(t, "tagName", asset.TagName)
assert.Equal(t, "body", asset.Body)
}

func TestGetJCLIAsset(t *testing.T) {
client, teardown := jClient.PrepareForGetJCLIAsset("tagName") //setup()
defer teardown()

ghClient := jClient.GitHubReleaseClient{
Client: client,
}
asset, err := ghClient.GetJCLIAsset("tagName")

assert.Nil(t, err)
assert.NotNil(t, asset)
assert.Equal(t, "tagName", asset.TagName)
assert.Equal(t, "body", asset.Body)
}

func TestGetReleaseAssetByTagName(t *testing.T) {
client, teardown := jClient.PrepareForGetReleaseAssetByTagName() //setup()
defer teardown()

ghClient := jClient.GitHubReleaseClient{
Client: client,
}
asset, err := ghClient.GetReleaseAssetByTagName("jenkins-zh", "jenkins-cli", "tagName")

assert.Nil(t, err)
assert.NotNil(t, asset)
assert.Equal(t, "tagName", asset.TagName)
assert.Equal(t, "body", asset.Body)
}
113 changes: 113 additions & 0 deletions github/release_test_common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package github

import (
"fmt"
"github.com/google/go-github/v29/github"
"net/http"
"net/http/httptest"
"net/url"
)

// PrepareForGetJCLIAsset only for test
func PrepareForGetJCLIAsset(ver string) (client *github.Client, teardown func()) {
var mux *http.ServeMux

client, mux, _, teardown = setup()

mux.HandleFunc("/repos/jenkins-zh/jenkins-cli/releases", func(w http.ResponseWriter, r *http.Request) {
//testMethod(t, r, http.MethodGet)
fmt.Fprint(w, fmt.Sprintf(`[{"id":3, "body":"body", "tag_name":"%s"}]`, ver))
})
return
}

// PrepareForGetReleaseAssetByTagName only for test
func PrepareForGetReleaseAssetByTagName() (client *github.Client, teardown func()) {
var mux *http.ServeMux

client, mux, _, teardown = setup()

mux.HandleFunc("/repos/jenkins-zh/jenkins-cli/releases", func(w http.ResponseWriter, r *http.Request) {
//testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `[{"id":3, "body":"body", "tag_name":"tagName"}]`)
})
return
}

// PrepareForGetLatestJCLIAsset only for test
func PrepareForGetLatestJCLIAsset() (client *github.Client, teardown func()) {
var mux *http.ServeMux

client, mux, _, teardown = setup()

mux.HandleFunc("/repos/jenkins-zh/jenkins-cli/releases/latest", func(w http.ResponseWriter, r *http.Request) {
//testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"id":3, "body":"body", "tag_name":"tagName"}`)
})
return
}

// PrepareForGetLatestReleaseAsset only for test
func PrepareForGetLatestReleaseAsset() (client *github.Client, teardown func()) {
var mux *http.ServeMux

client, mux, _, teardown = setup()

mux.HandleFunc("/repos/o/r/releases/latest", func(w http.ResponseWriter, r *http.Request) {
//testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"id":3, "body":"body", "tag_name":"tagName"}`)
})
return
}

const (
// baseURLPath is a non-empty Client.BaseURL path to use during tests,
// to ensure relative URLs are used for all endpoints. See issue #752.
baseURLPath = "/api-v3"
)

// setup sets up a test HTTP server along with a github.Client that is
// configured to talk to that test server. Tests should register handlers on
// mux which provide mock responses for the API method being tested.
// this was copied from https://github.com/google/go-github
func setup() (client *github.Client, mux *http.ServeMux, serverURL string, teardown func()) {
// mux is the HTTP request multiplexer used with the test server.
mux = http.NewServeMux()

// We want to ensure that tests catch mistakes where the endpoint URL is
// specified as absolute rather than relative. It only makes a difference
// when there's a non-empty base URL path. So, use that. See issue #752.
apiHandler := http.NewServeMux()
apiHandler.Handle(baseURLPath+"/", http.StripPrefix(baseURLPath, mux))

// uncomment here once it gets useful
//apiHandler.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
// fmt.Fprintln(os.Stderr, "FAIL: Client.BaseURL path prefix is not preserved in the request URL:")
// fmt.Fprintln(os.Stderr)
// fmt.Fprintln(os.Stderr, "\t"+req.URL.String())
// fmt.Fprintln(os.Stderr)
// fmt.Fprintln(os.Stderr, "\tDid you accidentally use an absolute endpoint URL rather than relative?")
// fmt.Fprintln(os.Stderr, "\tSee https://github.com/google/go-github/issues/752 for information.")
// http.Error(w, "Client.BaseURL path prefix is not preserved in the request URL.", http.StatusInternalServerError)
//})

// server is a test HTTP server used to provide mock API responses.
server := httptest.NewServer(apiHandler)

// client is the GitHub client being tested and is
// configured to use test server.
client = github.NewClient(nil)
url, _ := url.Parse(server.URL + baseURLPath + "/")
client.BaseURL = url
client.UploadURL = url

return client, mux, server.URL, server.Close
}

// this was copied from https://github.com/google/go-github
//func testMethod(t *testing.T, r *http.Request, want string) {
// t.Helper()
// if got := r.Method; got != want {
// t.Errorf("Request method: %v, want %v", got, want)
// }
//}
13 changes: 13 additions & 0 deletions github/setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package github_test

import (
"testing"

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

func TestJenkinsClient(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "github client test")
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ go 1.15

require (
github.com/golang/mock v1.4.4
github.com/google/go-github/v29 v29.0.3
github.com/jenkins-zh/jenkins-cli v0.0.32
github.com/linuxsuren/http-downloader v0.0.2-0.20201207132639-19888a6beaec // indirect
github.com/onsi/ginkgo v1.14.2
github.com/onsi/gomega v1.10.3
github.com/spf13/cobra v1.1.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.6.1
gopkg.in/yaml.v2 v2.4.0
)
Loading

0 comments on commit 1bde2e3

Please sign in to comment.