0

From the GITLAB API documentation,I want to create several commits on a single branch,and after that create a merge request in bash script.

   curl -i --request POST --header "PRIVATE-TOKEN: privatekey" --header "Content-Type: application/json" --data "$PAYLOAD" "https://hostgit/api/v4/projects/1234/repository/commits"

and about PAYLOAD

    PAYLOAD=$(cat << JSON 
{
  "branch": "mybranch",
  "commit_message": "some commit message",
  "actions": [
    {
      "action": "update",
      "file_path": "roles/test.j2"
    },
     {
          "action": "update",
      "file_path": "roles/prod.j2"
      }
  ]
}
JSON
)

script update the files so after this commit i use create MR but its an empty MR.why its empty ?? commit changes are 0. also i use to content in actions a file path but its didn't work and commits still empty.

zone sd
  • 15
  • 7

1 Answers1

1

For the update action, you have to specify the content of the file, and it should be the entire file content, not just the updated parts. Gitlab will then generate the diff and commit the changes correctly. You would need to update your payload so it looks something like:

PAYLOAD=$(cat << JSON
{
  "branch": "mybranch",
  "commit_message": "some commit message",
  "actions": [
    {
      "action": "update",
      "file_path": "roles/test.j2",
      "content": "this is the entire content in the test.j2 file after updating it"
    },
    {
      "action": "update",
      "file_path": "roles/prod.j2",
      "content": "This is the entire content in the prod.j2 file after updating it"
    }
  ]
}
JSON
)

You can see all the optional and required parameters in the docs. ]

Adam Marshall
  • 6,369
  • 1
  • 29
  • 45
  • thanks ,the files are firewall roles with 100 lines i cant put entire file in content !!! and i want make the commit and merge automate in script ,how can i do this? – zone sd Mar 12 '21 at 17:27
  • According to the docs, that's the only way to update a file via the API. Otherwise, you can always use `git`. – Adam Marshall Mar 12 '21 at 17:47
  • so i cant use api i guess in this case its better to use command line git commit and push in script.thank you – zone sd Mar 12 '21 at 17:56
  • You can use the API, but you have to send the files entire content. If that’s difficult due to file size, you can use got instead. – Adam Marshall Mar 12 '21 at 23:03
  • yes difficult , its 100 lines in file ,how i must do that? – zone sd Mar 13 '21 at 07:58
  • [This answer](https://stackoverflow.com/a/10771857/2307873) shows a good way to get a file’s contents into a variable which you can then use when hitting the api. – Adam Marshall Mar 13 '21 at 15:45