Skip to content

Commit

Permalink
comment-monitor: Added flag support; added built-in help command.
Browse files Browse the repository at this point in the history
Implements first phase of prometheus/proposals#41

Signed-off-by: bwplotka <bwplotka@gmail.com>
  • Loading branch information
bwplotka committed Dec 13, 2024
1 parent 7ce8bac commit c037ee0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
14 changes: 14 additions & 0 deletions tools/comment-monitor/internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func parseConfigContent(content []byte) (_ *Config, err error) {
if c.Name == "" && c.ArgsRegex == "" {
return nil, fmt.Errorf("/%v bad config; default commands cannot have empty args_regex (no required arguments)", p.Prefix)
}
if strings.ToLower(c.Name) == "help" {
return nil, fmt.Errorf("/%v bad config; 'help' command name is reserved", p.Prefix)

}
if strings.HasPrefix(c.ArgsRegex, "^") {
return nil, fmt.Errorf("/%v bad config; args_regex has to be front open, got %v", p.Prefix, c.ArgsRegex)
}
Expand Down Expand Up @@ -144,6 +148,16 @@ func ParseCommand(cfg *Config, comment string) (_ *Command, ok bool, err *Comman
cmdLine := comment[:i]
rest := cmdLine[len(prefix.Prefix):]

// Is it help?
if hasExactPrefix(rest, " help") {
return &Command{
Args: map[string]string{},
Prefix: prefix.Prefix,
SuccessCommentTemplate: prefix.Help,
DebugCMDLine: cmdLine,
}, true, nil
}

// Find the command.
var cmdConfig *CommandConfig
var defaultCmdConfig *CommandConfig
Expand Down
18 changes: 14 additions & 4 deletions tools/comment-monitor/internal/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ const (
eventTypeStop = "prombench_stop"
)

func testCommand(evenType, release string) *Command {
func testCommand(eventType, release string) *Command {
c := &Command{
Prefix: "/prombench",
Args: map[string]string{},
EventType: evenType,
EventType: eventType,
ShouldVerifyUser: true,
}
if release != "" {
Expand All @@ -41,6 +41,13 @@ func testCommand(evenType, release string) *Command {
return c
}

func helpCommand() *Command {
return &Command{
Prefix: "/prombench",
Args: map[string]string{},
}
}

type parseCommandCase struct {
comment string
expect *Command
Expand Down Expand Up @@ -99,9 +106,8 @@ func testParseCommand(t *testing.T, c *Config, cases []parseCommandCase) {
}

func TestParseCommand(t *testing.T) {
const testConfigFile = "./testconfig.yaml"

c, err := ParseConfig(testConfigFile)
c, err := ParseConfig("./testconfig.yaml")
if err != nil {
t.Fatal(err)
}
Expand All @@ -122,6 +128,10 @@ func TestParseCommand(t *testing.T) {
comment: "/prombench cancel",
expect: testCommand(eventTypeStop, ""),
},
{
comment: "/prombench help",
expect: helpCommand(),
},
// Different versions based on the provided args_regex: ^\s+(?P<RELEASE>master|main|v[0-9]+\.[0-9]+\.[0-9]+\S*)\s*$
{
comment: "/prombench main",
Expand Down
35 changes: 19 additions & 16 deletions tools/comment-monitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,14 @@ func (d *dispatcher) HandleIssue(w http.ResponseWriter, r *http.Request) {
}
return
}
}

allArgs := cmd.Args
if cmd.EventType != "" {
logger = logger.With("cmdLine", cmd.DebugCMDLine)
logger.Info("dispatching a new command and updating issue")

// Combine all arguments for both dispatch and the comment update.
allArgs := cmd.Args
allArgs["PR_NUMBER"] = strconv.Itoa(eventDetails.PR)
allArgs["LAST_COMMIT_SHA"], err = ghClient.GetLastCommitSHA()
if err != nil {
Expand All @@ -257,26 +259,27 @@ func (d *dispatcher) HandleIssue(w http.ResponseWriter, r *http.Request) {
return
}
logger.Info("dispatched repository GitHub payload")
}

// Update the issue.
comment, err := executeCommentTemplate(cmd.SuccessCommentTemplate, allArgs)
if err != nil {
handleErr(w, logger, "failed to execute template", http.StatusInternalServerError, err)
return
}
// Update the issue.
comment, err := executeCommentTemplate(cmd.SuccessCommentTemplate, allArgs)
if err != nil {
handleErr(w, logger, "failed to execute template", http.StatusInternalServerError, err)
return
}

if err = ghClient.PostComment(comment); err != nil {
handleErr(w, logger, "dispatch successful; but could not post comment to GitHub", http.StatusInternalServerError, err)
return
}
if err = ghClient.PostComment(comment); err != nil {
handleErr(w, logger, "dispatch successful; but could not post comment to GitHub", http.StatusInternalServerError, err)
return
}

if cmd.SuccessLabel != "" {
if err = ghClient.PostLabel(cmd.SuccessLabel); err != nil {
handleErr(w, logger, "dispatch successful; but could not post label to GitHub", http.StatusInternalServerError, err)
return
}
if cmd.SuccessLabel != "" {
if err = ghClient.PostLabel(cmd.SuccessLabel); err != nil {
handleErr(w, logger, "dispatch successful; but could not post label to GitHub", http.StatusInternalServerError, err)
return
}
}

}

func executeCommentTemplate(commentTemplate string, args map[string]string) (string, error) {
Expand Down

0 comments on commit c037ee0

Please sign in to comment.