diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 0e06201..5c13d17 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -7,194 +7,36 @@ on: branches: [main] jobs: - test-pull-request: + list-files: runs-on: ubuntu-latest + outputs: + files: ${{env.FILES}} steps: - uses: actions/checkout@v3 - - uses: ./ + - uses: actions/setup-go@v3 with: - data: | - { - "event_name": "pull_request", - "event": { - "number": 149, - "sender": { - "login": "suzuki-shunsuke" - }, - "repository": { - "full_name": "gha-trigger/gha-trigger", - "name": "gha-trigger", - "owner": { - "login": "gha-trigger" - } - }, - "pull_request": { - "base": { - "ref": "main" - }, - "head": { - "sha": "b6d8f3331c6845997b9408163a127e01fd32c120", - "ref": "renovate/github.com-aws-aws-sdk-go-1.x" - }, - "html_url": "https://github.com/gha-trigger/step-summary-action/pull/9" - } - }, - "pull_request": { - "merge_commit_sha": "4528ea7ab7a8eef7e596097934a78eab90fb8b33", - "base": { - "ref": "main" - }, - "head": { - "sha": "b6d8f3331c6845997b9408163a127e01fd32c120", - "ref": "renovate/github.com-aws-aws-sdk-go-1.x" - }, - "html_url": "https://github.com/gha-trigger/step-summary-action/pull/9" - } - } - - name: Test GHA_ACTOR - run: | - [ "$GHA_ACTOR" = suzuki-shunsuke ] - - name: Test GHA_EVENT_NAME - run: | - [ "$GHA_EVENT_NAME" = pull_request ] - - name: Test GHA_REPOSITORY - run: | - [ "$GHA_REPOSITORY" = gha-trigger/gha-trigger ] - - name: Test GHA_REPOSITORY_OWNER - run: | - [ "$GHA_REPOSITORY_OWNER" = gha-trigger ] - - name: Test GHA_REPOSITORY_NAME - run: | - [ "$GHA_REPOSITORY_NAME" = gha-trigger ] - - name: Test GHA_SHA - run: | - [ "$GHA_SHA" = 4528ea7ab7a8eef7e596097934a78eab90fb8b33 ] - - name: Test GHA_COMMIT_STATUS_SHA - run: | - [ "$GHA_COMMIT_STATUS_SHA" = b6d8f3331c6845997b9408163a127e01fd32c120 ] - - name: Test GHA_BASE_REF - run: | - [ "$GHA_BASE_REF" = main ] - - name: Test GHA_HEAD_SHA - run: | - [ "$GHA_HEAD_SHA" = b6d8f3331c6845997b9408163a127e01fd32c120 ] - - name: Test GHA_REF - run: | - [ "$GHA_REF" = refs/pull/149/merge ] - - name: Test GHA_REF_NAME - run: | - [ "$GHA_REF_NAME" = 149/merge ] - - name: Test GHA_PULL_REQUEST_NUMBER - run: | - [ "$GHA_PULL_REQUEST_NUMBER" = 149 ] - - name: Test GHA_ENV - run: | - [ -n "$GHA_ENV" ] - test-push: + go-version: "1.19.1" + cache: true + - run: go run ./cmd/list + + test: runs-on: ubuntu-latest + name: "test (${{matrix.file}})" + needs: list-files + env: + FILE: ${{matrix.file}} + strategy: + fail-fast: false + matrix: + file: ${{fromJSON(needs.list-files.outputs.files)}} steps: - uses: actions/checkout@v3 - - uses: ./ + - uses: actions/setup-go@v3 with: - data: | - { - "event_name": "push", - "event": { - "ref": "refs/heads/renovate/github.com-aws-aws-sdk-go-1.x", - "sender": { - "login": "suzuki-shunsuke" - }, - "head_commit": { - "id": "a705bd6d8bc3882c983477cb3f20ab74ef8c24c2" - }, - "repository": { - "full_name": "gha-trigger/gha-trigger", - "name": "gha-trigger", - "owner": { - "login": "gha-trigger" - } - } - } - } - - name: Test GHA_ACTOR - run: | - [ "$GHA_ACTOR" = suzuki-shunsuke ] - - name: Test GHA_EVENT_NAME - run: | - [ "$GHA_EVENT_NAME" = push ] - - name: Test GHA_REPOSITORY - run: | - [ "$GHA_REPOSITORY" = gha-trigger/gha-trigger ] - - name: Test GHA_REPOSITORY_OWNER - run: | - [ "$GHA_REPOSITORY_OWNER" = gha-trigger ] - - name: Test GHA_REPOSITORY_NAME - run: | - [ "$GHA_REPOSITORY_NAME" = gha-trigger ] - - name: Test GHA_SHA - run: | - [ "$GHA_SHA" = a705bd6d8bc3882c983477cb3f20ab74ef8c24c2 ] - - name: Test GHA_COMMIT_STATUS_SHA - run: | - [ "$GHA_COMMIT_STATUS_SHA" = a705bd6d8bc3882c983477cb3f20ab74ef8c24c2 ] - - name: Test GHA_REF - run: | - [ "$GHA_REF" = refs/heads/renovate/github.com-aws-aws-sdk-go-1.x ] - - name: Test GHA_REF_NAME - run: | - [ "$GHA_REF_NAME" = renovate/github.com-aws-aws-sdk-go-1.x ] - - name: Test GHA_ENV - run: | - [ -n "$GHA_ENV" ] - test-release: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 + go-version: "1.19.1" + cache: true + - run: go run ./cmd/setter "$FILE" - uses: ./ with: - data: | - { - "event_name": "release", - "event": { - "sender": { - "login": "suzuki-shunsuke" - }, - "release": { - "tag_name": "v0.1.0" - }, - "repository": { - "full_name": "gha-trigger/gha-trigger", - "name": "gha-trigger", - "owner": { - "login": "gha-trigger" - } - } - } - } - - name: Test GHA_ACTOR - run: | - [ "$GHA_ACTOR" = suzuki-shunsuke ] - - name: Test GHA_EVENT_NAME - run: | - [ "$GHA_EVENT_NAME" = release ] - - name: Test GHA_REPOSITORY - run: | - [ "$GHA_REPOSITORY" = gha-trigger/gha-trigger ] - - name: Test GHA_REPOSITORY_OWNER - run: | - [ "$GHA_REPOSITORY_OWNER" = gha-trigger ] - - name: Test GHA_REPOSITORY_NAME - run: | - [ "$GHA_REPOSITORY_NAME" = gha-trigger ] - - name: Test GHA_SHA - run: | - [ "$GHA_SHA" = "" ] - - name: Test GHA_REF - run: | - [ "$GHA_REF" = v0.1.0 ] - - name: Test GHA_REF_NAME - run: | - [ "$GHA_REF_NAME" = v0.1.0 ] - - name: Test GHA_ENV - run: | - [ -n "$GHA_ENV" ] + data: ${{toJSON(fromJSON(env.DATA).data)}} + - run: go run ./cmd/tester "$FILE" diff --git a/cmd/list/main.go b/cmd/list/main.go new file mode 100644 index 0000000..477fa85 --- /dev/null +++ b/cmd/list/main.go @@ -0,0 +1,29 @@ +package main + +import ( + "encoding/json" + "log" + "path/filepath" + + "github.com/sethvargo/go-githubactions" +) + +func main() { + if err := core(); err != nil { + log.Fatal(err) + } +} + +func core() error { + files, err := filepath.Glob("testdata/*/*") + if err != nil { + return err + } + b, err := json.Marshal(files) + if err != nil { + return err + } + + githubactions.SetEnv("FILES", string(b)) + return nil +} diff --git a/cmd/setter/main.go b/cmd/setter/main.go new file mode 100644 index 0000000..ff37154 --- /dev/null +++ b/cmd/setter/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "encoding/json" + "log" + "os" + + "github.com/sethvargo/go-githubactions" + "gopkg.in/yaml.v3" +) + +func main() { + if err := core(); err != nil { + log.Fatal(err) + } +} + +type Config struct { + Envs map[string]string + Data interface{} +} + +func core() error { + // read config file + // test envs + file := os.Args[1] + cfg := &Config{} + f, err := os.Open(file) + if err != nil { + return err + } + defer f.Close() + if err := yaml.NewDecoder(f).Decode(cfg); err != nil { + return err + } + + b, err := json.Marshal(cfg) + if err != nil { + return err + } + + githubactions.SetEnv("DATA", string(b)) + return nil +} diff --git a/cmd/tester/main.go b/cmd/tester/main.go new file mode 100644 index 0000000..314ba98 --- /dev/null +++ b/cmd/tester/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "errors" + "log" + "os" + + "gopkg.in/yaml.v3" +) + +func main() { + if err := core(); err != nil { + log.Fatal(err) + } +} + +type Config struct { + Envs map[string]string + Data interface{} +} + +func core() error { + // read config file + // test envs + file := os.Args[1] + cfg := &Config{} + f, err := os.Open(file) + if err != nil { + return err + } + defer f.Close() + if err := yaml.NewDecoder(f).Decode(cfg); err != nil { + return err + } + failed := false + for envName, expValue := range cfg.Envs { + actValue := os.Getenv(envName) + if expValue != actValue { + log.Printf("[ERROR] the environment variable %s is wrong: wanted %s, got %s", envName, expValue, actValue) + failed = true + } + } + if failed { + return errors.New("[ERROR] Some environment variables are wrong") + } + return nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ad34a8f --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module github.com/gha-trigger/set-env-action + +go 1.19 + +require ( + github.com/sethvargo/go-githubactions v1.0.0 + gopkg.in/yaml.v3 v3.0.1 +) + +require github.com/sethvargo/go-envconfig v0.6.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..36f579c --- /dev/null +++ b/go.sum @@ -0,0 +1,9 @@ +github.com/google/go-cmp v0.4.1 h1:/exdXoGamhu5ONeUJH0deniYLWYvQwW66yvlfiiKTu0= +github.com/sethvargo/go-envconfig v0.6.0 h1:GxxdoeiNpWgGiVEphNFNObgMYRN/ZvI2dN7rBwadyss= +github.com/sethvargo/go-envconfig v0.6.0/go.mod h1:00S1FAhRUuTNJazWBWcJGvEHOM+NO6DhoRMAOX7FY5o= +github.com/sethvargo/go-githubactions v1.0.0 h1:5mYGPNxIwIXaS8MLj4uYGWM8QM8giUVqA4FuSYOZjXE= +github.com/sethvargo/go-githubactions v1.0.0/go.mod h1:UaidDD1ENTLXzTtj/4MnYjY40/5WLijgn2O8KBsdv7o= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/testdata/pull_request/normal.yaml b/testdata/pull_request/normal.yaml new file mode 100644 index 0000000..3b66f75 --- /dev/null +++ b/testdata/pull_request/normal.yaml @@ -0,0 +1,31 @@ +envs: + GHA_ACTOR: suzuki-shunsuke + GHA_EVENT_NAME: pull_request + GHA_REPOSITORY: gha-trigger/gha-trigger + GHA_REPOSITORY_OWNER: gha-trigger + GHA_REPOSITORY_NAME: gha-trigger + GHA_SHA: 4528ea7ab7a8eef7e596097934a78eab90fb8b33 + GHA_COMMIT_STATUS_SHA: b6d8f3331c6845997b9408163a127e01fd32c120 + GHA_BASE_REF: main + GHA_HEAD_SHA: b6d8f3331c6845997b9408163a127e01fd32c120 + GHA_REF: refs/pull/149/merge + GHA_REF_NAME: 149/merge + GHA_PULL_REQUEST_NUMBER: "149" +data: + event_name: pull_request + event: + number: 149 + sender: + login: suzuki-shunsuke + repository: + full_name: gha-trigger/gha-trigger + name: gha-trigger + owner: + login: gha-trigger + pull_request: + merge_commit_sha: 4528ea7ab7a8eef7e596097934a78eab90fb8b33 + base: + ref: main + head: + sha: b6d8f3331c6845997b9408163a127e01fd32c120 + ref: renovate/github.com-aws-aws-sdk-go-1.x diff --git a/testdata/push/normal.yaml b/testdata/push/normal.yaml new file mode 100644 index 0000000..1b90c92 --- /dev/null +++ b/testdata/push/normal.yaml @@ -0,0 +1,24 @@ +envs: + GHA_ACTOR: suzuki-shunsuke + GHA_EVENT_NAME: push + GHA_REPOSITORY: gha-trigger/gha-trigger + GHA_REPOSITORY_OWNER: gha-trigger + GHA_REPOSITORY_NAME: gha-trigger + GHA_SHA: a705bd6d8bc3882c983477cb3f20ab74ef8c24c2 + GHA_COMMIT_STATUS_SHA: a705bd6d8bc3882c983477cb3f20ab74ef8c24c2 + GHA_REF: refs/heads/renovate/github.com-aws-aws-sdk-go-1.x + GHA_REF_NAME: renovate/github.com-aws-aws-sdk-go-1.x + +data: + event_name: push + event: + ref: refs/heads/renovate/github.com-aws-aws-sdk-go-1.x + sender: + login: suzuki-shunsuke + head_commit: + id: a705bd6d8bc3882c983477cb3f20ab74ef8c24c2 + repository: + full_name: gha-trigger/gha-trigger + name: gha-trigger + owner: + login: gha-trigger diff --git a/testdata/release/normal.yaml b/testdata/release/normal.yaml new file mode 100644 index 0000000..9f33075 --- /dev/null +++ b/testdata/release/normal.yaml @@ -0,0 +1,23 @@ +envs: + GHA_ACTOR: suzuki-shunsuke + GHA_EVENT_NAME: release + GHA_REPOSITORY: gha-trigger/gha-trigger + GHA_REPOSITORY_OWNER: gha-trigger + GHA_REPOSITORY_NAME: gha-trigger + GHA_SHA: + GHA_COMMIT_STATUS_SHA: + GHA_REF: v0.1.0 + GHA_REF_NAME: v0.1.0 + +data: + event_name: release + event: + sender: + login: suzuki-shunsuke + release: + tag_name: v0.1.0 + repository: + full_name: gha-trigger/gha-trigger + name: gha-trigger + owner: + login: gha-trigger