commentfix

package
v0.0.0-...-6ec82fb Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2025 License: BSD-3-Clause Imports: 20 Imported by: 0

Documentation

Overview

Package commentfix implements rule-based rewriting of issue comments.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fixer

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

A Fixer rewrites issue texts and issue comments using a set of rules. After creating a fixer with New, new rules can be added using the Fixer.AutoLink, Fixer.ReplaceText, and Fixer.ReplaceURL methods, and then repeated calls to Fixer.Run apply the replacements on GitHub.

The zero value of a Fixer can be used in “offline” mode with Fixer.Fix, which returns rewritten Markdown.

TODO(rsc): Separate the GitHub logic more cleanly from the rewrite logic.

func New

func New(lg *slog.Logger, gh *github.Client, db storage.DB, name string) *Fixer

New creates a new Fixer using the given logger and GitHub client.

The Fixer logs status and errors to lg; if lg is nil, the Fixer does not log anything.

The GitHub client is used to watch for new issues and comments and to edit issues and comments. If gh is nil, the Fixer can still be configured and applied to Markdown using Fixer.Fix, but calling Fixer.Run will panic.

The db is the database used to store locks.

The name is the handle by which the Fixer's “last position” is retrieved across multiple program invocations; each differently configured Fixer needs a different name.

func (f *Fixer) AutoLink(pattern, url string) error

AutoLink instructs the fixer to turn any text matching the regular expression pattern into a link to the URL. The URL can contain substitution values like $1 as supported by regexp.Regexp.Expand.

For example, to link CL nnn to https://go.dev/cl/nnn, you could use:

f.AutoLink(`\bCL (\d+)\b`, "https://go.dev/cl/$1")

func (*Fixer) EnableEdits

func (f *Fixer) EnableEdits()

EnableEdits configures the fixer to make edits to comments on GitHub. If EnableEdits is not called, the Fixer only prints what it would do, and it does not mark the issues and comments as “old”. This default mode is useful for experimenting with a Fixer to gauge its effects.

EnableEdits panics if the Fixer was not constructed by calling New with a non-nil github.Client.

func (*Fixer) EnableProject

func (f *Fixer) EnableProject(name string)

func (*Fixer) Fix

func (f *Fixer) Fix(text string) (newText string, fixed bool)

Fix applies the configured rewrites to the markdown text. If no fixes apply, it returns "", false. If any fixes apply, it returns the updated text and true.

func (*Fixer) Latest

func (f *Fixer) Latest() timed.DBTime

Latest returns the latest known DBTime marked old by the Fixer's Watcher.

func (*Fixer) LogFixGitHubIssue

func (f *Fixer) LogFixGitHubIssue(ctx context.Context, project string, issue int64) error

LogFixGitHubIssue adds rewrites to the issue body and comments of the specified GitHub issue to the action log, following the same logic as Fixer.Run.

It requires that the Fixer's github.Client contain one or more events for the issue.

It does not affect the watcher used by Fixer.Run and can be run concurrently with Fixer.Run.

However, any issues or comments for which fixes were applied will not be fixed again by subsequent calls to Fixer.Run or [Fixer.FixGitHubIssue] for a Fixer with the same name as this one. This is true even if the issue or comment body has changed since the fix was applied, in order to a prevent a non-idempotent fix from being applied multiple times.

It returns an error if any of the fixes cannot be applied or if no events are found for the issue.

func (*Fixer) ReplaceText

func (f *Fixer) ReplaceText(pattern, repl string) error

ReplaceText instructs the fixer to replace any text matching the regular expression pattern with the replacement repl. The replacement can contain substitution values like $1 as supported by regexp.Regexp.Expand.

ReplaceText only applies in Markdown plain text. It does not apply in backticked code text, or in backticked or indented code blocks, or to URLs. It does apply to the plain text inside headings, inside bold, italic, or link markup.

For example, you could correct “cancelled” to “canceled”, following Go's usual conventions, with:

f.ReplaceText(`cancelled`, "canceled")

func (*Fixer) ReplaceURL

func (f *Fixer) ReplaceURL(pattern, repl string) error

ReplaceURL instructs the fixer to replace any linked URLs matching the regular expression pattern with the replacement URL repl. The replacement can contain substitution values like $1 as supported by regexp.Regexp.Expand.

The regular expression pattern is automatically anchored to the start of the URL: there is no need to start it with \A or ^.

For example, to replace links to golang.org with links to go.dev, you could use:

f.ReplaceURL(`https://golang\.org(/?)`, "https://go.dev$1")

func (*Fixer) RequireApproval

func (f *Fixer) RequireApproval()

RequireApproval configures the fixer to add actions to the action log that require approval.

func (*Fixer) Run

func (f *Fixer) Run(ctx context.Context) error

Run adds to the action log the configured rewrites to issue texts and comments on GitHub that have been updated since the last call to Run for this fixer with edits enabled (including in different program invocations using the same fixer name).

By default, Run ignores issues texts and comments more than 30 days old. Use Fixer.SetTimeLimit to change the cutoff.

Run prints diffs of its edits to standard error in addition to logging them, because slog logs the diffs as single-line Go quoted strings that are too difficult to skim.

If Fixer.EnableEdits has not been called, Run processes recent issue texts and comments and prints diffs of its intended edits to standard error, but it does not add the changes to the action log. It also does not mark the issues and comments as processed, so that a future call to Run with edits enabled can rewrite them on GitHub.

Run panics if the Fixer was not constructed by calling New with a non-nil github.Client.

func (*Fixer) SetStderr

func (f *Fixer) SetStderr(w io.Writer)

SetStderr sets the writer to use for messages f intends to print to standard error. A Fixer writes directly to standard error (or this writer) so that it can print readable multiline debugging outputs. These are also logged via the slog.Logger passed to New, but multiline strings format as one very long Go-quoted string in slog and are not as easy to read.

func (*Fixer) SetTimeLimit

func (f *Fixer) SetTimeLimit(limit time.Time)

SetTimeLimit sets the time before which comments are not edited.

Jump to

Keyboard shortcuts

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