diff --git a/README.md b/README.md index 6a612da..afc89f3 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ The following parameters are used for the resource's `source` configuration: | `ignore_labels` | No | `["lifecycle/stale"]` | `[]` | The labels of the pull request not to react on. | | `comments` | No | `["^ping$"]` | `[]` | The regular expressions of the latest comment to react on. | | `ignore_comments` | No | `["ing$"]` | `[]` | The regular expressions of the latest comment not to react on. | +| `map_comments_meta` | No | `true` | `false` | Whether to map any regular expression keys and their corresponding values to the meta object provided in `in`. | ## Behaviour @@ -42,7 +43,7 @@ Concourse is Github's unique numerical ID for the comment. The following parameters may be used in the `get` step of the resource: -| Parameter | Required | Default | Description | +| Parameter | Required | Default | Description | | -------------- | -------- | ------------- | ---------------------------------------------- | | `comment_file` | No | `comment.txt` | A unique path to save the body of the comment. | @@ -59,8 +60,10 @@ resource. | `updated_at` | The timestamp of when the comment was last updated. | | `author_association` | The association the author of the comment has with the repository. | | `html_url` | The URL to the comment. | -| `user_login` | The username of the comment author on Github. | | `user_id` | The unique ID of the comment author on Github. | +| `user_login` | The username of the comment author on Github. | +| `user_name` | The name of the comment author on Github. | +| `user_email` | The email of the comment author on Github. | | `user_avatar_url` | The avatar URL for the comment author. | | `user_html_url` | The URL to the comment author's profile on Github. | @@ -69,7 +72,9 @@ formatted files which contain the information about the PR comment: * `version.json` which contains only contains the unique ID of the Github comment to the PR; and, - * `metadata.json` which contains a serialized version of the table above. + * `metadata.json` which contains a serialized version of the table above, + * Any additional attributes mapped from parsing comments using Golang's name + grouping. More details can be found [here](https://golang.org/pkg/regexp/syntax/). ### `out` diff --git a/actions/actions.go b/actions/actions.go index 28627e8..df87a39 100644 --- a/actions/actions.go +++ b/actions/actions.go @@ -62,6 +62,7 @@ type Source struct { Labels []string `json:"labels"` Comments []string `json:"comments"` CommenterAssociation []string `json:"commenter_association"` + MapCommentMeta bool `json:"map_comment_meta"` IgnoreStates []string `json:"ignore_states"` IgnoreLabels []string `json:"ignore_labels"` diff --git a/actions/in.go b/actions/in.go index 8bf9f47..eb16e9f 100644 --- a/actions/in.go +++ b/actions/in.go @@ -34,6 +34,7 @@ import ( "os" "fmt" "time" + "regexp" "strconv" "io/ioutil" "encoding/json" @@ -181,6 +182,16 @@ func In(outputDir string, req InRequest) (*InResponse, error) { UserHTMLURL: *comment.User.HTMLURL, }) + if req.Source.MapCommentMeta { + for _, commentStr := range req.Source.Comments { + extraMeta := getParams(commentStr, *comment.Body) + + for k, v := range extraMeta { + metadata.Add(k, v) + } + } + } + b, err := json.Marshal(req.Version) if err != nil { return nil, fmt.Errorf("failed to marshal version: %s", err) @@ -213,3 +224,18 @@ func In(outputDir string, req InRequest) (*InResponse, error) { Metadata: metadata, }, nil } + + +func getParams(regEx, comment string) (paramsMap map[string]string) { + var compRegEx = regexp.MustCompile(regEx) + match := compRegEx.FindStringSubmatch(comment) + + paramsMap = make(map[string]string) + for i, name := range compRegEx.SubexpNames() { + if i > 0 && i <= len(match) { + paramsMap[name] = match[i] + } + } + + return +}