jira.class.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. <?php
  2. class jira
  3. {
  4. /**
  5. * Jira接口的域名。
  6. * Jira api domain.
  7. *
  8. * @var string
  9. * @access public
  10. */
  11. public $jiraDomain;
  12. /**
  13. * Jira接口的用户名。
  14. * Jira api account.
  15. *
  16. * @var string
  17. * @access public
  18. */
  19. public $jiraAccount;
  20. /**
  21. * Jira接口的Token。
  22. * Jira api token.
  23. *
  24. * @var string
  25. * @access public
  26. */
  27. public $jiraToken;
  28. /**
  29. * 构造方法。
  30. * The construct function.
  31. *
  32. * @param string $jiraDomain
  33. * @param string $jiraAccount
  34. * @param string $jiraToken
  35. * @access public
  36. * @return void
  37. */
  38. public function __construct($jiraDomain, $jiraAccount, $jiraToken)
  39. {
  40. $this->jiraDomain = $jiraDomain;
  41. $this->jiraAccount = $jiraAccount;
  42. $this->jiraToken = $jiraToken;
  43. }
  44. /**
  45. * 获取Jira中所有项目。
  46. *
  47. * @param string $url Jira地址
  48. * @param string $account Jira账号
  49. * @param string $password Jira密码
  50. * @return array
  51. */
  52. public function getProjects($startAt = 0)
  53. {
  54. if($startAt > 0) return array(); // 项目没有分页,第二次直接返回空数组
  55. $url = $this->jiraDomain . '/rest/api/3/project?expand=lead,description';
  56. $account = $this->jiraAccount;
  57. $password = $this->jiraToken;
  58. $authHeader = base64_encode($account . ':' . $password);
  59. $header = array('Authorization: Basic ' . $authHeader);
  60. $result = common::http($url, null, array(), $header, 'data', 'GET');
  61. $result = json_decode($result, true);
  62. if(!$result) return array();
  63. $projects = array();
  64. foreach($result as $project)
  65. {
  66. $project['versions'] = $this->getBuilds($project['id']);
  67. $project['lead'] = !empty($project['lead']['accountId']) ? $project['lead']['accountId'] : '';
  68. $projects[$project['id']] = $project;
  69. }
  70. return $projects;
  71. }
  72. /**
  73. * 获取Jira中所有的版本。
  74. *
  75. * @param int $startAt 开始位置
  76. * @param int $maxResults 最大返回数量
  77. * @return array
  78. */
  79. public function getBuilds($projectID = 0, $startAt = 0, $maxResults = 50)
  80. {
  81. $url = $this->jiraDomain . '/rest/api/3/project/' . $projectID . '/versions?startAt=' . $startAt . '&maxResults=' . $maxResults;
  82. $account = $this->jiraAccount;
  83. $password = $this->jiraToken;
  84. $authHeader = base64_encode($account . ':' . $password);
  85. $header = array('Authorization: Basic ' . $authHeader);
  86. $result = common::http($url, null, array(), $header, 'data', 'GET');
  87. $versions = json_decode($result, true);
  88. if(!$versions) return array();
  89. if(count($versions) == $maxResults) $versions = arrayUnion($versions, $this->getBuilds($startAt + $maxResults, $maxResults));
  90. return $versions;
  91. }
  92. /**
  93. * 获取Jira中所有的Issue链接。
  94. *
  95. * @param int $startAt 开始位置
  96. * @param int $maxResults 最大返回数量
  97. * @return array
  98. */
  99. public function getIssueLinks($startAt = 0, $maxResults = 50)
  100. {
  101. $url = $this->jiraDomain . '/rest/api/2/issueLink?startAt=' . $startAt . '&maxResults=' . $maxResults;
  102. $account = $this->jiraAccount;
  103. $password = $this->jiraToken;
  104. $authHeader = base64_encode($account . ':' . $password);
  105. $header = array('Authorization: Basic ' . $authHeader);
  106. $result = common::http($url, null, array(), $header, 'data', 'GET');
  107. $issueLinks = json_decode($result, true);
  108. if(!$issueLinks) return array();
  109. if(count($issueLinks) == $maxResults) $issueLinks = arrayUnion($issueLinks, $this->getIssueLinks($startAt + $maxResults, $maxResults));
  110. return $issueLinks;
  111. }
  112. /**
  113. * 获取Jira中所有的Issue类型。
  114. *
  115. * @return array
  116. */
  117. public function getIssueTypes()
  118. {
  119. $url = $this->jiraDomain . '/rest/api/2/issuetype/';
  120. $account = $this->jiraAccount;
  121. $password = $this->jiraToken;
  122. $authHeader = base64_encode($account . ':' . $password);
  123. $header = array('Authorization: Basic ' . $authHeader);
  124. $result = common::http($url, null, array(), $header, 'data', 'GET');
  125. $issueTypes = json_decode($result, true);
  126. if(!$issueTypes) return array();
  127. $typeList = array();
  128. foreach($issueTypes as $issueType) $typeList[$issueType['id']] = $issueType;
  129. return $typeList;
  130. }
  131. /**
  132. * 获取Jira中所有的Issue链接类型。
  133. *
  134. * @return array
  135. */
  136. public function getIssueLinkTypes()
  137. {
  138. $url = $this->jiraDomain . '/rest/api/2/issueLinkType';
  139. $account = $this->jiraAccount;
  140. $password = $this->jiraToken;
  141. $authHeader = base64_encode($account . ':' . $password);
  142. $header = array('Authorization: Basic ' . $authHeader);
  143. $result = common::http($url, null, array(), $header, 'data', 'GET');
  144. $linkTypes = json_decode($result, true);
  145. if(!$linkTypes) return array();
  146. $linkTypeList = array();
  147. foreach($linkTypes['issueLinkTypes'] as $linkType) $linkTypeList[$linkType['id']] = $linkType;
  148. return $linkTypeList;
  149. }
  150. /**
  151. * 获取Jira中的所有issue。
  152. *
  153. * @param int $lastID
  154. * @param int $maxResults
  155. * @return string
  156. */
  157. public function getIssues($lastID = 0, $maxResults = 5000)
  158. {
  159. if(!$lastID) unset($_SESSION['nextPageToken']);
  160. if(!empty($_SESSION['nextPageToken']) && $_SESSION['nextPageToken'] == 'isLast')
  161. {
  162. unset($_SESSION['nextPageToken']);
  163. return array();
  164. }
  165. $url = $this->jiraDomain . '/rest/api/3/search/jql';
  166. $account = $this->jiraAccount;
  167. $password = $this->jiraToken;
  168. $maxResults = $maxResults ?: 5000;
  169. $nextPageToken = !empty($_SESSION['nextPageToken']) ? $_SESSION['nextPageToken'] : '';
  170. $authHeader = base64_encode($account . ':' . $password);
  171. $header = array('Authorization: Basic ' . $authHeader);
  172. $jql = 'created<=' . date('Y-m-d', strtotime('+1 day'));
  173. $url .= '?jql=' . urlencode($jql) . "&fields=*all&maxResults=$maxResults&nextPageToken=$nextPageToken&expand=renderedFields,changelog";
  174. $result = common::http($url, null, array(), $header, 'data', 'GET');
  175. $result = json_decode($result, true);
  176. if(!$result || empty($result['issues'])) return array();
  177. $issueList = array();
  178. foreach($result['issues'] as $issue)
  179. {
  180. if(!empty($issue['fields']))
  181. {
  182. foreach($issue['fields'] as $field => $value) $issue[$field] = $value;
  183. }
  184. if(!empty($issue['priority']['id'])) $issue['priority'] = $issue['priority']['id'];
  185. if(!empty($issue['project']['id'])) $issue['project'] = $issue['project']['id'];
  186. if(!empty($issue['status']['id'])) $issue['status'] = $issue['status']['id'];
  187. if(!empty($issue['creator']['accountId'])) $issue['creator'] = $issue['creator']['accountId'];
  188. if(!empty($issue['issuetype']['id'])) $issue['type'] = $issue['issuetype']['id'];
  189. if(!empty($issue['assignee']['accountId'])) $issue['assignee'] = $issue['assignee']['accountId'];
  190. if(!empty($issue['resolution']['id'])) $issue['resolution'] = $issue['resolution']['id'];
  191. if(!empty($issue['renderedFields']['description'])) $issue['description'] = $issue['renderedFields']['description'];
  192. if(!empty($issue['changelog']['histories']))
  193. {
  194. $changeGroups = array();
  195. $changeItems = array();
  196. foreach($issue['changelog']['histories'] as $history)
  197. {
  198. $changeGroup = array();
  199. $changeGroup['id'] = $history['id'];
  200. $changeGroup['issue'] = $issue['id'];
  201. $changeGroup['author'] = $history['author']['accountId'];
  202. $changeGroup['created'] = date('Y-m-d H:i:s', strtotime($history['created']));
  203. $changeGroups[$changeGroup['id']] = $changeGroup;
  204. foreach($history['items'] as $index => $item)
  205. {
  206. $changeItem = array();
  207. $changeItem['id'] = $changeGroup['id'] . '_' . $index;
  208. $changeItem['group'] = $changeGroup['id'];
  209. $changeItem['fieldtype'] = $item['fieldtype'];
  210. $changeItem['field'] = $item['field'];
  211. $changeItem['oldvalue'] = $item['from'];
  212. $changeItem['oldstring'] = $item['fromString'];
  213. $changeItem['newvalue'] = $item['to'];
  214. $changeItem['newstring'] = $item['toString'];
  215. $changeItems[$changeItem['id']] = $changeItem;
  216. }
  217. }
  218. $issue['changeGroups'] = $changeGroups;
  219. $issue['changeItems'] = $changeItems;
  220. }
  221. if(!empty($issue['comment']['comments']))
  222. {
  223. $comments = array();
  224. foreach($issue['comment']['comments'] as $index => $comment)
  225. {
  226. $commentItem = array();
  227. $commentItem['id'] = $comment['id'];
  228. $commentItem['issue'] = $issue['id'];
  229. $commentItem['body'] = $issue['renderedFields']['comment']['comments'][$index]['body'];
  230. $commentItem['author'] = $comment['author']['accountId'];
  231. $commentItem['created'] = date('Y-m-d H:i:s', strtotime($comment['created']));
  232. $comments[$commentItem['id']] = $commentItem;
  233. }
  234. $issue['comments'] = $comments;
  235. }
  236. if(!empty($issue['worklog']['worklogs']))
  237. {
  238. $worklogs = array();
  239. foreach($issue['worklog']['worklogs'] as $index => $work)
  240. {
  241. $worklog = array();
  242. $worklog['id'] = $work['id'];
  243. $worklog['issue'] = $issue['id'];
  244. $worklog['body'] = !empty($issue['renderedFields']['worklog']['worklogs'][$index]['comment']) ? $issue['renderedFields']['worklog']['worklogs'][$index]['comment'] : '';
  245. $worklog['author'] = $work['author']['accountId'];
  246. $worklog['timeworked'] = $work['timeSpentSeconds'];
  247. $worklog['created'] = date('Y-m-d H:i:s', strtotime($work['created']));
  248. $worklogs[$worklog['id']] = $worklog;
  249. }
  250. $issue['worklogs'] = $worklogs;
  251. }
  252. if(!empty($issue['attachment']))
  253. {
  254. $files = array();
  255. foreach($issue['attachment'] as $attachment)
  256. {
  257. $file = array();
  258. $file['issue'] = $issue['id'];
  259. $file['id'] = $attachment['id'];
  260. $file['filename'] = $attachment['filename'];
  261. $file['mimetype'] = $attachment['mimeType'];
  262. $file['filesize'] = $attachment['size'];
  263. $file['author'] = $attachment['author']['accountId'];
  264. $file['created'] = date('Y-m-d H:i:s', strtotime($attachment['created']));
  265. $file['content'] = $attachment['content'];
  266. $files[$file['id']] = $file;
  267. }
  268. $issue['files'] = $files;
  269. }
  270. if(!empty($issue['issuelinks']))
  271. {
  272. $links = array();
  273. foreach($issue['issuelinks'] as $issueLink)
  274. {
  275. if(empty($issueLink['outwardIssue']['id'])) continue;
  276. $link = array();
  277. $link['id'] = $issueLink['id'];
  278. $link['linktype'] = $issueLink['type']['id'];
  279. $link['source'] = $issueLink['outwardIssue']['id'];
  280. $link['destination'] = $issue['id'];
  281. $links[$link['id']] = $link;
  282. }
  283. $issue['links'] = $links;
  284. }
  285. if(!empty($issue['subtasks']))
  286. {
  287. $links = !empty($issue['links']) ? $issue['links'] : array();
  288. foreach($issue['subtasks'] as $subtask)
  289. {
  290. $link = array();
  291. $link['id'] = 'subtask' . $subtask['id'];
  292. $link['linktype'] = 'jiraSubTask';
  293. $link['source'] = $issue['id'];
  294. $link['destination'] = $subtask['id'];
  295. $links[$link['id']] = $link;
  296. }
  297. $issue['links'] = $links;
  298. }
  299. $issue['created'] = date('Y-m-d H:i:s', strtotime($issue['created']));
  300. $issueList[$issue['id']] = $issue;
  301. }
  302. if(empty($result['isLast']) && !empty($result['nextPageToken'])) $_SESSION['nextPageToken'] = $result['nextPageToken'];
  303. if(!empty($result['isLast'])) $_SESSION['nextPageToken'] = 'isLast';
  304. return $issueList;
  305. }
  306. /**
  307. * 获取Jira中的用户。
  308. *
  309. * @param int $startAt 开始位置
  310. * @param int $maxResults 最大返回数量
  311. * @return string
  312. */
  313. public function getUsers($startAt = 0, $maxResults = 50)
  314. {
  315. $url = $this->jiraDomain . '/rest/api/2/user/search?query=.&startAt=' . $startAt . '&maxResults=' . $maxResults;
  316. $account = $this->jiraAccount;
  317. $password = $this->jiraToken;
  318. $authHeader = base64_encode($account . ':' . $password);
  319. $header = array('Authorization: Basic ' . $authHeader);
  320. $result = common::http($url, null, array(), $header, 'data', 'GET');
  321. $result = json_decode($result, true);
  322. $users = array();
  323. foreach($result as $index => $user)
  324. {
  325. if($user['accountType'] != 'atlassian') continue;
  326. $user['id'] = $index;
  327. $user['lowerUserName'] = $user['accountId'];
  328. $user['lowerDisplayName'] = $user['displayName'];
  329. $user['createdDate'] = '';
  330. $users[$index] = $user;
  331. }
  332. if(count($result) == $maxResults) $users = arrayUnion($users, $this->getUsers($startAt + $maxResults, $maxResults));
  333. return $users;
  334. }
  335. /**
  336. * 从jira接口下载文件内容。
  337. * Download jira File.
  338. *
  339. * @param string $fileURL
  340. * @access public
  341. * @return void
  342. */
  343. public function downloadFile($fileURL)
  344. {
  345. $account = $this->jiraAccount;
  346. $password = $this->jiraToken;
  347. $authHeader = base64_encode($account . ':' . $password);
  348. $header = array('Authorization: Basic ' . $authHeader);
  349. return common::http($fileURL, null, array(), $header, 'data', 'GET');
  350. }
  351. /**
  352. * 获取Jira中指定项目的Board。
  353. *
  354. * @param int $projectID 项目ID
  355. * @return string
  356. */
  357. public function getBoardId($projectID)
  358. {
  359. $url = $this->jiraDomain . '/rest/agile/1.0/board?projectKeyOrId=' . $projectID;
  360. $account = $this->jiraAccount;
  361. $password = $this->jiraToken;
  362. $authHeader = base64_encode($account . ':' . $password);
  363. $header = array('Authorization: Basic ' . $authHeader);
  364. $result = common::http($url, null, array(), $header, 'data', 'GET');
  365. return json_decode($result, true);
  366. }
  367. /**
  368. * 获取Jira中的解决方式。
  369. *
  370. * @return array
  371. */
  372. public function getResolutions()
  373. {
  374. $url = $this->jiraDomain . '/rest/api/2/resolution';
  375. $account = $this->jiraAccount;
  376. $password = $this->jiraToken;
  377. $authHeader = base64_encode($account . ':' . $password);
  378. $header = array('Authorization: Basic ' . $authHeader);
  379. $result = common::http($url, null, array(), $header, 'data', 'GET');
  380. return json_decode($result, true);
  381. }
  382. /**
  383. * 获取Jira中所有的优先级。
  384. *
  385. * @return array
  386. */
  387. public function getPriority()
  388. {
  389. $url = $this->jiraDomain . '/rest/api/2/priority';
  390. $account = $this->jiraAccount;
  391. $password = $this->jiraToken;
  392. $authHeader = base64_encode($account . ':' . $password);
  393. $header = array('Authorization: Basic ' . $authHeader);
  394. $result = common::http($url, null, array(), $header, 'data', 'GET');
  395. return json_decode($result, true);
  396. }
  397. /**
  398. * 获取Jira中所有的状态。
  399. *
  400. * @return array
  401. */
  402. public function getStatus()
  403. {
  404. $url = $this->jiraDomain . '/rest/api/2/status';
  405. $account = $this->jiraAccount;
  406. $password = $this->jiraToken;
  407. $authHeader = base64_encode($account . ':' . $password);
  408. $header = array('Authorization: Basic ' . $authHeader);
  409. $result = common::http($url, null, array(), $header, 'data', 'GET');
  410. $result = json_decode($result, true);
  411. if(!$result) return array();
  412. $statusList = array();
  413. foreach($result as $status)
  414. {
  415. $status['name'] = $status['untranslatedName'];
  416. $statusList[$status['id']] = $status;
  417. }
  418. return $statusList;
  419. }
  420. /**
  421. * 获取Jira中的自定义字段。
  422. *
  423. * @return array
  424. */
  425. public function getCustomFields()
  426. {
  427. $url = $this->jiraDomain . '/rest/api/3/issue/createmeta?expand=projects.issuetypes.fields';
  428. $account = $this->jiraAccount;
  429. $password = $this->jiraToken;
  430. $authHeader = base64_encode($account . ':' . $password);
  431. $header = array('Authorization: Basic ' . $authHeader);
  432. $result = common::http($url, null, array(), $header, 'data', 'GET');
  433. $result = json_decode($result, true);
  434. if(!$result['projects']) return array();
  435. $customFields = array();
  436. foreach($result['projects'] as $project)
  437. {
  438. if(empty($project['issuetypes'])) continue;
  439. foreach($project['issuetypes'] as $issuetype)
  440. {
  441. foreach($issuetype['fields'] as $key => $field)
  442. {
  443. if(strpos($key, 'customfield_') === false) continue;
  444. $customField = array();
  445. $customField['id'] = str_replace('customfield_', '', $key);
  446. $customField['name'] = $field['name'];
  447. $customField['description'] = '';
  448. $customField['customfieldtypekey'] = $field['schema']['custom'];
  449. $customField['customfieldsearcherkey'] = '';
  450. $customField['issueTypeIds'] = '';
  451. $customFields[$issuetype['id']][$customField['id']] = $customField;
  452. }
  453. }
  454. }
  455. return $customFields;
  456. }
  457. /**
  458. * 获取Jira中的自定义字段选项。
  459. *
  460. * @return array
  461. */
  462. public function getCustomFieldOption()
  463. {
  464. $url = $this->jiraDomain . '/rest/api/3/issue/createmeta?expand=projects.issuetypes.fields';
  465. $account = $this->jiraAccount;
  466. $password = $this->jiraToken;
  467. $authHeader = base64_encode($account . ':' . $password);
  468. $header = array('Authorization: Basic ' . $authHeader);
  469. $result = common::http($url, null, array(), $header, 'data', 'GET');
  470. $result = json_decode($result, true);
  471. if(!$result['projects']) return array();
  472. $customFieldOptions = array();
  473. foreach($result['projects'] as $project)
  474. {
  475. if(empty($project['issuetypes'])) continue;
  476. foreach($project['issuetypes'] as $issuetype)
  477. {
  478. foreach($issuetype['fields'] as $key => $field)
  479. {
  480. if(strpos($key, 'customfield_') === false || empty($field['allowedValues'])) continue;
  481. foreach($field['allowedValues'] as $allowedValue)
  482. {
  483. $customFieldOption = array();
  484. $customFieldOption['id'] = str_replace('customfield_', '', $key);
  485. $customFieldOption['customfield'] = str_replace('customfield_', '', $key);
  486. $customFieldOption['customfieldconfig'] = $field['schema']['custom'];
  487. $customFieldOption['value'] = $allowedValue['value'];
  488. $customFieldOption['disabled'] = false;
  489. $customFieldOptions[$allowedValue['id']] = $customFieldOption;
  490. }
  491. }
  492. }
  493. }
  494. return $customFieldOptions;
  495. }
  496. }