internal

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TurnsFileSuffix = "_turns.json"
	SlackIDNotFound = " email not found"
)
View Source
const (
	URLs     = "PR URLs"
	Channels = "Slack channel IDs"
)
View Source
const (
	IndexByEmail       = 1
	IndexByBitbucketID = 2
	IndexByGitHubID    = 3
	IndexBySlackID     = 4
	IndexByRealName    = 5
)
View Source
const (
	BuildsFileSuffix = "_builds.json"
)
View Source
const (
	DiffstatFileSuffix = "_diffstat.json"
)
View Source
const (
	PRSnapshotFileSuffix = "_snapshot.json"
)

Variables

View Source
var (
	PullRequestURLPattern = regexp.MustCompile(`https://[^/]+/[^/]+/[^/]+/pull(-requests)?/\d+`)
	SlackChannelIDPattern = regexp.MustCompile(`^C[A-Z0-9]+`)
)

Functions

func AppendToCSVFile

func AppendToCSVFile(_ context.Context, record []string) error

func DelURLAndIDMapping

func DelURLAndIDMapping(_ context.Context, key string) error

func DeleteGenericPRFile

func DeleteGenericPRFile(_ context.Context, prURLWithSuffix string) error

DeleteGenericPRFile deletes a file related to a specific PR. Unlike os.Remove, this is idempotent: it does not return an error if the file does not exist.

func DeleteReminder

func DeleteReminder(_ context.Context, userID string) error

func FindPRsByCommit

func FindPRsByCommit(ctx context.Context, hash string) (prs []map[string]any, err error)

FindPRsByCommit returns all (0 or more) the PR snapshots that are currently associated with the given commit hash. This is used when processing Bitbucket commit events, to identify the relevant PR(s). To do this, this function scans through all the PR snapshots and checks their current commit hashes. Lastly, this function prunes redundant PR details, to reduce Temporal log size and noise.

func FreezeTurns

func FreezeTurns(ctx context.Context, opts client.Options, prURL, email string) (bool, error)

FreezeTurns marks the attention state of a specific PR as frozen by a specific user. This prevents most changes by SwitchTurn, and only by it, until it is unfrozen. If the turn is already frozen, this function returns false and does nothing.

func GetActivityTime

func GetActivityTime(ctx context.Context, opts client.Options, prURL, email string) (time.Time, error)

GetActivityTime returns the last activity timestamp of a specific user in a specific PR. If the user is not found or is a bot, this function returns a zero timestamp.

func GetSlackBotUserID

func GetSlackBotUserID(_ context.Context, botID string) (string, error)

func GetURLAndIDMapping

func GetURLAndIDMapping(_ context.Context, key string) (string, error)

func InitTurns

func InitTurns(prURL, authorEmail string) error

InitTurns initializes the attention state of a new PR with its author's email address. The initial state has no reviewers; they are added when they are added to the Slack channel. Happens only once per PR, in the beginning, so no need for a Temporal activity, mutex, etc.

func ListReminders

func ListReminders(_ context.Context) (map[string]string, error)

func ReadAllURLsOrChannels added in v1.9.3

func ReadAllURLsOrChannels(_ context.Context, what string) ([]string, error)

func ReadCurrentTurnEmails

func ReadCurrentTurnEmails(ctx context.Context, opts client.Options, prURL string) ([]string, error)

ReadCurrentTurnEmails returns the email addresses of all the users whose turn it is to pay attention to a specific PR. If the PR has no assigned reviewers, this function returns the PR author (as a reminder for them to assign reviewers). If any assigned reviewer has their turn flag set to false, we add the author to the list as well.

func ReadDiffstatPaths

func ReadDiffstatPaths(_ context.Context, prURL string) ([]string, error)

func ReadPRSnapshot

func ReadPRSnapshot(_ context.Context, prURL string) (map[string]any, error)

ReadPRSnapshot reads a snapshot of a PR, which is used to detect and analyze metadata changes. If a snapshot doesn't exist, this function returns a nil map and no error.

func ReadPRsPerSlackUser

func ReadPRsPerSlackUser(ctx context.Context, op client.Options, currentTurn, authors, reviewers bool, filter []string) (map[string][]string, error)

ReadPRsPerSlackUser scans all stored PR turn files, and returns a mapping from Slack user IDs to all the PR URLs they need to be reminded about.

func RemoveFollower

func RemoveFollower(_ context.Context, followerSlackID string) error

func RemoveReviewerFromTurns

func RemoveReviewerFromTurns(ctx context.Context, opts client.Options, prURL, email string, approved bool) error

RemoveReviewerFromTurns completely removes a reviewer from the attention state of a specific PR. This is called when that reviewer approves the PR, or is unassigned from it. This function is idempotent: if the reviewer does not exist, it does nothing.

func SetReminder

func SetReminder(_ context.Context, userID, kitchenTime, tz string) error

func SetReviewerTurn

func SetReviewerTurn(ctx context.Context, opts client.Options, prURL, email string, nudge bool) ([2]bool, error)

