4
4
"encoding/json"
5
5
"fmt"
6
6
"strings"
7
+ "time"
7
8
8
9
"github.com/golangci/golangci-api/internal/shared/config"
9
10
@@ -27,6 +28,25 @@ type State struct {
27
28
CommitSHA string
28
29
GithubPullRequestNumber int
29
30
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
+ }
30
50
}
31
51
32
52
func (s State ) FillLogContext (lctx logutil.Context ) {
@@ -41,7 +61,15 @@ type AnalyzedRepo struct {
41
61
42
62
type RepoPullRequest struct {
43
63
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
+ }
45
73
}
46
74
47
75
type Service interface {
@@ -74,15 +102,7 @@ func (s BasicService) GetAnalysisStateByAnalysisGUID(rc *request.InternalContext
74
102
return nil , errors .Wrapf (err , "can't get repo id %d" , analysis .RepoID )
75
103
}
76
104
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
86
106
}
87
107
88
108
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
115
135
return nil
116
136
}
117
137
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 ) {
119
139
var repos []models.Repo // use could have reconnected repo so we would have two repos
120
140
err := models .NewRepoQuerySet (rc .DB .Unscoped ()).
121
141
FullNameEq (req .FullName ()).ProviderEq (req .Provider ).
122
142
OrderDescByCreatedAt ().
123
143
All (& repos )
124
144
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" )
126
146
}
127
147
128
148
if len (repos ) == 0 {
129
149
if tryErr := s .tryGetRenamedRepo (rc , req , & repos ); tryErr != nil {
130
150
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 ())
132
152
}
133
153
134
154
// continue, found renamed repo
135
155
}
136
156
137
157
if repos [0 ].IsPrivate {
138
158
if err = s .RepoPolicy .CanReadPrivateRepo (rc , & repos [0 ]); err != nil {
139
- return nil , err
159
+ return nil , "" , err
140
160
}
141
161
}
142
162
@@ -145,27 +165,56 @@ func (s BasicService) GetAnalysisStateByPRNumber(rc *request.AnonymousContext, r
145
165
repoIDs = append (repoIDs , r .ID )
146
166
}
147
167
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 ).
150
178
PullRequestNumberEq (req .PullRequestNumber ).
151
179
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 )
155
194
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" ,
157
196
req .PullRequestNumber , repoIDs )
158
197
}
198
+ if len (analyzes ) == 0 {
199
+ return nil , fmt .Errorf ("got 0 pull request analyzes for repo ids %v" , repoIDs )
200
+ }
159
201
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
169
218
}
170
219
171
220
func (s BasicService ) UpdateAnalysisStateByAnalysisGUID (rc * request.InternalContext , req * AnalyzedRepo , state * State ) error {
0 commit comments