-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathpr_issues.go
More file actions
173 lines (160 loc) · 4.16 KB
/
pr_issues.go
File metadata and controls
173 lines (160 loc) · 4.16 KB
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
package issues
import (
"context"
"fmt"
"github.com/deepsourcelabs/cli/deepsource/graphqlclient"
"github.com/deepsourcelabs/cli/deepsource/issues"
"github.com/deepsourcelabs/cli/deepsource/pagination"
)
const fetchPRIssuesQuery = `query GetPRIssues(
$name: String!
$owner: String!
$provider: VCSProvider!
$prNumber: Int!
$limit: Int!
$after: String
$source: AnalysisIssueSource
$category: IssueCategory
$severity: IssueSeverity
$q: String
) {
repository(name: $name, login: $owner, vcsProvider: $provider) {
pullRequest(number: $prNumber) {
issues(first: $limit, after: $after, source: $source, category: $category, severity: $severity, q: $q) {
edges {
node {
source
path
beginLine
endLine
title
shortcode
category
severity
explanation
issue {
analyzer {
name
shortcode
}
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
}`
type PRIssuesListParams struct {
Owner string
RepoName string
Provider string
PRNumber int
Source *string
Category *string
Severity *string
Q *string
}
type PRIssuesListRequest struct {
client graphqlclient.GraphQLClient
Params PRIssuesListParams
}
type PRIssuesListResponse struct {
Repository struct {
PullRequest struct {
Issues struct {
Edges []struct {
Node struct {
Source string `json:"source"`
Path string `json:"path"`
BeginLine int `json:"beginLine"`
EndLine int `json:"endLine"`
Title string `json:"title"`
Shortcode string `json:"shortcode"`
Category string `json:"category"`
Severity string `json:"severity"`
Explanation string `json:"explanation"`
Issue *struct {
Analyzer struct {
Name string `json:"name"`
Shortcode string `json:"shortcode"`
} `json:"analyzer"`
} `json:"issue"`
} `json:"node"`
} `json:"edges"`
PageInfo pagination.PageInfo `json:"pageInfo"`
} `json:"issues"`
} `json:"pullRequest"`
} `json:"repository"`
}
func NewPRIssuesListRequest(client graphqlclient.GraphQLClient, params PRIssuesListParams) *PRIssuesListRequest {
return &PRIssuesListRequest{client: client, Params: params}
}
func (r *PRIssuesListRequest) Do(ctx context.Context) ([]issues.Issue, error) {
allIssues := make([]issues.Issue, 0)
var cursor *string
for {
vars := map[string]any{
"name": r.Params.RepoName,
"owner": r.Params.Owner,
"provider": r.Params.Provider,
"prNumber": r.Params.PRNumber,
"limit": pagination.DefaultPageSize,
}
if cursor != nil {
vars["after"] = *cursor
}
if r.Params.Source != nil {
vars["source"] = *r.Params.Source
}
if r.Params.Category != nil {
vars["category"] = *r.Params.Category
}
if r.Params.Severity != nil {
vars["severity"] = *r.Params.Severity
}
if r.Params.Q != nil {
vars["q"] = *r.Params.Q
}
var respData PRIssuesListResponse
if err := r.client.Query(ctx, fetchPRIssuesQuery, vars, &respData); err != nil {
return nil, fmt.Errorf("List PR issues: %w", err)
}
for _, edge := range respData.Repository.PullRequest.Issues.Edges {
node := edge.Node
issue := issues.Issue{
IssueText: node.Title,
IssueCode: node.Shortcode,
IssueCategory: node.Category,
IssueSeverity: node.Severity,
IssueSource: node.Source,
Description: node.Explanation,
Location: issues.Location{
Path: node.Path,
Position: issues.Position{
BeginLine: node.BeginLine,
EndLine: node.EndLine,
},
},
}
if node.Issue != nil {
issue.Analyzer = issues.AnalyzerMeta{
Name: node.Issue.Analyzer.Name,
Shortcode: node.Issue.Analyzer.Shortcode,
}
}
allIssues = append(allIssues, issue)
}
if len(allIssues) >= pagination.MaxResults {
break
}
if !respData.Repository.PullRequest.Issues.PageInfo.HasNextPage {
break
}
cursor = respData.Repository.PullRequest.Issues.PageInfo.EndCursor
}
return allIssues, nil
}