完成 Gitee PR 获取的网络请求

This commit is contained in:
巴拉迪维 2022-04-16 19:22:27 +08:00
parent 1a1d91352b
commit ebf21daa2f
7 changed files with 116 additions and 14 deletions

View File

@ -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
)

View File

@ -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"`
}

View File

@ -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

View File

@ -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)
}
})
}
}

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {