Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require a cloud directory #320

Merged
merged 4 commits into from
Oct 3, 2023
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
32 changes: 21 additions & 11 deletions .github/workflows/ci-go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version-file: 'go.mod'
noamcattan marked this conversation as resolved.
Show resolved Hide resolved
- name: Run Go linters
uses: golangci/golangci-lint-action@v3
with:
Expand All @@ -24,7 +24,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version-file: 'go.mod'
- name: Run tests
run: go test -race ./...
integration-test:
Expand All @@ -35,35 +35,45 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.20'
- name: install GitHub cli
go-version-file: 'go.mod'
- name: Install GitHub cli
run: |
sudo apt update
sudo apt install gh -y
- name: validate pull request is not created yet
- name: Validate pull request is not created yet
run: |
PULL_REQUESTS=$(gh pr list -R ${{ secrets.TEST_REPO }} --search "'add atlas ci workflow' in:title" --json headRefName | jq length)
if [ $PULL_REQUESTS -ne 0 ]; then
echo "Pull request already created"
exit 1
fi
- name: build extension
- name: Build extension
run: |
go build
- name: install extension
- name: Install extension
run: |
gh extension install .
- name: run GitHub atlas extension
- name: Run GitHub atlas extension
run: |
gh atlas init-action -R ${{ secrets.TEST_REPO }} --token=${{ secrets.TEST_ATLAS_TOKEN }} --dir-name="test-sqlite" --driver="sqlite" "sqlite_dir"
- name: validate pull request is created
gh atlas init-action -R ${{ secrets.TEST_REPO }} --token=${{ secrets.TEST_ATLAS_TOKEN }} --dir-name="sync-action-integration-test" --driver="sqlite" "sqlite_dir"
- 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"
- name: Check invalid dir error
if : steps.invalid_dir.outcome == 'success'
run: |
echo "Invalid dir should fail"
exit 1
- name: Validate pull request is created
uses: nick-fields/retry@v2
with:
timeout_seconds: 20
retry_wait_seconds: 5
max_attempts: 4
command: PULL_REQUESTS=$(gh pr list -R ${{ secrets.TEST_REPO }} --search "'add atlas ci workflow' in:title" --json headRefName | jq length) && [ $PULL_REQUESTS -eq 1 ]
- name: cleanup the created pr, branch and secret
- name: Cleanup the created pr, branch and secret
if: always()
run: |
BRANCH=$(gh pr list -R ${{ secrets.TEST_REPO }} --search "'add atlas ci workflow' in:title" --json headRefName | jq -r '.[0].headRefName')
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ariga.io/gh-atlas

go 1.20
go 1.21

