如何获取Jira上每个项目的发布版本?

时间:2016-05-11 13:32:45

标签: php rest symfony jira-rest-api

我正在使用Symfony 2.8(PHP),我希望通过API Rest Jira获取Jira上每个项目的所有版本,然后过滤主题以便仅选择已发布的版本。

我发现了这种方法,但我不知道如何使用'expand'参数来达到版本

Image 1

Image 2

1 个答案:

答案 0 :(得分:0)

第1步:在config.yml

有关Guzzle配置的更多信息:http://docs.guzzlephp.org/en/latest/quickstart.html

csa_guzzle:
    clients:
        jira:
            config: 
                base_uri: "https://jira.*****.*****.***/rest/api/2/"
                timeout: 20.0
                headers:
                    Accept: "application/json"
                    Content-Type: "application/json"
                verify: false
                auth: ['api','password','Basic']

第二步:创建一个GuzzleHttp客户端服务,用于向api发送请求

<?php

namespace AppBundle\Service\Atlassian\Jira\Client;

use GuzzleHttp\Client as GuzzleClientHttp;
use GuzzleHttp\Exception\ServerException;
use Psr\Http\Message\ResponseInterface;


class GuzzleClient
{
    /**
     * Guzzle Client.
     *
     * @var GuzzleClientHttp
     */
    protected $guzzle;

    /**
     * Response object of request.
     *
     * @var ResponseInterface
     */
    protected $response;

    /**
     * GuzzleClient constructor.
     *
     * @param GuzzleClientHttp $guzzle
     */
    public function __construct(GuzzleClientHttp $guzzle)
    {
        $this->guzzle = $guzzle ?: new GuzzleClientHttp();
    }


    public function send($method = 'GET', $url, $parameters = [])
    {
        try {
            $this->response = $this->guzzle->request($method, $url, ['query' => $parameters]);
        } catch (ServerException $exception) {
            $this->response = $exception->getResponse();
        }

        return $this->getContents();
    }

    /**
     * Return the contents as a string of last request.
     *
     * @return string
     */
    public function getContents()
    {
        return $this->response->getBody()->getContents();
    }


    /**
     * Getter for GuzzleClient.
     *
     * @return GuzzleClientHttp
     */
    public function getClient()
    {
        return $this->guzzle;
    }

    /**
     * Getter for last Response.
     *
     * @return ResponseInterface
     */
    public function getResponse()
    {
        return $this->response;
}

第3步:为获得所有版本提供服务

<?php

namespace AppBundle\Service\Atlassian\Jira;

use AppBundle\Service\Atlassian\Jira\Client\GuzzleClient;

class ApiService
{

    /**
     * Client HTTP.
     *
     * @var GuzzleClient
     */
    protected $client;

    /**
     * ApiService constructor.
     *
     * @param GuzzleClient $client
     */
    public function __construct(GuzzleClient $client)
    {
        $this->client = $client;
    }

    /**
     * Get all released versions for a given projectKey.
     *
     * @param string $projectKey
     * @return null|array
     */
    public function getVersions($projectKey)
    {
        $versions = json_decode($this->client->send('GET', 'project/'.$projectKey."/versions/"));
        for($i=0;$i< count($versions); $i++)
        {
            if($versions[$i]->released== false)
            {
                $result = $versions[$i]->name;
            }
        }

        return $versions;
    }

}