0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Python初心者向け:初めてのLinear Issue CSV解析 - Issue数を数えてみよう

Posted at

Linearユーザーの皆さん、LinearはCSV形式で設定メニューからIssueをエクスポートできます。この記事では、プログラミング初心者の方向けに、Pythonを使って手元の環境でLinearからエクスポートしたCSVファイルのIssue数を簡単に数える方法を解説します。

 実際には、一覧に表示されるので、数える必要は実際にはありませんが、今後CSVを解析する第一歩としてやってみましょう。

処理の概要

このスクリプトでは、引数から渡されたCSVファイルが存在するかを確認し、存在しなければ終了します。
そしてテキストとしてCSVファイルを読み込み各行を辞書形式に変換します。
最後にArchived(削除)されたものと、そうでないもの(Active)なIssue数を数えます。

Python Code

"""
License MIT
https://github.com/akjava/
"""

import os

import argparse
import csv


def parse_arguments():
    """引数を解析します。

    Returns:
    """
    parser = argparse.ArgumentParser(description="create rotated data")
    parser.add_argument("--input", "-i", default="example.csv", help="Input Linear CSV")
    return parser.parse_args()


def read_csv_with_newlines(filepath):
    """
    改行を含むCSVファイルを読み込む関数

    Parameters:
        filepath (str): CSVファイルのパス

    Returns:
        list: 読み込んだCSVデータをリストのリストで返す
    """
    data = []
    with open(filepath, "r", encoding="utf-8") as file:
        reader = csv.reader(file)
        for row in reader:
            data.append(row)
    return data


def issue_text_to_dict(head, csv_lines):
    """
    issue-csv-textを辞書に変換

    Parameters:
        head (str):header 通常csvの最初の行
        csv_lines:csvテキストのリスト

    Returns:
        list: 読み込んだissue-CSVデータを辞書のリストで返す
    """
    result = []
    for line in csv_lines:
        dict = {}
        for i, key in enumerate(head):
            if i < len(line):
                dict[key] = line[i]
        result.append(dict)
    return result


if __name__ == "__main__":
    args = parse_arguments()
    csv_path = args.input
    if not os.path.exists(csv_path):
        print(f"csv not exist {csv_path}")
        exit(0)

    csv_issue_texts = read_csv_with_newlines(csv_path)
    dicts = issue_text_to_dict(csv_issue_texts[0], csv_issue_texts[1:])
    archived = 0
    for dic in dicts:
        if dic["Archived"]:
            archived += 1
    print(f"total {len(dicts)} archived {archived} active {len(dicts)-archived}")

このPythonコードでは、csvモジュールを使ってCSVファイルを読み込んでいます。CSVの1行目は、"ID","Team","Title","Description","Status"...といったヘッダーになっており、それをキーとして各行を辞書形式に変換します。
念のため、データの長さをチェックしています。
Archivedとは、LinearでIssueが削除された時に一時的に設定される状態を表し、残りがActive Issueです。

実行結果

このコードは、コマンドプロンプトで実行できます。実行すると、以下のように、アーカイブされたIssueとアクティブなIssueの数が表示されます。

PS C:\pythons\test> python .\count_issue.py -i "Export Wed Jan 22 2025.csv"
total 104 archived 90 active 14

この結果から、現在のプロジェクトにおけるIssueの数を把握できます。

まとめ

LinearのCSVに含まれる項目は以下の通り豊富で。issueごとに、かかった作業時間をガントチャートにしたり、次のCycleの予定を予測したりもできそうだ。

"ID","Team","Title","Description","Status","Estimate","Priority","Project ID","Project","Creator","Assignee","Labels","Cycle Number","Cycle Name","Cycle Start","Cycle End","Created","Updated","Started","Triaged","Completed","Canceled","Archived","Due Date","Parent issue","Initiatives","Project Milestone ID","Project Milestone","SLA Status","Roadmaps"

 ぜひ皆さんも、いろいろ試してみてください。
私も以下レポジトリーで時間があれば、更新したいと思います。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?