threadrules

package
v0.8.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package threadrules holds the user's rules for their GitHub notifications, the threads GitHub keeps for each pull request and issue: whether the app reads them, whether it clears one on its own once the work behind it is finished, whether clearing marks it done or read, and which moments count as finished. Decide applies the rules to one thread and one such moment.

Settings writes the rules, under the keys declared here, and what reads and clears the threads obeys them. It is a leaf: it imports only the item kinds and the preference encoding, never the store, the GitHub client, or a service, so settings can write the rules without depending on what clears the threads.

Index

Constants

View Source
const (
	// KeyRead holds whether the app reads the user's GitHub notifications: "true" or
	// "false", on when unset.
	KeyRead = "threadrules.read"

	// KeyAuto holds whether the app clears a notification on its own once the work behind
	// it is finished: "true" or "false", off when unset, so nothing on GitHub changes
	// until the user turns it on.
	KeyAuto = "threadrules.auto"

	// KeyAction holds what clearing a notification does: "done" or "read", done when
	// unset or anything else.
	KeyAction = "threadrules.action"

	// KeyTriggersOff lists, as a JSON array, the triggers the user turned off. A trigger
	// is on unless listed, so one added later starts on.
	KeyTriggersOff = "threadrules.triggersOff"

	// KeyWatching holds whether a notification from a repository the user only watches
	// is cleared when GitHub says its item is finished: "true" or "false", on when unset.
	KeyWatching = "threadrules.watching"

	// KeyBotActivity holds whether a bot's activity after the item was finished still
	// counts as seen: "true" or "false", on when unset.
	KeyBotActivity = "threadrules.botActivity"
)

The preferences the rules are stored under. Settings writes them.

View Source
const Grace = 2 * time.Minute

Grace is how long after the moment an item was finished a thread may still have been updated and count as seen: GitHub updates a thread for the merge or close itself, and for what follows it at once, such as a deleted branch.

View Source
const WatchingReason = "subscribed"

WatchingReason is GitHub's reason for a thread the user gets only because they watch its repository.

Variables

View Source
var (
	// ErrUnknownAction is returned for an Action that is not one of Actions.
	ErrUnknownAction = errors.New("unknown way to clear a notification")

	// ErrUnknownTrigger is returned for a Trigger that is not one of Triggers, nor
	// TriggerByHand.
	ErrUnknownTrigger = errors.New("unknown trigger")

	// ErrNoSwitch is returned for TriggerByHand, which is always on.
	ErrNoSwitch = errors.New("clearing by hand has no switch")

	// ErrUnknownSwitch is returned for a Switch that is not one of Switches.
	ErrUnknownSwitch = errors.New("unknown switch")
)

Functions

func EncodeAction

func EncodeAction(a Action) (string, error)

EncodeAction returns the stored form of a. An Action that is not one of Actions is refused with ErrUnknownAction.

func EncodeTriggersOff

func EncodeTriggersOff(off []Trigger) string

EncodeTriggersOff writes the triggers that are off as the value of KeyTriggersOff, in the order of Triggers, each once, leaving out any that is not one of Triggers.

func Keys

func Keys() []string

Keys returns every preference the rules are stored under.

Types

type Action

type Action string

Action is what clearing a notification does.

const (
	// ActionDone marks the notification done, which takes it out of the user's GitHub
	// inbox. It is the default.
	ActionDone Action = "done"

	// ActionRead marks the notification read, which leaves it in the inbox.
	ActionRead Action = "read"
)

The ways to clear a notification.

func Actions

func Actions() []Action

Actions returns the ways to clear a notification, the default first.

func DecodeAction

func DecodeAction(stored string) Action

DecodeAction reads a stored Action: as stored when it is one of Actions, else the default, ActionDone.

func (Action) Valid

func (a Action) Valid() bool

Valid reports whether a is one of Actions.

type AfterMoment

type AfterMoment string

AfterMoment is who acted on a thread after the moment its item was finished, from a later check of its activity.

It is a promise about all of that activity, not its latest: AfterBot means every activity after the moment is known to be a bot's. A caller that has ever seen a person's activity on the thread after the moment keeps AfterPerson until the thread is read, even when a bot acted last, so a bot's comment never hides a person's.

const (
	// AfterUnknown is no check made, or none that could tell.
	AfterUnknown AfterMoment = ""

	// AfterBot is bots alone: every activity after the moment is known to be a bot's.
	AfterBot AfterMoment = "bot"

	// AfterPerson is a person, at least once since the moment, whoever acted last. It
	// stays until the thread is read.
	AfterPerson AfterMoment = "person"
)

Who acted after the moment.

type Moment

type Moment struct {
	// Trigger is what finished it.
	Trigger Trigger

	// At is when it was finished: the user's click in the app, the review's time, or
	// GitHub's closedAt.
	At time.Time

	// Outside is whether the moment comes from GitHub's state (a merge or a close the
	// app found), rather than from something the user did in the app. The app's own
	// merges and closes are not Outside.
	Outside bool
}

Moment is when and how the work behind a thread was finished.

