Skip to content

Commit

Permalink
internal/commentfix: create an action to execute
Browse files Browse the repository at this point in the history
First of a few CLs to refactor the commentfix package to separate
creating actions from executing them.

This moves the logic of what to do for an event (or whether to do
anything) into a separate method.

For #9.

Change-Id: I909b03f1e009437d4aab090821dea7edaaf8cbe4
Reviewed-on: https://go-review.googlesource.com/c/oscar/+/612895
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Zvonimir Pavlinovic <[email protected]>
  • Loading branch information
jba committed Sep 13, 2024
1 parent 144fba1 commit bc1c118
Showing 1 changed file with 44 additions and 26 deletions.
70 changes: 44 additions & 26 deletions internal/commentfix/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ func (f *Fixer) ReplaceURL(pattern, repl string) error {
return nil
}

// An action has all the information needed to edit a GitHub issue or comment.
type action struct {
ic *issueOrComment
body string // new body of issue or comment
}

// Run applies 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).
Expand Down Expand Up @@ -296,34 +302,12 @@ func (f *Fixer) Run(ctx context.Context) error {
}
last = e.DBTime

if !f.projects[e.Project] {
continue
}
var ic *issueOrComment
switch x := e.Typed.(type) {
default: // for example, *github.IssueEvent
f.slog.Info("fixer skip", "dbtime", e.DBTime, "type", reflect.TypeOf(e.Typed).String())
continue
case *github.Issue:
if x.PullRequest != nil {
// Do not edit pull request bodies,
// because they turn into commit messages
// and cannot contain things like hyperlinks.
continue
}
ic = &issueOrComment{issue: x}
f.slog.Info("fixer run issue", "dbtime", e.DBTime, "issue", ic.issue.Number)
case *github.IssueComment:
ic = &issueOrComment{comment: x}
f.slog.Info("fixer run comment", "dbtime", e.DBTime, "url", ic.comment.URL)
}
if tm, err := time.Parse(time.RFC3339, ic.updatedAt()); err == nil && tm.Before(f.timeLimit) {
continue
}
body, updated := f.Fix(ic.body())
if !updated {
a := f.newAction(e)
if a == nil {
continue
}
ic := a.ic
body := a.body
live, err := ic.download(ctx, f.github)
if err != nil {
// unreachable unless github error
Expand Down Expand Up @@ -367,6 +351,40 @@ func (f *Fixer) Run(ctx context.Context) error {
return nil
}

// newAction returns an newAction to take on the issue or comment of the event,
// or nil if there is nothing to do.
func (f *Fixer) newAction(e *github.Event) *action {
if !f.projects[e.Project] {
return nil
}
var ic *issueOrComment
switch x := e.Typed.(type) {
default: // for example, *github.IssueEvent
f.slog.Info("fixer skip", "dbtime", e.DBTime, "type", reflect.TypeOf(e.Typed).String())
return nil
case *github.Issue:
if x.PullRequest != nil {
// Do not edit pull request bodies,
// because they turn into commit messages
// and cannot contain things like hyperlinks.
return nil
}
ic = &issueOrComment{issue: x}
f.slog.Info("fixer run issue", "dbtime", e.DBTime, "issue", ic.issue.Number)
case *github.IssueComment:
ic = &issueOrComment{comment: x}
f.slog.Info("fixer run comment", "dbtime", e.DBTime, "url", ic.comment.URL)
}
if tm, err := time.Parse(time.RFC3339, ic.updatedAt()); err == nil && tm.Before(f.timeLimit) {
return nil
}
body, updated := f.Fix(ic.body())
if !updated {
return nil
}
return &action{ic: ic, body: body}
}

// Latest returns the latest known DBTime marked old by the Fixer's Watcher.
func (f *Fixer) Latest() timed.DBTime {
return f.watcher.Latest()
Expand Down

0 comments on commit bc1c118

Please sign in to comment.