Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/plugins/bitbucket/impl/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (p Bitbucket) GetTablesInfo() []dal.Tabler {
&models.BitbucketDeployment{},
&models.BitbucketPipelineStep{},
&models.BitbucketPrCommit{},
&models.BitbucketPrReviewer{},
&models.BitbucketScopeConfig{},
}
}
Expand Down Expand Up @@ -125,6 +126,7 @@ func (p Bitbucket) SubTaskMetas() []plugin.SubTaskMeta {
tasks.ConvertRepoMeta,
tasks.ConvertAccountsMeta,
tasks.ConvertPullRequestsMeta,
tasks.ConvertPrReviewersMeta,
tasks.ConvertPrCommentsMeta,
tasks.ConvertPrCommitsMeta,
tasks.ConvertCommitsMeta,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
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 migrationscripts

import (
"time"

"github.com/apache/incubator-devlake/core/context"
"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/models/migrationscripts/archived"
"github.com/apache/incubator-devlake/core/plugin"
)

var _ plugin.MigrationScript = (*addPrReviewerTable)(nil)

type prReviewer20251226 struct {
ConnectionId uint64 `gorm:"primaryKey"`
RepoId string `gorm:"primaryKey;type:varchar(255)"`
PullRequestId int `gorm:"primaryKey"`
AccountId string `gorm:"primaryKey;type:varchar(255)"`
DisplayName string `gorm:"type:varchar(255)"`
Role string `gorm:"type:varchar(100)"`
State string `gorm:"type:varchar(100)"`
Approved bool
ParticipatedOn *time.Time
archived.NoPKModel
}

func (prReviewer20251226) TableName() string {
return "_tool_bitbucket_pr_reviewers"
}

type addPrReviewerTable struct{}

func (*addPrReviewerTable) Up(basicRes context.BasicRes) errors.Error {
db := basicRes.GetDal()
if err := db.AutoMigrate(&prReviewer20251226{}); err != nil {
return err
}
return nil
}

func (*addPrReviewerTable) Version() uint64 {
return 20251226100000
}

func (*addPrReviewerTable) Name() string {
return "add _tool_bitbucket_pr_reviewers table"
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ func All() []plugin.MigrationScript {
new(addMergedByToPr),
new(changeIssueComponentType),
new(addApiTokenAuth),
new(addPrReviewerTable),
}
}
41 changes: 41 additions & 0 deletions backend/plugins/bitbucket/models/pr_reviewer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
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 models

import (
"time"

"github.com/apache/incubator-devlake/core/models/common"
)

type BitbucketPrReviewer struct {
ConnectionId uint64 `gorm:"primaryKey"`
RepoId string `gorm:"primaryKey;type:varchar(255)"`
PullRequestId int `gorm:"primaryKey"`
AccountId string `gorm:"primaryKey;type:varchar(255)"`
DisplayName string `gorm:"type:varchar(255)"`
Role string `gorm:"type:varchar(100)"` // PARTICIPANT, REVIEWER
State string `gorm:"type:varchar(100)"` // approved, changes_requested, null
Approved bool
ParticipatedOn *time.Time
common.NoPKModel
}

func (BitbucketPrReviewer) TableName() string {
return "_tool_bitbucket_pr_reviewers"
}
2 changes: 2 additions & 0 deletions backend/plugins/bitbucket/tasks/pr_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func CollectApiPullRequests(taskCtx plugin.SubTaskContext) errors.Error {
`values.merge_commit.hash,values.merge_commit.date,values.links.html,values.author,values.created_on,values.updated_on,`+
`values.destination.branch.name,values.destination.commit.hash,values.destination.repository.full_name,`+
`values.source.branch.name,values.source.commit.hash,values.source.repository.full_name,`+
`values.participants.user.account_id,values.participants.user.display_name,`+
`values.participants.role,values.participants.state,values.participants.approved,values.participants.participated_on,`+
`page,pagelen,size`,
collectorWithState),
GetTotalPages: GetTotalPagesFromResponse,
Expand Down
41 changes: 39 additions & 2 deletions backend/plugins/bitbucket/tasks/pr_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,15 @@ type BitbucketApiPullRequest struct {
} `json:"commit"`
Repo *models.BitbucketApiRepo `json:"repository"`
} `json:"source"`
//Reviewers []BitbucketAccountResponse `json:"reviewers"`
//Participants []BitbucketAccountResponse `json:"participants"`
Participants []BitbucketParticipant `json:"participants"`
}

