【问题标题】:JIRA API attachment names contain the whole paths of the posted filesJIRA API 附件名称包含发布文件的完整路径
【发布时间】:2013-08-26 09:35:03
【问题描述】:

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

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

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

/var/www/user/text.text

不用说它不能在 JIRA 中打开。我之前也有这个问题,后来突然消失了,现在又出现了。我真的不明白。顺便说一句,即使文档中可能会推荐它,我也没有为此请求使用 curl。

【问题讨论】:

    标签: php api jira attachment


    【解决方案1】:

    我意识到这个问题有点老了,但我遇到了类似的问题。似乎 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.2.10(请参阅this bug):

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

    【讨论】:

      【解决方案2】:

      是的,我在https://jira.atlassian.com/browse/JRA-30765 提交了一个问题 遗憾的是,通过 REST 向 JIRA 添加附件并没有它应有的用处。

      有趣的是问题消失了 - 也许您是从不同的位置运行脚本?

      【讨论】:

        猜你喜欢
        • 2011-01-08
        • 1970-01-01
        • 2011-04-19
        • 1970-01-01
        • 2011-04-13
        • 1970-01-01
        • 2011-12-22
        • 2011-07-10
        • 1970-01-01
        相关资源
        最近更新 更多