Skip to content

Commit

Permalink
actions: Comment regex groups to metadata
Browse files Browse the repository at this point in the history
This commit introduces the ability to save named regex groups
which are matched in the comment to the metadata for later use
by the pipeline.

Signed-off-by: Alexander Jung <alex@nderjung.net>
  • Loading branch information
nderjung committed Nov 23, 2020
1 parent 617c6ea commit 12a24ec
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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. |

Expand All @@ -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. |

Expand All @@ -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`

Expand Down
1 change: 1 addition & 0 deletions actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
26 changes: 26 additions & 0 deletions actions/in.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"os"
"fmt"
"time"
"regexp"
"strconv"
"io/ioutil"
"encoding/json"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit 12a24ec

Please sign in to comment.