簡體   English   中英

通過REST API將多個監視器添加到JIRA問題

[英]Add Multiple Watchers to JIRA Issue via REST API

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

但是,此方法只允許我添加一個觀察者。 有沒有辦法在現有問題上添加多個觀察者? 我試過這樣做:

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

但這會導致錯誤的請求錯誤。

因此,解決這個問題的唯一方法就是只需進行多次API調用即可添加觀察者。 由於某種原因,API不會接受具有多個名稱的格式正確的JSON調用。 如果只是一個名稱,JSON輸出只是"name1" ,但是對於數組中的多個名稱,它變為["name1","name2","name3"] (方括號顯然會拋出它)。

如果有人知道更好的方式請告訴我,但這是我最終做的事情(我真的認為這是一個解決方法而不是答案):

$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