From e5357be131cdaccaf9189c3a2fb61a3e54b033d3 Mon Sep 17 00:00:00 2001 From: KwangSeob Jeong Date: Wed, 13 May 2020 23:42:09 +0900 Subject: [PATCH] fixed #323 - if set $untranslatedName that find transition id from project status. --- phpstan.neon.dist | 2 +- src/Issue/IssueService.php | 44 ++++++++++++++++++++++++++++++++-- src/Project/ProjectService.php | 32 +++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 4b5fed7c..b219d5bb 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,7 +2,7 @@ includes: - phpstan-baseline.neon parameters: - level: 0 + level: 1 paths: - src excludes_analyse: diff --git a/src/Issue/IssueService.php b/src/Issue/IssueService.php index 7d4f6ef1..12a7b49a 100644 --- a/src/Issue/IssueService.php +++ b/src/Issue/IssueService.php @@ -3,6 +3,7 @@ namespace JiraRestApi\Issue; use JiraRestApi\JiraException; +use JiraRestApi\Project\ProjectService; class IssueService extends \JiraRestApi\JiraClient { @@ -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; } } @@ -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); @@ -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."); + } } diff --git a/src/Project/ProjectService.php b/src/Project/ProjectService.php index 1cff7895..bd59f759 100644 --- a/src/Project/ProjectService.php +++ b/src/Project/ProjectService.php @@ -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 *