From ebf21daa2f5ec8ca44726f00dc4afc3473c7d11c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B7=B4=E6=8B=89=E8=BF=AA=E7=BB=B4?= Date: Sat, 16 Apr 2022 19:22:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=20Gitee=20PR=20=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E7=9A=84=E7=BD=91=E7=BB=9C=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/gitee/const_m.go | 2 +- model/gitee/pull_request_m.go | 27 ++++++++++++++++ network/gitee_n.go | 51 +++++++++++++++++++++++++++--- network/gitee_n_test.go | 40 ++++++++++++++++++++--- storage/gitee/commit_s_test.go | 2 +- storage/gitee/issue_s_test.go | 4 +-- storage/gitee/repository_s_test.go | 4 +-- 7 files changed, 116 insertions(+), 14 deletions(-) create mode 100644 model/gitee/pull_request_m.go diff --git a/model/gitee/const_m.go b/model/gitee/const_m.go index 862c261..71ad201 100644 --- a/model/gitee/const_m.go +++ b/model/gitee/const_m.go @@ -13,5 +13,5 @@ const ( GITEE_OAUTH_V5PREFIX = "https://gitee.com/api/v5" GITEE_OAUTH_TOKEN_URL = "https://gitee.com/oauth/token" GITEE_API_START_PAGE = 0 - GITEE_API_PAGE_SIZE = 55 + GITEE_API_PAGE_SIZE = 100 ) diff --git a/model/gitee/pull_request_m.go b/model/gitee/pull_request_m.go new file mode 100644 index 0000000..61173d4 --- /dev/null +++ b/model/gitee/pull_request_m.go @@ -0,0 +1,27 @@ +package gitee + +import "time" + +type PullRequest struct { + ID int64 `json:"id"` + HTMLURL string `json:"html_url"` + DiffUrl string `json:"diff_url"` + PatchUrl string `json:"patch_url"` + Number int64 `json:"number"` + State string `json:"state"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + ClosedAt time.Time `json:"closed_at"` + MergedAt time.Time `json:"merged_at"` + Mergeable bool `json:"mergeable"` + CanMergeCheck bool `json:"can_merge_check"` + Title string `json:"title"` + User User `json:"user"` + Head struct { + Label string `json:"label"` + Ref string `json:"ref"` + Sha string `json:"sha"` + User User `json:"user"` + Repo Repository `json:"repo"` + } `json:"head"` +} diff --git a/network/gitee_n.go b/network/gitee_n.go index a82591e..a6505ef 100644 --- a/network/gitee_n.go +++ b/network/gitee_n.go @@ -20,10 +20,53 @@ import ( "time" ) +// 获取指定仓库的 PR +// +// +func GetGiteePullRequests(owner string, repo string) ([]gitee_model.PullRequest, error) { + token, err := validGiteeToken() + if err != nil { + return nil, err + } + + var allPrs = []gitee_model.PullRequest{} + page := gitee_model.GITEE_API_START_PAGE + for { + page += 1 + url := fmt.Sprintf("%s/repos/%s/%s/pulls", gitee_model.GITEE_OAUTH_V5PREFIX, owner, repo) + code, rs, err := HttpGet(token.AccessToken, url, nil, map[string]string{ + "page": strconv.Itoa(page), + "per_page": strconv.Itoa(gitee_model.GITEE_API_PAGE_SIZE), + "state": "all", + }) + + if err != nil { + return allPrs, err + } + + if code != http.StatusOK { + return allPrs, fmt.Errorf("GrabPullRequest failed during network. Status Code: %d", code) + } + + var prs = []gitee_model.PullRequest{} + e := json.Unmarshal([]byte(rs), &prs) + if e != nil { + return allPrs, e + } + + if len(prs) > 0 { + allPrs = append(allPrs, prs...) + continue + } + break + } //end of for + return allPrs, nil +} + // 获取组织下的所有公开仓库 // // 调用此方法之前,务必确保是组织帐号 -func GetOrgRepos(org string) ([]gitee_model.Repository, error) { +func GetGiteeOrgRepos(org string) ([]gitee_model.Repository, error) { token, err := validGiteeToken() if err != nil { return nil, err @@ -67,7 +110,7 @@ func GetOrgRepos(org string) ([]gitee_model.Repository, error) { // 获取个人用户名下的所有公开仓库 // // 调用此方法之前,务必确保是个人帐号 -func GetUserRepos(name string) ([]gitee_model.Repository, error) { +func GetGiteeUserRepos(name string) ([]gitee_model.Repository, error) { token, err := validGiteeToken() if err != nil { return nil, err @@ -112,7 +155,7 @@ func GetUserRepos(name string) ([]gitee_model.Repository, error) { //获取指定仓库的 issue // // -func GetIssues(owner string, repo string) ([]gitee_model.Issue, error) { +func GetGiteeIssues(owner string, repo string) ([]gitee_model.Issue, error) { token, err := validGiteeToken() if err != nil { @@ -156,7 +199,7 @@ func GetIssues(owner string, repo string) ([]gitee_model.Issue, error) { // 从仓库中获取提交记录 // // 从制定的 owner 和 repo 中获取全部提交 -func GetCommits(owner string, repo string) ([]gitee_model.Commit, error) { +func GetGiteeCommits(owner string, repo string) ([]gitee_model.Commit, error) { token, err := validGiteeToken() if err != nil { return nil, err diff --git a/network/gitee_n_test.go b/network/gitee_n_test.go index f9e6572..38d2f9b 100644 --- a/network/gitee_n_test.go +++ b/network/gitee_n_test.go @@ -44,7 +44,7 @@ func TestGetRepoCommits(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := GetCommits(tt.args.owner, tt.args.repo) + got, err := GetGiteeCommits(tt.args.owner, tt.args.repo) if (err != nil) != tt.wantErr { t.Errorf("GetCommits() error = %v, wantErr %v", err, tt.wantErr) return @@ -74,7 +74,7 @@ func TestGrabOrgRepos(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := GetOrgRepos(tt.args.org) + got, err := GetGiteeOrgRepos(tt.args.org) if (err != nil) != tt.wantErr { t.Errorf("GetOrgRepos() error = %v, wantErr %v", err, tt.wantErr) return @@ -104,7 +104,7 @@ func TestGrabUserRepos(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := GetUserRepos(tt.args.name) + got, err := GetGiteeUserRepos(tt.args.name) if (err != nil) != tt.wantErr { t.Errorf("GetUserRepos() error = %v, wantErr %v", err, tt.wantErr) return @@ -136,7 +136,7 @@ func TestGrabIssues(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := GetIssues(tt.args.owner, tt.args.repo) + got, err := GetGiteeIssues(tt.args.owner, tt.args.repo) if (err != nil) != tt.wantErr { t.Errorf("GetIssues() error = %v, wantErr %v", err, tt.wantErr) return @@ -147,3 +147,35 @@ func TestGrabIssues(t *testing.T) { }) } } + +func TestGetGiteePullRequests(t *testing.T) { + + testSetup(t) + defer testTeardown(t) + + type args struct { + owner string + repo string + } + tests := []struct { + name string + args args + want int + wantErr bool + }{ + {name: "TestPR barat/ohurlshortener", args: args{owner: "barat", repo: "ohurlshortener"}, want: 1, wantErr: false}, + {name: "TestPR openharmony/vendor_hisilicon", args: args{owner: "openharmony", repo: "vendor_hisilicon"}, want: 448, wantErr: false}, //should be 448 at the moment + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := GetGiteePullRequests(tt.args.owner, tt.args.repo) + if (err != nil) != tt.wantErr { + t.Errorf("GetGiteePullRequests() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(len(got), tt.want) { + t.Errorf("GetGiteePullRequests() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/storage/gitee/commit_s_test.go b/storage/gitee/commit_s_test.go index 950799d..50382a9 100644 --- a/storage/gitee/commit_s_test.go +++ b/storage/gitee/commit_s_test.go @@ -39,7 +39,7 @@ func TestBulkSaveCommits(t *testing.T) { testSetup(t) defer testTeardown(t) - found, err := network.GetCommits("openharmony", "community") + found, err := network.GetGiteeCommits("openharmony", "community") utils.ExitOnError(err) type args struct { diff --git a/storage/gitee/issue_s_test.go b/storage/gitee/issue_s_test.go index 63f505f..86ccf6a 100644 --- a/storage/gitee/issue_s_test.go +++ b/storage/gitee/issue_s_test.go @@ -20,10 +20,10 @@ func TestBulkSaveIssues(t *testing.T) { testSetup(t) defer testTeardown(t) - found, err := network.GetIssues("barat", "ohurlshortener") + found, err := network.GetGiteeIssues("barat", "ohurlshortener") utils.ExitOnError(err) - found2, err := network.GetIssues("openharmony", "community") + found2, err := network.GetGiteeIssues("openharmony", "community") utils.ExitOnError(err) type args struct { diff --git a/storage/gitee/repository_s_test.go b/storage/gitee/repository_s_test.go index ef2d0cb..6f7fd38 100644 --- a/storage/gitee/repository_s_test.go +++ b/storage/gitee/repository_s_test.go @@ -20,10 +20,10 @@ func TestBulkSaveRepos(t *testing.T) { testSetup(t) defer testTeardown(t) - found1, err := network.GetUserRepos("barat") + found1, err := network.GetGiteeUserRepos("barat") utils.ExitOnError(err) - found2, err := network.GetOrgRepos("openharmony") + found2, err := network.GetGiteeOrgRepos("openharmony") utils.ExitOnError(err) type args struct {