This is an automated email from the ASF dual-hosted git repository.
abeizn pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git
The following commit(s) were added to refs/heads/main by this push:
new 771976d21 feat: add jira remote api (#5367)
771976d21 is described below
commit 771976d21160227978542c27b605f2313d635346
Author: mappjzc <[email protected]>
AuthorDate: Tue Jun 6 15:03:48 2023 +0800
feat: add jira remote api (#5367)
Add jira remote API.
Nddtfjiang <[email protected]>
---
backend/plugins/jira/api/init.go | 8 +++
backend/plugins/jira/api/remote.go | 78 +++++++++++++++++++++++++
backend/plugins/jira/impl/impl.go | 3 +
backend/plugins/jira/models/board.go | 10 ++++
backend/plugins/jira/tasks/apiv2models/board.go | 18 ++++--
5 files changed, 111 insertions(+), 6 deletions(-)
diff --git a/backend/plugins/jira/api/init.go b/backend/plugins/jira/api/init.go
index b38d8218c..8f4132324 100644
--- a/backend/plugins/jira/api/init.go
+++ b/backend/plugins/jira/api/init.go
@@ -21,12 +21,14 @@ import (
"github.com/apache/incubator-devlake/core/context"
"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
"github.com/apache/incubator-devlake/plugins/jira/models"
+ "github.com/apache/incubator-devlake/plugins/jira/tasks/apiv2models"
"github.com/go-playground/validator/v10"
)
var vld *validator.Validate
var connectionHelper *api.ConnectionApiHelper
var scopeHelper *api.ScopeApiHelper[models.JiraConnection, models.JiraBoard,
models.JiraScopeConfig]
+var remoteHelper *api.RemoteApiHelper[models.JiraConnection, models.JiraBoard,
apiv2models.Board, api.NoRemoteGroupResponse]
var basicRes context.BasicRes
var scHelper *api.ScopeConfigHelper[models.JiraScopeConfig]
@@ -50,6 +52,12 @@ func Init(br context.BasicRes) {
params,
nil,
)
+
+ remoteHelper = api.NewRemoteHelper[models.JiraConnection,
models.JiraBoard, apiv2models.Board, api.NoRemoteGroupResponse](
+ basicRes,
+ vld,
+ connectionHelper,
+ )
scHelper = api.NewScopeConfigHelper[models.JiraScopeConfig](
basicRes,
vld,
diff --git a/backend/plugins/jira/api/remote.go
b/backend/plugins/jira/api/remote.go
new file mode 100644
index 000000000..5f38b6737
--- /dev/null
+++ b/backend/plugins/jira/api/remote.go
@@ -0,0 +1,78 @@
+/*
+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 (
+ "context"
+ "fmt"
+ "net/url"
+
+ context2 "github.com/apache/incubator-devlake/core/context"
+ "github.com/apache/incubator-devlake/core/errors"
+ "github.com/apache/incubator-devlake/core/plugin"
+ "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
+ "github.com/apache/incubator-devlake/plugins/jira/models"
+ "github.com/apache/incubator-devlake/plugins/jira/tasks/apiv2models"
+)
+
+// RemoteScopes list all available scope for users
+// @Summary list all available scope for users
+// @Description list all available scope for users
+// @Tags plugins/jira
+// @Accept application/json
+// @Param connectionId path int false "connection ID"
+// @Param groupId query string false "group ID"
+// @Param pageToken query string false "page Token"
+// @Success 200 {object} api.RemoteScopesOutput
+// @Failure 400 {object} shared.ApiBody "Bad Request"
+// @Failure 500 {object} shared.ApiBody "Internal Error"
+// @Router /plugins/jira/connections/{connectionId}/remote-scopes [GET]
+func RemoteScopes(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput,
errors.Error) {
+ return remoteHelper.GetScopesFromRemote(input,
+ nil,
+ func(basicRes context2.BasicRes, gid string, queryData
*api.RemoteQueryData, connection models.JiraConnection) ([]apiv2models.Board,
errors.Error) {
+ query := initialQuery(queryData)
+ // create api client
+ apiClient, err :=
api.NewApiClientFromConnection(context.TODO(), basicRes, &connection)
+ if err != nil {
+ return nil, err
+ }
+ res, err := apiClient.Get("agile/1.0/board", query, nil)
+ if err != nil {
+ return nil, err
+ }
+
+ resBody := struct {
+ Values []apiv2models.Board `json:"values"`
+ }{}
+
+ err = api.UnmarshalResponse(res, &resBody)
+ if err != nil {
+ return nil, err
+ }
+
+ return resBody.Values, err
+ })
+}
+
+func initialQuery(queryData *api.RemoteQueryData) url.Values {
+ query := url.Values{}
+ query.Set("maxResults", fmt.Sprintf("%v", queryData.PerPage))
+ query.Set("startAt", fmt.Sprintf("%v",
(queryData.Page-1)*queryData.PerPage))
+ return query
+}
diff --git a/backend/plugins/jira/impl/impl.go
b/backend/plugins/jira/impl/impl.go
index 1b0cba2ce..933d0c0cf 100644
--- a/backend/plugins/jira/impl/impl.go
+++ b/backend/plugins/jira/impl/impl.go
@@ -285,6 +285,9 @@ func (p Jira) ApiResources()
map[string]map[string]plugin.ApiResourceHandler {
"connections/:connectionId/proxy/rest/*path": {
"GET": api.Proxy,
},
+ "connections/:connectionId/remote-scopes": {
+ "GET": api.RemoteScopes,
+ },
"connections/:connectionId/scopes/:scopeId": {
"GET": api.GetScope,
"PATCH": api.UpdateScope,
diff --git a/backend/plugins/jira/models/board.go
b/backend/plugins/jira/models/board.go
index c87fc5620..d6e72bb91 100644
--- a/backend/plugins/jira/models/board.go
+++ b/backend/plugins/jira/models/board.go
@@ -18,6 +18,8 @@ limitations under the License.
package models
import (
+ "fmt"
+
"github.com/apache/incubator-devlake/core/models/common"
)
@@ -32,6 +34,14 @@ type JiraBoard struct {
Type string `json:"type" mapstructure:"type"
gorm:"type:varchar(100)"`
}
+func (b JiraBoard) ScopeId() string {
+ return fmt.Sprintf("%d", b.BoardId)
+}
+
+func (b JiraBoard) ScopeName() string {
+ return b.Name
+}
+
func (JiraBoard) TableName() string {
return "_tool_jira_boards"
}
diff --git a/backend/plugins/jira/tasks/apiv2models/board.go
b/backend/plugins/jira/tasks/apiv2models/board.go
index 41a64bfc5..eeb419042 100644
--- a/backend/plugins/jira/tasks/apiv2models/board.go
+++ b/backend/plugins/jira/tasks/apiv2models/board.go
@@ -18,6 +18,7 @@ limitations under the License.
package apiv2models
import (
+ "github.com/apache/incubator-devlake/core/plugin"
"github.com/apache/incubator-devlake/plugins/jira/models"
)
@@ -37,16 +38,21 @@ type Board struct {
} `json:"location"`
}
-func (b Board) ToToolLayer(connectionId uint64) *models.JiraBoard {
+func (b Board) ConvertApiScope() plugin.ToolLayerScope {
result := &models.JiraBoard{
- ConnectionId: connectionId,
- BoardId: b.ID,
- Name: b.Name,
- Self: b.Self,
- Type: b.Type,
+ BoardId: b.ID,
+ Name: b.Name,
+ Self: b.Self,
+ Type: b.Type,
}
if b.Location != nil {
result.ProjectId = b.Location.ProjectId
}
return result
}
+
+func (b Board) ToToolLayer(connectionId uint64) *models.JiraBoard {
+ result := b.ConvertApiScope().(*models.JiraBoard)
+ result.ConnectionId = connectionId
+ return result
+}