From efa5ac9390b0111c38a370801e2e3b2979891aae Mon Sep 17 00:00:00 2001 From: kairox Date: Thu, 19 Nov 2026 18:44:18 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=99=A4=E5=A4=96=E3=83=A9=E3=83=99?= =?UTF-8?q?=E3=83=AB=E3=81=AE=E4=BB=98=E3=81=84=E3=81=9F=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E3=82=92=E3=80=81=E9=99=A4=E5=8E=BB=E3=81=A7=E3=81=AF=E3=81=AA?= =?UTF-8?q?=E3=81=8F=E3=83=A1=E3=83=B3=E3=82=B7=E3=83=A7=E3=83=B3=E3=81=AE?= =?UTF-8?q?=E5=AF=BE=E8=B1=A1=E5=A4=96=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit EXCLUDE_LABELS のラベルが付いた通知を送信対象から外すのをやめ、 通常の通知として送ったうえでメンションの対象から外す。 投稿の分割でもメンション対象以外の側に入れる。 ラベルを調べるのは mention 系 reason の通知に限る。 Co-Authored-By: Claude Fable 5.2 --- README.md | 22 +++++----- spec/github/usecase_spec.cr | 79 +++++++++++++++++++++++++++--------- spec/notify/usecase_spec.cr | 80 ------------------------------------- src/github/models.cr | 6 +-- src/github/repository.cr | 3 +- src/github/usecase.cr | 49 ++++++++++++++--------- src/main.cr | 2 +- src/notify/models.cr | 3 ++ src/notify/usecase.cr | 19 +-------- 9 files changed, 112 insertions(+), 151 deletions(-) diff --git a/README.md b/README.md index bcb0587..73c8c0d 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ WEBHOOK_URL: <通知用webhook> ALERT_WEBHOOK_URL: <アラート用webhook> GITHUB_TOKEN: NOTIFY_MODE: -EXCLUDE_LABELS: <通知対象から外すラベル名(カンマ区切り。省略時は除外しない)> +EXCLUDE_LABELS: <メンションの対象から外すラベル名(カンマ区切り。省略時は外さない)> ENV: <環境名> ``` @@ -32,24 +32,24 @@ Slack では `@channel`(``)、Discord では `@everyone` を使う レビューできる状態になっていない PR でチャンネル全体を叩かないため。 通知そのものは従来どおり届く。 -## ラベルによる除外 +## ラベルによるメンションの除外 -`EXCLUDE_LABELS` に指定したラベルが 1 つでも付いている PR / Issue の通知は送らない。 -自動マージされる PR のように、人が確認しなくてよい通知を流さないための設定。 +`EXCLUDE_LABELS` に指定したラベルが 1 つでも付いている PR / Issue の通知は、メンションの対象にしない。 +自動マージされる PR のように、人が確認しなくてよい通知でチャンネル全体を叩かないための設定。 +通知そのものは届く。 ``` EXCLUDE_LABELS: automerge,dependencies ``` ラベル名はカンマ区切りで複数指定でき、大文字小文字は区別しない(GitHub のラベル名と同じ扱い)。 -省略した場合は除外しない。 +省略した場合はラベルを見ない。 -除外した通知も GitHub 上では既読にする。 -未読のまま残すと、毎分の実行で取得され続けるため。 +該当した通知は、投稿の分割でもメンション対象以外の側に入る。 -通知の payload にラベルは含まれないので、指定した場合は PR / Issue の通知 1 件につき -API 呼び出しが 1 回増える。 -ラベルを取得できなかった通知は除外せずに送る。 +通知の payload にラベルは含まれないので、指定した場合は mention 系 reason の PR / Issue の通知 +1 件につき API 呼び出しが 1 回増える。 +ラベルを取得できなかった通知は、メンションの対象から外さない。 ## 投稿の分割 @@ -60,5 +60,5 @@ API 呼び出しが 1 回増える。 分けるのは投稿の区切りだけで、並べ替えはしない。 通知は更新時刻の昇順のまま届き、メンション対象が無ければ投稿は 1 つになる。 -区切りの判定には reason だけを使う。 CI の状態でメンションが抑止された PR も、メンション対象の投稿に入る。 +`EXCLUDE_LABELS` のラベルが付いた通知は、メンション対象以外の投稿に入る。 diff --git a/spec/github/usecase_spec.cr b/spec/github/usecase_spec.cr index 9d3fe9f..7d2bd72 100644 --- a/spec/github/usecase_spec.cr +++ b/spec/github/usecase_spec.cr @@ -67,15 +67,28 @@ private def build(notify, comment) Github::Usecase.new(StubRepo.new(comment)).build_message(notify) end -# あらかじめ用意したラベルを返すリポジトリ。 +# あらかじめ用意したラベルを返し、ラベルとチェック状態の取得回数を記録するリポジトリ。 private class LabelRepo < Github::NotificationRepository + getter labels_calls = 0 + getter checks_calls = 0 + def initialize(@labels : Set(String)) super("token") end + def find_comment_by_url(url : String) : Github::Comment + Github::Comment.new "body" + end + def find_labels(notify : Github::Notification) : Set(String) + @labels_calls += 1 @labels end + + def find_checks_state(notify : Github::Notification) : Github::ChecksState + @checks_calls += 1 + Github::ChecksState::Success + end end describe Github::Usecase do @@ -94,23 +107,6 @@ describe Github::Usecase do end end - describe "#excluded?" do - it "除外ラベルが付いていれば true を返す" do - usecase = Github::Usecase.new LabelRepo.new(Set{"automerge", "bug"}), Set{"automerge"} - usecase.excluded?(notification).should be_true - end - - it "除外ラベルが付いていなければ false を返す" do - usecase = Github::Usecase.new LabelRepo.new(Set{"bug"}), Set{"automerge"} - usecase.excluded?(notification).should be_false - end - - it "除外ラベルが未設定なら false を返す" do - usecase = Github::Usecase.new LabelRepo.new(Set{"automerge"}) - usecase.excluded?(notification).should be_false - end - end - describe "#build_message" do it "reflects the reason in the pretext" do build(notification(reason: "review_requested"), comment).pretext.should eq "[Issue] レビューを依頼されました" @@ -237,7 +233,7 @@ describe Github::Usecase do end # 投稿を分ける基準(issue #120)。メンション自体は CI の状態で抑止されるが、 - # 抑止されても通知の重要度は下がらないため、判定は reason だけで行う。 + # 抑止されても通知の重要度は下がらないため、CI の状態は判定に使わない。 describe "#build_message important" do it "marks a mention reason as important" do build(notification(reason: "review_requested"), comment).important?.should be_true @@ -255,6 +251,51 @@ describe Github::Usecase do message.important?.should be_true end end + + # 除外ラベル(EXCLUDE_LABELS)の付いた通知は、送るがメンションの対象にしない。 + describe "#build_message exclude labels" do + it "除外ラベルの付いた通知はメンションせず、通常の通知として扱う" do + repo = LabelRepo.new Set{"automerge", "bug"} + message = Github::Usecase.new(repo, Set{"automerge"}).build_message(notification(reason: "review_requested")) + + message.mention?.should be_false + message.important?.should be_false + end + + it "除外ラベルが付いていなければメンションする" do + repo = LabelRepo.new Set{"bug"} + message = Github::Usecase.new(repo, Set{"automerge"}).build_message(notification(reason: "review_requested")) + + message.mention?.should be_true + message.important?.should be_true + end + + it "除外ラベルの付いた PR ではチェック状態を取得しない" do + repo = LabelRepo.new Set{"automerge"} + notify = notification( + url: "https://api.github.com/repos/octocat/Hello-World/pulls/42", + type: "PullRequest", + ) + Github::Usecase.new(repo, Set{"automerge"}).build_message(notify).mention?.should be_false + + repo.checks_calls.should eq 0 + end + + it "除外ラベルが未設定ならラベルを取得しない" do + repo = LabelRepo.new Set{"automerge"} + message = Github::Usecase.new(repo).build_message(notification(reason: "review_requested")) + + message.mention?.should be_true + repo.labels_calls.should eq 0 + end + + it "メンションしない reason ではラベルを取得しない" do + repo = LabelRepo.new Set{"automerge"} + Github::Usecase.new(repo, Set{"automerge"}).build_message(notification(reason: "subscribed")) + + repo.labels_calls.should eq 0 + end + end end private def pull_request(reason = "review_requested") diff --git a/spec/notify/usecase_spec.cr b/spec/notify/usecase_spec.cr index 12d01a5..5efebe4 100644 --- a/spec/notify/usecase_spec.cr +++ b/spec/notify/usecase_spec.cr @@ -24,9 +24,6 @@ end private class FakeNotificationRepo < Github::NotificationRepository getter read_calls = [] of Time? getter before_arg : Time? - # 通知の title をキーに、付いているラベルを指定する。 - property labels = {} of String => Set(String) - getter labels_calls = 0 # raise_read_after 回目の既読化呼び出しで例外を投げ、既読化失敗を再現する。 def initialize(@notifications : Array(Github::Notification), @raise_read_after : Int32? = nil) @@ -42,11 +39,6 @@ private class FakeNotificationRepo < Github::NotificationRepository Github::Comment.new "body" end - def find_labels(notify : Github::Notification) : Set(String) - @labels_calls += 1 - @labels[notify.subject.title]? || Set(String).new - end - def notification_to_read(last_read_at : Time? = nil) @read_calls << last_read_at if (limit = @raise_read_after) && @read_calls.size >= limit @@ -97,15 +89,6 @@ private def run(notifications, poster, &) repo end -# automerge ラベルを除外対象にし、labels で指定した title の通知にそのラベルを付けて実行する。 -private def run_excluding(notifications, poster, labeled : Array(String), &) - repo = FakeNotificationRepo.new notifications - labeled.each { |title| repo.labels[title] = Set{"automerge"} } - usecase = Notify::Usecase.new repo, Github::Usecase.new(repo, Set{"automerge"}), poster - yield usecase - repo -end - describe Notify::Usecase do describe "#check_notifications" do # last_read_at は排他的境界(updated_at < last_read_at のみ既読化、issue #100) @@ -226,69 +209,6 @@ describe Notify::Usecase do repo.read_calls.should eq [t(2)] end - # 除外ラベル(EXCLUDE_LABELS)の付いた通知は送らないが、既読化はする。 - - it "除外ラベルの付いた通知は送らない" do - notifications = [ - notif("2026-01-01T00:00:01Z", title: "a"), - notif("2026-01-01T00:00:02Z", title: "b"), - notif("2026-01-01T00:00:03Z", title: "c"), - ] - poster = RecordingPoster.new - run_excluding(notifications, poster, labeled: ["b"], &.check_notifications) - - poster.posts.flat_map(&.map(&.title)).should eq ["a", "c"] - end - - it "除外した通知も送信対象と一緒に既読化する" do - notifications = [ - notif("2026-01-01T00:00:01Z", title: "a"), - notif("2026-01-01T00:00:02Z", title: "b"), - ] - repo = run_excluding(notifications, RecordingPoster.new, labeled: ["b"]) do |usecase| - usecase.check_notifications - end - - # 送信対象は a だけ。全件送信済みなので境界は取得スナップショットまで進み、 - # 除外した b(t(2))も既読化される。 - repo.read_calls.should eq [repo.before_arg] - end - - it "未送信の通知より後に更新された除外通知は既読化しない" do - notifications = [ - notif("2026-01-01T00:00:01Z", title: "a"), - notif("2026-01-01T00:00:02Z", reason: "mention", title: "b"), - notif("2026-01-01T00:00:03Z", title: "c"), - ] - repo = run_excluding(notifications, RecordingPoster.new(fail_at: 1), labeled: ["c"]) do |usecase| - expect_raises(Exception, "send failed") { usecase.check_notifications } - end - - # b の送信に失敗。境界は b の t(2) で、それより後の c は未読に残り、 - # 次回もう一度ラベルを判定される。 - repo.read_calls.should eq [t(2)] - end - - it "全件が除外されたら送信せずに既読化する" do - notifications = [notif("2026-01-01T00:00:01Z", title: "a")] - poster = RecordingPoster.new - repo = run_excluding(notifications, poster, labeled: ["a"]) do |usecase| - usecase.check_notifications - end - - poster.posts.should be_empty - repo.read_calls.should eq [repo.before_arg] - end - - it "除外ラベルが未設定ならラベルを取得しない" do - notifications = [notif("2026-01-01T00:00:01Z")] - repo = run(notifications, RecordingPoster.new) do |usecase| - usecase.check_notifications - end - - repo.labels_calls.should eq 0 - end - it "既読化に失敗したら後続チャンクの送信を止める" do notifications = [notif("2026-01-01T00:00:01Z"), notif("2026-01-01T00:00:02Z"), notif("2026-01-01T00:00:03Z")] poster = ChunkPoster.new([1, 1, 1]) diff --git a/src/github/models.cr b/src/github/models.cr index 85db3cc..155c518 100644 --- a/src/github/models.cr +++ b/src/github/models.cr @@ -224,8 +224,8 @@ module Github type.in?(UPDATE_TYPES) end - # ラベルを持つ type か。ラベルで通知を除外できるのはこの type に限る - # (Github::Usecase#excluded?)。 + # ラベルを持つ type か。ラベルでメンションの対象から外せるのはこの type に限る + # (Github::Usecase#addressed?)。 def labeled? : Bool type.in?(LABELED_TYPES) end @@ -337,7 +337,7 @@ module Github end end - # ラベルによる除外の判定に使う subject 本体(PR / Issue)。labels だけ参照する。 + # ラベルによるメンション除外の判定に使う subject 本体(PR / Issue)。labels だけ参照する。 class Labeled include JSON::Serializable diff --git a/src/github/repository.cr b/src/github/repository.cr index 2a93cff..59939d5 100644 --- a/src/github/repository.cr +++ b/src/github/repository.cr @@ -160,7 +160,8 @@ module Github # あるとコメント側になりラベルを持たないので、それとは別に取得する。 # # ラベルを持たない type と取得できなかった場合は空集合を返す。空集合なら - # 呼び出し側は除外しないので、取得の失敗で通知が消えることはない。 + # 呼び出し側はメンションの対象から外さないので、取得の失敗でメンションが + # 消えることはない。 def find_labels(notify : Notification) : Set(String) empty = Set(String).new return empty unless notify.subject.labeled? diff --git a/src/github/usecase.cr b/src/github/usecase.cr index 4dc718b..4c7f373 100644 --- a/src/github/usecase.cr +++ b/src/github/usecase.cr @@ -8,7 +8,7 @@ module Github # 防ぐため組み立て側(Slack / Discord 共通)で切り詰める(issue #96)。 BODY_LIMIT = 500 - # exclude_labels は通知対象から外すラベル名(parse_labels で正規化した集合)。 + # exclude_labels はメンションの対象から外すラベル名(parse_labels で正規化した集合)。 def initialize(@repo : NotificationRepository, @exclude_labels : Set(String) = Set(String).new) end @@ -21,23 +21,14 @@ module Github raw.split(',').map(&.strip.downcase).reject(&.empty?).to_set end - # 除外ラベルが付いていて、通知対象から外す通知か。 - # - # 除外ラベルが未設定ならラベルを取得せずに false を返す。設定しない限り - # API 呼び出しは増えない。 - def excluded?(notify : Notification) : Bool - return false if @exclude_labels.empty? - - @repo.find_labels(notify).intersects?(@exclude_labels) - end - def build_message(notify : Notification) : Notify::Message comment = @repo.find_comment_by_url notify.subject.comment_url + addressed = addressed?(notify) Notify::Message.new( - mention: mention?(notify), - # 投稿の区切りは reason だけで決め、CI によるメンション抑止は反映しない + mention: addressed && checks_allow_mention?(notify), + # 投稿の区切りには、CI によるメンション抑止を反映しない # (issue #120。理由は Notify::Message#important? のコメント)。 - important: notify.mention?, + important: addressed, author_name: comment.poster.login, author_icon: comment.poster.avatar_url, author_link: comment.poster.html_url, @@ -53,13 +44,33 @@ module Github ) end - # メンション(`@channel` / `@everyone`)を付けるか。 + # 自分宛てとして扱う通知か。メンションと投稿の区切りの両方の前提になる。 + # + # mention 系 reason であっても、除外ラベル(EXCLUDE_LABELS)の付いた PR / Issue は + # 自分宛てとして扱わない。自動マージされる PR のように人が確認しなくてよい通知で + # チャンネル全体を叩かないためである。通知そのものは通常の通知として届く。 # - # mention 系 reason であることに加え、PR は CI などの自動チェックが失敗中でも - # 実行中でもないことを条件にする。まだレビューできる状態ではない PR でチャンネル全体を - # 叩かないため(issue #105)。通知そのものは抑止しない。 - private def mention?(notify : Notification) : Bool + # CI によるメンション抑止(checks_allow_mention?)と違い、こちらは投稿の区切りにも + # 反映する。CI が赤い PR は確認が要る通知のままだが、除外ラベルの付いた通知は + # 確認が要らないので、`@channel` の付く投稿に混ぜない。 + # + # ラベルを調べるのは mention 系 reason の通知に限る。それ以外はもともと + # メンションしないので、ラベルを取得しても結果が変わらない。除外ラベルが + # 未設定のときも取得しない。 + private def addressed?(notify : Notification) : Bool return false unless notify.mention? + return true if @exclude_labels.empty? + + !@repo.find_labels(notify).intersects?(@exclude_labels) + end + + # CI などの自動チェックの状態から見て、メンション(`@channel` / `@everyone`)を + # 付けてよいか。 + # + # PR は自動チェックが失敗中でも実行中でもないことを条件にする。まだレビューできる + # 状態ではない PR でチャンネル全体を叩かないため(issue #105)。通知そのものは + # 抑止しない。 + private def checks_allow_mention?(notify : Notification) : Bool return true unless notify.checks_gated? !@repo.find_checks_state(notify).blocks_mention? diff --git a/src/main.cr b/src/main.cr index 0459298..e8007c9 100644 --- a/src/main.cr +++ b/src/main.cr @@ -13,7 +13,7 @@ WEBHOOK_URL = ENV["WEBHOOK_URL"] ALERT_WEBHOOK_URL = ENV["ALERT_WEBHOOK_URL"] NOTIFY_MODE = ENV["NOTIFY_MODE"]? || "slack" APP_ENV = ENV["ENV"] -# 通知対象から外すラベル名(カンマ区切り)。未設定なら何も除外しない。 +# メンションの対象から外すラベル名(カンマ区切り)。未設定なら何も外さない。 EXCLUDE_LABELS = Github::Usecase.parse_labels ENV["EXCLUDE_LABELS"]? # NOTIFY_MODE に応じて送信先アダプタを生成する。 diff --git a/src/notify/models.cr b/src/notify/models.cr index 9d21fa8..cc1b7d1 100644 --- a/src/notify/models.cr +++ b/src/notify/models.cr @@ -10,6 +10,9 @@ module Notify # ための抑止(issue #105)が掛かった後の値で、抑止されても通知の重要度自体は # 下がらない。両者を同じフラグにすると、CI が赤いレビュー依頼が通常の通知に # 混ざってしまう。 + # + # 除外ラベル(EXCLUDE_LABELS)の付いた通知は、mention 系 reason でも false になる + # (Github::Usecase#addressed?)。 getter? important : Bool getter author_name : String? getter author_icon : String? diff --git a/src/notify/usecase.cr b/src/notify/usecase.cr index 11c1220..2e2952b 100644 --- a/src/notify/usecase.cr +++ b/src/notify/usecase.cr @@ -28,29 +28,14 @@ module Notify return {msg: "ok"} if notifications.empty? - # 除外ラベル(EXCLUDE_LABELS)の付いた通知は送信対象から外す。 - # - # 外した通知も既読化はする。未読のまま残すと、毎分取得されてそのたびに - # ラベルを調べ直すことになる。既読化の境界は送信対象だけで決めてよい。 - # 境界より前に更新された通知は外したものも含めて既読化され、境界以降の - # ものは未読に残って次回もう一度判定されるだけで、送信はされない。 - targets = notifications.reject { |item| @github_uc.excluded? item } - - # 全件を外した場合は送信が起きず、送信後の既読化も呼ばれない。 - # 取得した集合をここで既読化して、次回の再取得を止める。 - if targets.empty? - @github_repo.notification_to_read fetched_at - return {msg: "ok"} - end - # pretext は reason(なぜ通知されたか)を反映した文言になる(issue #96)。 - notices = targets.map { |item| @github_uc.build_message item } + notices = notifications.map { |item| @github_uc.build_message item } # チャンク送信が成功するたび、そこまでに送信済みの通知だけを既読化する。 # 途中で失敗しても送信済み分は既読化済みなので、未送信分だけが次回 # 再取得され、前半チャンクの重複投稿が起きない。 send_split notices do |sent_count| - mark_read_through targets, sent_count, fetched_at + mark_read_through notifications, sent_count, fetched_at end {msg: "ok"}