-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add a common version upgrade command * Fix the wrong position args * Fix the wrong position args
- Loading branch information
1 parent
db1facd
commit 1bde2e3
Showing
12 changed files
with
792 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
.idea | ||
test.xml | ||
|
||
*/***/*.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
// } | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.