Skip to content
This repository has been archived by the owner on Nov 13, 2023. It is now read-only.

ci: fix test #19

Merged
merged 8 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions .github/workflows/actionlint.yaml

This file was deleted.

14 changes: 0 additions & 14 deletions .github/workflows/prettier.yaml

This file was deleted.

22 changes: 0 additions & 22 deletions .github/workflows/renovate-config-validator.yaml

This file was deleted.

14 changes: 7 additions & 7 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
list-files:
runs-on: ubuntu-latest
outputs:
files: ${{env.FILES}}
dirs: ${{env.DIRS}}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
Expand All @@ -21,22 +21,22 @@ jobs:

test:
runs-on: ubuntu-latest
name: "test (${{matrix.file}})"
name: "test (${{matrix.dir}})"
needs: list-files
env:
FILE: ${{matrix.file}}
DIR: ${{matrix.dir}}
strategy:
fail-fast: false
matrix:
file: ${{fromJSON(needs.list-files.outputs.files)}}
dir: ${{fromJSON(needs.list-files.outputs.dirs)}}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: "1.19.1"
cache: true
- run: go run ./cmd/setter "$FILE"
- run: go run ./cmd/setter "$DIR"
- uses: ./
with:
data: ${{toJSON(fromJSON(env.DATA).data)}}
- run: go run ./cmd/tester "$FILE"
data: ${{env.DATA}}
- run: go run ./cmd/tester "$DIR"
38 changes: 37 additions & 1 deletion action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ runs:
GHA_SHA=${{fromJSON(inputs.data).event.sha}}
GHA_COMMIT_STATUS_SHA=${{fromJSON(inputs.data).event.sha}}
GHA_EVENT_PATH=$event_path
GHA_REF=refs/heads/${{fromJSON(inputs.data).event.repository.default_branch}}
GHA_REF_NAME=${{fromJSON(inputs.data).event.repository.default_branch}}
EOS
env:
INPUTS_EVENT: ${{toJSON(fromJSON(inputs.data).event)}}
Expand Down Expand Up @@ -64,7 +66,7 @@ runs:
# > For workflows triggered by release, this is the release tag created.
run: |
cat << EOS >> "$GITHUB_ENV"
GHA_REF=${{fromJSON(inputs.data).event.release.tag_name}}
GHA_REF=refs/tags/${{fromJSON(inputs.data).event.release.tag_name}}
GHA_REF_NAME=${{fromJSON(inputs.data).event.release.tag_name}}
EOS
if: env.GHA_EVENT_NAME == 'release'
Expand Down Expand Up @@ -99,6 +101,40 @@ runs:
EOS
if: "env.GHA_EVENT_NAME == 'push' && startsWith(env.GHA_REF, 'refs/tags/')"

- shell: bash
run: |
cat << EOS >> "$GITHUB_ENV"
GHA_REF_NAME=${{fromJSON(inputs.data).event.ref}}
EOS
if: contains(fromJSON('["create", "delete"]'), env.GHA_EVENT_NAME)

- shell: bash
run: |
cat << EOS >> "$GITHUB_ENV"
GHA_REF=refs/heads/${{fromJSON(inputs.data).event.ref}}
EOS
if: |
contains(fromJSON('["create", "delete"]'), env.GHA_EVENT_NAME) && fromJSON(inputs.data).event.ref_type == 'branch'

- shell: bash
run: |
cat << EOS >> "$GITHUB_ENV"
GHA_REF=refs/tags/${{fromJSON(inputs.data).event.ref}}
EOS
if: |
contains(fromJSON('["create", "delete"]'), env.GHA_EVENT_NAME) && fromJSON(inputs.data).event.ref_type == 'tag'

