-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
272 lines (248 loc) · 5.81 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package fixtures
import (
"encoding/json"
"fmt"
"path/filepath"
"github.com/google/go-github/github"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/lookout-sdk.v0/pb"
)
var fixtures = Fixtures{
{
Name: "new-go-file-too-long-line",
URL: PullRequestURL{
Owner: "src-d",
Repo: "lookout-test-fixtures",
Number: 14,
},
CurrentRevision: 0,
},
{
Name: "file-delete",
URL: PullRequestURL{
Owner: "src-d",
Repo: "lookout-test-fixtures",
Number: 2,
},
CurrentRevision: 0,
},
{
Name: "file-rename",
URL: PullRequestURL{
Owner: "src-d",
Repo: "lookout-test-fixtures",
Number: 3,
},
CurrentRevision: 0,
},
{
Name: "bblfsh-unknown-language",
URL: PullRequestURL{
Owner: "src-d",
Repo: "lookout-test-fixtures",
Number: 4,
},
CurrentRevision: 0,
},
{
Name: "not-ff-merge",
URL: PullRequestURL{
Owner: "src-d",
Repo: "lookout-test-fixtures",
Number: 1,
},
CurrentRevision: 0,
},
{
Name: "merge-conflict",
URL: PullRequestURL{
Owner: "src-d",
Repo: "lookout-test-fixtures",
Number: 7,
},
CurrentRevision: 0,
},
{
Name: "no-content-changes",
URL: PullRequestURL{
Owner: "src-d",
Repo: "lookout-test-fixtures",
Number: 8,
},
CurrentRevision: 0,
},
{
Name: "config-empty",
URL: PullRequestURL{
Owner: "src-d",
Repo: "lookout-test-fixtures",
Number: 10,
},
CurrentRevision: 0,
},
{
Name: "config-unknown-analyzer",
URL: PullRequestURL{
Owner: "src-d",
Repo: "lookout-test-fixtures",
Number: 11,
},
CurrentRevision: 0,
},
{
Name: "submodule-add",
URL: PullRequestURL{
Owner: "src-d",
Repo: "lookout-test-fixtures",
Number: 12,
},
CurrentRevision: 0,
},
{
Name: "submodule-update",
URL: PullRequestURL{
Owner: "src-d",
Repo: "lookout-test-fixtures",
Number: 13,
},
CurrentRevision: 0,
},
{
Name: "incremental-pr",
URL: PullRequestURL{
Owner: "src-d",
Repo: "lookout-test-fixtures",
Number: 16,
},
CurrentRevision: 1,
},
{
Name: "get-changes-from-outdated-pr",
URL: PullRequestURL{
Owner: "src-d",
Repo: "lookout-test-fixtures",
Number: 20,
},
CurrentRevision: 0,
},
{
Name: "pr-from-fork",
URL: PullRequestURL{
Owner: "src-d",
Repo: "lookout-test-fixtures",
Number: 26,
},
CurrentRevision: 0,
},
{
Name: "pr-draft",
URL: PullRequestURL{
Owner: "src-d",
Repo: "lookout-test-fixtures",
Number: 29,
},
CurrentRevision: 0,
},
}
// PullRequestURL is a struct with information about a pull request URL:
// github.com/<Owner>/<Repo>/pull/<Number>
type PullRequestURL struct {
Owner string
Repo string
Number int
}
// Fixture is struct for a test case
type Fixture struct {
Name string
URL PullRequestURL
// There can be more than one revision of the fixture, but it should have
// files for 0..CurrentRevision
CurrentRevision int
}
// GetRevision returns a copy of this Fixture at a different revision
func (f Fixture) GetRevision(revision int) *Fixture {
copy := f
copy.CurrentRevision = revision
return ©
}
// GetRevisions returns all this Fixture revisions, ordered from 0 to CurrentRevision
func (f Fixture) GetRevisions() []*Fixture {
list := make([]*Fixture, f.CurrentRevision+1)
for v := 0; v <= f.CurrentRevision; v++ {
list[v] = f.GetRevision(v)
}
return list
}
// GetPR returns the fixture saved from the github.Client.PullRequests.Get
// output
func (f Fixture) GetPR() *github.PullRequest {
filename := f.FilenamePR(f.CurrentRevision)
data, err := Asset(filename)
if err != nil {
panic(fmt.Sprintf("unexpected error while loading %s: %s", filename, err.Error()))
}
var pr github.PullRequest
err = json.Unmarshal(data, &pr)
if err != nil {
panic(fmt.Sprintf("unexpected error while unmarshalling %s: %s", filename, err.Error()))
}
return &pr
}
// GetCommitsComparison returns the fixture saved from the
// github.Client.Repositories.CompareCommits output
func (f Fixture) GetCommitsComparison() (*github.CommitsComparison, error) {
filename := f.FilenameCC(f.CurrentRevision)
data, err := Asset(filename)
if err != nil {
return nil, err
}
var cc github.CommitsComparison
err = json.Unmarshal(data, &cc)
if err != nil {
return nil, err
}
return &cc, nil
}
// GetCommitRevision builds a pb.CommitRevision from this fixture's GetPR()
func (f Fixture) GetCommitRevision() *pb.CommitRevision {
pr := f.GetPR()
return &pb.CommitRevision{
Base: pb.ReferencePointer{
InternalRepositoryURL: pr.GetBase().GetRepo().GetCloneURL(),
ReferenceName: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", pr.GetBase().GetRef())),
Hash: pr.GetBase().GetSHA(),
},
Head: pb.ReferencePointer{
InternalRepositoryURL: pr.GetHead().GetRepo().GetCloneURL(),
ReferenceName: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", pr.GetHead().GetRef())),
Hash: pr.GetHead().GetSHA(),
},
}
}
func (f Fixture) FilenamePR(revision int) string {
return filepath.FromSlash(fmt.Sprintf("fixtures/%s.%s.pr-%d-v%d.json", f.URL.Owner, f.URL.Repo, f.URL.Number, revision))
}
func (f Fixture) FilenameCC(revision int) string {
return filepath.FromSlash(fmt.Sprintf("fixtures/%s.%s.pr-%d-v%d-compare.json", f.URL.Owner, f.URL.Repo, f.URL.Number, revision))
}
// Fixtures is a list of fixtures. If a fixture has more than one revision the
// most recent one is used
type Fixtures []*Fixture
// GetByName returns fixture by name. If the fixture has more than one revision
// the most recent one is used
func (g Fixtures) GetByName(name string) *Fixture {
for _, f := range g {
if f.Name == name {
return f
}
}
return nil
}
// GetAll returns all fixtures
func GetAll() []*Fixture {
return fixtures
}
// GetByName returns fixture by name
func GetByName(name string) *Fixture {
return fixtures.GetByName(name)
}