JIRA API attachment names contain the whole paths of the posted files

浪尽此生 提交于 2019-12-13 18:14:46

问题


I have been working with Jira API and have seen inconsistent results for my requests. Sometimes it works and sometimes it doesn't. Last week I was able to post attachments to issues just fine, but now an old problem occurred: the names of the attachments contain the whole path of the posted file, hence the attachments can't be opened. I use json representation to post files:

$array = array("file"=>"@filename");
json_encode($array);
...

This gets the file posted but the problem is when it's posted the file names in JIRA are like this:

/var/www/user/text.text

Needless to say it can't be opened in JIRA. I had this problem before, then it suddenly disappeared, now it occurred again. I don't really get it. By the way I am not using curl for this request even though it might be recommended in the documentation.


回答1:


I realize this question is somewhat old but I had a similar problem. It seems Jira doesn't necessarily trim the filename as expected. I was able to fix it with the following. If you're using PHP >= 5.5.0:

$url = "http://example.com/jira/rest/api/2/issue/123456/attachments";
$headers = array("X-Atlassian-Token: nocheck");

$attachmentPath = "/full/path/to/file";
$filename = array_pop(explode('/', $attachmentPath));

$cfile = new CURLFile($attachmentPath);
$cfile->setPostFilename($filename);

$data = array('file'=>$cfile);

$ch = curl_init();

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$result = curl_exec($ch);

$ch_error = curl_error($ch);

if ($ch_error){
    echo "cURL Error: $ch_error"; exit();
} else {
    print_r($result);
}

For PHP <5.5.0 but > 5.2.10 (see this bug):

$data = array('file'=>"@{$attachmentPath};filename={$filename}");



回答2:


Yes, I filed an issue on this at https://jira.atlassian.com/browse/JRA-30765 Adding attachments to JIRA by REST is sadly not as useful as it could be.

Interesting that the problem went away - perhaps you were running your script from a different location?



来源:https://stackoverflow.com/questions/18441028/jira-api-attachment-names-contain-the-whole-paths-of-the-posted-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!