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

atlasaction: update test for lint with GitHub context #296

Merged
merged 1 commit into from
Jan 5, 2025
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: 30 additions & 2 deletions atlasaction/circleci_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package atlasaction_test

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -72,10 +74,36 @@ func TestCircleCI(t *testing.T) {
if err := os.Mkdir(dir, 0700); err != nil {
return err
}
m := http.NewServeMux()
m.Handle("GET /repos/{owner}/{repo}/pulls", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.NoError(t, json.NewEncoder(w).Encode([]struct {
URL string `json:"url"`
Number int `json:"number"`
}{
{
Number: 1,
URL: fmt.Sprintf("https://github.com/%s/%s/pull/1",
r.PathValue("owner"), r.PathValue("repo")),
},
}))
}))
m.Handle("GET /repos/{owner}/{repo}/issues/{num}/comments", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// No comments
w.Write([]byte(`[]`))
}))
m.Handle("POST /repos/{owner}/{repo}/issues/{num}/comments", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Created comment
w.WriteHeader(http.StatusCreated)
}))
m.Handle("GET /repos/{owner}/{repo}/pulls/{num}/files", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// No files
w.Write([]byte(`[]`))
}))
srv := httptest.NewServer(m)
e.Defer(srv.Close)
e.Setenv("MOCK_ATLAS", filepath.Join(wd, "mock-atlas.sh"))
e.Setenv("CIRCLECI", "true")
e.Setenv("CIRCLE_PROJECT_REPONAME", "atlas-orb")
e.Setenv("CIRCLE_SHA1", "1234567890")
e.Setenv("GITHUB_API_URL", srv.URL)
e.Setenv("BASH_ENV", filepath.Join(dir, "output.sh"))
return nil
},
Expand Down
118 changes: 55 additions & 63 deletions atlasaction/gitlab_ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"testing"

"ariga.io/atlas-action/internal/gitlab"
"github.com/gorilla/mux"
"github.com/rogpeppe/go-internal/testscript"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -58,71 +57,64 @@ func TestGitlabCI(t *testing.T) {

func mockClientHandler(dir, token string) http.Handler {
counter := 1
r := mux.NewRouter()
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if t := r.Header.Get("PRIVATE-TOKEN"); t != token {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
})
})
r.Methods(http.MethodGet).Path("/projects/{project}/merge_requests/{mr}/notes").
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
entries, err := os.ReadDir(dir)
m := http.NewServeMux()
m.HandleFunc("GET /projects/{project}/merge_requests/{mr}/notes", func(w http.ResponseWriter, r *http.Request) {
entries, err := os.ReadDir(dir)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
comments := make([]*gitlab.Note, len(entries))
for i, e := range entries {
b, err := os.ReadFile(filepath.Join(dir, e.Name()))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
comments := make([]*gitlab.Note, len(entries))
for i, e := range entries {
b, err := os.ReadFile(filepath.Join(dir, e.Name()))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
id, err := strconv.Atoi(e.Name())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
comments[i] = &gitlab.Note{ID: id, Body: string(b)}
}
if err = json.NewEncoder(w).Encode(comments); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
r.Methods(http.MethodPost).Path("/projects/{project}/merge_requests/{mr}/notes").
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var body struct {
Body string `json:"body"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := os.WriteFile(filepath.Join(dir, strconv.Itoa(counter)), []byte(body.Body+"\n"), 0666); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
counter++
w.WriteHeader(http.StatusCreated)
})
r.Methods(http.MethodPut).Path("/projects/{project}/merge_requests/{mr}/notes/{note}").
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
if _, err := os.Stat(filepath.Join(dir, vars["note"])); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
var body struct {
Body string `json:"body"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := os.WriteFile(filepath.Join(dir, vars["note"]), []byte(body.Body+"\n"), 0666); err != nil {
id, err := strconv.Atoi(e.Name())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
return r
comments[i] = &gitlab.Note{ID: id, Body: string(b)}
}
if err = json.NewEncoder(w).Encode(comments); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
m.HandleFunc("POST /projects/{project}/merge_requests/{mr}/notes", func(w http.ResponseWriter, r *http.Request) {
var body struct {
Body string `json:"body"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := os.WriteFile(filepath.Join(dir, strconv.Itoa(counter)), []byte(body.Body+"\n"), 0666); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
counter++
w.WriteHeader(http.StatusCreated)
})
m.HandleFunc("PUT /projects/{project}/merge_requests/{mr}/notes/{note}", func(w http.ResponseWriter, r *http.Request) {
if _, err := os.Stat(filepath.Join(dir, r.PathValue("note"))); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
var body struct {
Body string `json:"body"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := os.WriteFile(filepath.Join(dir, r.PathValue("note")), []byte(body.Body+"\n"), 0666); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if t := r.Header.Get("PRIVATE-TOKEN"); t != token {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
m.ServeHTTP(w, r)
})
}
5 changes: 4 additions & 1 deletion atlasaction/testdata/circleci/migrate-lint.txtar
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Mock the atlas command outputs
env ATLAS_PATH=$MOCK_ATLAS TEST_BATCH=./migrate-lint
# Run context
env CIRCLE_PROJECT_REPONAME=atlas-orb CIRCLE_SHA1=1234567890 CIRCLE_BRANCH=master
env GITHUB_TOKEN=foo GITHUB_REPOSITORY=foo/bar
# Setup the action input variables
env ATLAS_INPUT_CONFIG=file://testdata/config/atlas.hcl
env ATLAS_INPUT_ENV=test
Expand All @@ -16,7 +19,7 @@ atlas-action --action=migrate/lint
output output.sh

-- migrate-lint/1/args --
migrate lint -w --context {"repo":"atlas-orb","path":"file://testdata/migrations","commit":"1234567890"} --env test --config file://testdata/config/atlas.hcl --dev-url sqlite://file?mode=memory --dir file://testdata/migrations --base atlas://pupisu?tag=staging --var var1=value1 --var var2=value2 --format {{ json . }}
migrate lint -w --context {"repo":"foo/bar","path":"file://testdata/migrations","branch":"master","commit":"1234567890","url":"https://github.com/foo/bar/pull/1","scmType":"GITHUB"} --env test --config file://testdata/config/atlas.hcl --dev-url sqlite://file?mode=memory --dir file://testdata/migrations --base atlas://pupisu?tag=staging --var var1=value1 --var var2=value2 --format {{ json . }}
-- migrate-lint/1/stdout --
{"URL":"https://migration-lint-report-url"}
-- output-pre.sh --
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
ariga.io/atlas-go-sdk v0.6.5
github.com/alecthomas/kong v0.8.0
github.com/fatih/color v1.17.0
github.com/gorilla/mux v1.8.1
github.com/hashicorp/go-retryablehttp v0.7.7
github.com/mattn/go-sqlite3 v1.14.17
github.com/mitchellh/mapstructure v1.1.2
github.com/rogpeppe/go-internal v1.12.1-0.20240709150035-ccf4b4329d21
Expand All @@ -24,7 +24,6 @@ require (
github.com/go-openapi/inflect v0.19.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/hashicorp/hcl/v2 v2.18.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k=
github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU=
github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk=
github.com/hashicorp/hcl/v2 v2.18.1 h1:6nxnOJFku1EuSawSD81fuviYUV8DxFr3fp2dUi3ZYSo=
Expand Down
Loading