diff --git a/model/gitee/issue_m.go b/model/gitee/issue_m.go index 11754eb..e070851 100644 --- a/model/gitee/issue_m.go +++ b/model/gitee/issue_m.go @@ -15,8 +15,7 @@ import ( type Issue struct { ID int `json:"id" db:"id"` - RepoID int64 `json:"repo_id"` - RepositoryURL string `json:"repository_url" db:"repository_url"` + RepoID int64 `json:"repo_id" db:"repo_id"` HTMLURL string `json:"html_url" db:"html_url"` Number string `json:"number" db:"number"` State string `json:"state" db:"state"` diff --git a/model/gitee/repository_m.go b/model/gitee/repository_m.go index fcf8595..e63b90d 100644 --- a/model/gitee/repository_m.go +++ b/model/gitee/repository_m.go @@ -9,6 +9,7 @@ package gitee import ( + "errors" "reflect" "time" ) @@ -39,3 +40,17 @@ type Repository struct { func (r Repository) isNilOrEmpty() bool { 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 +} diff --git a/storage/gitee/commit_s.go b/storage/gitee/commit_s.go index 2d4ed68..e8471f3 100644 --- a/storage/gitee/commit_s.go +++ b/storage/gitee/commit_s.go @@ -13,7 +13,7 @@ import ( "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.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", @@ -32,21 +32,21 @@ func BulkSaveCommits(commits []gitee_model.Commit) error { func FindCommits() ([]gitee_model.Commit, error) { 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) return found, err } func FindCommitBySha(sha string) (gitee_model.Commit, error) { 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) return found, err } func FindCommitsByRepoID(repoID int) ([]gitee_model.Commit, error) { 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) return found, err } diff --git a/storage/gitee/issue_s.go b/storage/gitee/issue_s.go index 5fae961..acea1e0 100644 --- a/storage/gitee/issue_s.go +++ b/storage/gitee/issue_s.go @@ -13,6 +13,8 @@ import ( "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 { 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) @@ -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` 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 +} diff --git a/storage/gitee/issue_s_test.go b/storage/gitee/issue_s_test.go index 211f215..6e5e0bf 100644 --- a/storage/gitee/issue_s_test.go +++ b/storage/gitee/issue_s_test.go @@ -9,6 +9,7 @@ package gitee import ( + "reflect" gitee_model "repostats/model/gitee" "repostats/network" "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) + } + }) + } +} diff --git a/storage/gitee/pullrequest_s.go b/storage/gitee/pullrequest_s.go index 3d107e0..5799143 100644 --- a/storage/gitee/pullrequest_s.go +++ b/storage/gitee/pullrequest_s.go @@ -13,6 +13,9 @@ import ( "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 { 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, @@ -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` 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) +} diff --git a/storage/gitee/pullrequest_s_test.go b/storage/gitee/pullrequest_s_test.go index 239b7d4..b0f1e86 100644 --- a/storage/gitee/pullrequest_s_test.go +++ b/storage/gitee/pullrequest_s_test.go @@ -9,6 +9,7 @@ package gitee import ( + "reflect" gitee_mode "repostats/model/gitee" "repostats/network" "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) + } + }) + } +} diff --git a/storage/gitee/repository_s.go b/storage/gitee/repository_s.go index b25d582..0e16037 100644 --- a/storage/gitee/repository_s.go +++ b/storage/gitee/repository_s.go @@ -13,6 +13,8 @@ import ( "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 { 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) @@ -28,7 +30,19 @@ func BulkSaveRepos(repos []gitee_model.Repository) error { func FindRepos() ([]gitee_model.Repository, error) { 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) 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) +}