Skip to content

Commit

Permalink
Replace deprecated GetMergeRequestChanges function
Browse files Browse the repository at this point in the history
This will replace deprecated GetMergeRequestChanges function
to new function ListMergeRequestDiffs and related changes
in unit test with respect to return types and api url changes

fix #1708

Signed-off-by: Zaki Shaikh <[email protected]>
  • Loading branch information
Zaki Shaikh authored and chmouel committed Jul 4, 2024
1 parent aec0e67 commit d149fa8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 60 deletions.
6 changes: 3 additions & 3 deletions pkg/matcher/annotation_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ func TestMatchPipelinerunAnnotationAndRepositories(t *testing.T) {
Token: github.String("None"),
}
if len(tt.args.fileChanged) > 0 {
commitFiles := &gitlab.MergeRequest{}
commitFiles := []*gitlab.MergeRequestDiff{}
pushFileChanges := []*gitlab.Diff{}
if tt.args.runevent.TriggerTarget == "push" {
for _, v := range tt.args.fileChanged {
Expand All @@ -1040,14 +1040,14 @@ func TestMatchPipelinerunAnnotationAndRepositories(t *testing.T) {
})
} else {
for _, v := range tt.args.fileChanged {
commitFiles.Changes = append(commitFiles.Changes, &gitlab.MergeRequestDiff{
commitFiles = append(commitFiles, &gitlab.MergeRequestDiff{
NewPath: v.FileName,
RenamedFile: v.RenamedFile,
DeletedFile: v.DeletedFile,
NewFile: v.NewFile,
})
}
url := fmt.Sprintf("/projects/0/merge_requests/%d/changes", tt.args.runevent.PullRequestNumber)
url := fmt.Sprintf("/projects/0/merge_requests/%d/diffs", tt.args.runevent.PullRequestNumber)
glMux.HandleFunc(url, func(w http.ResponseWriter, _ *http.Request) {
jeez, err := json.Marshal(commitFiles)
assert.NilError(t, err)
Expand Down
5 changes: 2 additions & 3 deletions pkg/provider/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,13 @@ func (v *Provider) GetFiles(_ context.Context, runevent *info.Event) (changedfil
"exiting... (hint: did you forget setting a secret on your repo?)")
}
if runevent.TriggerTarget == triggertype.PullRequest {
//nolint: staticcheck
mrchanges, _, err := v.Client.MergeRequests.GetMergeRequestChanges(v.targetProjectID, runevent.PullRequestNumber, &gitlab.GetMergeRequestChangesOptions{})
mrchanges, _, err := v.Client.MergeRequests.ListMergeRequestDiffs(v.targetProjectID, runevent.PullRequestNumber, &gitlab.ListMergeRequestDiffsOptions{})
if err != nil {
return changedfiles.ChangedFiles{}, err
}

changedFiles := changedfiles.ChangedFiles{}
for _, change := range mrchanges.Changes {
for _, change := range mrchanges {
changedFiles.All = append(changedFiles.All, change.NewPath)
if change.NewFile {
changedFiles.Added = append(changedFiles.Added, change.NewPath)
Expand Down
102 changes: 48 additions & 54 deletions pkg/provider/gitlab/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ func TestGetFiles(t *testing.T) {
tests := []struct {
name string
event *info.Event
mrchanges *gitlab.MergeRequest
mrchanges []*gitlab.MergeRequestDiff
pushChanges []*gitlab.Diff
wantAddedFilesCount int
wantDeletedFilesCount int
Expand All @@ -503,23 +503,21 @@ func TestGetFiles(t *testing.T) {
Repository: "pullrequestrepository",
PullRequestNumber: 10,
},
mrchanges: &gitlab.MergeRequest{
Changes: []*gitlab.MergeRequestDiff{
{
NewPath: "modified.yaml",
},
{
NewPath: "added.doc",
NewFile: true,
},
{
NewPath: "removed.yaml",
DeletedFile: true,
},
{
NewPath: "renamed.doc",
RenamedFile: true,
},
mrchanges: []*gitlab.MergeRequestDiff{
{
NewPath: "modified.yaml",
},
{
NewPath: "added.doc",
NewFile: true,
},
{
NewPath: "removed.yaml",
DeletedFile: true,
},
{
NewPath: "renamed.doc",
RenamedFile: true,
},
},
wantAddedFilesCount: 1,
Expand All @@ -536,23 +534,21 @@ func TestGetFiles(t *testing.T) {
Repository: "pullrequestrepository",
PullRequestNumber: 10,
},
mrchanges: &gitlab.MergeRequest{
Changes: []*gitlab.MergeRequestDiff{
{
NewPath: "modified.yaml",
},
{
NewPath: "added.doc",
NewFile: true,
},
{
NewPath: "removed.yaml",
DeletedFile: true,
},
{
NewPath: "renamed.doc",
RenamedFile: true,
},
mrchanges: []*gitlab.MergeRequestDiff{
{
NewPath: "modified.yaml",
},
{
NewPath: "added.doc",
NewFile: true,
},
{
NewPath: "removed.yaml",
DeletedFile: true,
},
{
NewPath: "renamed.doc",
RenamedFile: true,
},
},
wantAddedFilesCount: 0,
Expand Down Expand Up @@ -599,27 +595,25 @@ func TestGetFiles(t *testing.T) {
ctx, _ := rtesting.SetupFakeContext(t)
fakeclient, mux, teardown := thelp.Setup(t)
defer teardown()
mergeFileChanges := &gitlab.MergeRequest{
Changes: []*gitlab.MergeRequestDiff{
{
NewPath: "modified.yaml",
},
{
NewPath: "added.doc",
NewFile: true,
},
{
NewPath: "removed.yaml",
DeletedFile: true,
},
{
NewPath: "renamed.doc",
RenamedFile: true,
},
mergeFileChanges := []*gitlab.MergeRequestDiff{
{
NewPath: "modified.yaml",
},
{
NewPath: "added.doc",
NewFile: true,
},
{
NewPath: "removed.yaml",
DeletedFile: true,
},
{
NewPath: "renamed.doc",
RenamedFile: true,
},
}
if tt.event.TriggerTarget == "pull_request" {
mux.HandleFunc(fmt.Sprintf("/projects/10/merge_requests/%d/changes",
mux.HandleFunc(fmt.Sprintf("/projects/10/merge_requests/%d/diffs",
tt.event.PullRequestNumber), func(rw http.ResponseWriter, _ *http.Request) {
jeez, err := json.Marshal(mergeFileChanges)
assert.NilError(t, err)
Expand Down Expand Up @@ -664,7 +658,7 @@ func TestGetFiles(t *testing.T) {

if tt.event.TriggerTarget == "pull_request" {
for i := range changedFiles.All {
assert.Equal(t, tt.mrchanges.Changes[i].NewPath, changedFiles.All[i])
assert.Equal(t, tt.mrchanges[i].NewPath, changedFiles.All[i])
}
}
if tt.event.TriggerTarget == "push" {
Expand Down

0 comments on commit d149fa8

Please sign in to comment.