完成 Gitee Collaborators 保存

This commit is contained in:
巴拉迪维 2022-04-18 13:39:28 +08:00
parent a7404ca20f
commit fc8b09f3b3
6 changed files with 146 additions and 11 deletions

View File

@ -13,12 +13,26 @@ import (
"time"
)
type Stargazers struct {
type Stargazer struct {
RepoID int64 `db:"repo_id"`
StarAt time.Time `json:"star_at" db:"star_at"`
User `db:"user"`
}
func (s Stargazers) isNilOrEmpty() bool {
return reflect.DeepEqual(s, Stargazers{})
func (s Stargazer) isNilOrEmpty() bool {
return reflect.DeepEqual(s, Stargazer{})
}
type Collaborator struct {
RepoID int64 `db:"repo_id"`
User `db:"user"`
Permissions struct {
Pull bool `json:"pull" db:"can_pull"`
Push bool `json:"push" db:"can_push"`
Admin bool `json:"admin" db:"can_admin"`
} `db:"permissions"`
}
func (c Collaborator) isNilOrEmpty() bool {
return reflect.DeepEqual(c, Collaborator{})
}

View File

@ -71,8 +71,8 @@ func GetGiteeOrgInfo(login string) (gitee_model.User, error) {
// 获取 star 了指定仓库的用户
//
//
func GetGiteeStargazers(owner, repo string) ([]gitee_model.Stargazers, error) {
var allStargazers = []gitee_model.Stargazers{}
func GetGiteeStargazers(owner, repo string) ([]gitee_model.Stargazer, error) {
var allStargazers = []gitee_model.Stargazer{}
token, err := validGiteeToken()
if err != nil {
return allStargazers, err
@ -95,7 +95,7 @@ func GetGiteeStargazers(owner, repo string) ([]gitee_model.Stargazers, error) {
return allStargazers, fmt.Errorf("GetGiteeStargazers failed during network. Status Code: %d", code)
}
var stargazers = []gitee_model.Stargazers{}
var stargazers = []gitee_model.Stargazer{}
e := json.Unmarshal([]byte(rs), &stargazers)
if e != nil {
return allStargazers, e
@ -111,6 +111,46 @@ func GetGiteeStargazers(owner, repo string) ([]gitee_model.Stargazers, error) {
return allStargazers, nil
}
// 获取仓库的 Collaborators
func GetGiteeCollaborators(owner, repo string) ([]gitee_model.Collaborator, error) {
var foundUser = []gitee_model.Collaborator{}
token, err := validGiteeToken()
if err != nil {
return foundUser, err
}
page := gitee_model.GITEE_API_START_PAGE
for {
page += 1
url := fmt.Sprintf("%s/repos/%s/%s/collaborators", 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),
})
if err != nil {
return foundUser, err
}
if code != http.StatusOK {
return foundUser, fmt.Errorf("GetGiteeCollaborators failed during network. Status Code: %d", code)
}
var users = []gitee_model.Collaborator{}
e := json.Unmarshal([]byte(rs), &users)
if e != nil {
return foundUser, err
}
if len(users) > 0 {
foundUser = append(foundUser, users...)
continue
}
break
} //end of for
return foundUser, nil
}
// 获取指定仓库的 PR
//
//
@ -338,7 +378,7 @@ func validGiteeToken() (OauthToken, error) {
return token, err
}
if time.Now().Unix() >= (token.CreatedAt+token.ExpiresIn)/2 {
if time.Now().Unix() >= (token.CreatedAt + token.ExpiresIn) {
err := refreshGiteeToken(&token)
if err != nil {
return token, err

View File

@ -271,3 +271,34 @@ func TestGetGiteeStargazers(t *testing.T) {
})
}
}
func TestGetGiteeCollaborators(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: "TestCase1", args: args{owner: "barat", repo: "ohurlshortener"}, want: 1, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetGiteeCollaborators(tt.args.owner, tt.args.repo)
if (err != nil) != tt.wantErr {
t.Errorf("GetGiteeCollaborators() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(len(got), tt.want) {
t.Errorf("GetGiteeCollaborators() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -117,7 +117,8 @@ CREATE TABLE gitee.stargazers (
CREATE TABLE gitee.collaborators (
"user_id" int8 NOT NULL,
repo_id int8 NOT NULL,
can_pull BOOLEAN,
can_push BOOLEAN,
can_admin BOOLEAN,
CONSTRAINT uni_gitee_rcs UNIQUE (user_id,repo_id)
);

View File

@ -13,8 +13,16 @@ import (
"repostats/storage"
)
func BulkSaveStargazers(s []gitee_mode.Stargazers) error {
func BulkSaveStargazers(s []gitee_mode.Stargazer) error {
query := `INSERT INTO gitee.stargazers (user_id, repo_id, star_at) VALUES(:user.id,:repo_id,:star_at)
ON CONFLICT (user_id,repo_id) DO UPDATE SET user_id=EXCLUDED.user_id,repo_id=EXCLUDED.repo_id,star_at=EXCLUDED.star_at`
return storage.DbNamedExec(query, s)
}
func BulkSaveCollaborators(c []gitee_mode.Collaborator) error {
query := `INSERT INTO gitee.collaborators (user_id, repo_id, can_pull, can_push, can_admin)
VALUES(:user.id,:repo_id,:permissions.can_pull,:permissions.can_push,:permissions.can_admin)
ON CONFLICT (user_id,repo_id) DO UPDATE SET user_id=EXCLUDED.user_id,repo_id=EXCLUDED.repo_id,
can_pull=EXCLUDED.can_pull,can_push=EXCLUDED.can_push,can_admin=EXCLUDED.can_admin`
return storage.DbNamedExec(query, c)
}

View File

@ -37,7 +37,7 @@ func TestBulkSaveStargazers(t *testing.T) {
}
type args struct {
s []gitee_mode.Stargazers
s []gitee_mode.Stargazer
}
tests := []struct {
name string
@ -55,3 +55,44 @@ func TestBulkSaveStargazers(t *testing.T) {
})
}
}
func TestBulkSaveCollaborators(t *testing.T) {
testSetup(t)
defer testTeardown(t)
found1, err := network.GetGiteeCollaborators("barat", "ohurlshortener")
utils.ExitOnError(err)
//TEST ONLY
for i := 0; i < len(found1); i++ {
found1[i].RepoID = 21133399
}
found2, err := network.GetGiteeCollaborators("openharmony", "communication_dsoftbus")
utils.ExitOnError(err)
//TEST ONLY
for i := 0; i < len(found2); i++ {
found2[i].RepoID = 16184960
}
type args struct {
c []gitee_mode.Collaborator
}
tests := []struct {
name string
args args
wantErr bool
}{
{name: "Testcase barat", args: args{c: found1}, wantErr: false},
{name: "Testcase openharmony", args: args{c: found2}, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := BulkSaveCollaborators(tt.args.c); (err != nil) != tt.wantErr {
t.Errorf("BulkSaveCollaborators() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}