This is an automated email from the ASF dual-hosted git repository.

zixuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-test-infra.git


The following commit(s) were added to refs/heads/master by this push:
     new 2841cb2  Improve comment messages (#71)
2841cb2 is described below

commit 2841cb26622223734e7b27a38eb5bb088ab5dba5
Author: Zixuan Liu <[email protected]>
AuthorDate: Tue Sep 27 18:44:31 2022 +0800

    Improve comment messages (#71)
---
 docbot/action.go        | 36 +++++++++++++++++++++++++++++-------
 docbot/action_config.go | 12 +++++++++---
 docbot/action_test.go   |  4 ++--
 3 files changed, 40 insertions(+), 12 deletions(-)

diff --git a/docbot/action.go b/docbot/action.go
index f8deead..407bc70 100644
--- a/docbot/action.go
+++ b/docbot/action.go
@@ -13,10 +13,7 @@ import (
 )
 
 const (
-       MessageLabelMissing = `Please provide a correct documentation label for 
your PR.
-Instructions see [Pulsar Documentation Label 
Guide](https://docs.google.com/document/d/1Qw7LHQdXWBW9t2-r-A7QdFDBwmZh6ytB4guwMoXHqc0).`
-       MessageLabelMultiple = `Please select only one documentation label for 
your PR.
-Instructions see [Pulsar Documentation Label 
Guide](https://docs.google.com/document/d/1Qw7LHQdXWBW9t2-r-A7QdFDBwmZh6ytB4guwMoXHqc0).`
+       MessageLabelMultiple = "Please select only one documentation label in 
your PR description."
 
        openedActionType    = "opened"
        editedActionType    = "edited"
@@ -24,6 +21,15 @@ Instructions see [Pulsar Documentation Label 
Guide](https://docs.google.com/docu
        unlabeledActionType = "unlabeled"
 )
 
+var builtInDescriptions = make(map[string]string)
+
+func init() {
+       builtInDescriptions["doc-required"] = "Your PR changes impact docs and 
you will update later"
+       builtInDescriptions["doc-not-needed"] = "Your PR changes do not impact 
docs"
+       builtInDescriptions["doc"] = "Your PR contains doc changes"
+       builtInDescriptions["doc-complete"] = "Docs have been already added"
+}
+
 type Action struct {
        config *ActionConfig
 
@@ -117,7 +123,7 @@ func (a *Action) checkLabels() error {
                        labelsToAdd[a.config.GetLabelMissing()] = struct{}{}
                } else {
                        logger.Infoln("Already added missing label.")
-                       return errors.New(MessageLabelMissing)
+                       return errors.New(a.getLabelMissingMessage())
                }
        } else {
                if !a.config.GetEnableLabelMultiple() && checkedCount > 1 {
@@ -170,11 +176,11 @@ func (a *Action) checkLabels() error {
        }
 
        if checkedCount == 0 {
-               err := a.addAndCleanupHelpComment(pr.User.GetLogin(), 
MessageLabelMissing)
+               err := a.addAndCleanupHelpComment(pr.User.GetLogin(), 
a.getLabelMissingMessage())
                if err != nil {
                        return err
                }
-               return errors.New(MessageLabelMissing)
+               return errors.New(a.getLabelMissingMessage())
        }
 
        return nil
@@ -319,3 +325,19 @@ func (a *Action) addAndCleanupHelpComment(login, body 
string) error {
 
        return nil
 }
+
+func (a *Action) getLabelMissingMessage() string {
+       msg := "Please add the following content to your PR description and 
select a checkbox:\n```\n"
+
+       for _, label := range a.config.labelWatchList {
+               desc := ""
+               if value, found := builtInDescriptions[label]; found {
+                       desc = fmt.Sprintf("<!-- %s -->", value)
+               }
+               msg += fmt.Sprintf("- [ ] `%s` %s\n", label, desc)
+       }
+
+       msg += "```"
+
+       return msg
+}
diff --git a/docbot/action_config.go b/docbot/action_config.go
index 34a810e..e39ea72 100644
--- a/docbot/action_config.go
+++ b/docbot/action_config.go
@@ -13,6 +13,7 @@ type ActionConfig struct {
 
        labelPattern        *string
        labelWatchSet       map[string]struct{}
+       labelWatchList      []string
        labelMissing        *string
        enableLabelMissing  *bool
        enableLabelMultiple *bool
@@ -34,14 +35,18 @@ func NewActionConfig() (*ActionConfig, error) {
        }
 
        labelWatchListSlug := os.Getenv("LABEL_WATCH_LIST")
-       labelWatchList := strings.Split(labelWatchListSlug, ",")
        labelWatchSet := make(map[string]struct{})
-       for _, l := range labelWatchList {
+       labelWatchList := make([]string, 0)
+       for _, l := range strings.Split(labelWatchListSlug, ",") {
                key := strings.TrimSpace(l)
                if key == "" {
                        continue
                }
-               labelWatchSet[key] = struct{}{}
+               _, found := labelWatchSet[key]
+               if !found {
+                       labelWatchSet[key] = struct{}{}
+                       labelWatchList = append(labelWatchList, key)
+               }
        }
 
        enableLabelMissingSlug := os.Getenv("ENABLE_LABEL_MISSING")
@@ -67,6 +72,7 @@ func NewActionConfig() (*ActionConfig, error) {
                owner:               &owner,
                labelPattern:        &labelPattern,
                labelWatchSet:       labelWatchSet,
+               labelWatchList:      labelWatchList,
                labelMissing:        &labelMissing,
                enableLabelMissing:  &enableLabelMissing,
                enableLabelMultiple: &enableLabelMultiple,
diff --git a/docbot/action_test.go b/docbot/action_test.go
index fccefe0..b31d790 100644
--- a/docbot/action_test.go
+++ b/docbot/action_test.go
@@ -184,7 +184,7 @@ Need to update docs?
        action := NewActionWithClient(context.Background(), config, 
github.NewClient(mockedHTTPClient))
 
        err := action.Run(1, openedActionType)
-       assertMessageLabel(t, err, MessageLabelMissing)
+       assertMessageLabel(t, err, action.getLabelMissingMessage())
 }
 
 func TestMultipleChecked_WhenMultipleLabelsNotEnabled(t *testing.T) {
@@ -316,7 +316,7 @@ Need to update docs?
        action := NewActionWithClient(context.Background(), config, 
github.NewClient(mockedHTTPClient))
 
        err := action.Run(1, openedActionType)
-       assertMessageLabel(t, err, MessageLabelMissing)
+       assertMessageLabel(t, err, action.getLabelMissingMessage())
 }
 
 func TestSingleChecked_WhenDocLabelExists(t *testing.T) {

Reply via email to