Project

General

Profile

Redmine - author_id Params Not Working When Creating New Issue Through REST API

Added by manikandan S 7 months ago

I have migrated the Redmine version from 3.4.6 to 5.0.5 and I used to create a Redmine issue via the rest API, After migration author_id param is not working

I used the below code to create an issue

Before migration: It created an issue with dynamic author_id
After migration: It created an issue with the user's ID that authenticates with the API. (author_id params not working)


<?php

// Redmine API endpoint for issues
$redmineUrl = 'https://live-url/issues.json';

$apiKey = 'XXXXXXXXXXXX';

// Issue data
$issueData = array(
    'issue' => array(
        'project_id' => 'xyz',
        'subject' => 'Issue subject',
        'description' => 'Issue description',
        'tracker_id' => 'bug', // Tracker ID for the type of issue (e.g., bug, feature, task)
        'status_id' => 'New', // Status ID for the initial status of the issue
        'author_id' => '12',
        'watcher_user_ids' => [70,16],   
    )
);

// Set up cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $redmineUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($issueData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'X-Redmine-API-Key: ' . $apiKey
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the request
$response = curl_exec($ch);

if ($response === false) {
    echo 'Error: ' . curl_error($ch);
} else {
    echo 'Issue created successfully!';
}

curl_close($ch);


Replies (1)

RE: Redmine - author_id Params Not Working When Creating New Issue Through REST API - Added by Diana Hill 6 months ago

It seems that the author_id parameter is not functioning as expected after your migration to Redmine version 5.0.5. In Redmine, the author of an issue is typically set to the user associated with the API key used to make the request. However, there is a feature called “User Impersonation” which allows an admin user to create issues on behalf of another user by setting the X-Redmine-Switch-User header to the login of the user you want to impersonate.

Here’s a simplified version of your PHP code with the addition of the X-Redmine-Switch-User header:

<?php

// Redmine API endpoint for issues
$redmineUrl = 'https://live-url/issues.json';

$apiKey = 'XXXXXXXXXXXX';
$impersonateUserLogin = 'user_login'; // Replace with the login of the user you want to impersonate

// Issue data
$issueData = array(
    'issue' => array(
        'project_id' => 'xyz',
        'subject' => 'Issue subject',
        'description' => 'Issue description',
        'tracker_id' => 'bug',
        'status_id' => 'New',
        // 'author_id' => '12', // This line is no longer needed
        'watcher_user_ids' => [70,16],   
    )
);

// Set up cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $redmineUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($issueData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'X-Redmine-API-Key: ' . $apiKey,
    'X-Redmine-Switch-User: ' . $impersonateUserLogin // Add this line
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the request
$response = curl_exec($ch);

if ($response === false) {
    echo 'Error: ' . curl_error($ch);
} else {
    echo 'Issue created successfully!';
}

curl_close($ch);
?>

Make sure to replace 'user_login' with the actual login of the user you want to impersonate.

I hope this step is helpful for you.

Best Regard,
diana658h

    (1-1/1)