Skip to content

Commit

Permalink
in: chnage way how to pull diff from MR
Browse files Browse the repository at this point in the history
use diff file and apply it

Signed-off-by: Jonathan  Monnet <jmonnet@baylibre.com>
  • Loading branch information
Baylibrejmonnet committed May 9, 2022
1 parent 0d8e01a commit eba7e30
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions pkg/in/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,37 +41,37 @@ func (command *Command) Run(destination string, request Request) (Response, erro
return Response{}, err
}

change, _, err := command.client.MergeRequests.GetMergeRequestChanges(request.Source.GetProjectPath(), request.Version.ID, &gitlab.GetMergeRequestChangesOptions{})
if err != nil {
return Response{}, err
}
mr.UpdatedAt = request.Version.UpdatedAt

target, err := command.createRepositoryUrl(mr.TargetProjectID, request.Source.PrivateToken)
if err != nil {
return Response{}, err
}
source, err := command.createRepositoryUrl(mr.SourceProjectID, request.Source.PrivateToken)

commit, _, err := command.client.MergeRequests.GetMergeRequestCommits(mr.ProjectID, mr.IID, &gitlab.GetMergeRequestCommitsOptions{})
if err != nil {
return Response{}, err
}

commit, _, err := command.client.Commits.GetCommit(mr.SourceProjectID, mr.SHA)
err = command.runner.Run("clone", "-c", "http.sslVerify="+strconv.FormatBool(!request.Source.Insecure), "-o", "target", "-b", mr.TargetBranch, target.String(), destination)
if err != nil {
return Response{}, err
}

err = command.runner.Run("clone", "-c", "http.sslVerify="+strconv.FormatBool(!request.Source.Insecure), "-o", "target", "-b", mr.TargetBranch, target.String(), destination)
os.Chdir(destination)

err = createDiffPatchFromApi(change)
if err != nil {
return Response{}, err
}

os.Chdir(destination)
command.runner.Run("remote", "add", "source", source.String())
command.runner.Run("remote", "update")
command.runner.Run("merge", "--no-ff", "--no-commit", mr.SHA)
command.runner.Run("apply", ".git/mr-id_"+strconv.Itoa(mr.ID)+".patch", "--whitespace=nowarn")
if err != nil {
createDiffPatchFromApi(mr)
command.runner.Run("am", ".git/mr-id_"+strconv.Itoa(mr.ID)+".patch")
if err != nil {
return Response{}, err
}
return Response{}, err
}

notes, _ := json.Marshal(mr)
Expand All @@ -85,7 +85,13 @@ func (command *Command) Run(destination string, request Request) (Response, erro
return Response{}, err
}

response := Response{Version: request.Version, Metadata: buildMetadata(mr, commit)}
jsonChanges, _ := json.Marshal(change)
err = ioutil.WriteFile(".git/merge-request-changes", jsonChanges, 0644)
if err != nil {
return Response{}, err
}

response := Response{Version: request.Version, Metadata: buildMetadata(mr, commit[0])}

return response, nil
}
Expand All @@ -105,30 +111,31 @@ func (command *Command) createRepositoryUrl(pid int, token string) (*url.URL, er

return u, nil
}
func createDiffPatchFromApi(mr *gitlab.MergeRequest) error {
fileName := fmt.Sprintf(".git/mr-id_%d.patch", mr.ID)
func createDiffPatchFromApi(mrChange *gitlab.MergeRequest) error {
fileName := fmt.Sprintf(".git/mr-id_" + strconv.Itoa(mrChange.ID) + ".patch")
var data string
_, err := os.Create(fileName)
if err != nil {
log.Fatal(err)
}
for _, change := range mr.Changes {
for _, change := range mrChange.Changes {
if change.NewFile {
data = fmt.Sprintf("new file mode %s\n--- /dev/null\n+++ %s\n%s", change.AMode, change.NewPath, change.Diff)
data = fmt.Sprintf(data+"new file mode %s\n--- /dev/null\n+++ b/%s\n%s", change.AMode, change.NewPath, change.Diff)

} else if change.DeletedFile {
data = fmt.Sprintf("deleted file mode %s\n--- %s\n+++ /dev/null\n%s", change.AMode, change.OldPath, change.Diff)
data = fmt.Sprintf(data+"deleted file mode %s\n--- a/%s\n+++ /dev/null\n%s", change.AMode, change.OldPath, change.Diff)

} else if change.RenamedFile {
data = fmt.Sprintf("--- %s\n+++ %s\n%s", change.OldPath, change.NewPath, change.Diff)
data = fmt.Sprintf(data+"--- a/%s\n+++ b/%s\n%s", change.OldPath, change.NewPath, change.Diff)

} else {
data = fmt.Sprintf("--- %s\n+++ %s\n%s\n", change.OldPath, change.NewPath, change.Diff)
data = fmt.Sprintf(data+"--- a/%s\n+++ b/%s\n%s\n", change.OldPath, change.NewPath, change.Diff)

}
ioutil.WriteFile(fileName, []byte(data), 0644)

}
return nil
ioutil.WriteFile(fileName, []byte(data), 0644)
return err
}
func buildMetadata(mr *gitlab.MergeRequest, commit *gitlab.Commit) pkg.Metadata {
return []pkg.MetadataField{
Expand All @@ -145,9 +152,13 @@ func buildMetadata(mr *gitlab.MergeRequest, commit *gitlab.Commit) pkg.Metadata
Value: mr.SHA,
},
{
Name: "message",
Name: "commit title",
Value: commit.Title,
},
{
Name: "commit message",
Value: commit.Message,
},
{
Name: "title",
Value: mr.Title,
Expand Down

0 comments on commit eba7e30

Please sign in to comment.