Skip to content
This repository was archived by the owner on Jun 2, 2023. It is now read-only.

Commit c7a072c

Browse files
authored
Merge pull request #79 from golangci/feature/return-previous-pull-analyzes
return links to previous pull analyzes
2 parents bbdc60c + a10320d commit c7a072c

File tree

12 files changed

+115
-64
lines changed

12 files changed

+115
-64
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ test_worker:
5353

5454
test_worker_dev: test_worker
5555

56-
test: test_api test_worker test_lint
56+
test: test_api test_worker
5757
test_dev: test_api_dev test_worker_dev test_lint_dev
5858

5959
connect_to_local_db:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP INDEX pull_request_analyzes_commit_sha_idx;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE INDEX pull_request_analyzes_commit_sha_idx ON pull_request_analyzes(commit_sha);

pkg/api/services/organization/endpoint.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/services/pranalysis/endpoint.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/services/pranalysis/service.go

Lines changed: 78 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"strings"
7+
"time"
78

89
"github.com/golangci/golangci-api/internal/shared/config"
910

@@ -27,6 +28,25 @@ type State struct {
2728
CommitSHA string
2829
GithubPullRequestNumber int
2930
GithubRepoName string
31+
32+
PreviousAnalyzes []SamePullStateLink `json:",omitempty"`
33+
}
34+
35+
type SamePullStateLink struct {
36+
CommitSHA string
37+
CreatedAt time.Time
38+
}
39+
40+
func stateFromAnalysis(analysis *models.PullRequestAnalysis, fullName string) *State {
41+
return &State{
42+
Model: analysis.Model,
43+
Status: analysis.Status,
44+
ReportedIssuesCount: analysis.ReportedIssuesCount,
45+
ResultJSON: analysis.ResultJSON,
46+
CommitSHA: analysis.CommitSHA,
47+
GithubPullRequestNumber: analysis.PullRequestNumber,
48+
GithubRepoName: fullName,
49+
}
3050
}
3151

