Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package migrationscripts

import (
"github.com/apache/incubator-devlake/core/context"
"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/plugin"
)

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

type mrAdditionsDeletions struct {
Additions int
Deletions int
}

func (mrAdditionsDeletions) TableName() string {
return "_tool_gitlab_merge_requests"
}

type addAdditionsDeletionsToMr struct{}

func (*addAdditionsDeletionsToMr) Up(basicRes context.BasicRes) errors.Error {
return errors.Convert(basicRes.GetDal().AutoMigrate(&mrAdditionsDeletions{}))
}

func (*addAdditionsDeletionsToMr) Version() uint64 {
return 20260615000001
}

func (*addAdditionsDeletionsToMr) Name() string {
return "gitlab: add additions and deletions to merge requests"
}
1 change: 1 addition & 0 deletions backend/plugins/gitlab/models/migrationscripts/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ func All() []plugin.MigrationScript {
new(changeIssueComponentType),
new(addIsChildToPipelines240906),
new(addPrSizeExcludedFileExtensions),
new(addAdditionsDeletionsToMr),
}
}
2 changes: 2 additions & 0 deletions backend/plugins/gitlab/models/mr.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type GitlabMergeRequest struct {
AuthorUsername string `gorm:"type:varchar(255)"`
AuthorUserId int
Component string `gorm:"type:varchar(255)"`
Additions int `gorm:"comment:Lines added in this MR diff only"`
Deletions int `gorm:"comment:Lines deleted in this MR diff only"`
common.NoPKModel
}

Expand Down
1 change: 1 addition & 0 deletions backend/plugins/gitlab/tasks/mr_detail_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func CollectApiMergeRequestDetails(taskCtx plugin.SubTaskContext) errors.Error {
Query: func(reqData *helper.RequestData) (url.Values, errors.Error) {
query := url.Values{}
query.Set("with_stats", "true")
query.Set("include_diff_stats", "true")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When was it added to the GitLab API? We need to make sure this won't break older GitLab on-premise instances.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @klesh, good catch!
Looked it up — diff_stats has been around since GitLab 13.0 (June 2020), so most instances should be fine. For anything older, I've made DiffStats a pointer so we can tell when it's missing and fall back to changes_count instead of storing zeros. And include_diff_stats=true in the query is harmless on old versions — they just ignore params they don't know.

return query, nil
},
ResponseParser: GetOneRawMessageFromResponse,
Expand Down
18 changes: 18 additions & 0 deletions backend/plugins/gitlab/tasks/mr_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ type MergeRequestRes struct {
Assignees []Assignee
FirstCommentTime common.Iso8601Time
Labels []string `json:"labels"`
DiffStats *struct {
Additions int `json:"additions"`
Deletions int `json:"deletions"`
} `json:"diff_stats"`
ChangesCount string `json:"changes_count"`
}

type Reviewer struct {
Expand Down Expand Up @@ -246,6 +251,19 @@ func convertMergeRequest(mr *MergeRequestRes) (*models.GitlabMergeRequest, error
AuthorUsername: mr.Author.Username,
AuthorUserId: mr.Author.Id,
}
// Use diff_stats when available (GitLab 13.0+); fall back to changes_count
// for older self-managed instances that don't support include_diff_stats
if mr.DiffStats != nil && (mr.DiffStats.Additions > 0 || mr.DiffStats.Deletions > 0) {
gitlabMergeRequest.Additions = mr.DiffStats.Additions
gitlabMergeRequest.Deletions = mr.DiffStats.Deletions
}
else if mr.ChangesCount != "" && mr.ChangesCount != "0" {
count, err := strconv.Atoi(mr.ChangesCount)
if err == nil {
// changes_count is cumulative (bug source), but better than zero
gitlabMergeRequest.Additions = count
}
}
return gitlabMergeRequest, nil
}

Expand Down