- shell: bash
run: |
# TODO GITHUB_REF: Branch or tag to be deployed (empty if created with a commit SHA)
cat << EOS >> "$GITHUB_ENV"
GHA_SHA=${{fromJSON(inputs.data).event.deployment.sha}}
GHA_COMMIT_STATUS_SHA=${{fromJSON(inputs.data).event.deployment.sha}}
GHA_REF=refs/heads/${{fromJSON(inputs.data).event.deployment.ref}}
GHA_REF_NAME=${{fromJSON(inputs.data).event.deployment.ref}}
EOS
if: env.GHA_EVENT_NAME == 'deployment' || env.GHA_EVENT_NAME == 'deployment_status'

# TODO GITHUB_REF
# > The branch or tag ref that triggered the workflow run.
# > For other triggers, this is the branch or tag ref that triggered the workflow run.
Expand Down
8 changes: 0 additions & 8 deletions aqua.yaml

This file was deleted.

11 changes: 8 additions & 3 deletions cmd/list/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ func main() {
}

func core() error {
files, err := filepath.Glob("testdata/*/*")
envFiles, err := filepath.Glob("testdata/*/*/envs.yaml")
if err != nil {
return err
}
b, err := json.Marshal(files)
dirs := make([]string, len(envFiles))
for i, envFile := range envFiles {
dirs[i] = filepath.Dir(envFile)
}

b, err := json.Marshal(dirs)
if err != nil {
return err
}

githubactions.SetEnv("FILES", string(b))
githubactions.SetEnv("DIRS", string(b))
return nil
}
33 changes: 22 additions & 11 deletions cmd/setter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"log"
"os"
"path/filepath"

"github.com/sethvargo/go-githubactions"
"gopkg.in/yaml.v3"
Expand All @@ -15,26 +16,36 @@ func main() {
}
}

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)
dir := os.Args[1]

dataFile, err := os.Open(filepath.Join(dir, "data.yaml"))
if err != nil {
return err
}
defer f.Close()
if err := yaml.NewDecoder(f).Decode(cfg); err != nil {
defer dataFile.Close()
data := map[string]interface{}{}
if err := yaml.NewDecoder(dataFile).Decode(&data); err != nil {
return err
}

eventFile, err := os.Open(filepath.Join(dir, "event.yaml"))
if err != nil {
eventFile, err = os.Open(filepath.Join(dir, "event.json"))
if err != nil {
return err
}
}
defer eventFile.Close()
event := map[string]interface{}{}
if err := yaml.NewDecoder(eventFile).Decode(&event); err != nil {
return err
}
data["event"] = event

b, err := json.Marshal(cfg)
b, err := json.Marshal(data)
if err != nil {
return err
}
Expand Down
16 changes: 6 additions & 10 deletions cmd/tester/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"log"
"os"
"path/filepath"

"gopkg.in/yaml.v3"
)
Expand All @@ -14,26 +15,21 @@ func main() {
}
}

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)
dir := os.Args[1]
envs := map[string]string{}
f, err := os.Open(filepath.Join(dir, "envs.yaml"))
if err != nil {
return err
}
defer f.Close()
if err := yaml.NewDecoder(f).Decode(cfg); err != nil {
if err := yaml.NewDecoder(f).Decode(&envs); err != nil {
return err
}
failed := false
for envName, expValue := range cfg.Envs {
for envName, expValue := range envs {
actValue := os.Getenv(envName)
if expValue != actValue {
log.Printf("[ERROR] the environment variable %s is wrong: wanted %s, got %s", envName, expValue, actValue)
Expand Down
1 change: 1 addition & 0 deletions testdata/branch_protection_rule/normal/data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
event_name: branch_protection_rule
9 changes: 9 additions & 0 deletions testdata/branch_protection_rule/normal/envs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
GHA_ACTOR: Codertocat
GHA_EVENT_NAME: branch_protection_rule
GHA_REPOSITORY: octo-org/octo-repo
GHA_REPOSITORY_OWNER: octo-org
GHA_REPOSITORY_NAME: octo-repo
GHA_SHA:
GHA_COMMIT_STATUS_SHA:
GHA_REF: refs/heads/main
GHA_REF_NAME: main
Loading