1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

全てのRedmineチケット情報を取得し、CSV出力するスクリプト

Last updated at Posted at 2023-03-21

Redminの全てのチケット情報からトラッカー、ステータス名でフィルタリングしてCSV出力するスクリプト

import csv
import requests

# RedmineのAPIキー、サーバURL、プロジェクトIDを設定
api_key = 'your_redmine_api_key'
redmine_url = 'https://your_redmine_server_url'
project_id = 'your_project_id'

# フィルタリングするトラッカーとステータス名
tracker_name_filter = 'Bug'
status_name_filter = 'New'

# REST APIを使って全チケット情報を取得する関数
def get_all_tickets(api_key, redmine_url, project_id):
    headers = {'X-Redmine-API-Key': api_key}
    url = f"{redmine_url}/issues.json?project_id={project_id}&limit=100"
    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        return response.json()
    else:
        print(f"Error: {response.status_code}")
        return None

# チケット情報をフィルタリングする関数
def filter_tickets(tickets, tracker_name_filter, status_name_filter):
    filtered_tickets = []
    for ticket in tickets['issues']:
        if ticket['tracker']['name'] == tracker_name_filter and ticket['status']['name'] == status_name_filter:
            filtered_tickets.append(ticket)
    return {'issues': filtered_tickets}

# チケット情報をCSVファイルに出力する関数
def export_tickets_to_csv(tickets, filename):
    with open(filename, 'w', newline='', encoding='utf-8') as csvfile:
        fieldnames = ['id', 'subject', 'tracker', 'status', 'priority', 'assignee', 'updated_on']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

        writer.writeheader()
        for ticket in tickets['issues']:
            writer.writerow({
                'id': ticket['id'],
                'subject': ticket['subject'],
                'tracker': ticket['tracker']['name'],
                'status': ticket['status']['name'],
                'priority': ticket['priority']['name'],
                'assignee': ticket['assigned_to']['name'] if 'assigned_to' in ticket else '',
                'updated_on': ticket['updated_on']
            })

def main():
    tickets = get_all_tickets(api_key, redmine_url, project_id)
    if tickets:
        filtered_tickets = filter_tickets(tickets, tracker_name_filter, status_name_filter)
        export_tickets_to_csv(filtered_tickets, 'filtered_tickets.csv')
        print("フィルタリングされたチケット情報がCSVファイルに出力されました。")

if __name__ == '__main__':
    main()

シェルスクリプトの場合(変数定義は別ファイルとした)

config.txt
API_KEY="your_redmine_api_key"
REDMINE_URL="https://your_redmine_server_url"
PROJECT_ID="your_project_id"
TRACKER_NAME_FILTER="Bug"
STATUS_NAME_FILTER="New"
#!/bin/bash

# 変数定義を含むファイルを読み込む
source config.txt

# REST APIを使って全チケット情報を取得
tickets=$(curl -s -H "X-Redmine-API-Key: $API_KEY" "${REDMINE_URL}/issues.json?project_id=${PROJECT_ID}&limit=100")

# チケット情報をフィルタリングし、CSVファイルに出力
echo "id,subject,tracker,status,priority,assignee,updated_on" > filtered_tickets.csv
echo "$tickets" | jq -r --arg tracker_filter "$TRACKER_NAME_FILTER" --arg status_filter "$STATUS_NAME_FILTER" '.issues[] | select(.tracker.name == $tracker_filter and .status.name == $status_filter) | [.id, .subject, .tracker.name, .status.name, .priority.name, (.assigned_to.name // ""), .updated_on] | @csv' >> filtered_tickets.csv

echo "フィルタリングされたチケット情報がCSVファイルに出力されました。"

100件以上ある場合

#!/bin/bash

API_KEY="your_api_key"
REDMINE_URL="https://your_redmine_url"
OUTPUT_FILE="tickets.csv"

# CSVヘッダの出力
echo "id,project,subject,status,assigned_to,created_on,updated_on" > $OUTPUT_FILE

# 各プロジェクトのチケット情報を取得する
project_ids=$(curl -s -H "X-Redmine-API-Key: $API_KEY" "$REDMINE_URL/projects.json?limit=100" | jq '.projects[].id')
for project_id in $project_ids; do
  offset=0
  while true; do
    tickets=$(curl -s -H "X-Redmine-API-Key: $API_KEY" "$REDMINE_URL/issues.json?project_id=$project_id&limit=100&offset=$offset")
    ticket_count=$(echo $tickets | jq '.issues | length')

    # チケット情報をCSV形式で出力する
    for ((i = 0; i < ticket_count; i++)); do
      ticket_info=$(echo $tickets | jq ".issues[$i] | {id: .id, project: .project.name, subject: .subject, status: .status.name, assigned_to: (.assigned_to | .name // \"\"), created_on: .created_on, updated_on: .updated_on}")
      id=$(echo $ticket_info | jq -r '.id')
      project=$(echo $ticket_info | jq -r '.project')
      subject=$(echo $ticket_info | jq -r '.subject')
      status=$(echo $ticket_info | jq -r '.status')
      assigned_to=$(echo $ticket_info | jq -r '.assigned_to')
      created_on=$(echo $ticket_info | jq -r '.created_on')
      updated_on=$(echo $ticket_info | jq -r '.updated_on')
      echo "$id,\"$project\",\"$subject\",\"$status\",\"$assigned_to\",\"$created_on\",\"$updated_on\"" >> $OUTPUT_FILE
    done

    if [[ $ticket_count -lt 100 ]]; then
      break
    fi
    offset=$((offset + 100))
  done
done
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?