简体   繁体   中英

Updating a jira issue with the rest api. NOT soap

my php function to update jira issue is like this.i have hardcoded the issue id.it generates an error in if (property_exists($result, 'errors')) . saying parameter is not an object.

function post_to($resource, $data) {
$jdata = json_encode($data);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_POST => 1,
CURLOPT_URL => JIRA_URL . '/rest/api/latest/' . $resource,
CURLOPT_USERPWD => USERNAME . ':' . PASSWORD,
CURLOPT_POSTFIELDS => $jdata,
CURLOPT_HTTPHEADER => array('Content-type: application/json'),
CURLOPT_RETURNTRANSFER => true
));
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}

function create_issue($issue) {
return post_to('issue/10224/editmeta', $issue);
}

$new_issue = array(
    'update' =>array(
        'fields' => array(
            'project' => array('key' => 'DOTNET'),
            'summary' => 'Test via REST',
            'description' => 'Description of issue goes here.',
            'issuetype' => array('name' => 'Task')
            ))
);

$result = create_issue($new_issue);
if (property_exists($result, 'errors')) {
echo "Error(s) creating issue:\n";
var_dump($result);
                }
            }

what am i doing wrong here? please answer.

Not really sure, let's try some thing:

change

CURLOPT_HTTPHEADER => array('Content-type: application/json'),

to:

CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'Content-Type: application/json'
);

and:

$new_issue = array(
    'update' =>array(
        'fields' => array(
            'project' => array('key' => 'DOTNET'),
            'summary' => 'Test via REST',
            'description' => 'Description of issue goes here.',
            'issuetype' => array('name' => 'Task')
            ))
);

to:

$new_issue = array(
    'fields' => array(
        'project' => array('key' => 'DOTNET'),
        'summary' => 'Test via REST',
        'description' => 'Description of issue goes here.',
        'issuetype' => array('name' => 'Task')
     )
);

lastly, change:

CURLOPT_URL => JIRA_URL . '/rest/api/latest/' . $resource,

to your real address, as well as writing 2 instead of latest , ie:

CURLOPT_URL=>'http://localhost/rest/api/2/issue/',

try this, and let me know how it's goes, good luck!

EDIT

try changing:

CURLOPT_POST => 1,

to:

CURL_POST=>true,
CURLOPT_VERBOSE=>1,

btw, where is your jira server? didn't you say it was hosted? localhost:8080 will work only if Jira is installed locally. If so, try opening it using a browser http://localhost:8084/rest/api/2/issue/

EDIT 2

Make sure the Allow Remote API Calls is turned ON under Administration > General Configuration.

to update an issue:

the URL should point to the soon-to-be-updated issue, ie:

http://localhost/rest/api/2/issue/TEST-31

and the data should be the same as before, meaning:

$new_issue = array(
    'fields' => array(
        'project' => array('key' => 'DOTNET'),
        'summary' => 'Test via REST',
        'description' => 'Description of issue goes here.',
        'issuetype' => array('name' => 'Task')
     )
);

just as you wrote when you tried to create an issue. you can find here some simple example of how to do so.

EDIT 3

Are you sure you have the right jira address? try again opening a browser and going to the URL and compare it to this example . If the page won't show, you will have to contact Jira's support and ask them how come you can't access the hosted Jira remote API.

editmeta should be used only to OBTAIN meta data from an issue.

To update an issue you must use the issue method.

You can check the Jira API details here: https://docs.atlassian.com/jira/REST/cloud/#api/2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM