issuesync

package
v0.3.103-beta Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: AGPL-3.0 Imports: 17 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 issuesync keeps a project's GitHub issues and its tickets in step (ADR 0200).

Index

Constants

View Source
const (
	TopicIssue        = "git.issue"
	TopicIssueComment = "git.issue_comment"

	TopicTicketUpdated          = "ticket.updated"
	TopicTicketStatusChanged    = "ticket.status_changed"
	TopicTicketDeveloperChanged = "ticket.developer_changed"
	TopicTicketLabelsChanged    = "ticket.labels_changed"

	TopicMessageCreated = "chat.message.created"
	TopicMessageUpdated = "chat.message.updated"
	TopicMessageDeleted = "chat.message.deleted"
)

The topics the sync consumes, published by gitprovider, tickets and chat; named here so the domain stays apart from them (ADR 0017).

View Source
const DefaultLabel = "nexul"

DefaultLabel is the label a new sync triggers on.

View Source
const Quiet = 10 * time.Second

Quiet is how long a ticket's changes rest before they are written to its issue, so a burst of edits makes one write.

View Source
const SweepEvery = 15 * time.Minute

SweepEvery is how often every enabled sync lists the issues changed since its last sweep.

Variables

This section is empty.

Functions

func Hash

func Hash(f TicketFields) string

Hash is SHA-256 over the title, body, state, sorted labels and first assignee, in that order: an issue whose hash matches what was last applied or last written carries nothing new.

Types

type Board

type Board interface {
	Done(ctx context.Context, ticketID string) (bool, error)
	// MoveTo moves the ticket to the first column of stage; a project without one leaves it where it is.
	MoveTo(ctx context.Context, ticketID string, stage Stage) error
}

Board reads and moves a ticket's stage.

type Bot

type Bot interface {
	BotLogin(ctx context.Context) (string, error)
}

Bot names the GitHub App's own account, "<app_slug>[bot]", empty before an App is set up.

type Change

type Change struct {
	Owner   string
	Repo    string
	Label   *string
	Mode    *Mode
	Enabled *bool
}

Change turns a sync on or off and sets its label and mode; a nil field keeps its value.

type Comment

type Comment struct {
	ID              int64     `json:"id"`
	Body            string    `json:"body"`
	Author          string    `json:"author"`
	AuthorAvatarURL string    `json:"author_avatar_url"`
	UpdatedAt       time.Time `json:"updated_at"`
}

Comment is a comment on a GitHub issue as git.issue_comment and a listing carry it.

type CommentLink struct {
	GitHubCommentID int64
	MessageID       string
	TicketID        string
}

CommentLink joins a GitHub comment to the ticket-thread message it became.

type CommentPush

type CommentPush struct {
	MessageID string
	TicketID  string
	Due       time.Time
	// Tried is when the comment was first sent, zero before; a retry looks for the comment it may have made.
	Tried    time.Time
	Attempts int
}

CommentPush is a person's message on a linked ticket's thread waiting to be mirrored to its issue.

type CommentWriter

type CommentWriter interface {
	CreateComment(ctx context.Context, owner, repo string, number int, body string) (Comment, error)
	UpdateComment(ctx context.Context, owner, repo string, commentID int64, body string) error
	DeleteComment(ctx context.Context, owner, repo string, commentID int64) error
}

CommentWriter writes issue comments as the GitHub App's installation.

type Comments

type Comments interface {
	// Post returns the new message's id; posting the same comment again returns the same id.
	Post(ctx context.Context, ticketID string, c Comment) (string, error)
	Edit(ctx context.Context, messageID string, c Comment) error
	Delete(ctx context.Context, messageID string, c Comment) error
}

Comments mirrors GitHub comments as external messages on a ticket's thread.

type Deps

type Deps struct {
	Repo          Repo
	Desk          Desk
	Board         Board
	Notes         Notes
	Comments      Comments
	People        People
	Issues        Issues
	Bot           Bot
	Repos         Repos
	Gate          Gate
	Source        Source
	IssueWriter   IssueWriter
	CommentWriter CommentWriter
	Now           func() time.Time
	// Quiet is how long a ticket's changes rest before they are written; zero is the default, Quiet.
	Quiet time.Duration
	// Retry is the wait after GitHub first fails a write, doubling with each failure; zero is 30 seconds.
	Retry time.Duration
}

Deps are the Service's seams; Gate may be nil in tests, which leaves the calls ungated.

type Desk