SetReviewerTurn records that it's a specific user's turn to review a specific PR: either because they were added as a reviewer, or because they're an existing reviewer and were nudged. This function is idempotent either way, but the return values indicate the state for nudge calls: The first boolean indicates whether the requested nudge is allowed (the user is tracked as a reviewer), and the second one indicates whether the user already approved the PR (in case the first value is false).

func SetSlackBotUserID

func SetSlackBotUserID(_ context.Context, botID, userID string) error

func SetURLAndIDMapping

func SetURLAndIDMapping(_ context.Context, url, ids string) error

func SwitchTurn

func SwitchTurn(ctx context.Context, opts client.Options, prURL, email string, force bool) error

SwitchTurn switches the turn of a specific user in a specific PR to others. If the user is not found or is a bot, this function does nothing. If turns are frozen and the switch isn't forced, it only records the activity. If the user is the PR author, it adds all reviewers to the attention state. If the user is a reviewer, it adds the author to the attention state.

func UnfreezeTurns

func UnfreezeTurns(ctx context.Context, opts client.Options, prURL string) (bool, error)

UnfreezeTurns is the inverse of FreezeTurns. If the turn is not frozen, this function returns false and does nothing.

func UpdateActivityTime

func UpdateActivityTime(ctx context.Context, opts client.Options, prURL, email string) error

UpdateActivityTime updates the last activity timestamp of a specific user in a specific PR. If the user is not found or is a bot, this function does nothing. This is called when the user interacts with the PR in any way that doesn't change their turn (such as PR edits, commit pushes, and review actions).

func UpdateBitbucketBuilds

func UpdateBitbucketBuilds(_ context.Context, prURL, commitHash, key string, cs CommitStatus) error

func WriteDiffstat

func WriteDiffstat(_ context.Context, prURL string, files any) error

func WritePRSnapshot

func WritePRSnapshot(_ context.Context, prURL string, pr any) error

WritePRSnapshot writes a snapshot of a PR, which is used to detect and analyze metadata changes.

Types

type CommitStatus

type CommitStatus struct {
	Name  string `json:"name"`
	State string `json:"state"`
	Desc  string `json:"desc"`
	URL   string `json:"url"`
}

type Frozen

type Frozen struct {
	At time.Time `json:"at"`
	By string    `json:"by"`
}

Frozen is used to return the result of IsFrozen in a single struct, instead of two separate values.

func IsFrozen

func IsFrozen(ctx context.Context, opts client.Options, prURL string) (Frozen, error)

IsFrozen returns the timestamp and user email of when and who froze the attention state of a specific PR. If the turn is not frozen, it returns a zero timestamp and an empty string.

type PRStatus

type PRStatus struct {
	CommitHash string                  `json:"commit_hash"`
	Builds     map[string]CommitStatus `json:"builds"`
}

PRStatus represents the current status of all reported builds for a specific Bitbucket PR at a specific commit.

func ReadBitbucketBuilds

func ReadBitbucketBuilds(_ context.Context, prURL string) (*PRStatus, error)

type PRTurns

type PRTurns struct {
	Author string `json:"author"` // Email address of the PR author.

	Reviewers map[string]bool      `json:"reviewers,omitempty"` // Email address -> is it their turn?
	Activity  map[string]time.Time `json:"activity,omitempty"`  // When each user last interacted with the PR.
	Approvers map[string]time.Time `json:"approvers,omitempty"` // When each user approved the PR.

	FrozenAt time.Time `json:"frozen_at,omitzero"`
	FrozenBy string    `json:"frozen_by,omitempty"`
}

PRTurns represents the attention state for all the author-reviewer pairs in a specific pull request, and also records activity timestamps.

type User

type User struct {
	Email string `json:"email,omitempty"`

	BitbucketID string `json:"bitbucket_id,omitempty"`
	GitHubID    string `json:"github_id,omitempty"`
	SlackID     string `json:"slack_id,omitempty"`
	ThrippyLink string `json:"thrippy_link,omitempty"`

	RealName string `json:"real_name,omitempty"` // Not guaranteed to be unique, unlike the fields above.

	// Slack user IDs, controlled by the un/follow Slack commands, used when creating channels.
	Followers []string `json:"followers,omitempty"`

	Created time.Time `json:"created,omitzero"`
	Updated time.Time `json:"updated,omitzero"`
	Deleted time.Time `json:"deleted,omitzero"`
}

User represents a mapping between a unique email address and at least 2 other unique identifiers for that person across different systems (Bitbucket, GitHub, Slack, Thrippy).

func FollowUser

func FollowUser(_ context.Context, followerSlackID, followedSlackID string) (User, error)

func SelectUser

func SelectUser(_ context.Context, indexType int, id string) (User, error)

func UnfollowUser

func UnfollowUser(_ context.Context, followerSlackID, followedSlackID string) (User, error)

func UpsertUser

func UpsertUser(_ context.Context, email, realName, bitbucketID, githubID, slackID, thrippyLink string) (User, error)

func (User) IsOptedIn

func (u User) IsOptedIn() bool

type Users

type Users struct {
	// contains filtered or unexported fields
}

Users is an indexed copy of a collection of User entries. This should really be stored in a relational database.

Jump to

Keyboard shortcuts

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