Skip to content

Commit

Permalink
fixed #323 - if set $untranslatedName that find transition id from pr…
Browse files Browse the repository at this point in the history
…oject status.
  • Loading branch information
lesstif committed May 13, 2020
1 parent 855f4ce commit e5357be
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ includes:
- phpstan-baseline.neon

parameters:
level: 0
level: 1
paths:
- src
excludes_analyse:
Expand Down
44 changes: 42 additions & 2 deletions src/Issue/IssueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace JiraRestApi\Issue;

use JiraRestApi\JiraException;
use JiraRestApi\Project\ProjectService;

class IssueService extends \JiraRestApi\JiraClient
{
Expand Down Expand Up @@ -480,7 +481,7 @@ public function findTransitonId($issueIdOrKey, $transitionToName)

$this->log->debug('getTransitions result='.var_export($ret, true));

if (strcmp($toName, $transitionToName) == 0) {
if (strcasecmp($toName, $transitionToName) === 0) {
return $trans->id;
}
}
Expand All @@ -504,7 +505,13 @@ public function transition($issueIdOrKey, $transition)
$this->log->debug('transition='.var_export($transition, true));

if (!isset($transition->transition['id'])) {
$transition->transition['id'] = $this->findTransitonId($issueIdOrKey, $transition->transition['name']);
if (isset($transition->transition['untranslatedName'])) {
$transition->transition['id'] = $this->findTransitonIdByUntranslatedName($issueIdOrKey, $transition->transition['untranslatedName']);
} elseif (isset($transition->transition['untranslatedName'])) {
$transition->transition['id'] = $this->findTransitonId($issueIdOrKey, $transition->transition['name']);
} else {
throw new JiraException("you must set either name or untranslatedName for performing transition.");
}
}

$data = json_encode($transition);
Expand Down Expand Up @@ -1204,4 +1211,37 @@ public function updateFixVersions($issueIdOrKey, $addFixVersionsParam, $removeFi

return $ret;
}

/**
* find transition id by transition's untranslatedName.
*
* @param string|int $issueIdOrKey
* @param string $untranslatedName
*
* @throws JiraException
*
* @return string
*/
public function findTransitonIdByUntranslatedName($issueIdOrKey, $untranslatedName)
{
$this->log->debug('findTransitonIdByUntranslatedName=');

$prj = new ProjectService();
$pkey = explode('-', $issueIdOrKey);
$transitionArray = $prj->getProjectTransitionsToArray($pkey[0]);

$this->log->debug('getTransitions result='.var_export($transitionArray, true));

foreach ($transitionArray as $trans) {

if (strcasecmp($trans['name'], $untranslatedName) === 0 ||
strcasecmp($trans['untranslatedName'] ?? '', $untranslatedName) === 0) {

return $trans['id'];
}
}

// transition keyword not found
throw new JiraException("Transition name '$untranslatedName' not found on JIRA Server.");
}
}
32 changes: 32 additions & 0 deletions src/Project/ProjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,38 @@ public function getStatuses($projectIdOrKey)
return $results;
}

/**
* make transition info array for project issue transition.
*
* @param $projectIdOrKey
* @return array
* @throws JiraException
*/
public function getProjectTransitionsToArray($projectIdOrKey)
{
$ret = $this->exec($this->uri."/$projectIdOrKey/statuses", null);
$json = json_decode($ret);
$results = array_map(function ($elem) {
return $this->json_mapper->map($elem, new IssueType());
}, $json);

$transitions = [];
foreach ($results as $issueType) {
foreach ($issueType->statuses as $status) {

if (! in_array($status->id, array_column($transitions, 'id'))) {
$transitions[] = [
'id' => $status->id,
'name' => $status->name,
'untranslatedName' => $status->untranslatedName ?? $status->name,
];
}
}
}

return $transitions;
}

/**
* @throws \JiraRestApi\JiraException
*
Expand Down

0 comments on commit e5357be

Please sign in to comment.