├── .github └── workflows │ └── cd.yml ├── .gitignore ├── Dockerfile ├── LICENCE ├── README.md ├── entrypoint.sh ├── eslint-action.go ├── go.mod ├── go.sum └── screenshot.png /.github/workflows/cd.yml: -------------------------------------------------------------------------------- 1 | on: push 2 | name: Continuous Delivery 3 | jobs: 4 | cd: 5 | name: Continuous Delivery 6 | runs-on: ubuntu-latest 7 | steps: 8 | - uses: actions/checkout@master 9 | - name: Docker Build 10 | uses: actions/docker/cli@master 11 | with: 12 | args: build -t eslint-action . 13 | - name: Docker Tag 14 | uses: actions/docker/tag@master 15 | with: 16 | args: --no-sha eslint-action rkusa/eslint-action 17 | - name: Docker Login 18 | uses: actions/docker/login@master 19 | env: 20 | DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} 21 | DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} 22 | - name: Docker Push 23 | uses: actions/docker/cli@master 24 | with: 25 | args: push rkusa/eslint-action 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .* 2 | !.gitignore 3 | !.github 4 | /eslint-action -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:alpine3.8 as builder 2 | 3 | RUN apk --update upgrade 4 | RUN apk --no-cache --no-progress add make git gcc musl-dev 5 | 6 | WORKDIR /build 7 | COPY . . 8 | RUN go build . 9 | 10 | FROM node:10-alpine 11 | RUN apk update && apk add --no-cache --virtual ca-certificates 12 | COPY --from=builder /build/eslint-action /usr/bin/eslint-action 13 | 14 | LABEL version="1.0.0" 15 | LABEL repository="https://github.com/rkusa/eslint-action" 16 | LABEL homepage="https://github.com/rkusa/eslint-action" 17 | LABEL maintainer="Markus Ast " 18 | 19 | LABEL com.github.actions.name="Annotated ESLint" 20 | LABEL com.github.actions.description="Execute ESLint and add linting error annotations" 21 | LABEL com.github.actions.icon="octagon" 22 | LABEL com.github.actions.color="blue" 23 | 24 | ENV ESLINT_CMD ./node_modules/.bin/eslint 25 | COPY entrypoint.sh /entrypoint.sh 26 | 27 | ENTRYPOINT ["/entrypoint.sh"] 28 | CMD [""] -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 Markus Ast and contributors 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 13 | all 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 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Does not work with the release version of Github actions anymore, use [Problem Matchers](https://github.com/actions/toolkit/blob/master/docs/commands.md#problem-matchers) instead. 2 | 3 | --- 4 | 5 | # Github Action for ESLint (with Annotations) 6 | 7 | This Action runs [ESLint](https://github.com/eslint/eslint) on your codebase and adds annotations to the Github check the action is run in. 8 | 9 | Annotaiton Example: https://github.com/rkusa/eslint-action-example/pull/1/files 10 | 11 | ![Annotation Example](screenshot.png) 12 | 13 | ## Usage 14 | 15 | ```hcl 16 | workflow "Lint" { 17 | on = "push" 18 | resolves = ["Eslint"] 19 | } 20 | 21 | action "Dependencies" { 22 | uses = "actions/npm@master" 23 | args = "install" 24 | } 25 | 26 | action "Eslint" { 27 | uses = "docker://rkusa/eslint-action:latest" 28 | secrets = ["GITHUB_TOKEN"] 29 | args = "" 30 | needs = ["Dependencies"] 31 | } 32 | ``` 33 | 34 | ### Secrets 35 | 36 | * `GITHUB_TOKEN` - **Required**. Required to add annotations to the check that is executing the Github action. 37 | 38 | ### Environment variables 39 | 40 | * `ESLINT_CMD` - **Optional**. The path the ESLint command - defaults to `./node_modules/.bin/eslint`. 41 | 42 | #### Example 43 | 44 | To run ESLint, either use the published docker image ... 45 | 46 | ```hcl 47 | action "Eslint" { 48 | uses = "docker://rkusa/eslint-action:latest" 49 | secrets = ["GITHUB_TOKEN"] 50 | args = "" 51 | } 52 | ``` 53 | 54 | ... or the Github repo: 55 | 56 | ```hcl 57 | action "Eslint" { 58 | uses = "rkusa/eslint-action@master" 59 | secrets = ["GITHUB_TOKEN"] 60 | args = "" 61 | } 62 | ``` 63 | 64 | ## License 65 | 66 | The Dockerfile and associated scripts and documentation in this project are released under the [MIT License](LICENSE). 67 | 68 | Container images built with this project include third party materials. View license information for [Node.js](https://github.com/nodejs/node/blob/master/LICENSE), [Node.js Docker project](https://github.com/nodejs/docker-node/blob/master/LICENSE), [ESLint](https://github.com/eslint/eslint/blob/master/LICENSE), [Go](https://golang.org/LICENSE), [google/go-github](https://github.com/google/go-github/blob/master/LICENSE) or [ldez/ghactions](https://github.com/ldez/ghactions/blob/master/LICENSE). As with all Docker images, these likely also contain other software which may be under other licenses. It is the image user's responsibility to ensure that any use of this image complies with any relevant licenses for all software contained within. 69 | -------------------------------------------------------------------------------- /entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -e 4 | 5 | sh -c "$ESLINT_CMD $* --format json . | /usr/bin/eslint-action" -------------------------------------------------------------------------------- /eslint-action.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "encoding/json" 6 | "fmt" 7 | "log" 8 | "os" 9 | "strings" 10 | 11 | "github.com/google/go-github/v24/github" 12 | "github.com/ldez/ghactions" 13 | ) 14 | 15 | type Message struct { 16 | RuleId string 17 | Severity int 18 | Message string 19 | Line int 20 | } 21 | 22 | type File struct { 23 | FilePath string 24 | Messages []*Message 25 | ErrorCount int 26 | WarningCount int 27 | } 28 | 29 | func main() { 30 | var report []*File 31 | err := json.NewDecoder(os.Stdin).Decode(&report) 32 | if err != nil { 33 | log.Fatal(err) 34 | } 35 | 36 | ctx := context.Background() 37 | action := ghactions.NewAction(ctx) 38 | 39 | action.OnPush(func(client *github.Client, event *github.PushEvent) error { 40 | return handlePush(ctx, client, event, report) 41 | }) 42 | 43 | if err := action.Run(); err != nil { 44 | log.Fatal(err) 45 | } 46 | } 47 | 48 | func handlePush(ctx context.Context, client *github.Client, event *github.PushEvent, report []*File) error { 49 | head := os.Getenv(ghactions.GithubSha) 50 | owner, repoName := ghactions.GetRepoInfo() 51 | 52 | // find the action's checkrun 53 | checkName := os.Getenv(ghactions.GithubAction) 54 | result, _, err := client.Checks.ListCheckRunsForRef(ctx, owner, repoName, head, &github.ListCheckRunsOptions{ 55 | CheckName: github.String(checkName), 56 | Status: github.String("in_progress"), 57 | }) 58 | if err != nil { 59 | return err 60 | } 61 | 62 | if len(result.CheckRuns) == 0 { 63 | return fmt.Errorf("Unable to find check run for action: %s", checkName) 64 | } 65 | checkRun := result.CheckRuns[0] 66 | 67 | // add annotations for lint issues 68 | workspacePath := os.Getenv(ghactions.GithubWorkspace) + "/" 69 | var annotations []*github.CheckRunAnnotation 70 | errorCount := 0 71 | warningCount := 0 72 | for _, r := range report { 73 | path := strings.TrimPrefix(r.FilePath, workspacePath) 74 | 75 | errorCount += r.ErrorCount 76 | warningCount += r.WarningCount 77 | for _, m := range r.Messages { 78 | var level *string 79 | switch m.Severity { 80 | case 1: 81 | level = github.String("warning") 82 | case 2: 83 | level = github.String("failure") 84 | } 85 | 86 | annotations = append(annotations, &github.CheckRunAnnotation{ 87 | Path: github.String(path), 88 | StartLine: github.Int(m.Line), 89 | EndLine: github.Int(m.Line), 90 | AnnotationLevel: level, 91 | Title: github.String(m.RuleId), 92 | Message: github.String(m.Message), 93 | }) 94 | } 95 | } 96 | 97 | var summary string 98 | if errorCount == 0 { 99 | summary = "No problems found" 100 | } else { 101 | summary = fmt.Sprintf( 102 | "%d problems (%d errors, %d warnings)", 103 | errorCount+warningCount, 104 | errorCount, 105 | warningCount, 106 | ) 107 | } 108 | 109 | // add annotations in #50 chunks 110 | for i := 0; i < len(annotations); i += 50 { 111 | end := i + 50 112 | 113 | if end > len(annotations) { 114 | end = len(annotations) 115 | } 116 | 117 | _, _, err = client.Checks.UpdateCheckRun(ctx, owner, repoName, checkRun.GetID(), github.UpdateCheckRunOptions{ 118 | Name: checkName, 119 | HeadSHA: github.String(head), 120 | Output: &github.CheckRunOutput{ 121 | Title: github.String("Result"), 122 | Summary: github.String(summary), 123 | Annotations: annotations[i:end], 124 | }, 125 | }) 126 | if err != nil { 127 | return err 128 | } 129 | } 130 | 131 | if errorCount > 0 { 132 | return fmt.Errorf(summary) 133 | } else { 134 | return nil 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/rkusa/eslint-action 2 | 3 | require ( 4 | github.com/google/go-github/v24 v24.0.0 5 | github.com/ldez/ghactions v1.0.0 6 | ) 7 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 2 | github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= 3 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 4 | github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= 5 | github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= 6 | github.com/google/go-github/v24 v24.0.0 h1:11gKs9XbwcUO5yEgg+GAbtg6z6CH7tK77wDM3mnAnXc= 7 | github.com/google/go-github/v24 v24.0.0/go.mod h1:CRqaW1Uns1TCkP0wqTpxYyRxRjxwvKU/XSS44u6X74M= 8 | github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= 9 | github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= 10 | github.com/ldez/ghactions v1.0.0 h1:XR1ptq/J3Imwa+wsqSn0wLcXZJxAHsrZHitfWCXrB5E= 11 | github.com/ldez/ghactions v1.0.0/go.mod h1:a3m3n/nCjcIvgXH4cSoqBKY5C8sQt0oQAIJgZcd6TDk= 12 | golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 13 | golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 14 | golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 15 | golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg= 16 | golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 17 | golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= 18 | golang.org/x/oauth2 v0.0.0-20190212230446-3e8b2be13635 h1:dOJmQysgY8iOBECuNp0vlKHWEtfiTnyjisEizRV3/4o= 19 | golang.org/x/oauth2 v0.0.0-20190212230446-3e8b2be13635/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= 20 | golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw= 21 | golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 22 | golang.org/x/sys v0.0.0-20180824143301-4910a1d54f87/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 23 | golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= 24 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 25 | google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= 26 | google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508= 27 | google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= 28 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rkusa/eslint-action/cc05733141aff2a2bc2bd0afdb373c74dfe82861/screenshot.png --------------------------------------------------------------------------------