type Reason

type Reason string

Reason is why Decide skips or holds a thread.

const (
	// ReasonNotItem is a thread about neither a pull request nor an issue, which is never
	// touched, not even by hand.
	ReasonNotItem Reason = "not_item"

	// ReasonAlreadyRead is a thread already read while clearing marks one read.
	ReasonAlreadyRead Reason = "already_read"

	// ReasonOff is reading the notifications turned off, which leaves every thread alone,
	// even by hand, or clearing on its own turned off.
	ReasonOff Reason = "off"

	// ReasonRuleOff is a trigger turned off or unknown, one that can't finish the
	// thread's kind of item, or a thread from a repository the user only watches while
	// such threads are left alone.
	ReasonRuleOff Reason = "rule_off"

	// ReasonStillOpen is an item whose state doesn't bear the trigger out, one reopened
	// included.
	ReasonStillOpen Reason = "still_open"

	// ReasonNewActivity is an unread thread not known to be quiet since the moment: one
	// updated more than Grace after it, or whose update time isn't known, unless the
	// rules let a bot's activity pass.
	ReasonNewActivity Reason = "new_activity"
)

The reasons.

type Rules

type Rules struct {
	// Read is whether the app reads the user's GitHub notifications. On by default. Off,
	// nothing touches a notification, not even by hand.
	Read bool `json:"read"`

	// Auto is whether the app clears a notification on its own once the work behind it is
	// finished. Off by default: nothing on GitHub changes until the user turns it on.
	Auto bool `json:"auto"`

	// Action is what clearing a notification does, ActionDone by default. Decide reads
	// anything but ActionRead as ActionDone.
	Action Action `json:"action"`

	// TriggersOff are the triggers the user turned off, in the order of Triggers. Never
	// nil.
	TriggersOff []Trigger `json:"triggersOff"`

	// Watching is whether a notification from a repository the user only watches (GitHub's
	// reason WatchingReason) is cleared when GitHub says its item is finished. On by
	// default. What the user did in the app clears such a notification either way.
	Watching bool `json:"watching"`

	// BotActivity is whether a bot's activity after the item was finished still counts
	// as seen, so it doesn't hold the notification back. On by default.
	BotActivity bool `json:"botActivity"`
}

Rules are the user's rules for their GitHub notifications, as the settings show them.

func Decode

func Decode(stored map[string]string) Rules

Decode reads the rules from stored preferences, each written as the settings store it: a switch with prefval.EncodeBool, the action with EncodeAction, and the triggers that are off with EncodeTriggersOff. A value that is unset or can't be read keeps its default, an Action that is not one of Actions reads as ActionDone, and a trigger that is not one of Triggers is dropped.

func Defaults

func Defaults() Rules

Defaults returns the rules of a fresh database.

func (Rules) On

func (r Rules) On(trigger Trigger) bool

On reports whether trigger is on: TriggerByHand always, one of Triggers unless the user turned it off, and any other never.

type State

type State string

State is the state of a thread's pull request or issue, as GitHub last reported it.

const (
	// StateUnknown is a state not reported, or not known yet.
	StateUnknown State = ""

	// StateOpen is an open pull request or issue, one reopened included.
	StateOpen State = "OPEN"

	// StateClosed is a pull request closed without merging, or a closed issue.
	StateClosed State = "CLOSED"

	// StateMerged is a merged pull request.
	StateMerged State = "MERGED"
)

The states.

type Switch

type Switch string

Switch names one of the rules' switches, for the settings' one setter of them.

const (
	// SwitchRead is whether the app reads the user's GitHub notifications.
	SwitchRead Switch = "read"

	// SwitchAuto is whether the app clears a notification on its own.
	SwitchAuto Switch = "auto"

	// SwitchWatching is whether a notification from a repository the user only watches
	// is cleared when GitHub says its item is finished.
	SwitchWatching Switch = "watching"

	// SwitchBotActivity is whether a bot's activity after the item was finished still
	// counts as seen.
	SwitchBotActivity Switch = "botActivity"
)

The switches.

func Switches

func Switches() []Switch

Switches returns the switches, in the order the settings list them.

func (Switch) Key

func (s Switch) Key() string

Key returns the preference key s is stored under, or "" for a Switch that is not one of Switches.

func (Switch) Valid

func (s Switch) Valid() bool

Valid reports whether s is one of Switches.

type Thread

type Thread struct {
	// Kind is the kind of the thread's subject, or "" when it is neither a pull request
	// nor an issue.
	Kind item.Kind

	// Unread is whether the thread is unread.
	Unread bool

	// UpdatedAt is when GitHub last updated the thread. The zero time is not known, and
	// an unread thread that isn't known to be quiet since the moment isn't seen.
	UpdatedAt time.Time

	// Reason is GitHub's reason the user gets the thread, such as WatchingReason.
	Reason string

	// State is the state of the thread's subject, as GitHub last reported it.
	State State

	// AfterMoment is who acted on the thread after the moment, as far as a later check
	// could tell. AfterBot promises that every activity after the moment is a bot's: a
	// caller that ever saw a person's activity after the moment keeps AfterPerson until
	// the thread is read.
	AfterMoment AfterMoment
}

