完成 Gitee PR 存储

This commit is contained in:
巴拉迪维 2022-04-16 22:11:29 +08:00
parent ebf21daa2f
commit 00f46c7437
11 changed files with 166 additions and 62 deletions

View File

@ -15,10 +15,8 @@ import (
type Commit struct {
RepoID int `db:"repo_id"`
RepoURL string `db:"repo_url"`
Sha string `db:"sha" json:"sha"`
CommitURL string `db:"commit_url" json:"html_url"`
Sha string `db:"sha" json:"sha"`
HtmlUrl string `db:"html_url" json:"html_url"`
Committer User `json:"committer"`
Author User `json:"author"`
@ -39,6 +37,6 @@ type Commit struct {
} `json:"commit" db:"commit"`
}
func (u Commit) isNilOrEmpty() bool {
return reflect.DeepEqual(u, Commit{})
func (c Commit) isNilOrEmpty() bool {
return reflect.DeepEqual(c, Commit{})
}

View File

@ -9,11 +9,13 @@
package gitee
import (
"reflect"
"time"
)
type Issue struct {
ID int `json:"id" db:"id"`
RepoID int64 `json:"repo_id"`
RepositoryURL string `json:"repository_url" db:"repository_url"`
HTMLURL string `json:"html_url" db:"html_url"`
Number string `json:"number" db:"number"`
@ -31,3 +33,7 @@ type Issue struct {
SecurityHole bool `json:"security_hole" db:"security_hole"`
IssueState string `json:"issue_state" db:"issue_state"`
}
func (i Issue) isNilOrEmpty() bool {
return reflect.DeepEqual(i, Issue{})
}

View File

@ -1,27 +0,0 @@
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

@ -0,0 +1,43 @@
// Copyright (c) [2022] [巴拉迪维 BaratSemet]
// [ohUrlShortener] is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
package gitee
import (
"reflect"
"time"
)
type PullRequest struct {
ID int64 `json:"id" db:"id"`
RepoID int64 `json:"repo_id" db:"repo_id"`
HTMLURL string `json:"html_url" db:"html_url"`
DiffUrl string `json:"diff_url" db:"diff_url"`
PatchUrl string `json:"patch_url" db:"patch_url"`
Number int64 `json:"number" db:"number"`
State string `json:"state" db:"state"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
ClosedAt time.Time `json:"closed_at" db:"closed_at"`
MergedAt time.Time `json:"merged_at" db:"merged_at"`
Mergeable bool `json:"mergeable" db:"mergeable"`
CanMergeCheck bool `json:"can_merge_check" db:"can_merge_check"`
Title string `json:"title" db:"title"`
User User `json:"user" db:"user"`
Head struct {
Label string `json:"label" db:"label"`
Ref string `json:"ref" db:"ref"`
Sha string `json:"sha" db:"sha"`
User User `json:"user" db:"user"`
Repo Repository `json:"repo" db:"repo"`
} `json:"head" db:"head"`
}
func (pr PullRequest) isNilOrEmpty() bool {
return reflect.DeepEqual(pr, PullRequest{})
}

View File

@ -29,31 +29,31 @@ CREATE TABLE gitee.repos (
CONSTRAINT uni_gitee_repo_id UNIQUE (id)
);
-- Commits
CREATE TABLE gitee.commits (
repo_id int8,
repo_url VARCHAR(2000),
sha VARCHAR(80) NOT NULL,
commit_url VARCHAR(2000) NOT NULL,
author_name VARCHAR(500) NULL,
author_email VARCHAR(500) NULL,
repo_id int8 NOT NULL,
html_url VARCHAR(2000),
author_name VARCHAR(500),
author_email VARCHAR(500),
author_date TIMESTAMP WITH TIME ZONE,
committer_name VARCHAR(200) ,
committer_email VARCHAR(200) ,
committer_date TIMESTAMP WITH TIME ZONE ,
detail_message TEXT ,
detail_message TEXT,
tree VARCHAR(80),
CONSTRAINT uni_gitee_commits UNIQUE (sha,repo_id)
);
-- Issues
CREATE TABLE gitee.issues (
id int8 ,
id int8 NOT NULL,
"user_id" int8 NOT NULL,
repo_id int8 NOT NULL,
html_url VARCHAR(2000),
"number" VARCHAR(100),
"state" VARCHAR(100),
title VARCHAR(1000),
"user_id" int8,
repo_id int8 ,
title VARCHAR(1000),
finished_at TIMESTAMP WITH TIME ZONE,
created_at TIMESTAMP WITH TIME ZONE,
updated_at TIMESTAMP WITH TIME ZONE,
@ -63,25 +63,32 @@ CREATE TABLE gitee.issues (
issue_type VARCHAR(100),
issue_state VARCHAR(100),
security_hole BOOLEAN,
CONSTRAINT uni_gitee_issue_id UNIQUE (id)
CONSTRAINT uni_gitee_issue_id UNIQUE (id,repo_id)
);
-- Pull requests
CREATE TABLE gitee.pull_requests (
id int8 NOT NULL,
repo_id int8 NOT NULL,
"user_id" BIGINT NOT NULL,
html_url VARCHAR(500) NOT NULL,
"number" VARCHAR(40) NOT NULL,
"state" VARCHAR(40) NOT NULL,
finished_at TIMESTAMP WITH TIME ZONE,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
"user_id" int8 NOT NULL,
html_url VARCHAR(2000),
diff_url VARCHAR(2000),
patch_url VARCHAR(2000),
"number" int8,
"state" VARCHAR(100),
created_at TIMESTAMP WITH TIME ZONE,
updated_at TIMESTAMP WITH TIME ZONE,
closed_at TIMESTAMP WITH TIME ZONE,
merged_at TIMESTAMP WITH TIME ZONE,
mergeable BOOLEAN,
can_merge_check BOOLEAN,
CONSTRAINT uni_gitee_prs UNIQUE (id)
title varchar(1000),
head_label VARCHAR(100),
head_ref VARCHAR(100),
head_sha VARCHAR(100),
head_user_id int8,
head_repo_id int8,
CONSTRAINT uni_gitee_prs UNIQUE (id,repo_id)
);
-- Stargazers

View File

@ -14,11 +14,11 @@ import (
)
func BulkSaveCommits(commits []gitee_model.Commit) error {
query := `INSERT INTO gitee.commits (repo_id, repo_url, sha, commit_url, author_name, author_email, author_date, committer_name,
query := `INSERT INTO gitee.commits (repo_id, sha, html_url, author_name, author_email, author_date, committer_name,
committer_email, committer_date, detail_message,tree)
VALUES(:repo_id, :repo_url,:sha,:commit_url,:commit.author.name, :commit.author.email,:commit.author.date,
VALUES(:repo_id,:sha,:html_url,:commit.author.name, :commit.author.email,:commit.author.date,
:commit.committer.name,:commit.committer.email,:commit.committer.date,:commit.message,:commit.tree.sha)
ON CONFLICT (repo_id,sha) DO UPDATE SET repo_id=EXCLUDED.repo_id,repo_url=EXCLUDED.repo_url, commit_url=EXCLUDED.commit_url,
ON CONFLICT (repo_id,sha) DO UPDATE SET repo_id=EXCLUDED.repo_id,html_url=EXCLUDED.html_url,
author_name=EXCLUDED.author_name,author_email=EXCLUDED.author_email,author_date=EXCLUDED.author_date,committer_name=EXCLUDED.committer_name,
committer_email=EXCLUDED.committer_email,committer_date=EXCLUDED.committer_date,detail_message=EXCLUDED.detail_message,tree=EXCLUDED.tree`
return storage.DbNamedExec(query, commits)

View File

@ -42,6 +42,10 @@ func TestBulkSaveCommits(t *testing.T) {
found, err := network.GetGiteeCommits("openharmony", "community")
utils.ExitOnError(err)
for i := 0; i < len(found); i++ {
found[i].RepoID = 10918992
}
type args struct {
commits []gitee_model.Commit
}
@ -50,7 +54,6 @@ func TestBulkSaveCommits(t *testing.T) {
args args
wantErr bool
}{
{name: "TestCase1", args: args{commits: found}, wantErr: false},
}
for _, tt := range tests {

View File

@ -18,7 +18,7 @@ func BulkSaveIssues(iss []gitee_model.Issue) error {
updated_at, plan_started_at, "comments", priority, issue_type, issue_state, security_hole)
VALUES(:id,:html_url,:number,:state,:title,:user.id,:repository.id,:finished_at,:created_at,
:updated_at,:plan_started_at,:comments,:priority,:issue_type, :issue_state, :security_hole)
ON CONFLICT (id) DO UPDATE SET id=EXCLUDED.id,html_url=EXCLUDED.html_url,number=EXCLUDED.number,
ON CONFLICT (id,repo_id) DO UPDATE SET id=EXCLUDED.id,html_url=EXCLUDED.html_url,number=EXCLUDED.number,
state=EXCLUDED.state,title=EXCLUDED.title,user_id=EXCLUDED.user_id,repo_id=EXCLUDED.repo_id,
finished_at=EXCLUDED.finished_at,created_at=EXCLUDED.created_at, updated_at=EXCLUDED.updated_at,
plan_started_at=EXCLUDED.plan_started_at,comments=EXCLUDED.comments,priority=EXCLUDED.priority,

View File

@ -20,11 +20,12 @@ func TestBulkSaveIssues(t *testing.T) {
testSetup(t)
defer testTeardown(t)
found, err := network.GetGiteeIssues("barat", "ohurlshortener")
found, err := network.GetGiteeIssues("openharmony", "community")
utils.ExitOnError(err)
found2, err := network.GetGiteeIssues("openharmony", "community")
utils.ExitOnError(err)
for i := 0; i < len(found); i++ {
found[i].RepoID = 10918992
}
type args struct {
iss []gitee_model.Issue
@ -34,9 +35,8 @@ func TestBulkSaveIssues(t *testing.T) {
args args
wantErr bool
}{
{name: "Testcase barat/ohurlshortener", args: args{iss: found}, wantErr: false},
{name: "Testcase barat/ohurlshortener again", args: args{iss: found}, wantErr: false},
{name: "Testcase openharmony/community", args: args{iss: found2}, wantErr: false},
{name: "Testcase openharmony/community", args: args{iss: found}, wantErr: false},
{name: "Testcase openharmony/community again", args: args{iss: found}, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

View File

@ -0,0 +1,27 @@
// Copyright (c) [2022] [巴拉迪维 BaratSemet]
// [ohUrlShortener] is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
package gitee
import (
gitee_mode "repostats/model/gitee"
"repostats/storage"
)
func BulkSavePullRequests(prs []gitee_mode.PullRequest) error {
query := `INSERT INTO gitee.pull_requests (id, repo_id, user_id, html_url, diff_url, patch_url, "number",
state, created_at, updated_at, closed_at, merged_at, mergeable, can_merge_check, title, head_label, head_ref,
head_sha, head_user_id, head_repo_id)
VALUES(:id, :repo_id,:user.id,:html_url,:diff_url,:patch_url,:number,:state,:created_at,:updated_at,:closed_at,
:merged_at, :mergeable, :can_merge_check, :title, :head.label, :head.ref, :head.sha,:head.user.id,:head.repo.id)
ON CONFLICT (id,repo_id) DO UPDATE SET user_id=EXCLUDED.user_id, html_url=EXCLUDED.html_url,diff_url=EXCLUDED.diff_url,
patch_url=EXCLUDED.patch_url,number=EXCLUDED.number,state=EXCLUDED.state,created_at=EXCLUDED.created_at,updated_at=EXCLUDED.updated_at,
closed_at=EXCLUDED.closed_at,merged_at=EXCLUDED.merged_at,mergeable=EXCLUDED.mergeable,can_merge_check=EXCLUDED.can_merge_check,title=EXCLUDED.title,
head_label=EXCLUDED.head_label,head_ref=EXCLUDED.head_ref,head_sha=EXCLUDED.head_sha,head_user_id=EXCLUDED.head_user_id,head_repo_id=EXCLUDED.head_repo_id`
return storage.DbNamedExec(query, prs)
}

View File

@ -0,0 +1,47 @@
// Copyright (c) [2022] [巴拉迪维 BaratSemet]
// [ohUrlShortener] is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
package gitee
import (
gitee_mode "repostats/model/gitee"
"repostats/network"
"repostats/utils"
"testing"
)
func TestBulkSavePullRequests(t *testing.T) {
testSetup(t)
defer testTeardown(t)
found, err := network.GetGiteePullRequests("openharmony", "community")
utils.ExitOnError(err)
for i := 0; i < len(found); i++ {
found[i].RepoID = 10918992
}
type args struct {
prs []gitee_mode.PullRequest
}
tests := []struct {
name string
args args
wantErr bool
}{
{name: "TestCase openharmony/community", args: args{prs: found}, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := BulkSavePullRequests(tt.args.prs); (err != nil) != tt.wantErr {
t.Errorf("BulkSavePullRequests() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}