type BitbucketParticipant struct {
User *BitbucketAccountResponse `json:"user"`
Role string `json:"role"`
State *string `json:"state"`
Approved bool `json:"approved"`
ParticipatedOn *common.Iso8601Time `json:"participated_on"`
}

func ExtractApiPullRequests(taskCtx plugin.SubTaskContext) errors.Error {
Expand Down Expand Up @@ -117,6 +124,36 @@ func ExtractApiPullRequests(taskCtx plugin.SubTaskContext) errors.Error {
}
results = append(results, bitbucketPr)

// Extract participants/reviewers
for _, participant := range rawL.Participants {
if participant.User == nil {
continue
}
reviewer := &models.BitbucketPrReviewer{
ConnectionId: data.Options.ConnectionId,
RepoId: data.Options.FullName,
PullRequestId: rawL.BitbucketId,
AccountId: participant.User.AccountId,
DisplayName: participant.User.DisplayName,
Role: participant.Role,
Approved: participant.Approved,
}
if participant.State != nil {
reviewer.State = *participant.State
}
if participant.ParticipatedOn != nil {
reviewer.ParticipatedOn = participant.ParticipatedOn.ToNullableTime()
}
results = append(results, reviewer)

// Also save the user account
bitbucketUser, err := convertAccount(participant.User, data.Options.ConnectionId)
if err != nil {
return nil, err
}
results = append(results, bitbucketUser)
}

return results, nil
},
})
Expand Down
80 changes: 80 additions & 0 deletions backend/plugins/bitbucket/tasks/pr_reviewer_convertor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
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 tasks

import (
"reflect"

"github.com/apache/incubator-devlake/core/dal"
"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/models/domainlayer/code"
"github.com/apache/incubator-devlake/core/models/domainlayer/didgen"
plugin "github.com/apache/incubator-devlake/core/plugin"
"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
"github.com/apache/incubator-devlake/plugins/bitbucket/models"
)

var ConvertPrReviewersMeta = plugin.SubTaskMeta{
Name: "Convert PR Reviewers",
EntryPoint: ConvertPrReviewers,
EnabledByDefault: true,
Required: false,
Description: "Convert tool layer PR reviewers to domain layer",
DomainTypes: []string{plugin.DOMAIN_TYPE_CODE_REVIEW},
}

func ConvertPrReviewers(taskCtx plugin.SubTaskContext) errors.Error {
rawDataSubTaskArgs, data := CreateRawDataSubTaskArgs(taskCtx, RAW_PULL_REQUEST_TABLE)
db := taskCtx.GetDal()
repoId := data.Options.FullName

cursor, err := db.Cursor(
dal.From(&models.BitbucketPrReviewer{}),
dal.Where("repo_id = ? and connection_id = ?", repoId, data.Options.ConnectionId),
)
if err != nil {
return err
}
defer cursor.Close()

prIdGen := didgen.NewDomainIdGenerator(&models.BitbucketPullRequest{})
accountIdGen := didgen.NewDomainIdGenerator(&models.BitbucketAccount{})

converter, err := api.NewDataConverter(api.DataConverterArgs{
InputRowType: reflect.TypeOf(models.BitbucketPrReviewer{}),
Input: cursor,
RawDataSubTaskArgs: *rawDataSubTaskArgs,
Convert: func(inputRow interface{}) ([]interface{}, errors.Error) {
reviewer := inputRow.(*models.BitbucketPrReviewer)
domainReviewer := &code.PullRequestReviewer{
PullRequestId: prIdGen.Generate(data.Options.ConnectionId, reviewer.RepoId, reviewer.PullRequestId),
ReviewerId: accountIdGen.Generate(data.Options.ConnectionId, reviewer.AccountId),
Name: reviewer.DisplayName,
UserName: reviewer.DisplayName,
}
return []interface{}{
domainReviewer,
}, nil
},
})
if err != nil {
return err
}

return converter.Execute()
}
Loading
Loading