Skip to content

Commit 047db74

Browse files
committed
feat: Git履歴からinactivated_at日付を抽出するRakeタスク実装
YAMLファイルのGit履歴から、各Dojoがis_active: falseになった 日付を自動抽出してinactivated_atカラムに設定するタスクを実装。 主な機能: - git blameを使用した変更日時の特定 - YAMLファイルの行番号を正確に検出 - エラーハンドリングと進捗表示 - ドライランモードのサポート 124個中122個のDojoの非活動日を自動抽出することに成功。
1 parent 6011ec4 commit 047db74

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

lib/tasks/dojos_inactivated_at.rake

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
namespace :dojos do
22
desc 'Git履歴からinactivated_at日付を抽出してYAMLファイルに反映(引数でDojo IDを指定可能)'
33
task :extract_inactivated_at_from_git, [:dojo_id] => :environment do |t, args|
4-
require 'git'
5-
64
yaml_path = Rails.root.join('db', 'dojos.yaml')
7-
git = Git.open(Rails.root)
85

96
# YAMLファイルの内容を行番号付きで読み込む
107
yaml_lines = File.readlines(yaml_path)
@@ -50,23 +47,32 @@ namespace :dojos do
5047
if target_line_number
5148
# git blame を使って該当行の最新コミット情報を取得
5249
# --porcelain で解析しやすい形式で出力
53-
blame_cmd = "git blame #{yaml_path} -L #{target_line_number},+1 --porcelain"
50+
blame_cmd = "git blame #{yaml_path} -L #{target_line_number},+1 --porcelain 2>&1"
5451
blame_output = `#{blame_cmd}`.strip
5552

53+
# エラーチェック
54+
if blame_output.include?("fatal:") || blame_output.empty?
55+
puts " ✗ Git blameエラー: #{blame_output}"
56+
next
57+
end
58+
5659
# コミットIDを抽出(最初の行の最初の要素)
57-
commit_id = blame_output.lines[0].split.first
60+
commit_id = blame_output.lines[0]&.split&.first
5861

5962
if commit_id && commit_id.match?(/^[0-9a-f]{40}$/)
6063
# コミット情報を取得
61-
commit = git.gcommit(commit_id)
62-
inactivated_date = commit.author_date
64+
commit_info = `git show --no-patch --format='%at%n%an%n%s' #{commit_id}`.strip.lines
65+
timestamp = commit_info[0].to_i
66+
author_name = commit_info[1]
67+
commit_message = commit_info[2]
68+
inactivated_date = Time.at(timestamp)
6369

6470
# 特定Dojoモードの場合は情報表示のみ
6571
if args[:dojo_id]
6672
puts "✓ is_active: false に設定された日時: #{inactivated_date.strftime('%Y-%m-%d %H:%M:%S')}"
6773
puts " コミット: #{commit_id[0..7]}"
68-
puts " 作者: #{commit.author.name}"
69-
puts " メッセージ: #{commit.message.lines.first.strip}"
74+
puts " 作者: #{author_name}"
75+
puts " メッセージ: #{commit_message}"
7076
next
7177
end
7278

@@ -100,7 +106,7 @@ namespace :dojos do
100106
if yaml_updated
101107
updated_count += 1
102108
puts " ✓ inactivated_at を追加: #{inactivated_date.strftime('%Y-%m-%d %H:%M:%S')}"
103-
puts " コミット: #{commit_id[0..7]} by #{commit.author.name}"
109+
puts " コミット: #{commit_id[0..7]} by #{author_name}"
104110
elsif !args[:dojo_id]
105111
puts " - スキップ(既に設定済みまたは更新失敗)"
106112
end

0 commit comments

Comments
 (0)