Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/plugins/teambition/api/blueprint_v200.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func MakeDataSourcePipelinePlanV200(

func makePipelinePlanV200(
subtaskMetas []plugin.SubTaskMeta,
scopeDetails []*srvhelper.ScopeDetail[models.TeambitionProject, srvhelper.NoScopeConfig],
scopeDetails []*srvhelper.ScopeDetail[models.TeambitionProject, models.TeambitionScopeConfig],
connection *models.TeambitionConnection,
) (coreModels.PipelinePlan, errors.Error) {
plan := make(coreModels.PipelinePlan, len(scopeDetails))
Expand Down Expand Up @@ -85,7 +85,7 @@ func makePipelinePlanV200(
}

func makeScopesV200(
scopeDetails []*srvhelper.ScopeDetail[models.TeambitionProject, srvhelper.NoScopeConfig],
scopeDetails []*srvhelper.ScopeDetail[models.TeambitionProject, models.TeambitionScopeConfig],
connection *models.TeambitionConnection,
) ([]plugin.Scope, errors.Error) {
scopes := make([]plugin.Scope, 0, len(scopeDetails))
Expand Down
11 changes: 8 additions & 3 deletions backend/plugins/teambition/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,24 @@ import (
"github.com/apache/incubator-devlake/core/context"
"github.com/apache/incubator-devlake/core/plugin"
"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
"github.com/apache/incubator-devlake/helpers/srvhelper"
"github.com/apache/incubator-devlake/plugins/teambition/models"
"github.com/go-playground/validator/v10"
)

var vld *validator.Validate
var dsHelper *api.DsHelper[models.TeambitionConnection, models.TeambitionProject, srvhelper.NoScopeConfig]
var dsHelper *api.DsHelper[models.TeambitionConnection, models.TeambitionProject, models.TeambitionScopeConfig]
var basicRes context.BasicRes
var raProxy *api.DsRemoteApiProxyHelper[models.TeambitionConnection]
var raScopeList *api.DsRemoteApiScopeListHelper[models.TeambitionConnection, models.TeambitionProject, TeambitionPagination]
var raScopeSearch *api.DsRemoteApiScopeSearchHelper[models.TeambitionConnection, models.TeambitionProject]

func Init(br context.BasicRes, p plugin.PluginMeta) {
basicRes = br
vld = validator.New()
dsHelper = api.NewDataSourceHelper[
models.TeambitionConnection,
models.TeambitionProject,
srvhelper.NoScopeConfig,
models.TeambitionScopeConfig,
](
br,
p.Name(),
Expand All @@ -47,4 +49,7 @@ func Init(br context.BasicRes, p plugin.PluginMeta) {
nil,
nil,
)
raProxy = api.NewDsRemoteApiProxyHelper(dsHelper.ConnApi.ModelApiHelper)
raScopeList = api.NewDsRemoteApiScopeListHelper(raProxy, listTeambitionRemoteScopes)
raScopeSearch = api.NewDsRemoteApiScopeSearchHelper(raProxy, searchTeambitionRemoteProjects)
}
170 changes: 170 additions & 0 deletions backend/plugins/teambition/api/remote_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
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/teambition/models"
)

type TeambitionPagination struct {
PageToken string `json:"pageToken"`
PageSize int `json:"pageSize"`
}

func queryTeambitionProjects(
apiClient plugin.ApiClient,
keyword string,
page TeambitionPagination,
) (
children []dsmodels.DsRemoteApiScopeListEntry[models.TeambitionProject],
nextPage *TeambitionPagination,
err errors.Error,
) {
if page.PageSize == 0 {
page.PageSize = 50
}
res, err := apiClient.Get("v3/project/query", url.Values{
"name": {keyword},
"pageSize": {fmt.Sprintf("%v", page.PageSize)},
"pageToken": {page.PageToken},
}, nil)
if err != nil {
return
}
resBody := struct {
Result []models.TeambitionProject `json:"result"`
NextPageToken string `json:"nextPageToken"`
}{}
err = api.UnmarshalResponse(res, &resBody)
if err != nil {
return
}
for _, project := range resBody.Result {
children = append(children, dsmodels.DsRemoteApiScopeListEntry[models.TeambitionProject]{
Type: api.RAS_ENTRY_TYPE_SCOPE,
Id: fmt.Sprintf("%v", project.Id),
ParentId: nil,
Name: project.Name,
FullName: project.Name,
Data: &project,
})
}
if resBody.NextPageToken != "" {
nextPage = &TeambitionPagination{
PageToken: resBody.NextPageToken,
PageSize: page.PageSize,
}
}
return
}

func listTeambitionRemoteScopes(
connection *models.TeambitionConnection,
apiClient plugin.ApiClient,
groupId string,
page TeambitionPagination,
) (
children []dsmodels.DsRemoteApiScopeListEntry[models.TeambitionProject],
nextPage *TeambitionPagination,
err errors.Error,
) {
// construct the query and request
return queryTeambitionProjects(apiClient, "", page)
}

func searchTeambitionRemoteProjects(
apiClient plugin.ApiClient,
params *dsmodels.DsRemoteApiScopeSearchParams,
) (
children []dsmodels.DsRemoteApiScopeListEntry[models.TeambitionProject],
err errors.Error,
) {
if params.Page == 0 {
params.Page = 1
}
page := TeambitionPagination{
PageSize: params.PageSize,
}
children, _, err = queryTeambitionProjects(apiClient, params.Search, page)
return
}

type Entry = dsmodels.DsRemoteApiScopeListEntry[models.TeambitionProject]
type Node struct {
entry *Entry
}
type Children []*Node

func (a Children) Len() int { return len(a) }
func (a Children) Less(i, j int) bool {
if a[i].entry.Type != a[j].entry.Type {
return a[i].entry.Type < a[j].entry.Type
}
return a[i].entry.Name < a[j].entry.Name
}
func (a Children) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

// RemoteScopes list all available scope for users
// @Summary list all available scope for users
// @Description list all available scope for users
// @Tags plugins/tapd
// @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} dsmodels.DsRemoteApiScopeList[models.TapdWorkspace]
// @Failure 400 {object} shared.ApiBody "Bad Request"
// @Failure 500 {object} shared.ApiBody "Internal Error"
// @Router /plugins/teambition/connections/{connectionId}/remote-scopes [GET]
func RemoteScopes(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
return raScopeList.Get(input)
}

// 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/github
// @Router /plugins/teambition/connections/{connectionId}/proxy/{path} [GET]
func Proxy(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
return raProxy.Proxy(input)
}
107 changes: 107 additions & 0 deletions backend/plugins/teambition/api/scope_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
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 (
"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/teambition/models"
)

type PutScopesReqBody api.PutScopesReqBody[models.TeambitionProject]
type ScopeDetail api.ScopeDetail[models.TeambitionProject, models.TeambitionScopeConfig]

// PutScopes create or update Azure DevOps repo
// @Summary create or update Azure DevOps repo
// @Description Create or update Azure DevOps repo
// @Tags plugins/teambition
// @Accept application/json
// @Param connectionId path int true "connection ID"
// @Param scope body PutScopesReqBody true "json"
// @Success 200 {object} []models.teambitionRepo
// @Failure 400 {object} shared.ApiBody "Bad Request"
// @Failure 500 {object} shared.ApiBody "Internal Error"
// @Router /plugins/teambition/connections/{connectionId}/scopes [PUT]
func PutScopes(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
return dsHelper.ScopeApi.PutMultiple(input)
}

// PatchScope patch to Azure DevOps repo
// @Summary patch to Azure DevOps repo
// @Description patch to Azure DevOps repo
// @Tags plugins/teambition
// @Accept application/json
// @Param connectionId path int true "connection ID"
// @Param scopeId path int true "scope ID"
// @Param scope body models.teambitionRepo true "json"
// @Success 200 {object} models.teambitionRepo
// @Failure 400 {object} shared.ApiBody "Bad Request"
// @Failure 500 {object} shared.ApiBody "Internal Error"
// @Router /plugins/teambition/connections/{connectionId}/scopes/{scopeId} [PATCH]
func PatchScope(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
return dsHelper.ScopeApi.Patch(input)
}

// GetScopes get Azure DevOps repos
// @Summary get Azure DevOps repos
// @Description get Azure DevOps repos
// @Tags plugins/teambition
// @Param connectionId path int true "connection ID"
// @Param searchTerm query string false "search term for scope name"
// @Param pageSize query int false "page size, default 50"
// @Param page query int false "page size, default 1"
// @Param blueprints query bool false "also return blueprints using these scopes as part of the payload"
// @Success 200 {object} []ScopeDetail
// @Failure 400 {object} shared.ApiBody "Bad Request"
// @Failure 500 {object} shared.ApiBody "Internal Error"
// @Router /plugins/teambition/connections/{connectionId}/scopes [GET]
func GetScopes(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
return dsHelper.ScopeApi.GetPage(input)
}

// GetScope get one Azure DevOps repo
// @Summary get one Azure DevOps repo
// @Description get one Azure DevOps repo
// @Tags plugins/teambition
// @Param connectionId path int true "connection ID"
// @Param scopeId path int true "scope ID"
// @Param blueprints query bool false "also return blueprints using these scopes as part of the payload"
// @Success 200 {object} ScopeDetail
// @Failure 400 {object} shared.ApiBody "Bad Request"
// @Failure 500 {object} shared.ApiBody "Internal Error"
// @Router /plugins/teambition/connections/{connectionId}/scopes/{scopeId} [GET]
func GetScope(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
return dsHelper.ScopeApi.GetScopeDetail(input)
}

// DeleteScope delete plugin data associated with the scope and optionally the scope itself
// @Summary delete plugin data associated with the scope and optionally the scope itself
// @Description delete data associated with plugin scope
// @Tags plugins/teambition
// @Param connectionId path int true "connection ID"
// @Param scopeId path int true "scope ID"
// @Param delete_data_only query bool false "Only delete the scope data, not the scope itself"
// @Success 200 {object} models.teambitionRepo
// @Failure 400 {object} shared.ApiBody "Bad Request"
// @Failure 409 {object} srvhelper.DsRefs "References exist to this scope"
// @Failure 500 {object} shared.ApiBody "Internal Error"
// @Router /plugins/teambition/connections/{connectionId}/scopes/{scopeId} [DELETE]
func DeleteScope(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
return dsHelper.ScopeApi.Delete(input)
}
Loading
Loading