| /* |
| 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 impl |
| |
| import ( |
| "fmt" |
| |
| "github.com/apache/incubator-devlake/migration" |
| "github.com/apache/incubator-devlake/plugins/core" |
| "github.com/apache/incubator-devlake/plugins/github/api" |
| "github.com/apache/incubator-devlake/plugins/github/models" |
| "github.com/apache/incubator-devlake/plugins/github/models/migrationscripts" |
| "github.com/apache/incubator-devlake/plugins/github/tasks" |
| "github.com/apache/incubator-devlake/plugins/helper" |
| "github.com/spf13/viper" |
| "gorm.io/gorm" |
| ) |
| |
| var _ core.PluginMeta = (*Github)(nil) |
| var _ core.PluginInit = (*Github)(nil) |
| var _ core.PluginTask = (*Github)(nil) |
| var _ core.PluginApi = (*Github)(nil) |
| var _ core.Migratable = (*Github)(nil) |
| var _ core.PluginBlueprintV100 = (*Github)(nil) |
| var _ core.CloseablePluginTask = (*Github)(nil) |
| |
| type Github struct{} |
| |
| func (plugin Github) Init(config *viper.Viper, logger core.Logger, db *gorm.DB) error { |
| api.Init(config, logger, db) |
| return nil |
| } |
| |
| func (plugin Github) Description() string { |
| return "To collect and enrich data from GitHub" |
| } |
| |
| func (plugin Github) SubTaskMetas() []core.SubTaskMeta { |
| return []core.SubTaskMeta{ |
| tasks.CollectApiRepoMeta, |
| tasks.ExtractApiRepoMeta, |
| tasks.CollectApiIssuesMeta, |
| tasks.ExtractApiIssuesMeta, |
| tasks.CollectApiPullRequestsMeta, |
| tasks.ExtractApiPullRequestsMeta, |
| tasks.CollectApiCommentsMeta, |
| tasks.ExtractApiCommentsMeta, |
| tasks.CollectApiEventsMeta, |
| tasks.ExtractApiEventsMeta, |
| tasks.CollectApiPullRequestCommitsMeta, |
| tasks.ExtractApiPullRequestCommitsMeta, |
| tasks.CollectApiPullRequestReviewsMeta, |
| tasks.ExtractApiPullRequestReviewsMeta, |
| tasks.CollectApiPrReviewCommentsMeta, |
| tasks.ExtractApiPrReviewCommentsMeta, |
| tasks.CollectApiCommitsMeta, |
| tasks.ExtractApiCommitsMeta, |
| tasks.CollectApiCommitStatsMeta, |
| tasks.ExtractApiCommitStatsMeta, |
| tasks.CollectMilestonesMeta, |
| tasks.ExtractMilestonesMeta, |
| tasks.CollectAccountsMeta, |
| tasks.ExtractAccountsMeta, |
| tasks.CollectAccountOrgMeta, |
| tasks.ExtractAccountOrgMeta, |
| tasks.EnrichPullRequestIssuesMeta, |
| tasks.ConvertRepoMeta, |
| tasks.ConvertIssuesMeta, |
| tasks.ConvertCommitsMeta, |
| tasks.ConvertIssueLabelsMeta, |
| tasks.ConvertPullRequestCommitsMeta, |
| tasks.ConvertPullRequestsMeta, |
| tasks.ConvertPullRequestReviewsMeta, |
| tasks.ConvertPullRequestLabelsMeta, |
| tasks.ConvertPullRequestIssuesMeta, |
| tasks.ConvertIssueCommentsMeta, |
| tasks.ConvertPullRequestCommentsMeta, |
| tasks.ConvertMilestonesMeta, |
| tasks.ConvertAccountsMeta, |
| } |
| } |
| |
| func (plugin Github) PrepareTaskData(taskCtx core.TaskContext, options map[string]interface{}) (interface{}, error) { |
| op, err := tasks.DecodeAndValidateTaskOptions(options) |
| if err != nil { |
| return nil, err |
| } |
| connectionHelper := helper.NewConnectionHelper( |
| taskCtx, |
| nil, |
| ) |
| connection := &models.GithubConnection{} |
| err = connectionHelper.FirstById(connection, op.ConnectionId) |
| if err != nil { |
| return nil, fmt.Errorf("unable to get github connection by the given connection ID: %v", err) |
| } |
| |
| apiClient, err := tasks.CreateApiClient(taskCtx, connection) |
| if err != nil { |
| return nil, fmt.Errorf("unable to get github API client instance: %v", err) |
| } |
| |
| return &tasks.GithubTaskData{ |
| Options: op, |
| ApiClient: apiClient, |
| }, nil |
| } |
| |
| func (plugin Github) RootPkgPath() string { |
| return "github.com/apache/incubator-devlake/plugins/github" |
| } |
| |
| func (plugin Github) MigrationScripts() []migration.Script { |
| return migrationscripts.All() |
| } |
| |
| func (plugin Github) ApiResources() map[string]map[string]core.ApiResourceHandler { |
| return map[string]map[string]core.ApiResourceHandler{ |
| "test": { |
| "POST": api.TestConnection, |
| }, |
| "connections": { |
| "POST": api.PostConnections, |
| "GET": api.ListConnections, |
| }, |
| "connections/:connectionId": { |
| "GET": api.GetConnection, |
| "PATCH": api.PatchConnection, |
| "DELETE": api.DeleteConnection, |
| }, |
| } |
| } |
| |
| func (plugin Github) MakePipelinePlan(connectionId uint64, scope []*core.BlueprintScopeV100) (core.PipelinePlan, error) { |
| return api.MakePipelinePlan(plugin.SubTaskMetas(), connectionId, scope) |
| } |
| |
| func (plugin Github) Close(taskCtx core.TaskContext) error { |
| data, ok := taskCtx.GetData().(*tasks.GithubTaskData) |
| if !ok { |
| return fmt.Errorf("GetData failed when try to close %+v", taskCtx) |
| } |
| data.ApiClient.Release() |
| return nil |
| } |