| /* |
| 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" |
| "net/http" |
| |
| "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/github/models" |
| "github.com/apache/incubator-devlake/server/api/shared" |
| ) |
| |
| type GithubTestConnResponse struct { |
| shared.ApiBody |
| Login string `json:"login"` |
| } |
| |
| // @Summary test github connection |
| // @Description Test github Connection |
| // @Tags plugins/github |
| // @Param body body models.GithubConn true "json body" |
| // @Success 200 {object} GithubTestConnResponse |
| // @Failure 400 {string} errcode.Error "Bad Request" |
| // @Failure 500 {string} errcode.Error "Internal Error" |
| // @Router /plugins/github/test [POST] |
| func TestConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| // process input |
| var conn models.GithubConn |
| err := api.Decode(input.Body, &conn, vld) |
| if err != nil { |
| return nil, err |
| } |
| |
| apiClient, err := api.NewApiClientFromConnection(context.TODO(), basicRes, &conn) |
| if err != nil { |
| return nil, err |
| } |
| res, err := apiClient.Get("user", nil, nil) |
| if err != nil { |
| return nil, errors.BadInput.Wrap(err, "verify token failed") |
| } |
| if res.StatusCode != http.StatusOK { |
| return nil, errors.HttpStatus(res.StatusCode).New("unexpected status code while testing connection") |
| } |
| |
| githubUserOfToken := &models.GithubUserOfToken{} |
| err = api.UnmarshalResponse(res, githubUserOfToken) |
| if err != nil { |
| return nil, errors.BadInput.Wrap(err, "verify token failed") |
| } else if githubUserOfToken.Login == "" { |
| return nil, errors.BadInput.Wrap(err, "invalid token") |
| } |
| |
| githubApiResponse := &GithubTestConnResponse{} |
| githubApiResponse.Success = true |
| githubApiResponse.Message = "success" |
| githubApiResponse.Login = githubUserOfToken.Login |
| return &plugin.ApiResourceOutput{Body: githubApiResponse, Status: http.StatusOK}, nil |
| } |
| |
| // @Summary create github connection |
| // @Description Create github connection |
| // @Tags plugins/github |
| // @Param body body models.GithubConnection true "json body" |
| // @Success 200 {object} models.GithubConnection |
| // @Failure 400 {string} errcode.Error "Bad Request" |
| // @Failure 500 {string} errcode.Error "Internal Error" |
| // @Router /plugins/github/connections [POST] |
| func PostConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| connection := &models.GithubConnection{} |
| err := connectionHelper.Create(connection, input) |
| if err != nil { |
| return nil, err |
| } |
| return &plugin.ApiResourceOutput{Body: connection, Status: http.StatusOK}, nil |
| } |
| |
| // @Summary patch github connection |
| // @Description Patch github connection |
| // @Tags plugins/github |
| // @Param body body models.GithubConnection true "json body" |
| // @Success 200 {object} models.GithubConnection |
| // @Failure 400 {string} errcode.Error "Bad Request" |
| // @Failure 500 {string} errcode.Error "Internal Error" |
| // @Router /plugins/github/connections/{connectionId} [PATCH] |
| func PatchConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| connection := &models.GithubConnection{} |
| err := connectionHelper.Patch(connection, input) |
| if err != nil { |
| return nil, err |
| } |
| return &plugin.ApiResourceOutput{Body: connection, Status: http.StatusOK}, nil |
| } |
| |
| // @Summary delete a github connection |
| // @Description Delete a github connection |
| // @Tags plugins/github |
| // @Success 200 {object} models.GithubConnection |
| // @Failure 400 {string} errcode.Error "Bad Request" |
| // @Failure 500 {string} errcode.Error "Internal Error" |
| // @Router /plugins/github/connections/{connectionId} [DELETE] |
| func DeleteConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| connection := &models.GithubConnection{} |
| err := connectionHelper.First(connection, input.Params) |
| if err != nil { |
| return nil, err |
| } |
| err = connectionHelper.Delete(connection) |
| return &plugin.ApiResourceOutput{Body: connection}, err |
| } |
| |
| // @Summary get all github connections |
| // @Description Get all github connections |
| // @Tags plugins/github |
| // @Success 200 {object} []models.GithubConnection |
| // @Failure 400 {string} errcode.Error "Bad Request" |
| // @Failure 500 {string} errcode.Error "Internal Error" |
| // @Router /plugins/github/connections [GET] |
| func ListConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| var connections []models.GithubConnection |
| err := connectionHelper.List(&connections) |
| if err != nil { |
| return nil, err |
| } |
| |
| return &plugin.ApiResourceOutput{Body: connections}, nil |
| } |
| |
| // @Summary get github connection detail |
| // @Description Get github connection detail |
| // @Tags plugins/github |
| // @Success 200 {object} models.GithubConnection |
| // @Failure 400 {string} errcode.Error "Bad Request" |
| // @Failure 500 {string} errcode.Error "Internal Error" |
| // @Router /plugins/github/connections/{connectionId} [GET] |
| func GetConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| connection := &models.GithubConnection{} |
| err := connectionHelper.First(connection, input.Params) |
| if err != nil { |
| return nil, err |
| } |
| return &plugin.ApiResourceOutput{Body: connection}, nil |
| } |