| /* |
| 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" |
| "github.com/apache/incubator-devlake/errors" |
| "net/http" |
| "time" |
| |
| "github.com/apache/incubator-devlake/plugins/bitbucket/models" |
| "github.com/apache/incubator-devlake/plugins/core" |
| "github.com/apache/incubator-devlake/plugins/helper" |
| ) |
| |
| // @Summary test bitbucket connection |
| // @Description Test bitbucket Connection |
| // @Tags plugins/bitbucket |
| // @Param body body models.TestConnectionRequest true "json body" |
| // @Success 200 {object} shared.ApiBody "Success" |
| // @Failure 400 {string} errcode.Error "Bad Request" |
| // @Failure 500 {string} errcode.Error "Internel Error" |
| // @Router /plugins/bitbucket/test [POST] |
| func TestConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) { |
| // decode |
| var err errors.Error |
| var connection models.TestConnectionRequest |
| if err := helper.Decode(input.Body, &connection, vld); err != nil { |
| return nil, errors.BadInput.Wrap(err, "could not decode request parameters") |
| } |
| // test connection |
| apiClient, err := helper.NewApiClient( |
| context.TODO(), |
| connection.Endpoint, |
| map[string]string{ |
| "Authorization": fmt.Sprintf("Basic %v", connection.GetEncodedToken()), |
| }, |
| 3*time.Second, |
| connection.Proxy, |
| basicRes, |
| ) |
| if err != nil { |
| return nil, err |
| } |
| res, err := apiClient.Get("user", nil, nil) |
| if err != nil { |
| return nil, err |
| } |
| |
| if res.StatusCode != http.StatusOK { |
| return nil, errors.HttpStatus(res.StatusCode).New("unexpected status code when testing connection") |
| } |
| return nil, nil |
| } |
| |
| // @Summary create bitbucket connection |
| // @Description Create bitbucket connection |
| // @Tags plugins/bitbucket |
| // @Param body body models.BitbucketConnection true "json body" |
| // @Success 200 {object} models.BitbucketConnection |
| // @Failure 400 {string} errcode.Error "Bad Request" |
| // @Failure 500 {string} errcode.Error "Internel Error" |
| // @Router /plugins/bitbucket/connections [POST] |
| func PostConnections(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) { |
| // update from request and save to database |
| connection := &models.BitbucketConnection{} |
| err := connectionHelper.Create(connection, input) |
| if err != nil { |
| return nil, err |
| } |
| return &core.ApiResourceOutput{Body: connection, Status: http.StatusOK}, nil |
| } |
| |
| // @Summary patch bitbucket connection |
| // @Description Patch bitbucket connection |
| // @Tags plugins/bitbucket |
| // @Param body body models.BitbucketConnection true "json body" |
| // @Success 200 {object} models.BitbucketConnection |
| // @Failure 400 {string} errcode.Error "Bad Request" |
| // @Failure 500 {string} errcode.Error "Internel Error" |
| // @Router /plugins/bitbucket/connections/{connectionId} [PATCH] |
| func PatchConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) { |
| connection := &models.BitbucketConnection{} |
| err := connectionHelper.Patch(connection, input) |
| if err != nil { |
| return nil, err |
| } |
| return &core.ApiResourceOutput{Body: connection}, nil |
| } |
| |
| // @Summary delete a bitbucket connection |
| // @Description Delete a bitbucket connection |
| // @Tags plugins/bitbucket |
| // @Success 200 {object} models.BitbucketConnection |
| // @Failure 400 {string} errcode.Error "Bad Request" |
| // @Failure 500 {string} errcode.Error "Internel Error" |
| // @Router /plugins/bitbucket/connections/{connectionId} [DELETE] |
| func DeleteConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) { |
| connection := &models.BitbucketConnection{} |
| err := connectionHelper.First(connection, input.Params) |
| if err != nil { |
| return nil, err |
| } |
| err = connectionHelper.Delete(connection) |
| return &core.ApiResourceOutput{Body: connection}, err |
| } |
| |
| // @Summary get all bitbucket connections |
| // @Description Get all bitbucket connections |
| // @Tags plugins/bitbucket |
| // @Success 200 {object} []models.BitbucketConnection |
| // @Failure 400 {string} errcode.Error "Bad Request" |
| // @Failure 500 {string} errcode.Error "Internel Error" |
| // @Router /plugins/bitbucket/connections [GET] |
| func ListConnections(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) { |
| var connections []models.BitbucketConnection |
| err := connectionHelper.List(&connections) |
| if err != nil { |
| return nil, err |
| } |
| return &core.ApiResourceOutput{Body: connections, Status: http.StatusOK}, nil |
| } |
| |
| // @Summary get bitbucket connection detail |
| // @Description Get bitbucket connection detail |
| // @Tags plugins/bitbucket |
| // @Success 200 {object} models.BitbucketConnection |
| // @Failure 400 {string} errcode.Error "Bad Request" |
| // @Failure 500 {string} errcode.Error "Internel Error" |
| // @Router /plugins/bitbucket/connections/{connectionId} [GET] |
| func GetConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) { |
| connection := &models.BitbucketConnection{} |
| err := connectionHelper.First(connection, input.Params) |
| return &core.ApiResourceOutput{Body: connection}, err |
| } |