Skip to content

Commit

Permalink
fixed #143 setting issue security id
Browse files Browse the repository at this point in the history
  • Loading branch information
lesstif committed Mar 29, 2018
1 parent b4c4d60 commit 56c3671
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,10 @@ try {
->setIssueType("Bug")
->setDescription("Full description for issue")
->addVersion(["1.0.1", "1.0.3"])
->addComponents(['Component-1', 'Component-2']);
->addComponents(['Component-1', 'Component-2'])
// set issue security if you need.
->setSecurity(10001 /* security scheme id */)
;

$issueService = new IssueService();

Expand Down
19 changes: 19 additions & 0 deletions src/Issue/IssueField.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ class IssueField implements \JsonSerializable
/** @var array|null */
public $customFields;

/** @var SecurityScheme */
public $security;

public function __construct($updateIssue = false)
{
if ($updateIssue != true) {
Expand Down Expand Up @@ -382,4 +385,20 @@ public function addComponents($component)

return $this;
}

/**
* set security level
*
* @param $securityId
* @return $this
*/
public function setSecurity($securityId)
{
if ($this->security == null) {
$this->security = new SecurityScheme();
}
$this->security->id = $securityId;

return $this;
}
}
47 changes: 47 additions & 0 deletions src/Issue/IssueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -875,4 +875,51 @@ public function createOrUpdateRemoteIssueLink($issueIdOrKey, RemoteIssueLink $ri

return $res;
}

/**
* get all issue security schemes
*
* @throws JiraException
* @throws \JsonMapper_Exception
*
* @return SecurityScheme[] array of SecurityScheme class
*/
public function getAllIssueSecuritySchemes()
{
$url = '/issuesecurityschemes';

$ret = $this->exec($url);

$data = json_decode($ret, true);

// extract schem field
$schemes = json_decode(json_encode($data['issueSecuritySchemes']), false);

$res = $this->json_mapper->mapArray(
$schemes, new \ArrayObject(), '\JiraRestApi\Issue\SecurityScheme'
);

return $res;
}

/**
* get issue security scheme
*
* @param int $securityId security scheme id
* @return SecurityScheme SecurityScheme
* @throws JiraException
* @throws \JsonMapper_Exception
*/
public function getIssueSecuritySchemes($securityId)
{
$url = '/issuesecurityschemes/' . $id;

$ret = $this->exec($url);

$res = $this->json_mapper->map(
json_decode($ret), new SecurityScheme()
);

return $res;
}
}
28 changes: 28 additions & 0 deletions src/Issue/SecurityScheme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
namespace JiraRestApi\Issue;

class SecurityScheme implements \JsonSerializable
{
/** @var string */
public $self;

/** @var integer */
public $id;

/** @var string */
public $name;

/** @var string */
public $description;

/** @var integer */
public $defaultSecurityLevelId;

/** @var array security level */
public $levels;

public function jsonSerialize()
{
return array_filter(get_object_vars($this));
}
}
94 changes: 94 additions & 0 deletions tests/IssueSecuritySechemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

use JiraRestApi\Dumper;
use JiraRestApi\Issue\Comment;
use JiraRestApi\Issue\Issue;
use JiraRestApi\Issue\IssueField;
use JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\Transition;
use JiraRestApi\JiraException;

class IssueSecuritySechemTest extends PHPUnit_Framework_TestCase
{
public function testGetAllSecuritySchemes()
{
try {
$issueService = new IssueService();

$securitySchemes = $issueService->getAllIssueSecuritySchemes();

$this->assertGreaterThan(1, count($securitySchemes), 'security scheme must greater than 1');
$this->assertEquals(true, array_key_exists('id', $securitySchemes[0]), 'security id not found');

return $securitySchemes;
} catch (HTTPException $e) {
$this->assertTrue(false, $e->getMessage());
}
}

/**
* @depends testGetAllSecuritySchemes
*
* @param $securitySchem
* @throws Exception
* @throws JiraException
* @throws JsonMapper_Exception
*/
public function testGetSecurityScheme($securitySchemes)
{
$securityId = 0;

try {
$issueService = new IssueService();

foreach ($securitySchemes as $s) {
$ss = $issueService->getIssueSecuritySchemes($s->id);

$this->assertObjectHasAttribute('id', $ss, 'security id not found');
$this->assertObjectHasAttribute('levels', $ss, 'security level not found');

if ($securityId === 0)
$securityId = $ss->id;
}

return $securityId;
} catch (HTTPException $e) {
$this->assertTrue(false, $e->getMessage());
}
}

/**
* @depends testGetSecurityScheme
*
* @param $schemeId
* @return mixed
* @throws Exception
* @throws JsonMapper_Exception
*/
public function testCreateIssueWithSecurityScheme($securityId)
{
try {
$issueField = new IssueField();

$issueField->setProjectKey('TEST')
->setSummary("issue security level test")
->setAssigneeName('lesstif')
->setPriorityName('Critical')
->setIssueType('Bug')
->setDescription('Full description for issue')
->addVersion(['1.0.1', '1.0.3'])
->addComponents(['Component-1', 'Component-2'])
->setSecurity($securityId)
;

$issueService = new IssueService();

$ret = $issueService->create($issueField);

$this->assertInstanceOf(Issue::class, $ret);

} catch (JiraException $e) {
$this->assertTrue(false, 'Create Failed : '.$e->getMessage());
}
}
}

0 comments on commit 56c3671

Please sign in to comment.