简体   繁体   中英

Add Multiple Watchers to JIRA Issue via REST API

Using PHP and the JIRA REST API, I can add watchers to existing issues via this code:

$username = 'xxxx';
$password = 'xxxx';
$proxy = 'http://xxxx:8080/';
$url = "http://xxxx/rest/api/2/issue/xxxx/watchers";

$data = 'name1';

$ch = curl_init();

$headers = array(
    'Accept: application/json',
    'Content-Type: application/json'
);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

$result = curl_exec($ch);
$ch_error = curl_error($ch);

if ($ch_error) {
    echo "cURL Error: $ch_error";
} else {
    echo $result;
}
curl_close($ch);

However, this method only allows me to add one watcher. Is there a way of adding multiple watchers to an existing issue? I tried doing it this way:

$data = array(
    'name1',
    'name2',
);

But this results in a bad request error.

So the only way I could get around this is to just make multiple API calls to add the watchers. For some reason the API won't accept properly formatted JSON calls with multiple names. If just a single name, the JSON output is just "name1" , but for multiple names in an array, it becomes ["name1","name2","name3"] (the square brackets is what throws it off apparently).

If anyone knows a better way please let me know, but this is what I ended up doing (I really consider this a workaround more than an answer though):

$username = 'xxxx';
$password = 'xxxx';
$proxy = 'http://xxxx:8080/';
$url = "http://xxxx/rest/api/2/issue/xxxx/watchers";
$data = array(
    'name1',
    'name2',
    'name3'
);
$headers = array(
    'Accept: application/json',
    'Content-Type: application/json'
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

foreach ($data as $key => $user)
{
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($user));
    $result = curl_exec($ch);
}

curl_close($ch);

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