Skip to content

Commit

Permalink
Add support for sprig functions in Go templates in the format argument (
Browse files Browse the repository at this point in the history
#6)

* Add support for sprig functions in Go templates in the format argument

* Update README

* Update README
  • Loading branch information
aknysh authored Dec 19, 2018
1 parent 71d7eda commit 71f44b4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ GitHub API supports these types of comments:

Since GitHub considers Pull Requests as Issues, `Comments on Issues` and `Comments on Pull Requests` use the same API.

The module supports all of these types of comments (type: `commit`, `pr-review`, `pr-file`, `issue`, `pr`).
The utility supports all these types of comments (`commit`, `pr-review`, `pr-file`, `issue`, `pr`).


---
Expand Down Expand Up @@ -58,7 +58,7 @@ It's 100% Open Source and licensed under the [APACHE2](LICENSE).

__NOTE__: Create a [GitHub token](https://help.github.com/articles/creating-an-access-token-for-command-line-use) with `repo:status` and `public_repo` scopes.

__NOTE__: The module accepts parameters as command-line arguments or as ENV variables (or any combination of command-line arguments and ENV vars).
__NOTE__: The utility accepts parameters as command-line arguments or as ENV variables (or any combination of command-line arguments and ENV vars).
Command-line arguments take precedence over ENV vars.


Expand All @@ -77,9 +77,9 @@ Command-line arguments take precedence over ENV vars.
| delete-comment-regex | GITHUB_DELETE_COMMENT_REGEX | Regex to find previous comments to delete before creating the new comment. Supported for comment types `commit`, `pr-file`, `issue` and `pr` |


__NOTE__: The module accepts the text of the comment from the command-line argument `comment`, from the ENV variable `GITHUB_COMMENT`, or from the standard input.
__NOTE__: The utility accepts the text of the comment from the command-line argument `comment`, from the ENV variable `GITHUB_COMMENT`, or from the standard input.
Command-line argument takes precedence over ENV var, and ENV var takes precedence over standard input.
Accepting comments from `stdin` allows using Unix pipes to send the output from another program as the input to the module:
Accepting comments from `stdin` allows using Unix pipes to send the output from another program as the input to the tool:

```sh
cat comment.txt | github-commenter ...
Expand All @@ -89,12 +89,22 @@ Accepting comments from `stdin` allows using Unix pipes to send the output from
terraform plan 2>&1 | github-commenter -format "Output from `terraform plan`<br/>```{{.}}```"
```

__NOTE__: The utility supports [sprig functions](http://masterminds.github.io/sprig/) in `Go` templates, allowing to use string replacement and Regular Expressions in the `format` argument.

See [string functions](http://masterminds.github.io/sprig/strings.html) for more details.

For example:

```sh
GITHUB_COMMENT_FORMAT="Helm diff:<br><br><pre>{{regexReplaceAllLiteral `\\n` . `<br>` }}<pre>"
```




## Examples

The module can be called directly or as a Docker container.
The utility can be called directly or as a Docker container.

### Build the Go program locally

Expand Down Expand Up @@ -170,8 +180,8 @@ export GITHUB_OWNER=cloudposse
export GITHUB_REPO=github-commenter
export GITHUB_COMMENT_TYPE=pr
export GITHUB_PR_ISSUE_NUMBER=1
export GITHUB_COMMENT_FORMAT="My comment:<br/>{{.}}"
export GITHUB_COMMENT="+1 LGTM"
export GITHUB_COMMENT_FORMAT="Helm diff:<br><br><pre>{{regexReplaceAllLiteral `\\n` . `<br>` }}<pre>"
export GITHUB_COMMENT="Helm diff comment"

docker run -i --rm \
-e GITHUB_TOKEN \
Expand Down
24 changes: 17 additions & 7 deletions README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ description: |-
Since GitHub considers Pull Requests as Issues, `Comments on Issues` and `Comments on Pull Requests` use the same API.
The module supports all of these types of comments (type: `commit`, `pr-review`, `pr-file`, `issue`, `pr`).
The utility supports all these types of comments (`commit`, `pr-review`, `pr-file`, `issue`, `pr`).
related:
- name: "github-status-updater"
Expand All @@ -61,7 +61,7 @@ screenshots:
usage: |-
__NOTE__: Create a [GitHub token](https://help.github.com/articles/creating-an-access-token-for-command-line-use) with `repo:status` and `public_repo` scopes.
__NOTE__: The module accepts parameters as command-line arguments or as ENV variables (or any combination of command-line arguments and ENV vars).
__NOTE__: The utility accepts parameters as command-line arguments or as ENV variables (or any combination of command-line arguments and ENV vars).
Command-line arguments take precedence over ENV vars.
Expand All @@ -80,9 +80,9 @@ usage: |-
| delete-comment-regex | GITHUB_DELETE_COMMENT_REGEX | Regex to find previous comments to delete before creating the new comment. Supported for comment types `commit`, `pr-file`, `issue` and `pr` |
__NOTE__: The module accepts the text of the comment from the command-line argument `comment`, from the ENV variable `GITHUB_COMMENT`, or from the standard input.
__NOTE__: The utility accepts the text of the comment from the command-line argument `comment`, from the ENV variable `GITHUB_COMMENT`, or from the standard input.
Command-line argument takes precedence over ENV var, and ENV var takes precedence over standard input.
Accepting comments from `stdin` allows using Unix pipes to send the output from another program as the input to the module:
Accepting comments from `stdin` allows using Unix pipes to send the output from another program as the input to the tool:
```sh
cat comment.txt | github-commenter ...
Expand All @@ -92,8 +92,18 @@ usage: |-
terraform plan 2>&1 | github-commenter -format "Output from `terraform plan`<br/>```{{.}}```"
```
__NOTE__: The utility supports [sprig functions](http://masterminds.github.io/sprig/) in `Go` templates, allowing to use string replacement and Regular Expressions in the `format` argument.
See [string functions](http://masterminds.github.io/sprig/strings.html) for more details.
For example:
```sh
GITHUB_COMMENT_FORMAT="Helm diff:<br><br><pre>{{regexReplaceAllLiteral `\\n` . `<br>` }}<pre>"
```
examples: |-
The module can be called directly or as a Docker container.
The utility can be called directly or as a Docker container.
### Build the Go program locally
Expand Down Expand Up @@ -169,8 +179,8 @@ examples: |-
export GITHUB_REPO=github-commenter
export GITHUB_COMMENT_TYPE=pr
export GITHUB_PR_ISSUE_NUMBER=1
export GITHUB_COMMENT_FORMAT="My comment:<br/>{{.}}"
export GITHUB_COMMENT="+1 LGTM"
export GITHUB_COMMENT_FORMAT="Helm diff:<br><br><pre>{{regexReplaceAllLiteral `\\n` . `<br>` }}<pre>"
export GITHUB_COMMENT="Helm diff comment"
docker run -i --rm \
-e GITHUB_TOKEN \
Expand Down
1 change: 1 addition & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ package: github.com/cloudposse/github-commenter
import:
- package: github.com/google/go-github/github
- package: golang.org/x/net/context
- package: github.com/Masterminds/sprig
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"flag"
"fmt"
"github.com/Masterminds/sprig"
"github.com/google/go-github/github"
"github.com/pkg/errors"
"golang.org/x/net/context"
Expand Down Expand Up @@ -95,7 +96,7 @@ func formatComment(comment string) (string, error) {
return comment, nil
}

t, err := template.New("formatComment").Parse(*format)
t, err := template.New("formatComment").Funcs(sprig.TxtFuncMap()).Parse(*format)
if err != nil {
return "", err
}
Expand Down

0 comments on commit 71f44b4

Please sign in to comment.