2

In git lab, you can create an issue, then within the issue you can create a branch. This branch is linked to the issue (I think because of the issue number at the start of the branch name), such that when you do a merge request on that branch it automatically closes the issue.

So my question is - how can you do this via the API? I can create the issue, but there is no control (as far as I can see) within the issue API to create the related branch.

Is that possible?

It would be nice to be able to create an issue with branch in one go - but I don't think that is possible?

code_fodder
  • 15,263
  • 17
  • 90
  • 167

2 Answers2

1

Ok - its not perhaps the best answer, but here is what I came up with for a interim solution (in linux bash):

  1. Raise the issue store response in cmd_resp
  2. Extract the issue ID: echo $cmd_resp | grep -o -P '(?<=iid":).*(?=,"project_id)'. Where the issue ID is found by looking for: iid":<ISSUE-ID>,"project_id
  3. Create a branch with the name: <ISSUE-ID>-some-branch-name - by having the issue ID at the start of the branch name, gitlab automatically makes a relation to that issue.

So - its quite a simple approach, but it does not feel very integrated. I would still prefer to do that from the point of view of the issue.

code_fodder
  • 15,263
  • 17
  • 90
  • 167
1

It is not possible to create a branch related to an issue via the issues API.

However, this is in line with how RESTful APIs should be designed. If you want to create a branch, you need to make a POST request to the branches API.

POST /projects/:id/repository/branches

As you have already found out, GitLab is quite good at automatically linking issues, MRs and branches together. For a branch to be linked to an issue, simply start the branch with the issue ID. However, usually it is enough if a merge request is linked to an issue. In my opinion, you shouldn't really be concerned with the branch. You can later access the branch via issue->MR->branch

Merge requests are linked to issues whenever a MR's description text links to an issue (e.g. #1). If you add an issue ID to the Closes statement, the issue will also be closed upon MR resolution.

Therefore, you could simply create a branch via the API, name it however you want. Then, create a MR from that issue and include Closes #1 in your MR description, where 1 is your issue ID.

Further, I would recommend using a more sophisticated REST client. You shouldn't have to parse the issue ID yourself. It is properly set as a field in the JSON response.

Thomas Kainrad
  • 2,542
  • 21
  • 26
  • 1
    Thanks for the method/approach via MR - that sounds good +1 for that (and the answer that I can't do it from issues). But I specifically only want to raise an issue with the branch linked. The reason is for a special case - otherwise my gitlab IDE does all the work for me already. My use-case is that I have a whole pile of projects that all communicate with each other and so I am having to create issue-branch pairs on them and it gets tedious doing it one by one. So I wrote a script to do it via the gitlab API, it saves me a lot of time! – code_fodder Apr 09 '19 at 11:06
  • Thanks for the feedback, glad you found ways to make it work for you! – Thomas Kainrad Apr 09 '19 at 12:20