简体   繁体   English

通过REST API将多个监视器添加到JIRA问题

[英]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: 使用PHP和JIRA REST API,我可以通过以下代码将观察者添加到现有问题中:

$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. 因此,解决这个问题的唯一方法就是只需进行多次API调用即可添加观察者。 For some reason the API won't accept properly formatted JSON calls with multiple names. 由于某种原因,API不会接受具有多个名称的格式正确的JSON调用。 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). 如果只是一个名称,JSON输出只是"name1" ,但是对于数组中的多个名称,它变为["name1","name2","name3"] (方括号显然会抛出它)。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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