| /* |
| 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" |
| "github.com/apache/incubator-devlake/errors" |
| "net/http" |
| "net/url" |
| "time" |
| |
| "github.com/apache/incubator-devlake/plugins/gitee/models" |
| |
| "github.com/apache/incubator-devlake/plugins/helper" |
| "github.com/mitchellh/mapstructure" |
| |
| "github.com/apache/incubator-devlake/plugins/core" |
| ) |
| |
| // @Summary test gitee connection |
| // @Description Test gitee Connection |
| // @Tags plugins/gitee |
| // @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/gitee/test [POST] |
| func TestConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) { |
| var connection models.TestConnectionRequest |
| if err := mapstructure.Decode(input.Body, &connection); err != nil { |
| return nil, errors.BadInput.Wrap(err, "could not decode request parameters") |
| } |
| if err := vld.Struct(connection); err != nil { |
| return nil, errors.BadInput.Wrap(err, "could not validate request parameters") |
| } |
| // test connection |
| apiClient, err := helper.NewApiClient( |
| context.TODO(), |
| connection.Endpoint, |
| nil, |
| 3*time.Second, |
| connection.Proxy, |
| basicRes, |
| ) |
| if err != nil { |
| return nil, err |
| } |
| query := url.Values{} |
| query.Set("access_token", connection.Token) |
| |
| res, err := apiClient.Get("user", query, nil) |
| if err != nil { |
| return nil, err |
| } |
| resBody := &models.ApiUserResponse{} |
| err = helper.UnmarshalResponse(res, resBody) |
| 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 gitee connection |
| // @Description Create gitee connection |
| // @Tags plugins/gitee |
| // @Param body body models.GithubConnection true "json body" |
| // @Success 200 {object} models.GiteeConnection |
| // @Failure 400 {string} errcode.Error "Bad Request" |
| // @Failure 500 {string} errcode.Error "Internel Error" |
| // @Router /plugins/gitee/connections [POST] |
| func PostConnections(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) { |
| connection := &models.GiteeConnection{} |
| err := connectionHelper.Create(connection, input) |
| if err != nil { |
| return nil, err |
| } |
| return &core.ApiResourceOutput{Body: connection, Status: http.StatusOK}, nil |
| } |
| |
| // @Summary patch gitee connection |
| // @Description Patch gitee connection |
| // @Tags plugins/gitee |
| // @Param body body models.GithubConnection true "json body" |
| // @Success 200 {object} models.GiteeConnection |
| // @Failure 400 {string} errcode.Error "Bad Request" |
| // @Failure 500 {string} errcode.Error "Internel Error" |
| // @Router /plugins/gitee/connections/{connectionId} [PATCH] |
| func PatchConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) { |
| connection := &models.GiteeConnection{} |
| err := connectionHelper.Patch(connection, input) |
| if err != nil { |
| return nil, err |
| } |
| return &core.ApiResourceOutput{Body: connection, Status: http.StatusOK}, nil |
| } |
| |
| // @Summary delete a gitee connection |
| // @Description Delete a gitee connection |
| // @Tags plugins/gitee |
| // @Success 200 {object} models.GiteeConnection |
| // @Failure 400 {string} errcode.Error "Bad Request" |
| // @Failure 500 {string} errcode.Error "Internel Error" |
| // @Router /plugins/gitee/connections/{connectionId} [DELETE] |
| func DeleteConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) { |
| connection := &models.GiteeConnection{} |
| 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 gitee connections |
| // @Description Get all gitee connections |
| // @Tags plugins/gitee |
| // @Success 200 {object} models.GiteeConnection |
| // @Failure 400 {string} errcode.Error "Bad Request" |
| // @Failure 500 {string} errcode.Error "Internel Error" |
| // @Router /plugins/gitee/connections [GET] |
| func ListConnections(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) { |
| var connections []models.GiteeConnection |
| err := connectionHelper.List(&connections) |
| if err != nil { |
| return nil, err |
| } |
| |
| return &core.ApiResourceOutput{Body: connections}, nil |
| } |
| |
| // @Summary get gitee connection detail |
| // @Description Get gitee connection detail |
| // @Tags plugins/gitee |
| // @Success 200 {object} models.GiteeConnection |
| // @Failure 400 {string} errcode.Error "Bad Request" |
| // @Failure 500 {string} errcode.Error "Internel Error" |
| // @Router /plugins/gitee/connections/{connectionId} [GET] |
| func GetConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) { |
| connection := &models.GiteeConnection{} |
| err := connectionHelper.First(connection, input.Params) |
| return &core.ApiResourceOutput{Body: connection}, err |
| } |