填充 Commit、Issue、PR、Repo 相关的查询方法
This commit is contained in:
parent
089a033748
commit
9cc66a05f4
|
@ -15,8 +15,7 @@ import (
|
||||||
|
|
||||||
type Issue struct {
|
type Issue struct {
|
||||||
ID int `json:"id" db:"id"`
|
ID int `json:"id" db:"id"`
|
||||||
RepoID int64 `json:"repo_id"`
|
RepoID int64 `json:"repo_id" db:"repo_id"`
|
||||||
RepositoryURL string `json:"repository_url" db:"repository_url"`
|
|
||||||
HTMLURL string `json:"html_url" db:"html_url"`
|
HTMLURL string `json:"html_url" db:"html_url"`
|
||||||
Number string `json:"number" db:"number"`
|
Number string `json:"number" db:"number"`
|
||||||
State string `json:"state" db:"state"`
|
State string `json:"state" db:"state"`
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
package gitee
|
package gitee
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -39,3 +40,17 @@ type Repository struct {
|
||||||
func (r Repository) isNilOrEmpty() bool {
|
func (r Repository) isNilOrEmpty() bool {
|
||||||
return reflect.DeepEqual(r, Repository{})
|
return reflect.DeepEqual(r, Repository{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Repository) Scan(src interface{}) error {
|
||||||
|
var repoID int
|
||||||
|
switch src.(type) {
|
||||||
|
case int64:
|
||||||
|
repoID = int(src.(int64))
|
||||||
|
case int32:
|
||||||
|
repoID = int(src.(int32))
|
||||||
|
default:
|
||||||
|
return errors.New("can not find any valid user")
|
||||||
|
}
|
||||||
|
*r = Repository{ID: repoID}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
"repostats/storage"
|
"repostats/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
var selectQueryPrefix = `SELECT c.author_name AS "author.name",c.author_email AS "author.email",
|
var commitQueryPrefix = `SELECT c.author_name AS "author.name",c.author_email AS "author.email",
|
||||||
c.author_name AS "commit.author.name", c.author_email AS "commit.author.email", c.author_date AS "commit.author.date",
|
c.author_name AS "commit.author.name", c.author_email AS "commit.author.email", c.author_date AS "commit.author.date",
|
||||||
c.committer_name AS "committer.name",c.committer_email AS "committer.email",
|
c.committer_name AS "committer.name",c.committer_email AS "committer.email",
|
||||||
c.committer_name AS "commit.committer.name", c.committer_email AS "commit.committer.email", c.committer_date AS "commit.committer.date",
|
c.committer_name AS "commit.committer.name", c.committer_email AS "commit.committer.email", c.committer_date AS "commit.committer.date",
|
||||||
|
@ -32,21 +32,21 @@ func BulkSaveCommits(commits []gitee_model.Commit) error {
|
||||||
|
|
||||||
func FindCommits() ([]gitee_model.Commit, error) {
|
func FindCommits() ([]gitee_model.Commit, error) {
|
||||||
found := []gitee_model.Commit{}
|
found := []gitee_model.Commit{}
|
||||||
query := selectQueryPrefix + ` ORDER BY c.author_date DESC`
|
query := commitQueryPrefix + ` ORDER BY c.author_date DESC`
|
||||||
err := storage.DbSelect(query, &found)
|
err := storage.DbSelect(query, &found)
|
||||||
return found, err
|
return found, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindCommitBySha(sha string) (gitee_model.Commit, error) {
|
func FindCommitBySha(sha string) (gitee_model.Commit, error) {
|
||||||
found := gitee_model.Commit{}
|
found := gitee_model.Commit{}
|
||||||
query := selectQueryPrefix + ` WHERE c.sha = $1 ORDER BY c.author_date DESC`
|
query := commitQueryPrefix + ` WHERE c.sha = $1 ORDER BY c.author_date DESC`
|
||||||
err := storage.DbGet(query, &found, sha)
|
err := storage.DbGet(query, &found, sha)
|
||||||
return found, err
|
return found, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindCommitsByRepoID(repoID int) ([]gitee_model.Commit, error) {
|
func FindCommitsByRepoID(repoID int) ([]gitee_model.Commit, error) {
|
||||||
found := []gitee_model.Commit{}
|
found := []gitee_model.Commit{}
|
||||||
query := selectQueryPrefix + ` WHERE c.repo_id = $1 ORDER BY c.author_date DESC`
|
query := commitQueryPrefix + ` WHERE c.repo_id = $1 ORDER BY c.author_date DESC`
|
||||||
err := storage.DbSelect(query, &found, repoID)
|
err := storage.DbSelect(query, &found, repoID)
|
||||||
return found, err
|
return found, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,8 @@ import (
|
||||||
"repostats/storage"
|
"repostats/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var issueQueryPrefix = `SELECT iss.repo_id AS "repository", iss.user_id AS "user", iss.* FROM gitee.issues iss `
|
||||||
|
|
||||||
func BulkSaveIssues(iss []gitee_model.Issue) error {
|
func BulkSaveIssues(iss []gitee_model.Issue) error {
|
||||||
query := `INSERT INTO gitee.issues (id, html_url, "number", state, title, user_id, repo_id, finished_at, created_at,
|
query := `INSERT INTO gitee.issues (id, html_url, "number", state, title, user_id, repo_id, finished_at, created_at,
|
||||||
updated_at, plan_started_at, "comments", priority, issue_type, issue_state, security_hole)
|
updated_at, plan_started_at, "comments", priority, issue_type, issue_state, security_hole)
|
||||||
|
@ -25,3 +27,31 @@ func BulkSaveIssues(iss []gitee_model.Issue) error {
|
||||||
issue_type=EXCLUDED.issue_type,issue_state=EXCLUDED.issue_state,security_hole=EXCLUDED.security_hole`
|
issue_type=EXCLUDED.issue_type,issue_state=EXCLUDED.issue_state,security_hole=EXCLUDED.security_hole`
|
||||||
return storage.DbNamedExec(query, iss)
|
return storage.DbNamedExec(query, iss)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FindIssues() ([]gitee_model.Issue, error) {
|
||||||
|
found := []gitee_model.Issue{}
|
||||||
|
query := issueQueryPrefix + ` ORDER BY iss.created_at DESC`
|
||||||
|
err := storage.DbSelect(query, &found)
|
||||||
|
return found, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindIssuesByRepoID(repoID int) ([]gitee_model.Issue, error) {
|
||||||
|
found := []gitee_model.Issue{}
|
||||||
|
query := issueQueryPrefix + ` WHERE iss.repo_id = $1 ORDER BY iss.created_at DESC`
|
||||||
|
err := storage.DbSelect(query, &found, repoID)
|
||||||
|
return found, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindIssueByID(id int) (gitee_model.Issue, error) {
|
||||||
|
found := gitee_model.Issue{}
|
||||||
|
query := issueQueryPrefix + ` WHERE iss.id = $1 ORDER BY iss.created_at DESC`
|
||||||
|
err := storage.DbGet(query, &found, id)
|
||||||
|
return found, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindIssueByNumber(number string) (gitee_model.Issue, error) {
|
||||||
|
found := gitee_model.Issue{}
|
||||||
|
query := issueQueryPrefix + ` WHERE iss.number = $1 ORDER BY iss.created_at DESC`
|
||||||
|
err := storage.DbGet(query, &found, number)
|
||||||
|
return found, err
|
||||||
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
package gitee
|
package gitee
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
gitee_model "repostats/model/gitee"
|
gitee_model "repostats/model/gitee"
|
||||||
"repostats/network"
|
"repostats/network"
|
||||||
"repostats/utils"
|
"repostats/utils"
|
||||||
|
@ -46,3 +47,29 @@ func TestBulkSaveIssues(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFindIssues(t *testing.T) {
|
||||||
|
|
||||||
|
testSetup(t)
|
||||||
|
defer testTeardown(t)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
want int
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{name: "TestCase", want: 107, wantErr: false},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := FindIssues()
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("FindIssues() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(len(got), tt.want) {
|
||||||
|
t.Errorf("FindIssues() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -13,6 +13,9 @@ import (
|
||||||
"repostats/storage"
|
"repostats/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var prQueryPrefix = `SELECT pr.user_id AS "user", pr.head_label AS "head.label", pr.head_ref AS "head.ref", pr.head_sha AS "head.sha",
|
||||||
|
pr.head_user_id AS "head.user", pr.head_repo_id AS "head.repo",pr.* FROM gitee.pull_requests pr `
|
||||||
|
|
||||||
func BulkSavePullRequests(prs []gitee_mode.PullRequest) error {
|
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",
|
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,
|
state, created_at, updated_at, closed_at, merged_at, mergeable, can_merge_check, title, head_label, head_ref,
|
||||||
|
@ -25,3 +28,26 @@ func BulkSavePullRequests(prs []gitee_mode.PullRequest) error {
|
||||||
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`
|
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)
|
return storage.DbNamedExec(query, prs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FindPRByID(prID int) (gitee_mode.PullRequest, error) {
|
||||||
|
found := gitee_mode.PullRequest{}
|
||||||
|
err := storage.DbGet(prQueryPrefix+` WHERE pr.id = $1`, &found, prID)
|
||||||
|
return found, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindPRs() ([]gitee_mode.PullRequest, error) {
|
||||||
|
found := []gitee_mode.PullRequest{}
|
||||||
|
err := storage.DbSelect(prQueryPrefix+` ORDER BY pr.updated_at DESC`, &found)
|
||||||
|
return found, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindPRsByRepoID(repoID int) ([]gitee_mode.PullRequest, error) {
|
||||||
|
found := []gitee_mode.PullRequest{}
|
||||||
|
err := storage.DbSelect(prQueryPrefix+` WHERE pr.repo_id = $1 ORDER BY pr.updated_at DESC`, &found, repoID)
|
||||||
|
return found, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeletePR(prID int) error {
|
||||||
|
query := `DELETE FROM gitee.pull_requests WHERE id = $1`
|
||||||
|
return storage.DbNamedExec(query, prID)
|
||||||
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
package gitee
|
package gitee
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
gitee_mode "repostats/model/gitee"
|
gitee_mode "repostats/model/gitee"
|
||||||
"repostats/network"
|
"repostats/network"
|
||||||
"repostats/utils"
|
"repostats/utils"
|
||||||
|
@ -45,3 +46,59 @@ func TestBulkSavePullRequests(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFindPRs(t *testing.T) {
|
||||||
|
testSetup(t)
|
||||||
|
defer testTeardown(t)
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
want int
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{name: "TestCase FindPRs", want: 869, wantErr: false},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := FindPRs()
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("FindPRs() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(len(got), tt.want) {
|
||||||
|
t.Errorf("FindPRs() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFindPRByID(t *testing.T) {
|
||||||
|
|
||||||
|
testSetup(t)
|
||||||
|
defer testTeardown(t)
|
||||||
|
|
||||||
|
type args struct {
|
||||||
|
prID int
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want int64
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{name: "TestCase TestFindPRByID", args: args{prID: 5855633}, want: 850, wantErr: false},
|
||||||
|
{name: "TestCase TestFindPRByID", args: args{prID: 111}, want: 0, wantErr: false},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := FindPRByID(tt.args.prID)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("FindPRByID() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(got.Number, tt.want) {
|
||||||
|
t.Errorf("FindPRByID() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -13,6 +13,8 @@ import (
|
||||||
"repostats/storage"
|
"repostats/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var repoQueryPrefix = `SELECT r.owner_id AS "owner", r.assigner_id AS "assigner",r.* FROM gitee.repos `
|
||||||
|
|
||||||
func BulkSaveRepos(repos []gitee_model.Repository) error {
|
func BulkSaveRepos(repos []gitee_model.Repository) error {
|
||||||
query := `INSERT INTO gitee.repos (id, full_name, human_name, path,name, url, owner_id,assigner_id, description,
|
query := `INSERT INTO gitee.repos (id, full_name, human_name, path,name, url, owner_id,assigner_id, description,
|
||||||
html_url, ssh_url,forked_repo,default_branch, forks_count, stargazers_count, watchers_count,license, pushed_at, created_at, updated_at)
|
html_url, ssh_url,forked_repo,default_branch, forks_count, stargazers_count, watchers_count,license, pushed_at, created_at, updated_at)
|
||||||
|
@ -28,7 +30,19 @@ func BulkSaveRepos(repos []gitee_model.Repository) error {
|
||||||
|
|
||||||
func FindRepos() ([]gitee_model.Repository, error) {
|
func FindRepos() ([]gitee_model.Repository, error) {
|
||||||
repos := []gitee_model.Repository{}
|
repos := []gitee_model.Repository{}
|
||||||
query := `SELECT r.owner_id AS "owner", r.assigner_id AS "assigner",r.* FROM gitee.repos r ORDER BY r.id DESC`
|
query := repoQueryPrefix + ` ORDER BY r.id DESC`
|
||||||
err := storage.DbSelect(query, &repos)
|
err := storage.DbSelect(query, &repos)
|
||||||
return repos, err
|
return repos, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FindRepoByID(repoID int) (gitee_model.Repository, error) {
|
||||||
|
found := gitee_model.Repository{}
|
||||||
|
query := repoQueryPrefix + ` WHERE r.id = $1`
|
||||||
|
err := storage.DbGet(query, &found, repoID)
|
||||||
|
return found, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteRepo(repoID int) error {
|
||||||
|
query := `DELETE FROM gitee.repos WHERE id = $1`
|
||||||
|
return storage.DbNamedExec(query, repoID)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue