Project

General

Profile

Submitting Issues through PHP

Added by Nick Fitzgerald almost 15 years ago

We have a custom error handler written in PHP for our applications. The error handler was previously written so that when an error occurs it checks Redmine for the error and submits a new issue using curl requests. This worked fine up until the security fix in r3051. Now due to the authenticity token this method no longer works (easily).

I was wondering if anyone else is submitting issues to Redmine through a script or another system. The only thing I would like to avoid is inserting the issues directly into the database. I imagine this would be easily fixed once the REST API for Issues (#1214) is completed but we would like to have a solution before then.

Any help or suggestions would be greatly appreciated, thanks!


Replies (20)

RE: Submitting Issues through PHP - Added by Felix Schäfer almost 15 years ago

I think #3920 / r3220 should accommodate your use-case quite nicely, as it allows http-basic or even token-based auth to (I think) any non-html part of redmine. If you run stable only, you'll have to wait for 0.9, but that shouldn't be (too) far around the corner.

RE: Submitting Issues through PHP - Added by Felix Schäfer almost 15 years ago

Come to think of it, the other possibility to submit a ticket would be to make your redmine install accept incoming emails and make your php script sending emails, although that won't give you a list of already submitted tickets to avoid duplication.

RE: Submitting Issues through PHP - Added by Nick Fitzgerald almost 15 years ago

Based on RedmineReceivingEmails the only option available to us would be reading emails from standard input which wouldn't be ideal.

As for the REST API for authentication, I think that only satisfies part of our issue (simulating a login to Redmine). That alone I don't think will allow us to then submit issues. The authenticity token is embedded in every form (including new issues) which is the issue we need to work around.

RE: Submitting Issues through PHP - Added by Nick Fitzgerald almost 15 years ago

Just wanted to give this a little bump to see if anyone else has any suggestions.

Does anyone know if the mylyn plugin could work for this?

RE: Submitting Issues through PHP - Added by Nick Fitzgerald over 14 years ago

So with r3310 an XML REST API for issues has been added. We're a little closer to accomplishing what we want now.

I was able to find a PHP ActiveResource client library at:
http://github.com/lux/phpactiveresource/downloads

I've played around with getting it to work but keep getting a 500 Internal Server Error (or authentication errors). I just wanted to bring this to everyone's attention in case someone else is looking at doing something similar.

RE: Submitting Issues through PHP - Added by Wesley Spikes over 14 years ago

Have you considered just inserting directly into the database? I'd imagine based off what I've seen from `show tables` that the structure and insert process would be pretty simple. At least this should work well for a quick fix until the REST API is completed. :)

RE: Submitting Issues through PHP - Added by Jean-Philippe Lang over 14 years ago

You need to update to r3331 if you want the REST API to properly work with PHP ActiveResource.

The key is to set request_format to 'xml' so that Redmine CSRF protection does not trigger an error.
You can find working examples here: Rest_api_with_php.

RE: Submitting Issues through PHP - Added by Nick Fitzgerald over 14 years ago

Yes, I got it working on the weekend. I was waiting until I got to work today to post. I wasn't setting the request_format to xml before which was the problem. I did a test with the request_format set to xml before r3331 and did it appear to work fine (only tried creating a simple issue).

I see that the API for projects has also been added, thanks a lot! These will be very useful for us.

RE: Submitting Issues through PHP - Added by Nick Fitzgerald over 14 years ago

Jean-Philippe, is it possible to add a new note to an issue with this API? Updating an issue seems to only work for some fields. A working example (if possible) of adding a new comment to an issue would be great.

RE: Submitting Issues through PHP - Added by Michael Holm almost 14 years ago

I've reviewed the working examples, and have searched and read everything I can find. However, I still don't find much solid help in getting the Redmine API to work with PHP. I am successfully able to complete some functionality, but custom fields are a bit of a problem, still. Has anywone had futher progress or luck getting the API to work with custom fields?

RE: Submitting Issues through PHP - Added by Aaron Devey over 13 years ago

Michael Holm wrote:

I've reviewed the working examples, and have searched and read everything I can find. However, I still don't find much solid help in getting the Redmine API to work with PHP. I am successfully able to complete some functionality, but custom fields are a bit of a problem, still. Has anywone had futher progress or luck getting the API to work with custom fields?

Yes, but I had to comment out several lines in ActiveResource.php. Lines: 278, 280, 290, 296, 298
Your mileage may vary on the line numbers. They are all relevant to changing how the XML is generated for numeric indexes. Treating numeric indexes as normal makes custom fields work.

I'm sure there's a much more elegant solution than commenting out those lines, but that's how I got it working.

With those lines commented, ticket creation with custom fields looks something like this:

  $customfields = array('4' => "data", '8' => "more data");
  $ticketinfo = array('subject' => "Ticket subject", 'description' => "Ticket description", 'custom_field_values' => $customfields);
  $issue = new Issue($ticketinfo);
  $issue->save();

In that example, '4' and '8' are the IDs of the custom fields.

RE: Submitting Issues through PHP - Added by Michael Pfannkuchen over 13 years ago

Just coding on update issues with PHP and REST. I found it difficult to push the custom_fields and the notes updates into the Redmine system, that's why I'd like to share my knowledge here. Hope it's helpful for some people out there.

This is my environment:
Redmine 1.1.1
Developing from Windows machine with PHP 5.1.4, Server is at a Centos Linux 5 anywhere in the Internet
ActiveResource.php: no changes of most recent version (not performing the changes mentioned above)

This code worked for updating an Issue (looking for the Subject line):

require_once ('ActiveResource.php');
class Issue extends ActiveResource {
var $site = 'http://user:pass@www.project-roadmap.com/project-portal/';
var $request_format = 'xml'; // REQUIRED!
}
// Get an Issue object of the project with id 16
$issue = new Issue (array ('project_id' => '16'));
// now find my Issue
$issueID=false;
$issues = $issue->find ('all');
for ($i=0; $i < count($issues); $i++) {
echo $issues[$i]->subject; // list Issue subjects for convinience here ...
if (strpos($issues[$i]->subject,'XML API Analyzing')!==false) {
$issueID=$i;
}
}
if ($issueID !== false) {
// find and update the issue with subject 'XML API Analyzing'
$issue->find ($issues[$issueID]->id);
echo 'issueID='.$issue->id.' subject='.$issue->subject;
$issue->set ('subject', 'XML API Analyzing reloaded')->save (); // Subject (and all other fields) will be updated
$customFields=array(); // ! this is drama: apparently you have to setup at least 2 custom fields!!
array_push($customFields,array('name'=>'Task Team', 'id'=>"2", 'value' => 'Steven, Michael')); // id '2' depends on Custom Field "Task Team"
array_push($customFields,array('name'=>'Task Team', 'id'=>"2", 'value' => 'Steven, Michael')); // id '2' depends on Custom Field "Task Team" - this is completely identical to the line above: that's the trick if you only use 1 custom field
$issue->set ('custom_fields', $customFields)->save(); // Just save the custom fields
$issue->set ('notes', 'New note' )->save (); // Just add (not update!) a note
} else {
echo "Issue not found..";
}

RE: Submitting Issues through PHP - Added by Patrick GUILLOU over 13 years ago

Hi everyone, i'm also trying to use rest api with the ActiveResource.php class, following the exemples found on the wiki... but i always got the same error :

Notice: Undefined property: Issue::$id in C:\Program Files\EasyPHP-5.3.3\www\redmineUserImportService\ActiveResource.php on line 506

Ruby console is telling me :

NoMethodError (undefined method 'match' for nil:NilClass)....

I don't know what i'm doing wrong since Rest API is activated under Configuration-> Authentification...

PHP CODE :

require_once ('ActiveResource.php');

if (! extension_loaded ('curl')) {
echo 'cURL extension not loaded.';
exit();
}

class Issue extends ActiveResource {
var $site = 'http://xxxx:xxxxx@localhost:3000/';
var $request_format = 'xml'; // REQUIRED!

}

$issue = new Issue (array ('subject' => 'XML REST API', 'project_id' => '1'));
$issue->save ();
echo $issue->id;

?>

Any idea ?

Patrick

RE: Submitting Issues through PHP - Added by Michael Pfannkuchen over 13 years ago

