├── fmt ├── README.md ├── Dockerfile └── entrypoint.sh ├── lint ├── README.md ├── Dockerfile └── entrypoint.sh ├── LICENSE └── README.md /fmt/README.md: -------------------------------------------------------------------------------- 1 | # go fmt 2 | 3 | Runs `gofmt`. To learn more about `gofmt`, see the [official docs](https://golang.org/cmd/gofmt/). 4 | 5 | ```hcl 6 | workflow "Go" { 7 | on = "pull_request" 8 | resolves = ["gofmt"] 9 | } 10 | 11 | action "gofmt" { 12 | uses = "sjkaliski/go-github-actions/fmt@v1.0.0" 13 | needs = "previous-action" 14 | secrets = ["GITHUB_TOKEN"] 15 | 16 | env { 17 | GO_WORKING_DIR = "./path/to/go/files" 18 | GO_IGNORE_DIRS = "./vendor" 19 | } 20 | } 21 | ``` 22 | -------------------------------------------------------------------------------- /fmt/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.14 2 | 3 | LABEL "com.github.actions.name"="go fmt" 4 | LABEL "com.github.actions.description"="Run go fmt" 5 | LABEL "com.github.actions.icon"="terminal" 6 | LABEL "com.github.actions.color"="blue" 7 | 8 | LABEL "repository"="https://github.com/sjkaliski/go-github-actions" 9 | LABEL "homepage"="https://github.com/sjkaliski/go-github-actions" 10 | LABEL "maintainer"="Steve Kaliski " 11 | 12 | RUN apt-get update 13 | RUN apt-get install -y jq 14 | 15 | ADD entrypoint.sh /entrypoint.sh 16 | ENTRYPOINT ["/entrypoint.sh"] 17 | -------------------------------------------------------------------------------- /lint/README.md: -------------------------------------------------------------------------------- 1 | # golint 2 | 3 | Runs `golint`. To learn more about `golint`, see the [golint repository](https://github.com/golang/lint/). 4 | 5 | Use `GO_LINT_PATHS` to specify directories to evaluate. Defaults to `./...`. 6 | 7 | ```hcl 8 | workflow "Go" { 9 | on = "pull_request" 10 | resolves = ["golint"] 11 | } 12 | 13 | action "golint" { 14 | uses = "sjkaliski/go-github-actions/lint@v1.0.0" 15 | needs = "previous-action" 16 | secrets = ["GITHUB_TOKEN"] 17 | 18 | env { 19 | GO_WORKING_DIR = "./path/to/go/files" 20 | GO_LINT_PATHS = "./pkg/... ./cmd/..." 21 | } 22 | } 23 | ``` 24 | -------------------------------------------------------------------------------- /lint/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.14 2 | 3 | LABEL "com.github.actions.name"="golint" 4 | LABEL "com.github.actions.description"="Run golint" 5 | LABEL "com.github.actions.icon"="terminal" 6 | LABEL "com.github.actions.color"="blue" 7 | 8 | LABEL "repository"="https://github.com/sjkaliski/go-github-actions" 9 | LABEL "homepage"="https://github.com/sjkaliski/go-github-actions" 10 | LABEL "maintainer"="Steve Kaliski " 11 | 12 | RUN apt-get update && \ 13 | apt-get install -y jq && \ 14 | go get -u golang.org/x/lint/golint 15 | 16 | ADD entrypoint.sh /entrypoint.sh 17 | ENTRYPOINT ["/entrypoint.sh"] 18 | -------------------------------------------------------------------------------- /lint/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | 4 | cd "${GO_WORKING_DIR:-.}" 5 | 6 | # Check if lint fails on any files. 7 | set +e 8 | OUTPUT="$(golint -set_exit_status ${GO_LINT_PATHS:-./...})" 9 | SUCCESS=$? 10 | set -e 11 | 12 | # Exit if `golint` passes. 13 | if [ $SUCCESS -eq 0 ]; then 14 | exit 0 15 | fi 16 | 17 | # Post results back as comment. 18 | COMMENT="#### \`golint\` 19 | \`\`\` 20 | $OUTPUT 21 | \`\`\` 22 | " 23 | PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT" '.body = $body') 24 | COMMENTS_URL=$(cat /github/workflow/event.json | jq -r .pull_request.comments_url) 25 | 26 | if [ "COMMENTS_URL" != null ]; then 27 | curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$PAYLOAD" "$COMMENTS_URL" > /dev/null 28 | fi 29 | 30 | exit $SUCCESS 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Steve Kaliski 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-github-actions 2 | 3 | A collection of [GitHub Actions](https://github.com/features/actions) for use in [Golang](https://golang.org/) projects. 4 | 5 | ## Actions 6 | 7 | Currently there is support for `gofmt` and `golint`. If triggered by a `pull_request`, any failure will be posted back to the PR as a comment. 8 | 9 | ### gofmt 10 | 11 | Runs `gofmt` on files in the directory. Fails if any file is not properly formatted. 12 | 13 | ```hcl 14 | workflow "Go" { 15 | on = "pull_request" 16 | resolves = ["gofmt"] 17 | } 18 | 19 | action "gofmt" { 20 | uses = "sjkaliski/go-github-actions/fmt@v1.0.0" 21 | needs = "previous-action" 22 | secrets = ["GITHUB_TOKEN"] 23 | 24 | env { 25 | GO_WORKING_DIR = "./path/to/go/files" 26 | GO_IGNORE_DIRS = "./vendor" 27 | } 28 | } 29 | ``` 30 | 31 | To learn more about `gofmt`, visit the [official docs](https://golang.org/cmd/gofmt/). 32 | 33 | ### golint 34 | 35 | Runs `golint` on files in the directory. Fails if any file fails lint checks. 36 | 37 | ```hcl 38 | workflow "Go" { 39 | on = "pull_request" 40 | resolves = ["golint"] 41 | } 42 | 43 | action "golint" { 44 | uses = "sjkaliski/go-github-actions/lint@v1.0.0" 45 | needs = "previous-action" 46 | secrets = ["GITHUB_TOKEN"] 47 | 48 | env { 49 | GO_WORKING_DIR = "./path/to/go/files" 50 | GO_LINT_PATHS = "./pkg/... ./cmd/..." 51 | } 52 | } 53 | ``` 54 | 55 | To learn more about `golint`, see the [golint repository](https://github.com/golang/lint/). 56 | -------------------------------------------------------------------------------- /fmt/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | cd "${GO_WORKING_DIR:-.}" 5 | 6 | # Build ignored directories 7 | IGNORED_DIRS="" 8 | if [ -n "${GO_IGNORE_DIRS}" ]; then 9 | IGNORE_DIRS_ARR=($GO_IGNORE_DIRS) 10 | for DIR in "${IGNORE_DIRS_ARR[@]}"; do 11 | # If the directory doesn't end in "/*", add it 12 | if [[ ! "${DIR}" =~ .*\/\*$ ]]; then 13 | DIR="${DIR}/*" 14 | fi 15 | # Append to our list of directories to ignore 16 | IGNORED_DIRS+=" -not -path \"${DIR}\"" 17 | done 18 | fi 19 | 20 | # Use an eval to avoid glob expansion 21 | FIND_EXEC="find . -type f -iname '*.go' ${IGNORED_DIRS}" 22 | 23 | # Get a list of files that we are interested in 24 | CHECK_FILES=$(eval ${FIND_EXEC}) 25 | 26 | # Check if any files are not formatted. 27 | set +e 28 | test -z "$(gofmt -l -d -e ${CHECK_FILES})" 29 | SUCCESS=$? 30 | set -e 31 | 32 | # Exit if `go fmt` passes. 33 | if [ $SUCCESS -eq 0 ]; then 34 | exit 0 35 | fi 36 | 37 | # Get list of unformatted files. 38 | set +e 39 | ISSUE_FILES=$(gofmt -l ${CHECK_FILES}) 40 | echo "${ISSUE_FILES}" 41 | set -e 42 | 43 | # Iterate through each unformatted file. 44 | OUTPUT="" 45 | for FILE in $ISSUE_FILES; do 46 | DIFF=$(gofmt -d -e "${FILE}") 47 | OUTPUT="$OUTPUT 48 | \`${FILE}\` 49 | 50 | \`\`\`diff 51 | $DIFF 52 | \`\`\` 53 | " 54 | done 55 | 56 | # Post results back as comment. 57 | COMMENT="#### \`go fmt\` 58 | $OUTPUT 59 | " 60 | PAYLOAD=$(echo '{}' | jq --arg body "$COMMENT" '.body = $body') 61 | COMMENTS_URL=$(cat /github/workflow/event.json | jq -r .pull_request.comments_url) 62 | 63 | if [ "COMMENTS_URL" != null ]; then 64 | curl -s -S -H "Authorization: token $GITHUB_TOKEN" --header "Content-Type: application/json" --data "$PAYLOAD" "$COMMENTS_URL" > /dev/null 65 | fi 66 | 67 | exit $SUCCESS 68 | --------------------------------------------------------------------------------