type Desk interface {
	// File creates the ticket in the project's first backlog column, else its first, reported by the GitHub login.
	File(ctx context.Context, projectID string, f TicketFields, developer, reporter string) (string, error)
	// Apply sets the ticket's title, body and labels to f's, and its developer when developer is set.
	Apply(ctx context.Context, ticketID string, f TicketFields, developer string) error
}

Desk is the tickets side of the sync (ADR 0017).

type Gate

type Gate interface {
	RequireProject(ctx context.Context, projectID string, action permissions.Action) error
}

Gate checks the caller's project permissions.

type Handler

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

Handler adapts the sync to the HTTP/JSON gateway (ADR 0019).

func NewHandler

func NewHandler(svc *Service) *Handler

NewHandler wires the sync's routes over svc.

func (*Handler) Routes

func (h *Handler) Routes() http.Handler

Routes returns the project's issue sync endpoints.

type Issue

type Issue struct {
	ID          int64     `json:"id"`
	Number      int       `json:"number"`
	Title       string    `json:"title"`
	Body        string    `json:"body"`
	State       string    `json:"state"`
	StateReason string    `json:"state_reason"`
	Labels      []string  `json:"labels"`
	Assignees   []string  `json:"assignees"`
	Author      string    `json:"author"`
	HTMLURL     string    `json:"html_url"`
	UpdatedAt   time.Time `json:"updated_at"`
}

Issue is a GitHub issue as git.issue and a listing carry it, declared here so the domain stays apart from gitprovider (ADR 0017).

type IssueQuery

type IssueQuery struct {
	State  string
	Labels []string
	Since  time.Time
	Page   int
}

IssueQuery filters a listing; Page starts at 1.

type IssueWrite

type IssueWrite struct {
	Title     string
	Body      string
	Closed    bool
	Labels    []string
	Assignees []string
}

IssueWrite is what Nexul sets on an issue; nil Assignees leave the issue's as they are.

type IssueWriter

type IssueWriter interface {
	CreateIssue(ctx context.Context, owner, repo string, w IssueWrite) (Issue, error)
	UpdateIssue(ctx context.Context, owner, repo string, number int, w IssueWrite) (Issue, error)
}

IssueWriter writes issues as the GitHub App's installation.

type Issues

type Issues interface {
	// ListIssues returns one page and the next page number, 0 after the last.
	ListIssues(ctx context.Context, owner, repo string, q IssueQuery) ([]Issue, int, error)
	ListComments(ctx context.Context, owner, repo string, number int, since time.Time) ([]Comment, error)
	RateLimit(ctx context.Context, owner, repo string) (Rate, error)
}

Issues reads a repository's issues from GitHub for the backfill and the sweep.

type Link struct {
	Owner       string
	Repo        string
	IssueNumber int
	IssueID     int64
	TicketID    string
	// Paused is a label-mode issue whose label was removed; the link stays so adding it back resumes.
	Paused bool
	// InboundHash is Hash of the issue last applied to the ticket; OutboundHash of what Nexul last wrote to GitHub.
	InboundHash  string
	OutboundHash string
	// TicketHash is Hash of the ticket as its issue takes it when the two were last in step, so what GitHub changed is
	// never written back.
	TicketHash     string
	IssueUpdatedAt time.Time
	LinkedAt       time.Time
}

Link joins one GitHub issue to the ticket it became.

type LinkRepo

type LinkRepo interface {
	// GetLink is ErrNotFound for an issue no ticket came from.
	GetLink(ctx context.Context, owner, repo string, number int) (Link, error)
	PutLink(ctx context.Context, l Link) error
	// DeleteLink removes the link and its ticket's comment links; the ticket stays.
	DeleteLink(ctx context.Context, l Link) error
	// GetCommentLink is ErrNotFound for a comment no message came from.
	GetCommentLink(ctx context.Context, commentID int64) (CommentLink, error)
	PutCommentLink(ctx context.Context, l CommentLink) error
	DeleteCommentLink(ctx context.Context, commentID int64) error
}

LinkRepo stores the issue and comment links.

type Mirror

type Mirror struct {
	// Person is a message a person wrote; an agent's, a system note and a mirrored GitHub comment never go back.
	Person  bool
	Deleted bool
	// Body is the comment as GitHub shows it, naming who wrote it.
	Body string
}

Mirror is a ticket-thread message as its issue's comment takes it.

type Mode

type Mode string

Mode says which of a repository's issues become tickets.

const (
	// ModeLabel syncs the issues carrying the sync's label; removing it pauses the issue's sync.
	ModeLabel Mode = "label"
	// ModeAll syncs every issue.
	ModeAll Mode = "all"
)

type Notes

type Notes interface {
	// Note posts body once per key.
	Note(ctx context.Context, ticketID, key, body string) error
}

Notes posts system notes on a ticket's thread.

type Outbound

type Outbound struct {
	ProjectID string
	// Fields are the title, the body as markdown for GitHub, done as closed, the labels, and the developer's GitHub
	// login as the one assignee, none when they have no GitHub login.
	Fields TicketFields
}

Outbound is a ticket as its issue takes it.

type People

type People interface {
	Member(ctx context.Context, projectID, githubLogin string) (string, error)
}

People maps a GitHub login to the login of a member who may open the project, empty when none does.

type PushRepo

type PushRepo interface {
	// GetLinkByTicket is ErrNotFound for a ticket no issue is linked to.
	GetLinkByTicket(ctx context.Context, ticketID string) (Link, error)
	// GetCommentLinkByMessage is ErrNotFound for a message no comment is linked to.
	GetCommentLinkByMessage(ctx context.Context, messageID string) (CommentLink, error)
	// QueueTicketPush stores p, moving a waiting push's due to p's and keeping a create it asked for.
	QueueTicketPush(ctx context.Context, p TicketPush) error
	QueueCommentPush(ctx context.Context, p CommentPush) error
	DueTicketPushes(ctx context.Context, now time.Time, limit int) ([]TicketPush, error)
	DueCommentPushes(ctx context.Context, now time.Time, limit int) ([]CommentPush, error)
	// NextPushDue is the earliest due of every waiting push, false when none waits.
	NextPushDue(ctx context.Context) (time.Time, bool, error)
	// DeferTicketPush stores p's due unless the ticket was queued again since was, its tried unless one is stored, and
	// its attempts unless more are stored, so a stale copy never loses a first try.
	DeferTicketPush(ctx context.Context, p TicketPush, was time.Time) error
	DeferCommentPush(ctx context.Context, p CommentPush, was time.Time) error
	// DoneTicketPush drops p unless the ticket was queued again since p was read.
	DoneTicketPush(ctx context.Context, p TicketPush) error
	DoneCommentPush(ctx context.Context, p CommentPush) error
}

PushRepo stores what waits to be written to GitHub, and finds the links a write follows.

type Rate

type Rate struct {
	Remaining int
	Reset     time.Time
}

Rate is what is left of the token's GitHub rate limit, and when it resets.

type Repo

type Repo interface {
	SyncRepo
	LinkRepo
	PushRepo
}

Repo is everything the sync stores.

type Repos

type Repos interface {
	ProjectOf(ctx context.Context, owner, repo string) (string, error)
}

Repos answers which project a repository is attached to.

type Service

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

Service applies GitHub issues to tickets.

func New

func New(d Deps) *Service

New builds the Service.

func (*Service) HandleComment

func (s *Service) HandleComment(ctx context.Context, owner, repo string, number int, action string, c Comment, sender string) error

HandleComment applies one git.issue_comment delivery to the linked ticket's thread.

func (*Service) HandleCommentEvent

func (s *Service) HandleCommentEvent(ctx context.Context, ev eventbus.Event) error

HandleCommentEvent is registered on git.issue_comment.

func (*Service) HandleIssue

func (s *Service) HandleIssue(ctx context.Context, owner, repo, action string, i Issue, sender string) error

HandleIssue applies one git.issue delivery.

func (*Service) HandleIssueEvent

func (s *Service) HandleIssueEvent(ctx context.Context, ev eventbus.Event) error

HandleIssueEvent is registered on git.issue.

func (*Service) HandleMessageDeletedEvent

func (s *Service) HandleMessageDeletedEvent(ctx context.Context, ev eventbus.Event) error

HandleMessageDeletedEvent is registered on chat.message.deleted; the push reads whether a person wrote it.

func (*Service) HandleMessageEvent

func (s *Service) HandleMessageEvent(ctx context.Context, ev eventbus.Event) error

HandleMessageEvent is registered on chat.message.created and chat.message.updated; only a person's message goes on.

func (*Service) HandleTicketEvent

func (s *Service) HandleTicketEvent(ctx context.Context, ev eventbus.Event) error

HandleTicketEvent is registered on the ticket topics whose change the issue takes.

func (*Service) MessageChanged

func (s *Service) MessageChanged(ctx context.Context, conversationID, messageID string) error

MessageChanged queues a message on a linked ticket's thread to be mirrored to its issue: posted, edited or deleted.

func (*Service) Push

func (s *Service) Push(ctx context.Context) (time.Duration, error)

