Skip to content

Commit

Permalink
support passing atlas config file into ci-atlas yaml (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenlu authored Dec 25, 2023
1 parent 13bec2b commit 5275927
Show file tree
Hide file tree
Showing 12 changed files with 456 additions and 76 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci-go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ jobs:
gh extension install .
- name: Run GitHub atlas extension
run: |
gh atlas init-action -R ${{ secrets.TEST_REPO }} --token=${{ secrets.TEST_ATLAS_TOKEN }} --dir-name="sync-action-integration-test" --driver="sqlite" "sqlite_dir"
gh atlas init-action -R ${{ secrets.TEST_REPO }} --token=${{ secrets.TEST_ATLAS_TOKEN }} --dir-name="sync-action-integration-test" --driver="sqlite" "sqlite_dir" \
--config-path="atlas.hcl" --config-env="local"
- name: Run GitHub atlas extension with invalid dir
id: invalid_dir
continue-on-error: true
run: |
gh atlas init-action -R ${{ secrets.TEST_REPO }} --token=${{ secrets.TEST_ATLAS_TOKEN }} --dir-name="invalid" --driver="sqlite" "sqlite_dir"
gh atlas init-action -R ${{ secrets.TEST_REPO }} --token=${{ secrets.TEST_ATLAS_TOKEN }} --dir-name="invalid" --driver="sqlite" "sqlite_dir" \
--config-path="atlas.hcl" --config-env="local"
- name: Check invalid dir error
if : steps.invalid_dir.outcome == 'success'
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/gh-atlas
/gh-atlas.exe
.idea/
18 changes: 18 additions & 0 deletions gen/atlas.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ permissions:
pull-requests: write
jobs:
atlas:
{{- if .CreateDevURL }}
{{- template "services" . }}
{{- end }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -30,7 +32,15 @@ jobs:
{{- with .DirName }}
dir-name: '{{ . }}'
{{- end }}
{{- if .CreateDevURL }}
{{ template "UseServices" . }}
{{- end }}
{{- with .ConfigPath }}
config: 'file://{{ . }}'
{{- end }}
{{- with .Env }}
env: '{{ . }}'
{{- end }}
env:
GITHUB_TOKEN: {{`${{ github.token }}`}}
- uses: ariga/atlas-action/migrate/push@v1
Expand All @@ -40,4 +50,12 @@ jobs:
{{- with .DirName }}
dir-name: '{{ . }}'
{{- end }}
{{- if .CreateDevURL }}
{{ template "UseServices" . }}
{{- end }}
{{- with .ConfigPath }}
config: 'file://{{ . }}'
{{- end }}
{{- with .Env }}
env: '{{ . }}'
{{- end }}
4 changes: 3 additions & 1 deletion gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ type (
SecretName string
DefaultBranch string
Driver string
Services string
ConfigPath string
Env string
CreateDevURL bool
}
)

Expand Down
10 changes: 9 additions & 1 deletion gen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@ func TestGen(t *testing.T) {
t.Run(f.Name(), func(t *testing.T) {
expected, err := os.ReadFile("testdata/" + f.Name())
require.NoError(t, err)
name := strings.TrimSuffix(f.Name(), ".yml")
cfg := &Config{
Path: "migrations",
DirName: "name",
DefaultBranch: "master",
SecretName: "ATLAS_CLOUD_TOKEN",
Driver: strings.TrimSuffix(f.Name(), ".yml"),
Driver: name,
CreateDevURL: true,
}
if strings.Contains(name, "atlas_config") {
cfg.Driver = strings.Split(name, "_")[0]
cfg.ConfigPath = "atlas.hcl"
cfg.Env = "dev"
cfg.CreateDevURL = false
}
actual, err := Generate(cfg)
require.NoError(t, err)
Expand Down
40 changes: 40 additions & 0 deletions gen/testdata/postgres_atlas_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Atlas
on:
push:
branches:
- master
paths:
- .github/workflows/ci-atlas.yaml
- 'migrations/*'
pull_request:
paths:
- 'migrations/*'
# Permissions to write comments on the pull request.
permissions:
contents: read
pull-requests: write
jobs:
atlas:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: ariga/setup-atlas@v0
with:
cloud-token: ${{ secrets.ATLAS_CLOUD_TOKEN }}
- uses: ariga/atlas-action/migrate/lint@v1
with:
dir: 'file://migrations'
dir-name: 'name'
config: 'file://atlas.hcl'
env: 'dev'
env:
GITHUB_TOKEN: ${{ github.token }}
- uses: ariga/atlas-action/migrate/push@v1
if: github.ref == 'refs/heads/master'
with:
dir: 'file://migrations'
dir-name: 'name'
config: 'file://atlas.hcl'
env: 'dev'
24 changes: 24 additions & 0 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,27 @@ func (r *Repository) MigrationDirectories(ctx context.Context) ([]string, error)
}
return paths, nil
}

// ConfigFiles returns a list of paths to atlas.hcl files in the repository.
func (r *Repository) ConfigFiles(ctx context.Context) ([]string, error) {
t, _, err := r.client.Git.GetTree(ctx, r.owner, r.name, r.defaultBranch, true)
if err != nil {
return nil, err
}
var paths []string
for _, e := range t.Entries {
if e.GetType() == "blob" && strings.HasSuffix(e.GetPath(), "atlas.hcl") {
paths = append(paths, e.GetPath())
}
}
return paths, nil
}

// ReadContent of the file at the given path.
func (r *Repository) ReadContent(ctx context.Context, path string) (string, error) {
fileContents, _, _, err := r.client.Repositories.GetContents(ctx, r.owner, r.name, path, nil)
if err != nil {
return "", err
}
return fileContents.GetContent()
}
43 changes: 25 additions & 18 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,43 @@ go 1.21

require (
github.com/1lann/promptui v0.8.1-0.20220708222609-81fad96dd5e1
github.com/alecthomas/kong v0.7.1
github.com/alecthomas/kong v0.8.1
github.com/cli/go-gh v1.2.1
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-github/v49 v49.1.0
github.com/google/go-replayers/httpreplay v1.2.0
github.com/hashicorp/hcl/v2 v2.19.1
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.2
github.com/vektah/gqlparser/v2 v2.5.6
golang.org/x/crypto v0.6.0
github.com/stretchr/testify v1.8.4
github.com/vektah/gqlparser/v2 v2.5.10
golang.org/x/crypto v0.17.0
)

require (
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/cli/safeexec v1.0.0 // indirect
github.com/cli/shurcooL-graphql v0.0.2 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/cli/safeexec v1.0.1 // indirect
github.com/cli/shurcooL-graphql v0.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/martian/v3 v3.3.2 // indirect
github.com/henvic/httpretty v0.0.6 // indirect
github.com/henvic/httpretty v0.1.3 // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/muesli/termenv v0.12.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.8.0 // indirect
github.com/zclconf/go-cty v1.14.1 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 5275927

Please sign in to comment.