github.com/Mistwind/reviewdog@v0.0.0-20230317041057-48e69b6d9e86/service/gitlab/gitlab_push_commits_diff.go (about) 1 package gitlab 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 "os/exec" 8 9 "github.com/xanzy/go-gitlab" 10 11 "github.com/reviewdog/reviewdog" 12 "github.com/reviewdog/reviewdog/service/serviceutil" 13 ) 14 15 var _ reviewdog.DiffService = &PushCommitsDiff{} 16 17 // PushCommitsDiff is a diff service for GitLab Push Commits. 18 type PushCommitsDiff struct { 19 cli *gitlab.Client 20 sha string 21 beforeSHA string 22 projects string 23 24 // wd is working directory relative to root of repository. 25 wd string 26 } 27 28 // NewGitLabPushCommitsDiff returns a new PushCommitsDiff service. 29 // itLabPushCommitsDiff service needs git command in $PATH. 30 func NewGitLabPushCommitsDiff(cli *gitlab.Client, owner, repo string, sha string, beforeSHA string) (*PushCommitsDiff, error) { 31 workDir, err := serviceutil.GitRelWorkdir() 32 if err != nil { 33 return nil, fmt.Errorf("PushCommitsDiff needs 'git' command: %w", err) 34 } 35 return &PushCommitsDiff{ 36 cli: cli, 37 sha: sha, 38 beforeSHA: beforeSHA, 39 projects: owner + "/" + repo, 40 wd: workDir, 41 }, nil 42 } 43 44 // Diff returns a diff of PushCommits. It runs `git diff` locally instead of 45 // diff_url of GitLab Merge Request because diff of diff_url is not suited for 46 // comment API in a sense that diff of diff_url is equivalent to 47 // `git diff --no-renames`, we want diff which is equivalent to 48 // `git diff --find-renames`. 49 // git diff old new 50 func (g *PushCommitsDiff) Diff(ctx context.Context) ([]byte, error) { 51 b, err := g.gitDiff(ctx, g.beforeSHA, g.sha) 52 log.Printf("reviewdog: [gitlab-push-commit-report] PushCommitsDiff: %s\n", string(b)) 53 return b, err 54 } 55 56 func (g *PushCommitsDiff) gitDiff(_ context.Context, baseSha, targetSha string) ([]byte, error) { 57 bytes, err := exec.Command("git", "diff", "--find-renames", baseSha, targetSha).Output() 58 if err != nil { 59 return nil, fmt.Errorf("failed to run git diff: %w", err) 60 } 61 return bytes, nil 62 } 63 64 // Strip returns 1 as a strip of git diff. 65 func (g *PushCommitsDiff) Strip() int { 66 return 1 67 }