Travis
Travis

Reputation: 174

How to edit a github issue using the API (curl)? (especially: close)

I'm planning to migrate a couple hundred bugs tracked in another (home-rolled) system into GitHub's issue system. Most of these bugs were closed in the past. I can use github's API to create an issue, e.g.

curl -u $GITHUB_TOKEN:x-oauth-basic https://api.github.com/repos/my_organization/my_repo/issues -d '{
    "title": "test",
    "body": "the body"
}'

... however, this will leave me with a bunch of open issues. How to close those? I've tried just closing at the time of creation, e.g.:

curl -u $GITHUB_TOKEN:x-oauth-basic https://api.github.com/repos/my_organization/my_repo/issues -d '{
    "title": "test",
    "body": "the body",
    "state": "closed"
}'

... but the result is to create an open issue (i.e. the "state" is ignored).

It looks to me like I should be able to "edit" an issue to close it (https://developer.github.com/v3/issues/#edit-an-issue) ... but I'm unable to figure out what the corresponding curl command is supposed to look like. Any guidance?

Extra credit: I'd really like to be able to assign a "closed" date, to agree with the actual closed date captured in our current system. It's not clear that this is possible.

Thanks!

Upvotes: 3

Views: 1086

Answers (2)

Travis
Travis

Reputation: 174

As suggested by hanshenrik, the correct altered curl command is:

curl -u $GITHUB_TOKEN:x-oauth-basic https://api.github.com/repos/my_organization/my_repo/issues/5 -d '{
"state": "closed"
}'

I'd failed to understand the documentation referenced in his answer: /repos/:owner/:repo/issues/:number translates to https://api.github.com/repos/my_organization/my_repo/issues/5 (I now understand that fields starting with ":" are variables)

For the record, I'm planning to script the calls to curl. :)

Upvotes: 1

hanshenrik
hanshenrik

Reputation: 21493

migrating a bunch of issues to github with the command line? are you crazy?

anyway, using php and hhb_curl from https://github.com/divinity76/hhb_.inc.php/blob/master/hhb_.inc.php , this worked for me, unfortunately couldn't set the "closed_at" date (it was ignored by the api), but i could emulate it using labels, then it looked like

enter image description here

, the code should give you something to work on when porting it to command line:

<?php
declare(strict_types = 1);
require_once ('hhb_.inc.php');
$hc=new hhb_curl();
define('BASE_URL','https://api.github.com');
$hc->_setComfortableOptions();
$data=array(
        'state'=>'closed',
        'closed_at'=> '2011-04-22T13:33:48Z',// << unfortunately, ignored
        'labels'=>array(
                'closed at 2011-04-22T13:33:48Z' // << we can fake it using labels...
        )
);
$data=json_encode($data);
$hc->setopt_array(array(
        CURLOPT_CUSTOMREQUEST=>'PATCH',
        // /repos/:owner/:repo/issues/:number
        // https://github.com/divinity76/GitHubCrashTest/issues/1
        CURLOPT_URL=>BASE_URL.'/repos/divinity76/GitHubCrashTest/issues/1',
        CURLOPT_USERAGENT=>'test',
        CURLOPT_HTTPHEADER=>array(
                'Accept: application/vnd.github.v3+json',
                'Content-Type: application/json',
                'Authorization: token <removed>'
        ),
        CURLOPT_POSTFIELDS=>$data,      
));
$hc->exec();
hhb_var_dump($hc->getStdErr(),$hc->getResponseBody());

(i modified the "Authorization: token" line before posting it on stackoverflow ofc)

Upvotes: 1

Related Questions