Thread is what Decide needs to know about one notification thread.

type Trigger

type Trigger string

Trigger is a moment the work behind a notification is finished.

const (
	// TriggerMerged is a pull request merged, in the app or anywhere else.
	TriggerMerged Trigger = "merged"

	// TriggerPRClosed is a pull request closed without merging, in the app or anywhere
	// else.
	TriggerPRClosed Trigger = "pr_closed"

	// TriggerIssueClosed is an issue closed, in the app or anywhere else.
	TriggerIssueClosed Trigger = "issue_closed"

	// TriggerReviewed is a review of the user's that approves a pull request or requests
	// changes, in the app or anywhere else (an agent's gh, a browser), once the inbox
	// sees it, at GitHub's time for it.
	TriggerReviewed Trigger = "reviewed"

	// TriggerDismissed is an item the user dismissed in the app.
	TriggerDismissed Trigger = "dismissed"

	// TriggerUnassigned is an item the user took themselves off in the app: unassigned
	// themselves, or removed themselves as a reviewer.
	TriggerUnassigned Trigger = "unassigned"

	// TriggerByHand is the user clearing a notification themselves, as Autopilot's Mark
	// the GitHub notification done does. It has no switch and no guard but the thread's own.
	TriggerByHand Trigger = "by_hand"
)

The triggers. Every one but TriggerByHand has a switch, and each is on until the user turns it off.

func DecodeTriggersOff

func DecodeTriggersOff(stored string) []Trigger

DecodeTriggersOff reads the triggers that are off from the stored value of KeyTriggersOff, in the order of Triggers, each once. A value that is unset or can't be read lists none, and a name that is not one of Triggers, TriggerByHand's included, is dropped.

func SetTrigger

func SetTrigger(off []Trigger, trigger Trigger, on bool) ([]Trigger, error)

SetTrigger turns trigger on or off in off, the triggers that are off, and returns the new list in the order of Triggers. TriggerByHand is refused with ErrNoSwitch, and any other trigger that is not one of Triggers with ErrUnknownTrigger.

func Triggers

func Triggers() []Trigger

Triggers returns the triggers that have a switch, in the order the settings list them. TriggerByHand is not among them.

func (Trigger) AppliesTo

func (t Trigger) AppliesTo(k item.Kind) bool

AppliesTo reports whether t can finish an item of kind k: a merge, a pull request's close, and a review only a pull request, an issue's close only an issue, and the rest either. No trigger applies to a kind that is neither, and an unknown trigger to none.

func (Trigger) HasSwitch

func (t Trigger) HasSwitch() bool

HasSwitch reports whether t is one of Triggers, the ones the user can turn off.

func (Trigger) MatchesState

func (t Trigger) MatchesState(s State) bool

MatchesState reports whether s, the state GitHub last reported for the item, bears t out: a merge needs MERGED, and a close CLOSED (a merged pull request is MERGED, not CLOSED); what the user did in the app needs no state. An unknown trigger matches none.

func (Trigger) Valid

func (t Trigger) Valid() bool

Valid reports whether t is one of Triggers or TriggerByHand.

type Verdict

type Verdict struct {
	// Kind is the verdict.
	Kind VerdictKind `json:"kind"`

	// Reason is why the thread is skipped or held, "" when it is cleared.
	Reason Reason `json:"reason"`

	// Trigger is the moment's trigger.
	Trigger Trigger `json:"trigger"`
}

Verdict is what Decide says to do with a thread, and why.

func Decide

func Decide(r Rules, t Thread, m Moment) Verdict

Decide says what to do with thread t once the work behind it was finished at moment m, under rules r. It reads no clock and allocates nothing. The first rule that applies wins:

  1. A thread about neither a pull request nor an issue is skipped, not even cleared by hand.
  2. While reading the notifications is off, the thread is skipped, even by hand.
  3. While clearing marks a thread read, one already read is skipped.
  4. Clearing by hand clears.
  5. While clearing on its own is off, the thread is skipped.
  6. A trigger turned off, one that can't finish the thread's kind of item, or a moment from GitHub's state on a thread from a repository the user only watches, while Rules.Watching is off, skips the thread.
  7. A state that doesn't bear the trigger out (Trigger.MatchesState) skips it.
  8. A thread not seen is held: one unread and updated more than Grace after the moment, or at a time not known, unless Rules.BotActivity lets a bot's activity pass (see AfterBot).
  9. Otherwise the thread is cleared.

type VerdictKind

type VerdictKind string

VerdictKind is what Decide says to do with a thread.

const (
	// VerdictClear clears the thread, as Rules.Action says.
	VerdictClear VerdictKind = "clear"

	// VerdictSkip leaves the thread alone, for its Reason.
	VerdictSkip VerdictKind = "skip"

	// VerdictHold leaves the thread alone for now: it changed after the moment, and the
	// user may not have seen what changed.
	VerdictHold VerdictKind = "hold"
)

The verdicts.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL