JIRA API附件名称包含发布文件的完整路径


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

我一直在使用Jira API,我的请求结果不一致。有时有效,有时无效。上周,我可以很好地发布问题的附件,但现在出现了一个老问题:附件的名称包含发布文件的整个路径,因此无法打开附件。我使用json表示来发布文件:

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

这会发布文件,但问题是当它发布时,JIRA中的文件名是这样的:

/var/www/user/text.text

不用说,它不能在JIRA中打开。我以前有这个问题,然后它突然消失了,现在又发生了。我真的不明白。顺便说一句,尽管文档中可能会推荐使用curl,但我并没有对这个请求使用curl。

我意识到这个问题有点老了,但我也遇到了类似的问题。Jira似乎不一定会像预期的那样修剪文件名。我用以下方法修复了它。如果您使用的是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);
}

对于PHP<5.5.0但>5.2.10(参见此错误):

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

是的,我在https://jira.atlassian.com/browse/JRA-30765遗憾的是,通过REST向JIRA添加附件并没有发挥应有的作用

有趣的是,问题消失了——也许你在不同的地方运行脚本?