2

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.

Sp3LLingzz
  • 303
  • 1
  • 3
  • 14
  • try changing `return json_decode($result);` to `return print_r($result);die();` and add the output. thanks! – Kuf Dec 17 '12 at 12:58
  • HTTP Status 405 - Method Not Allowed any luck? – Sp3LLingzz Dec 17 '12 at 13:14
  • I'd test this on a plain local instance on my machine, then check I have permission to create such an issue in the browser on the real machine. Can you use REST methods that retrieve the data from the real machine? – mdoar Dec 17 '12 at 19:56
  • No @mdoar i cannot even try the url on the browser.it doesnt show anything.only a 405 method not allowed message.and i can create issues in the specified project in the hosted jira – Sp3LLingzz Dec 18 '12 at 04:54

2 Answers2

1

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/

tcbrazil
  • 1,315
  • 12
  • 25
0

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, i.e.:

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, i.e.:

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.

Kuf
  • 17,318
  • 6
  • 67
  • 91
  • Nope nothing..im using this url. http://localhost:8080/rest/api/2/issue/10224/editmeta this is right? i think return $result contain empty array. know why? – Sp3LLingzz Dec 17 '12 at 13:19
  • yes we have it hosted.. but i used this to test my app..if something went wrong in that..you can imagine what will happend..lol... and yes i tried it in browser and it displayed all the issues in a json.. and i tried your update its generating a error Warning: First parameter must either be an object or the name of an existing class in G:\Drupal\xampp\htdocs\imap\move.php on line 57 its referring to the (property_exists($result, 'errors')) – Sp3LLingzz Dec 17 '12 at 13:31
  • :) so that's that. `localhost` is getting to your own machine, and once it doesn't find Jira, it fails. Try connecting to your real Jira (you can create a test project) or download jira locally for development. – Kuf Dec 17 '12 at 13:34
  • nothing @Kuf...i used this same code to create an issue. and it works fine..i only edited the json array to include update attribute and the url...i think its sth wrong with the curl_setopt...but not sure – Sp3LLingzz Dec 17 '12 at 13:37
  • ok i will try the real thing and will let you know..thanks..:) – Sp3LLingzz Dec 17 '12 at 13:39
  • what URL are you using? if you type is in a browser, do you get a page? – Kuf Dec 17 '12 at 14:02
  • hey @Kuf my hosted server uses a https..is that a prob? – Sp3LLingzz Dec 17 '12 at 14:04
  • no, as long as the `Allow Remote API Calls` option is turned ON. the page should show. – Kuf Dec 17 '12 at 14:24
  • Hey @Kuf its not working.even the create new issue function is not working aswell.but it works in the localhost not in the real server. – Sp3LLingzz Dec 18 '12 at 04:12
  • is `Allow Remote API Calls option` is turned ON? are you sure you have the right jira adderess? try again opening a browser and going to the URL, you should get [the same page as this](https://jira.atlassian.com/rest/api/2/issue/JRA-9). If you don't, you will have to contact [Jira's support](https://answers.atlassian.com) and ask them how come you can't access the hosted Jira remote API. – Kuf Dec 18 '12 at 07:00
  • im using a ondemand version. does that change anything? no i dont get anything. only a access not allowed error..sigh..i'll try. thanks man – Sp3LLingzz Dec 18 '12 at 07:02
  • that's weird since the `ondemmand` suppose to support the API call by default. just make sure the `Allow Remote API Calls` is turned ON under Administration > General Configuration. – Kuf Dec 18 '12 at 07:04
  • the name might be little different, just allow any remote API calls in the `General Configuration` – Kuf Dec 18 '12 at 07:08
  • sorry...yes that gives me the json of that issue..whats wrong in this? i have tried everything..i can access the issue by typing it in the browser. but cant create anything..and its turned on aswell.. – Sp3LLingzz Dec 18 '12 at 07:10
  • just to make sure, when logging to Jira using the user and password your script uses, can you open an issue? (same type you try to open using the script) – Kuf Dec 18 '12 at 07:33
  • hey @Kuf thanks for all your help. i got it to working. found a great class file in github that does all what i need. and it was a error with the ssl. in https we have to call curl in a different way..:) thanks for all your help.. – Sp3LLingzz Dec 19 '12 at 10:26
  • hey @Kuf i have posted a question.can you take a look at that..thanks http://stackoverflow.com/questions/14108563/updating-status-in-jira-via-php – Sp3LLingzz Jan 01 '13 at 07:22