Hallo Patrick,
I guess there is an simple solution for your REST API problem. Just did run in a maybe similar strange thing:
ID fields will only then be handled correctly by the API, if they are an numeric var: this will checked by an is_numeric() function call. In your case, you have to use
$issue = new Issue (array ('subject' => 'XML REST API', 'project_id' => 1));

If you want to use the REST var project_id given by an variable, you should use code similar to the following:
$xmlId_project_id = $myVarWithProjectId + 0;
$issue = new Issue (array ('subject' => 'XML REST API', 'project_id' => $xmlId_project_id));

Happy Easter: Michael

RE: Submitting Issues through PHP - Added by Patrick GUILLOU over 13 years ago

Hello Michael and thank's a lot for your answer !
As you said i did this
$issue = new Issue (array ('subject' => 'XML REST API', 'project_id' => 1));

unfortunatly this didn't solve totally the problem...
i now have this error from ActiveRessource.php ::

Undefined property: Issue::$id in C:\Program Files\EasyPHP-5.3.3\www\ActiveResource.php on line 475

Could it be because my ActiveResource.php and rest_test.php files are in a different folder than Redmine App ?

I'm gonna make further tests when i'll have time

Anyway thank's a lot Michael...

RE: Submitting Issues through PHP - Added by Yann Droniou over 13 years ago

For custom field, I found this solution:

For one element:
$customFields=array(
'@type' => "array",
'custom_field' => array('id'=>1, 'value' => 'Steven, Michael')
);
$issue->set ('custom_fields', $customFields);
$issue->save();

For more elements:
$customFields=array(
'@type' => "array",
'custom_field' => array(
array('id'=>1, 'value' => 'Steven, Michael'),
array('id'=>2, 'value' => '13245678454526789012345678901235'),
)
);
$issue->set ('custom_fields', $customFields);
$issue->save();

Don't forget to add key 'type' with the value 'array' in custom_fields.
Without this, I have an Internal error from redMine

RE: Submitting Issues through PHP - Added by Michael Pfannkuchen over 13 years ago

Just to keep people back from time-wasting:
Unfortunately Redmine 1.2.0 changed Issues REST-API for the custom_field handling in an ActiveResource incompatible way. I guess you have to patch ActiveResource.php for the handling of custom fields by now.
See: #8698
Good luck: Michael

RE: Submitting Issues through PHP - Added by Michael Pfannkuchen over 13 years ago

Michael Pfannkuchen wrote:

Just to keep people back from time-wasting:
Unfortunately Redmine 1.2.0 changed Issues REST-API for the custom_field handling in an ActiveResource incompatible way. I guess you have to patch ActiveResource.php for the handling of custom fields by now.
See: #8698
Good luck: Michael

Hm, seems I was wrong with my message here - with Redmine version 1.2.0 changed the underlying Rails version and with this the behaviour of the REST-API a little bit, I guess. At last Yann Droniou's message above is the with Redmine 1.2.0 working solution.

RE: Submitting Issues through PHP - Added by Michael Pfannkuchen over 13 years ago

Michael Pfannkuchen wrote:

By Redmine 1.2.0 the custom_fields related code above has to change to:

$customFields=array( '@type' => "array", 'custom_field' => array() );
array_push($customFields['custom_field'],array( 'id'=>2 , 'value' => 'Steven, Michael'));  // id '2' depends on Custom Field "Task Team" 
$issue->set ('custom_fields', $customFields)->save(); // Just save the custom fields

RE: Submitting Issues through PHP - Added by Christian Biggins almost 13 years ago

I know this is old, but for those still struggling with this, I dissected ActiveResource and discovered the correct structure is this (tested on 1.2.2);


$user = array(
    'login' => 'Name'
<snip removed other fields>
    'custom_fields' => array('@type' => "array", 
                                          'custom_field'  => array('@id' => '3', 
                                                                                array('value' => 'Yikes')),
);

Which provides the correct xml structure;


<user>
    <login>Name</login>
    <snip removed other elements>
    <custom_fields type="array">
        <custom_field id="3">
            <value>Yikes</value>
        </custom_field>
    </custom_fields>
</user>

Ensure your request format is correct;

    var $request_format = 'xml';
    (1-20/20)