Python – How to set up an issue pipeline using the ZenHub API

How to set up an issue pipeline using the ZenHub API… here is a solution to the problem.

How to set up an issue pipeline using the ZenHub API

We use ZenHub in enterprise GitHub installations. I’m writing a script to transfer issues from one GitHub repository to another, including ZenHub information. I’ve copied the issue, set the label and milestone. Then I use the ZenHub API to set estimates and create epics. Everything works fine. My last step was to assign the issue to the ZenHub pipeline. The following works fine (get information about the problem):

zenhub_headers = {"X-Authentication-Token": "%s" % zenhub_token}
url = '%s/p1/repositories/%d/issues/15' % (zenhub_endpoint, repo)
response = requests.get(url, headers=zenhub_headers, verify=False)

However, when I try to transfer the same issue to a pipeline with the following:

params = json.dumps({"pipeline_id": "5a36d8584b9b9e57bc9729f9"} )
zenhub_headers = {"X-Authentication-Token": "%s" % zenhub_token}
url = '%s/p1/repositories/%d/issues/15/moves' % (zenhub_endpoint, repo)
response = requests.post(url, headers=zenhub_headers, data=params, verify=False)

I get a 400:b'{"message":"Invalid Field for pipeline_id: undefined"}'. I have verified that pipeline 5a36d8584b9e57bc9729f9 does exist in the target repository.

The API is still in beta. I wonder if this is a bug in the API or if I’m doing something wrong.

Solution

Here is Pablo from ZenHub. The problem here is that the request is malformed. Lack of position parameter, no need to encode the request body into a string, just send a dictionary directly:

import requests

# No need to stringify
params = {
    "pipeline_id": "5a36d8584b9b9e57bc9729f9",
    "position": "top"
}

# some code omitted here...

response = requests.post(url, headers=zenhub_headers, data=params, verify=False)

Documentation for moving the problem endpoint is available here

Related Problems and Solutions