Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ WEBHOOK_URL: <通知用webhook>
ALERT_WEBHOOK_URL: <アラート用webhook>
GITHUB_TOKEN: <token>
NOTIFY_MODE: <slack | discord(省略時 slack)>
EXCLUDE_LABELS: <通知対象から外すラベル名(カンマ区切り。省略時は除外しない)>
EXCLUDE_LABELS: <メンションの対象から外すラベル名(カンマ区切り。省略時は外さない)>
ENV: <環境名>
```

Expand All @@ -32,24 +32,24 @@ Slack では `@channel`(`<!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 回増える。
ラベルを取得できなかった通知は、メンションの対象から外さない。

## 投稿の分割

Expand All @@ -60,5 +60,5 @@ API 呼び出しが 1 回増える。
分けるのは投稿の区切りだけで、並べ替えはしない。
通知は更新時刻の昇順のまま届き、メンション対象が無ければ投稿は 1 つになる。

区切りの判定には reason だけを使う。
CI の状態でメンションが抑止された PR も、メンション対象の投稿に入る。
`EXCLUDE_LABELS` のラベルが付いた通知は、メンション対象以外の投稿に入る。
79 changes: 60 additions & 19 deletions spec/github/usecase_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] レビューを依頼されました"
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down
80 changes: 0 additions & 80 deletions spec/notify/usecase_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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])
Expand Down
6 changes: 3 additions & 3 deletions src/github/models.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -337,7 +337,7 @@ module Github
end
end

# ラベルによる除外の判定に使う subject 本体(PR / Issue)。labels だけ参照する。
# ラベルによるメンション除外の判定に使う subject 本体(PR / Issue)。labels だけ参照する。
class Labeled
include JSON::Serializable

Expand Down
3 changes: 2 additions & 1 deletion src/github/repository.cr
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ module Github
# あるとコメント側になりラベルを持たないので、それとは別に取得する。
#
# ラベルを持たない type と取得できなかった場合は空集合を返す。空集合なら
# 呼び出し側は除外しないので、取得の失敗で通知が消えることはない。
# 呼び出し側はメンションの対象から外さないので、取得の失敗でメンションが
# 消えることはない。
def find_labels(notify : Notification) : Set(String)
empty = Set(String).new
return empty unless notify.subject.labeled?
Expand Down
49 changes: 30 additions & 19 deletions src/github/usecase.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand All @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion src/main.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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 に応じて送信先アダプタを生成する。
Expand Down
3 changes: 3 additions & 0 deletions src/notify/models.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
Loading
Loading