mirror of https://github.com/knative/pkg.git
Add list branch function for github util package (#1400)
* Add list branch function for github util package * Fix unit test
This commit is contained in:
parent
fef70cc261
commit
2ddcfef739
|
|
@ -61,6 +61,7 @@ type GithubOperations interface {
|
||||||
ListCommits(org, repo string, ID int) ([]*github.RepositoryCommit, error)
|
ListCommits(org, repo string, ID int) ([]*github.RepositoryCommit, error)
|
||||||
ListFiles(org, repo string, ID int) ([]*github.CommitFile, error)
|
ListFiles(org, repo string, ID int) ([]*github.CommitFile, error)
|
||||||
CreatePullRequest(org, repo, head, base, title, body string) (*github.PullRequest, error)
|
CreatePullRequest(org, repo, head, base, title, body string) (*github.PullRequest, error)
|
||||||
|
ListBranches(org, repo string) ([]*github.Branch, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GithubClient provides methods to perform github operations
|
// GithubClient provides methods to perform github operations
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ type FakeGithubClient struct {
|
||||||
PullRequests map[string]map[int]*github.PullRequest // map of repo: map of PullRequest Number: pullrequests
|
PullRequests map[string]map[int]*github.PullRequest // map of repo: map of PullRequest Number: pullrequests
|
||||||
PRCommits map[int][]*github.RepositoryCommit // map of PR number: slice of commits
|
PRCommits map[int][]*github.RepositoryCommit // map of PR number: slice of commits
|
||||||
CommitFiles map[string][]*github.CommitFile // map of commit SHA: slice of files
|
CommitFiles map[string][]*github.CommitFile // map of commit SHA: slice of files
|
||||||
|
Branches map[string][]*github.Branch // map of repo: branches
|
||||||
|
|
||||||
NextNumber int // number to be assigned to next newly created issue/comment
|
NextNumber int // number to be assigned to next newly created issue/comment
|
||||||
BaseURL string // base URL of Github
|
BaseURL string // base URL of Github
|
||||||
|
|
@ -330,6 +331,15 @@ func (fgc *FakeGithubClient) CreatePullRequest(org, repo, head, base, title, bod
|
||||||
return PR, nil
|
return PR, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListBranches lists branchs for given repo
|
||||||
|
func (fgc *FakeGithubClient) ListBranches(org, repo string) ([]*github.Branch, error) {
|
||||||
|
var branches []*github.Branch
|
||||||
|
for _, b := range fgc.Branches {
|
||||||
|
branches = append(branches, b...)
|
||||||
|
}
|
||||||
|
return branches, nil
|
||||||
|
}
|
||||||
|
|
||||||
// AddFileToCommit adds file to commit
|
// AddFileToCommit adds file to commit
|
||||||
// This is complementary of mocking CreatePullRequest, so that newly created pull request can have files
|
// This is complementary of mocking CreatePullRequest, so that newly created pull request can have files
|
||||||
func (fgc *FakeGithubClient) AddFileToCommit(org, repo, SHA, filename, patch string) error {
|
func (fgc *FakeGithubClient) AddFileToCommit(org, repo, SHA, filename, patch string) error {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,49 @@
|
||||||
|
/*
|
||||||
|
Copyright 2020 The Knative Authors
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// repo.go provides generic functions related to Repo
|
||||||
|
|
||||||
|
package ghutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/google/go-github/v27/github"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ListBranches lists branchs for given repo
|
||||||
|
func (gc *GithubClient) ListBranches(org, repo string) ([]*github.Branch, error) {
|
||||||
|
genericList, err := gc.depaginate(
|
||||||
|
fmt.Sprintf("listing Pull request from org %q and base %q", org, repo),
|
||||||
|
maxRetryCount,
|
||||||
|
&github.ListOptions{},
|
||||||
|
func() ([]interface{}, *github.Response, error) {
|
||||||
|
page, resp, err := gc.Client.Repositories.ListBranches(ctx, org, repo, nil)
|
||||||
|
var interfaceList []interface{}
|
||||||
|
if nil == err {
|
||||||
|
for _, PR := range page {
|
||||||
|
interfaceList = append(interfaceList, PR)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return interfaceList, resp, err
|
||||||
|
},
|
||||||
|
)
|
||||||
|
res := make([]*github.Branch, len(genericList))
|
||||||
|
for i, elem := range genericList {
|
||||||
|
res[i] = elem.(*github.Branch)
|
||||||
|
}
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue