|
| 1 | +/* |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +contributor license agreements. See the NOTICE file distributed with |
| 4 | +this work for additional information regarding copyright ownership. |
| 5 | +The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +(the "License"); you may not use this file except in compliance with |
| 7 | +the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package tasks |
| 19 | + |
| 20 | +import ( |
| 21 | + "reflect" |
| 22 | + |
| 23 | + "github.com/apache/incubator-devlake/core/dal" |
| 24 | + "github.com/apache/incubator-devlake/core/errors" |
| 25 | + "github.com/apache/incubator-devlake/core/models/domainlayer/code" |
| 26 | + "github.com/apache/incubator-devlake/core/models/domainlayer/didgen" |
| 27 | + plugin "github.com/apache/incubator-devlake/core/plugin" |
| 28 | + "github.com/apache/incubator-devlake/helpers/pluginhelper/api" |
| 29 | + "github.com/apache/incubator-devlake/plugins/bitbucket/models" |
| 30 | +) |
| 31 | + |
| 32 | +var ConvertPrReviewersMeta = plugin.SubTaskMeta{ |
| 33 | + Name: "Convert PR Reviewers", |
| 34 | + EntryPoint: ConvertPrReviewers, |
| 35 | + EnabledByDefault: true, |
| 36 | + Required: false, |
| 37 | + Description: "Convert tool layer PR reviewers to domain layer", |
| 38 | + DomainTypes: []string{plugin.DOMAIN_TYPE_CODE_REVIEW}, |
| 39 | +} |
| 40 | + |
| 41 | +func ConvertPrReviewers(taskCtx plugin.SubTaskContext) errors.Error { |
| 42 | + rawDataSubTaskArgs, data := CreateRawDataSubTaskArgs(taskCtx, RAW_PULL_REQUEST_TABLE) |
| 43 | + db := taskCtx.GetDal() |
| 44 | + repoId := data.Options.FullName |
| 45 | + |
| 46 | + cursor, err := db.Cursor( |
| 47 | + dal.From(&models.BitbucketPrReviewer{}), |
| 48 | + dal.Where("repo_id = ? and connection_id = ?", repoId, data.Options.ConnectionId), |
| 49 | + ) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + defer cursor.Close() |
| 54 | + |
| 55 | + prIdGen := didgen.NewDomainIdGenerator(&models.BitbucketPullRequest{}) |
| 56 | + accountIdGen := didgen.NewDomainIdGenerator(&models.BitbucketAccount{}) |
| 57 | + |
| 58 | + converter, err := api.NewDataConverter(api.DataConverterArgs{ |
| 59 | + InputRowType: reflect.TypeOf(models.BitbucketPrReviewer{}), |
| 60 | + Input: cursor, |
| 61 | + RawDataSubTaskArgs: *rawDataSubTaskArgs, |
| 62 | + Convert: func(inputRow interface{}) ([]interface{}, errors.Error) { |
| 63 | + reviewer := inputRow.(*models.BitbucketPrReviewer) |
| 64 | + domainReviewer := &code.PullRequestReviewer{ |
| 65 | + PullRequestId: prIdGen.Generate(data.Options.ConnectionId, reviewer.RepoId, reviewer.PullRequestId), |
| 66 | + ReviewerId: accountIdGen.Generate(data.Options.ConnectionId, reviewer.AccountId), |
| 67 | + Name: reviewer.DisplayName, |
| 68 | + UserName: reviewer.DisplayName, |
| 69 | + } |
| 70 | + return []interface{}{ |
| 71 | + domainReviewer, |
| 72 | + }, nil |
| 73 | + }, |
| 74 | + }) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + return converter.Execute() |
| 80 | +} |
0 commit comments