require (
github.com/1lann/promptui v0.8.1-0.20220708222609-81fad96dd5e1
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ github.com/1lann/promptui v0.8.1-0.20220708222609-81fad96dd5e1/go.mod h1:cnC/60I
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/agnivade/levenshtein v1.1.1/go.mod h1:veldBMzWxcCG2ZvUTKD2kJNRdCk5hVbJomOvKkmgYbo=
github.com/alecthomas/assert/v2 v2.1.0 h1:tbredtNcQnoSd3QBhQWI7QZ3XHOVkw1Moklp2ojoH/0=
github.com/alecthomas/assert/v2 v2.1.0/go.mod h1:b/+1DI2Q6NckYi+3mXyH3wFb8qG37K/DuK80n7WefXA=
github.com/alecthomas/kong v0.7.1 h1:azoTh0IOfwlAX3qN9sHWTxACE2oV8Bg2gAwBsMwDQY4=
github.com/alecthomas/kong v0.7.1/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U=
github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE=
github.com/alecthomas/repr v0.1.0/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
Expand Down Expand Up @@ -132,6 +135,7 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v49 v49.1.0 h1:LFkMgawGQ8dfzWLH/rNE0b3u1D3n6/dw7ZmrN3b+YFY=
github.com/google/go-github/v49 v49.1.0/go.mod h1:MUUzHPrhGniB6vUKa27y37likpipzG+BXXJbG04J334=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
Expand Down Expand Up @@ -161,11 +165,13 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/henvic/httpretty v0.0.6 h1:JdzGzKZBajBfnvlMALXXMVQWxWMF/ofTy8C3/OSUTxs=
github.com/henvic/httpretty v0.0.6/go.mod h1:X38wLjWXHkXT7r2+uK8LjCMne9rsuNaBLJ+5cU2/Pmo=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
Expand All @@ -187,6 +193,7 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.12.0 h1:KuQRUE3PgxRFWhq4gHvZtPSLCGDqM5q/cYr1pZ39ytc=
github.com/muesli/termenv v0.12.0/go.mod h1:WCCv32tusQ/EEZ5S8oUIIrC/nIuBcxCVqlN4Xfkv+7A=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
Expand Down Expand Up @@ -583,6 +590,7 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=
gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
16 changes: 10 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"log"
"math/rand"
"slices"

"github.com/alecthomas/kong"
"github.com/cli/go-gh"
Expand Down Expand Up @@ -106,15 +107,18 @@ func (i *InitActionCmd) Run(ctx context.Context, client *githubClient, current r
if err = cloud.ValidateToken(ctx); err != nil {
return errors.New("the given atlas token is invalid, please generate a new one and try again")
}
dirNames, err := cloud.DirNames(ctx)
if err != nil {
switch dirNames, err := cloud.DirNames(ctx); {
case err != nil:
return err
}
// If dir name is not set by the user, and there are dirs in the cloud, prompt the user to choose one.
if i.DirName == "" && len(dirNames) > 0 {
if err = i.setDirName(dirNames); err != nil {
case len(dirNames) == 0:
return errors.New("no migration directories found in your organization")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add a link to the docs here later

case i.DirName == "":
// If the dir name was not provided by the user, set it interactively.
if err := i.setDirName(dirNames); err != nil {
return err
}
case !slices.Contains(dirNames, i.DirName):
return fmt.Errorf("no migration directory with name %q found in your organization", i.DirName)
}
if err = repo.SetSecret(ctx, secretName, i.Token); err != nil {
return err
Expand Down
75 changes: 68 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ func createGHClient(repoSvc repositoriesService) *githubClient {

func TestRunInitActionCmd(t *testing.T) {
repo, err := repository.Parse("owner/repo")
dirsByToken := map[string][]string{
"token": {"name"},
"multiple": {"name", "name2"},
"empty": {},
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var (
input struct {
Expand All @@ -128,7 +133,10 @@ func TestRunInitActionCmd(t *testing.T) {
}
}
if strings.Contains(input.Query, "dirSlugs") {
payload.Data.DirSlugs = []string{"name"}
token, ok := strings.CutPrefix(r.Header.Get("Authorization"), "Bearer ")
require.True(t, ok)
payload.Data.DirSlugs, ok = dirsByToken[token]
require.True(t, ok)
}
body, err := json.Marshal(payload)
require.NoError(t, err)
Expand Down Expand Up @@ -192,19 +200,72 @@ func TestRunInitActionCmd(t *testing.T) {
},
},
{
name: "no dir name supplied dont use cloud",
name: "no dirs in organization",
client: createGHClient(&mockService{getContentError: &github.ErrorResponse{Message: "Not Found"}}),
cmd: &InitActionCmd{
DirPath: "migrations",
Driver: "mysql",
Token: "empty",
},
wantErr: true,
},
{
name: "dir name provided but doesn't exist in cloud",
client: createGHClient(&mockService{getContentError: &github.ErrorResponse{Message: "Not Found"}}),
cmd: &InitActionCmd{
DirPath: "migrations",
Driver: "mysql",
DirName: "invalid",
Token: "token",
},
wantErr: true,
},
{
name: "single dir in organization",
client: createGHClient(&mockService{getContentError: &github.ErrorResponse{Message: "Not Found"}}),
cmd: &InitActionCmd{
DirPath: "migrations",
Driver: "mysql",
Token: "token",
},
expected: &InitActionCmd{
DirPath: "migrations",
DirName: "name",
Driver: "mysql",
Token: "token",
},
},
{
name: "multiple dirs in organization",
client: createGHClient(&mockService{getContentError: &github.ErrorResponse{Message: "Not Found"}}),
cmd: &InitActionCmd{
DirPath: "migrations",
Driver: "mysql",
Token: "multiple",
},
// use arrow key down and then enter
prompt: "\x1b[B\n\n",
expected: &InitActionCmd{
DirPath: "migrations",
DirName: "name2",
Driver: "mysql",
Token: "token",
Token: "multiple",
},
},
{
name: "provide directory name",
client: createGHClient(&mockService{getContentError: &github.ErrorResponse{Message: "Not Found"}}),
cmd: &InitActionCmd{
DirPath: "migrations",
Driver: "mysql",
DirName: "name",
Token: "multiple",
},
expected: &InitActionCmd{
DirPath: "migrations",
DirName: "name",
Driver: "mysql",
Token: "multiple",
},
},
{
Expand All @@ -215,12 +276,12 @@ func TestRunInitActionCmd(t *testing.T) {
DirName: "name",
Driver: "mysql",
},
prompt: "my token\n",
prompt: "token\n",
expected: &InitActionCmd{
DirPath: "migrations",
DirName: "name",
Driver: "mysql",
Token: "my token",
Token: "token",
},
},
{
Expand All @@ -244,7 +305,7 @@ func TestRunInitActionCmd(t *testing.T) {
wantErr: true,
},
{
name: "file exists",
name: "ci file exists",
client: createGHClient(&mockService{}),
prompt: "my token\n",
cmd: &InitActionCmd{
Expand All @@ -256,7 +317,7 @@ func TestRunInitActionCmd(t *testing.T) {
wantErr: true,
},
{
name: "replace existing file",
name: "replace existing ci file",
client: createGHClient(&mockService{}),
prompt: "my token\n",
cmd: &InitActionCmd{
Expand Down
22 changes: 6 additions & 16 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,16 @@ func (i *InitActionCmd) setParams(dirs []string) error {
}

func (i *InitActionCmd) setDirName(names []string) error {
var err error
prompt := promptui.Select{
Label: "Choose action",
Items: []string{"Use existing directory from the Cloud", "Create new directory"},
Stdin: i.stdin,
}
_, rsp, err := prompt.Run()
if err != nil {
return err
}
if rsp == "Create new directory" {
if len(names) == 1 {
i.DirName = names[0]
return nil
}
prompt = promptui.Select{
var err error
prompt := promptui.Select{
Label: "Choose name of cloud migration directory",
Items: names,
Stdin: i.stdin,
}
if _, i.DirName, err = prompt.Run(); err != nil {
return err
}
return nil
_, i.DirName, err = prompt.Run()
return err
}
Loading