Redmine Api

2024-09-15 工作雜記 redmine

以下是 redmine API 筆記

在使用 API 之前記得先取得 token,token 會在 [我的帳戶] - [API 存取金鑰] 中,每個帳戶都會有一組。

取得 Token 之後就可以進行操作了

在這邊我會使用兩個參數方便接下來的操作

API_KRY=your_token
REDMINE_URL="redmine 網址"

新增專案

首先我們來新增專案,這邊我們紀錄一下 identifier 這個欄位資訊: 「new1」,等等新增 ISSUE 會使用到

curl -k -X POST -H "Content-Type: application/json" \
     -H "X-Redmine-API-Key: ${API_KRY}" \
     -d '{
           "project": {
             "name": "New Project",
             "identifier": "new1",
             "description": "This is a new project",
             "parent_id": 15, 
             "is_public": false,
             "inherit_members": true,
             "enabled_module_names": ["issue_tracking","time_tracking","calendar","gantt"],
             "tracker_ids": [38,39,42],
             "issue_custom_field_ids": [21]
           }
         }' \
     "${REDMINE_URL}/projects.json"

特別說明這邊有用到下列欄位

  • parent_id: 父專案
  • is_public: 公開
  • enabled_module_names: 模組
  • tracker_ids: 追蹤標籤清單
  • issue_custom_field_ids: 自訂欄位清單

實際操作如下

❯ curl -k -X POST -H "Content-Type: application/json" -H "X-Redmine-API-Key: ${API_KRY}" -d '{"project": {"name": "New Project","identifier": "new1","description": "This is a new project","parent_id": 15, "is_public": false,"inherit_members": true,"enabled_module_names": ["issue_tracking","time_tracking","calendar","gantt"],"tracker_ids": [38,39,42],"issue_custom_field_ids": [21]}}' "${REDMINE_URL}/projects.json"
{"project":{"id":33,"name":"New Project","identifier":"new1","description":"This is a new project","homepage":"","parent":{"id":15,"name":"05_外部系統管理"},"status":1,"is_public":false,"inherit_members":true,"created_on":"2024-09-15T14:45:47Z","updated_on":"2024-09-15T14:45:47Z"}}%   

查詢專案

curl -k -H "X-Redmine-API-Key: ${API_KRY}" "${REDMINE_URL}/projects.json"

回傳結果

{
  "projects": [
    {
      "id": 5,
      "name": "01_日常管理",
      "identifier": "mis",
      "description": "MIS日常固定業務",
      "homepage": "",
      "status": 1,
      "is_public": false,
      "inherit_members": false,
      "created_on": "2024-09-12T15:11:20Z",
      "updated_on": "2024-09-12T15:14:39Z"
    }
  ],
  "total_count": 1,
  "offset": 0,
  "limit": 25
}

刪除專案

curl -k -X DELETE -H "X-Redmine-API-Key: ${API_KRY}" "${REDMINE_URL}/projects/new1.json"

新增 ISSUE

新增完專案之後接下來就可以新增 ISSUE。還記得剛剛紀錄的 identifier 欄位資訊: 「new1」,這個範例會將新的 ISSUE 新增到 new1 這個專案

curl -k -X POST -H "Content-Type: application/json" \
     -H "X-Redmine-API-Key: ${API_KRY}" \
     -d '{
           "issue": {
             "project_id": "new1",
             "subject": "資料庫 datafile 擴充", 
             "description": "Oracle datafile 快滿了,需要進行擴充",
             "tracker_id": 38, 
             "status_id": 1, 
             "priority_id": 4, 
             "assigned_to_id": 5,
             "start_date": "2024-09-16",
             "custom_fields": [
               {
                 "id": 13,
                 "value": "2024-09-15"
               }
             ]
           }
         }' \
     "${REDMINE_URL}issues.json"

特別說明這邊有用到下列欄位

  • custom_fields: 自訂欄位

實際操作如下

❯ curl -k -X POST -H "Content-Type: application/json" \
     -H "X-Redmine-API-Key: ${API_KRY}" \
     -d '{
           "issue": {
             "project_id": "new1",
             "subject": "資料庫 datafile 擴充", 
             "description": "Oracle datafile 快滿了,需要進行擴充",
             "tracker_id": 38, 
             "status_id": 1, 
             "priority_id": 4, 
             "assigned_to_id": 5,
             "start_date": "2024-09-16",
             "custom_fields": [
               {
                 "id": 13,
                 "value": "2024-09-15"
               }
             ]
           }
         }' \
     "${REDMINE_URL}issues.json"
{"issue":{"id":12,"project":{"id":16,"name":"This is a new project"},"tracker":{"id":38,"name":"服務-資料庫"},"status":{"id":1,"name":"New","is_closed":false},"priority":{"id":4,"name":"Urgent(緊迫的)"},"author":{"id":5,"name":"PolloChang"},"assigned_to":{"id":5,"name":"PolloChang"},"subject":"資料庫 datafile 擴充","description":"Oracle datafile 快滿了,需要進行擴充","start_date":"2024-09-16","due_date":null,"done_ratio":0,"is_private":false,"estimated_hours":null,"total_estimated_hours":null,"custom_fields":[{"id":13,"name":"希望完成日期","value":"2024-09-15"}],"created_on":"2024-09-15T15:15:06Z","updated_on":"2024-09-15T15:15:06Z","closed_on":null}}

API 欄位這麼多要怎麼知道? 答案是瀏覽器 F12

在 redmine 中談慶非常大,可以自訂很多不同的欄位,而官網的API文件似乎沒有提到很詳細。其實使用瀏覽器 F12 開啟管理員工具,在檢視元素中就可以取得相關欄位的資料。

參考資料