Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: read merge base from gitlab MR API #1307

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 3 additions & 12 deletions service/gitlab/gitlab_mr_diff.go
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"os/exec"
"strings"

"github.com/xanzy/go-gitlab"

Expand Down Expand Up @@ -51,20 +50,12 @@ func (g *MergeRequestDiff) Diff(ctx context.Context) ([]byte, error) {
if err != nil {
return nil, err
}
targetBranch, _, err := g.cli.Branches.GetBranch(mr.TargetProjectID, mr.TargetBranch, nil)
if err != nil {
return nil, err
}
return g.gitDiff(ctx, g.sha, targetBranch.Commit.ID)
targetSha := mr.DiffRefs.BaseSha
return g.gitDiff(ctx, g.sha, targetSha)
}

func (g *MergeRequestDiff) gitDiff(_ context.Context, baseSha, targetSha string) ([]byte, error) {
b, err := exec.Command("git", "merge-base", targetSha, baseSha).Output()
if err != nil {
return nil, fmt.Errorf("failed to get merge-base commit: %w", err)
}
mergeBase := strings.Trim(string(b), "\n")
bytes, err := exec.Command("git", "diff", "--find-renames", mergeBase, baseSha).Output()
bytes, err := exec.Command("git", "diff", "--find-renames", targetSha, baseSha).Output()
if err != nil {
return nil, fmt.Errorf("failed to run git diff: %w", err)
}
Expand Down
13 changes: 1 addition & 12 deletions service/gitlab/gitlab_mr_diff_test.go
Expand Up @@ -11,21 +11,13 @@ import (

func TestGitLabMergeRequestDiff_Diff(t *testing.T) {
getMRAPICall := 0
getBranchAPICall := 0
mux := http.NewServeMux()
mux.HandleFunc("/api/v4/projects/o/r/merge_requests/14", func(w http.ResponseWriter, r *http.Request) {
getMRAPICall++
if r.Method != http.MethodGet {
t.Errorf("unexpected access: %v %v", r.Method, r.URL)
}
w.Write([]byte(`{"target_project_id": 14, "target_branch": "test-branch"}`))
})
mux.HandleFunc("/api/v4/projects/14/repository/branches/test-branch", func(w http.ResponseWriter, r *http.Request) {
getBranchAPICall++
if r.Method != http.MethodGet {
t.Errorf("unexpected access: %v %v", r.Method, r.URL)
}
w.Write([]byte(`{"commit": {"id": "HEAD~"}}`))
w.Write([]byte(`{"target_project_id": 14, "target_branch": "test-branch","diff_refs": {"base_sha": "HEAD~", "head_sha": "HEAD", "start_sha": "HEAD"}}`))
})

ts := httptest.NewServer(mux)
Expand All @@ -46,7 +38,4 @@ func TestGitLabMergeRequestDiff_Diff(t *testing.T) {
if getMRAPICall != 1 {
t.Errorf("Get GitLab MergeRequest API called %v times, want once", getMRAPICall)
}
if getBranchAPICall != 1 {
t.Errorf("Get GitLab Branch API called %v times, want once", getBranchAPICall)
}
}