3252
func (s State) FillLogContext(lctx logutil.Context) {
@@ -41,7 +61,15 @@ type AnalyzedRepo struct {
4161

4262
type RepoPullRequest struct {
4363
request.Repo
44-
PullRequestNumber int `request:",urlPart,"`
64+
PullRequestNumber int `request:",urlPart,"`
65+
CommitSHA string `request:"commit_sha,urlParam,optional"`
66+
}
67+
68+
func (r RepoPullRequest) FillLogContext(lctx logutil.Context) {
69+
r.Repo.FillLogContext(lctx)
70+
if r.CommitSHA != "" {
71+
lctx["commit_sha"] = r.CommitSHA
72+
}
4573
}
4674

4775
type Service interface {
@@ -74,15 +102,7 @@ func (s BasicService) GetAnalysisStateByAnalysisGUID(rc *request.InternalContext
74102
return nil, errors.Wrapf(err, "can't get repo id %d", analysis.RepoID)
75103
}
76104

77-
return &State{
78-
Model: analysis.Model,
79-
Status: analysis.Status,
80-
ReportedIssuesCount: analysis.ReportedIssuesCount,
81-
ResultJSON: analysis.ResultJSON,
82-
CommitSHA: analysis.CommitSHA,
83-
GithubPullRequestNumber: analysis.PullRequestNumber,
84-
GithubRepoName: repo.FullName,
85-
}, nil
105+
return stateFromAnalysis(&analysis, repo.FullName), nil
86106
}
87107

88108
func (s BasicService) tryGetRenamedRepo(rc *request.AnonymousContext, req *RepoPullRequest, repos *[]models.Repo) error {
@@ -115,28 +135,28 @@ func (s BasicService) tryGetRenamedRepo(rc *request.AnonymousContext, req *RepoP
115135
return nil
116136
}
117137

118-
func (s BasicService) GetAnalysisStateByPRNumber(rc *request.AnonymousContext, req *RepoPullRequest) (*State, error) {
138+
func (s BasicService) getRepoIDsForPullRequest(rc *request.AnonymousContext, req *RepoPullRequest) ([]uint, string, error) {
119139
var repos []models.Repo // use could have reconnected repo so we would have two repos
120140
err := models.NewRepoQuerySet(rc.DB.Unscoped()).
121141
FullNameEq(req.FullName()).ProviderEq(req.Provider).
122142
OrderDescByCreatedAt().
123143
All(&repos)
124144
if err != nil {
125-
return nil, errors.Wrapf(err, "can't get repo from db")
145+
return nil, "", errors.Wrapf(err, "can't get repo from db")
126146
}
127147

128148
if len(repos) == 0 {
129149
if tryErr := s.tryGetRenamedRepo(rc, req, &repos); tryErr != nil {
130150
rc.Log.Warnf("Failed to check renamed repo: %s", tryErr)
131-
return nil, errors.Wrapf(apierrors.ErrNotFound, "failed to find repos with name %s", req.FullName())
151+
return nil, "", errors.Wrapf(apierrors.ErrNotFound, "failed to find repos with name %s", req.FullName())
132152
}
133153

134154
// continue, found renamed repo
135155
}
136156

137157
if repos[0].IsPrivate {
138158
if err = s.RepoPolicy.CanReadPrivateRepo(rc, &repos[0]); err != nil {
139-
return nil, err
159+
return nil, "", err
140160
}
141161
}
142162

@@ -145,27 +165,56 @@ func (s BasicService) GetAnalysisStateByPRNumber(rc *request.AnonymousContext, r
145165
repoIDs = append(repoIDs, r.ID)
146166
}
147167

148-
var analysis models.PullRequestAnalysis
149-
err = models.NewPullRequestAnalysisQuerySet(rc.DB).
168+
return repoIDs, repos[0].FullName, nil
169+
}
170+
171+
func (s BasicService) GetAnalysisStateByPRNumber(rc *request.AnonymousContext, req *RepoPullRequest) (*State, error) {
172+
repoIDs, fullName, err := s.getRepoIDsForPullRequest(rc, req)
173+
if err != nil {
174+
return nil, err
175+
}
176+
177+
qs := models.NewPullRequestAnalysisQuerySet(rc.DB).
150178
PullRequestNumberEq(req.PullRequestNumber).
151179
RepoIDIn(repoIDs...).
152-
OrderDescByID(). // get last
153-
Limit(1).
154-
One(&analysis)
180+
OrderDescByID()
181+
if req.CommitSHA != "" {
182+
var analysis models.PullRequestAnalysis
183+
if err = qs.CommitSHAEq(req.CommitSHA).One(&analysis); err != nil {
184+
return nil, errors.Wrapf(err, "can't get pull request analysus with number %d and repo ids %v and commit sha %s",
185+
req.PullRequestNumber, repoIDs, req.CommitSHA)
186+
}
187+
return stateFromAnalysis(&analysis, fullName), nil
188+
}
189+
190+
const maxPreviousAnalyzesCount = 5
191+
192+
var analyzes []models.PullRequestAnalysis
193+
err = qs.Limit(maxPreviousAnalyzesCount * 3 /* reserve for repeating commitSHA */).All(&analyzes)
155194
if err != nil {
156-
return nil, errors.Wrapf(err, "can't get pull request analysis with number %d and repo ids %v",
195+
return nil, errors.Wrapf(err, "can't get pull request analyzes with number %d and repo ids %v",
157196
req.PullRequestNumber, repoIDs)
158197
}
198+
if len(analyzes) == 0 {
199+
return nil, fmt.Errorf("got 0 pull request analyzes for repo ids %v", repoIDs)
200+
}
159201

160-
return &State{
161-
Model: analysis.Model,
162-
Status: analysis.Status,
163-
ReportedIssuesCount: analysis.ReportedIssuesCount,
164-
ResultJSON: analysis.ResultJSON,
165-
CommitSHA: analysis.CommitSHA,
166-
GithubPullRequestNumber: analysis.PullRequestNumber,
167-
GithubRepoName: repos[0].FullName,
168-
}, nil
202+
state := stateFromAnalysis(&analyzes[0], fullName)
203+
204+
seenCommitSHAs := map[string]bool{state.CommitSHA: true}
205+
for _, a := range analyzes[1:] {
206+
if seenCommitSHAs[a.CommitSHA] {
207+
// TODO: make uniq index and remove it
208+
continue
209+
}
210+
211+
seenCommitSHAs[a.CommitSHA] = true
212+
state.PreviousAnalyzes = append(state.PreviousAnalyzes, SamePullStateLink{CommitSHA: a.CommitSHA, CreatedAt: a.CreatedAt})
213+
if len(state.PreviousAnalyzes) == maxPreviousAnalyzesCount {
214+
break
215+
}
216+
}
217+
return state, nil
169218
}
170219

171220
func (s BasicService) UpdateAnalysisStateByAnalysisGUID(rc *request.InternalContext, req *AnalyzedRepo, state *State) error {

pkg/api/services/repo/endpoint.go

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/services/repoanalysis/endpoint.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/services/repohook/endpoint.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/services/subscription/endpoint.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)