简体   繁体   English

使用 Jira php API 创建问题

[英]Create issue using Jira php API

I'm trying to create issue using jira rest api, here's my code:我正在尝试使用 jira rest api 创建问题,这是我的代码:

 $new_issue = array(
            'fields' => array(
                'project' => array('key' => "KEY"),
                'summary' => $this->Summary,
                'description' => $this->Description,
                'issuetype' => array('name' => 'Bug')
            )
        );

        $body = json_encode($new_issue);

 self::$handle = curl_init();

 curl_setopt(self::$handle, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt(self::$handle, CURLOPT_POSTFIELDS, $body);
 curl_setopt_array(self::$handle, array(
    CURLOPT_URL => "jiraUrl//rest/api/2/issue/",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_HTTPHEADER => array("content-type:application/json"),
    CURLOPT_HEADER => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_ENCODING => ''
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    CURLOPT_USERPWD => $username . ':' . $password
      ));


     $response   = curl_exec(self::$handle);
            $error      = curl_error(self::$handle);

I'm getting this error: {"errorMessages":["No content to map to Object due to end of input"]} any suggestions?我收到此错误:{"errorMessages":["由于输入结束,没有要映射到对象的内容"]} 有什么建议吗?

Missing closing quotes here:此处缺少结束语:

CURLOPT_URL => "jiraUrl//rest/api/2/issue/

and jiraUrl probably needs to be like $jiraUrl .jiraUrl可能需要像$jiraUrl

So, my take:所以,我的看法:

CURLOPT_URL => "$jiraUrl//rest/api/2/issue/",

For anyone still wondering how to create an issue with JIRA REST API the below minimal code is working for me:对于仍然想知道如何使用 JIRA REST API 创建问题的任何人,以下最小代码对我有用:

$url = "http://your.domain.here/rest/api/latest/issue/"
$username = "username";
$password = "password";
$txt = '{
    "fields": {
        "project": {
            "key": "KEY"
        },
        "summary": "SUMMARY",
        "description": "DESCRIPTION",
        "issuetype": {
            "name": "ISSUETYPE"
        }
    }
}';

// Create a new cURL resource
$ch = curl_init ();

// Set URL and other appropriate options
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $txt );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_USERPWD, $username . ":" . $password );

$headers = array ();
$headers [] = "Content-Type: application/json";
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );

// Grab URL and pass it to the browser
$result = curl_exec ( $ch );

echo $result;

Update for 2021, you can use this PHP Jira REST client: https://github.com/lesstif/php-jira-rest-client 2021 年更新,您可以使用此 PHP Jira REST 客户端: https : //github.com/lesstif/php-jira-rest-client

<?php
require 'vendor/autoload.php';

use JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\IssueField;
use JiraRestApi\JiraException;

try {
    $issueField = new IssueField();

    $issueField->setProjectKey("TEST")
                ->setSummary("something's wrong")
                ->setAssigneeName("lesstif")
                ->setPriorityName("Critical")
                ->setIssueType("Bug")
                ->setDescription("Full description for issue")
                ->addVersion(["1.0.1", "1.0.3"])
                ->addComponents(['Component-1', 'Component-2'])
                // set issue security if you need.
                ->setSecurityId(10001 /* security scheme id */)
                ->setDueDate('2019-06-19')
            ;
    
    $issueService = new IssueService();

    $ret = $issueService->create($issueField);
    
    //If success, Returns a link to the created issue.
    var_dump($ret);
} catch (JiraRestApi\JiraException $e) {
    print("Error Occured! " . $e->getMessage());
}

And link to Jira docs并链接到Jira 文档

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

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