Push writes every due ticket and comment and returns how long until the next is due, -1 when nothing waits; one write's failure leaves the others to run, and one that failed before it settled waits a retry, so a failing store never spins the loop. A push already done or deferred is left as it is.

func (*Service) RunPushLoop

func (s *Service) RunPushLoop(ctx context.Context, wake func() <-chan struct{}, logger *slog.Logger)

RunPushLoop writes what waits for GitHub once it is due, woken by the commit that queues it and by the next due.

func (*Service) RunSweepLoop

func (s *Service) RunSweepLoop(ctx context.Context, wake func() <-chan struct{}, logger *slog.Logger)

RunSweepLoop backfills each sync once it is turned on, woken by the commit that stores it, and sweeps every enabled sync at start and every SweepEvery.

func (*Service) Set

func (s *Service) Set(ctx context.Context, projectID string, c Change) (Sync, error)

Set changes the sync of one of the project's repositories; turning it on links the open issues it matches, in the background loop, which the stored change wakes.

func (*Service) Sweep

func (s *Service) Sweep(ctx context.Context, all bool) error

Sweep backfills every enabled sync not yet backfilled and, with all, lists every other one's issues changed since its last sweep; one repository's failure leaves the others to run.

func (*Service) Syncs

func (s *Service) Syncs(ctx context.Context, projectID string) ([]Sync, error)

Syncs lists a project's issue syncs, one per repository sync was ever turned on for.

func (*Service) TicketChanged

func (s *Service) TicketChanged(ctx context.Context, ticketID, projectID string, added []string) error

TicketChanged queues a linked ticket's fields to be written to its issue once its changes rest for the quiet spell; added are the labels the change added, and a synced repository's trigger label among them queues an unlinked ticket's issue to be made.

type Source

type Source interface {
	// Ticket is ErrNotFound for a ticket that is gone.
	Ticket(ctx context.Context, ticketID string) (Outbound, error)
	// Message is ErrNotFound for a message that is gone.
	Message(ctx context.Context, messageID string) (Mirror, error)
	// ThreadOf is the ticket a conversation is the thread of, empty for any other conversation.
	ThreadOf(ctx context.Context, conversationID string) (string, error)
}

Source reads tickets and messages as GitHub takes them (ADR 0017).

type Stage

type Stage string

Stage names the board stage a ticket moves to.

const (
	StageBacklog Stage = "backlog"
	StageDone    Stage = "done"
)

type Sync

type Sync struct {
	ProjectID string `json:"project_id"`
	Owner     string `json:"owner"`
	Repo      string `json:"repo"`
	Label     string `json:"label"`
	Mode      Mode   `json:"mode"`
	Enabled   bool   `json:"enabled"`
	// LastSweptAt is when issues were last listed from GitHub; nil until the first backfill.
	LastSweptAt *time.Time `json:"last_swept_at,omitempty"`
}

Sync is one repository's issue sync, owned by the project the repository is attached to.

type SyncRepo

type SyncRepo interface {
	// GetSync is ErrNotFound for a repository nobody turned sync on for.
	GetSync(ctx context.Context, owner, repo string) (Sync, error)
	ListSyncsByProject(ctx context.Context, projectID string) ([]Sync, error)
	ListEnabledSyncs(ctx context.Context) ([]Sync, error)
	PutSync(ctx context.Context, s Sync) error
	SetSwept(ctx context.Context, owner, repo string, at time.Time) error
}

SyncRepo stores each repository's sync settings.

type TicketFields

type TicketFields struct {
	Title  string
	Body   string
	Closed bool
	// StateReason is why GitHub closed or reopened it, such as completed or not_planned.
	StateReason string
	// Labels are the issue's labels, sorted, without the trigger label.
	Labels []string
	// Assignees are the issue's assignee logins in GitHub's order; the first one a member holds becomes the developer.
	Assignees []string
}

TicketFields is what a ticket takes from a GitHub issue.

func FieldsFromIssue

func FieldsFromIssue(i Issue, trigger string) TicketFields

FieldsFromIssue maps an issue onto ticket fields, leaving out trigger, the label that turns a label-mode sync on, and the hidden marker of the ticket Nexul made the issue for.

type TicketPush

type TicketPush struct {
	TicketID string
	Owner    string
	Repo     string
	// Create makes the issue in Owner/Repo when the ticket has none, once its trigger label was added.
	Create bool
	Due    time.Time
	// Tried is when a create was first sent, zero before; a retry looks for the issue it may have made.
	Tried    time.Time
	Attempts int
}

TicketPush is a ticket's fields waiting to be written to its issue.

Jump to

Keyboard shortcuts

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