-
Notifications
You must be signed in to change notification settings - Fork 704
Expand file tree
/
Copy pathremote_api.go
More file actions
168 lines (151 loc) · 5.42 KB
/
remote_api.go
File metadata and controls
168 lines (151 loc) · 5.42 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
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package api
import (
"fmt"
"net/url"
"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/plugin"
"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
dsmodels "github.com/apache/incubator-devlake/helpers/pluginhelper/api/models"
"github.com/apache/incubator-devlake/plugins/sonarqube/models"
)
type SonarqubeRemotePagination struct {
Page int `json:"p"`
PageSize int `json:"ps"`
}
func querySonarqubeProjects(
apiClient plugin.ApiClient,
keyword string,
page SonarqubeRemotePagination,
) (
children []dsmodels.DsRemoteApiScopeListEntry[models.SonarqubeProject],
nextPage *SonarqubeRemotePagination,
err errors.Error,
) {
if page.PageSize == 0 {
page.PageSize = 100
}
if page.Page == 0 {
page.Page = 1
}
res, err := apiClient.Get("projects/search", url.Values{
"p": []string{fmt.Sprintf("%v", page.Page)},
"ps": []string{fmt.Sprintf("%v", page.PageSize)},
"q": []string{keyword},
"qualifiers": []string{"TRK"},
}, nil)
if err != nil {
return
}
resBody := struct {
Paging struct {
PageIndex int `json:"pageIndex"`
PageSize int `json:"pageSize"`
Total int `json:"total"`
} `json:"paging"`
Components []*models.SonarqubeApiProject
}{}
err = api.UnmarshalResponse(res, &resBody)
if err != nil {
return
}
for _, project := range resBody.Components {
children = append(children, dsmodels.DsRemoteApiScopeListEntry[models.SonarqubeProject]{
Type: api.RAS_ENTRY_TYPE_SCOPE,
Id: fmt.Sprintf("%v", project.ProjectKey),
ParentId: nil,
Name: project.Name,
FullName: project.Name,
Data: project.ConvertApiScope(),
})
}
if resBody.Paging.Total > resBody.Paging.PageIndex*resBody.Paging.PageSize {
nextPage = &SonarqubeRemotePagination{
Page: resBody.Paging.PageIndex + 1,
PageSize: resBody.Paging.PageSize,
}
}
return
}
func listSonarqubeRemoteScopes(
connection *models.SonarqubeConnection,
apiClient plugin.ApiClient,
groupId string,
page SonarqubeRemotePagination,
) (
children []dsmodels.DsRemoteApiScopeListEntry[models.SonarqubeProject],
nextPage *SonarqubeRemotePagination,
err errors.Error,
) {
return querySonarqubeProjects(apiClient, "", page)
}
// RemoteScopes list all available scopes on the remote server
// @Summary list all available scopes on the remote server
// @Description list all available scopes on the remote server
// @Accept application/json
// @Param connectionId path int false "connection ID"
// @Param groupId query string false "group ID"
// @Param pageToken query string false "page Token"
// @Failure 400 {object} shared.ApiBody "Bad Request"
// @Failure 500 {object} shared.ApiBody "Internal Error"
// @Success 200 {object} dsmodels.DsRemoteApiScopeList[models.SonarqubeProject]
// @Tags plugins/sonarqube
// @Router /plugins/sonarqube/connections/{connectionId}/remote-scopes [GET]
func RemoteScopes(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
return raScopeList.Get(input)
}
func searchSonarqubeRemoteProjects(
apiClient plugin.ApiClient,
params *dsmodels.DsRemoteApiScopeSearchParams,
) (
children []dsmodels.DsRemoteApiScopeListEntry[models.SonarqubeProject],
err errors.Error,
) {
if params.Page == 0 {
params.Page = 1
}
page := SonarqubeRemotePagination{
Page: params.Page,
PageSize: params.PageSize,
}
children, _, err = querySonarqubeProjects(apiClient, params.Search, page)
return
}
// SearchRemoteScopes searches scopes on the remote server
// @Summary searches scopes on the remote server
// @Description searches scopes on the remote server
// @Accept application/json
// @Param connectionId path int false "connection ID"
// @Param search query string false "search"
// @Param page query int false "page number"
// @Param pageSize query int false "page size per page"
// @Failure 400 {object} shared.ApiBody "Bad Request"
// @Failure 500 {object} shared.ApiBody "Internal Error"
// @Success 200 {object} dsmodels.DsRemoteApiScopeList[models.SonarqubeProject] "the parentIds are always null"
// @Tags plugins/sonarqube
// @Router /plugins/sonarqube/connections/{connectionId}/search-remote-scopes [GET]
func SearchRemoteScopes(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
return raScopeSearch.Get(input)
}
// @Summary Remote server API proxy
// @Description Forward API requests to the specified remote server
// @Param connectionId path int true "connection ID"
// @Param path path string true "path to a API endpoint"
// @Tags plugins/sonarqube
// @Router /plugins/sonarqube/connections/{connectionId}/proxy/{path} [GET]
func Proxy(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
return raProxy.Proxy(input)
}