| /* |
| 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" |
| "github.com/apache/incubator-devlake/config" |
| "github.com/apache/incubator-devlake/plugins/github/models" |
| "net/http" |
| "strings" |
| "time" |
| |
| "github.com/apache/incubator-devlake/plugins/helper" |
| "github.com/go-playground/validator/v10" |
| "github.com/mitchellh/mapstructure" |
| |
| "github.com/apache/incubator-devlake/plugins/core" |
| ) |
| |
| type ApiUserPublicEmailResponse []models.PublicEmail |
| |
| var vld = validator.New() |
| |
| /* |
| POST /plugins/github/test |
| */ |
| func TestConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, error) { |
| // process input |
| var params models.TestConnectionRequest |
| err := mapstructure.Decode(input.Body, ¶ms) |
| if err != nil { |
| return nil, err |
| } |
| err = vld.Struct(params) |
| if err != nil { |
| return nil, err |
| } |
| tokens := strings.Split(params.Auth, ",") |
| |
| // verify multiple token in parallel |
| // PLEASE NOTE: This works because GitHub API Client rotates tokens on each request |
| results := make(chan error) |
| for i := 0; i < len(tokens); i++ { |
| token := tokens[i] |
| j := i + 1 |
| go func() { |
| apiClient, err := helper.NewApiClient( |
| params.Endpoint, |
| map[string]string{ |
| "Authorization": fmt.Sprintf("Bearer %s", token), |
| }, |
| 3*time.Second, |
| params.Proxy, |
| nil, |
| ) |
| if err != nil { |
| results <- fmt.Errorf("verify token failed for #%v %s %w", j, token, err) |
| return |
| } |
| res, err := apiClient.Get("user/public_emails", nil, nil) |
| if err != nil { |
| results <- fmt.Errorf("verify token failed for #%v %s %w", j, token, err) |
| return |
| } |
| githubApiResponse := &ApiUserPublicEmailResponse{} |
| err = helper.UnmarshalResponse(res, githubApiResponse) |
| if err != nil { |
| results <- fmt.Errorf("verify token failed for #%v %s %w", j, token, err) |
| } else { |
| results <- nil |
| } |
| }() |
| } |
| |
| // collect verification results |
| msgs := make([]string, 0) |
| i := 0 |
| for err := range results { |
| if err != nil { |
| msgs = append(msgs, err.Error()) |
| } |
| i++ |
| if i == len(tokens) { |
| close(results) |
| } |
| } |
| |
| if len(msgs) > 0 { |
| return nil, fmt.Errorf(strings.Join(msgs, "\n")) |
| } |
| |
| // output |
| return nil, nil |
| } |
| |
| /* |
| PATCH /plugins/github/connections/:connectionId |
| */ |
| func PatchConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, error) { |
| v := config.GetConfig() |
| connection := &models.GithubConnection{} |
| err := helper.EncodeStruct(v, connection, "env") |
| if err != nil { |
| return nil, err |
| } |
| // update from request and save to .env |
| err = helper.DecodeStruct(v, connection, input.Body, "env") |
| if err != nil { |
| return nil, err |
| } |
| err = config.WriteConfig(v) |
| if err != nil { |
| return nil, err |
| } |
| response := models.GithubResponse{ |
| GithubConnection: *connection, |
| Name: "Github", |
| ID: 1, |
| } |
| return &core.ApiResourceOutput{Body: response, Status: http.StatusOK}, nil |
| } |
| |
| /* |
| GET /plugins/github/connections |
| */ |
| func ListConnections(input *core.ApiResourceInput) (*core.ApiResourceOutput, error) { |
| // RETURN ONLY 1 SOURCE (FROM ENV) until multi-connection is developed. |
| v := config.GetConfig() |
| connection := &models.GithubConnection{} |
| |
| err := helper.EncodeStruct(v, connection, "env") |
| if err != nil { |
| return nil, err |
| } |
| response := models.GithubResponse{ |
| GithubConnection: *connection, |
| Name: "Github", |
| ID: 1, |
| } |
| |
| return &core.ApiResourceOutput{Body: []models.GithubResponse{response}}, nil |
| } |
| |
| /* |
| GET /plugins/github/connections/:connectionId |
| */ |
| func GetConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, error) { |
| // RETURN ONLY 1 SOURCE FROM ENV (Ignore ID until multi-connection is developed.) |
| v := config.GetConfig() |
| connection := &models.GithubConnection{} |
| err := helper.EncodeStruct(v, connection, "env") |
| if err != nil { |
| return nil, err |
| } |
| response := &models.GithubResponse{ |
| GithubConnection: *connection, |
| Name: "Github", |
| ID: 1, |
| } |
| return &core.ApiResourceOutput{Body: response}, nil |
| } |