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: scm is working independent with the actions #275

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 24 additions & 4 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type (
Version string
Atlas AtlasExec
CloudClient func(string, string, *atlasexec.Version) CloudClient
Getenv func(string) string
}

// Action interface for Atlas.
Expand All @@ -53,8 +54,6 @@ type (
GetTriggerContext() (*TriggerContext, error)
// AddStepSummary adds a summary to the action step.
AddStepSummary(string)
// SCM returns a SCMClient.
SCM() (SCMClient, error)
}

// SCMClient contains methods for interacting with SCM platforms (GitHub, Gitlab etc...).
Expand Down Expand Up @@ -194,6 +193,7 @@ func New(opts ...Option) (*Actions, error) {
Atlas: cfg.atlas,
CloudClient: cfg.cloudClient,
Version: cfg.version,
Getenv: cfg.getenv,
}, nil
}

Expand Down Expand Up @@ -527,7 +527,7 @@ func (a *Actions) MigrateLint(ctx context.Context) error {
return nil
}
// In case of a pull request, we need to add comments and suggestion to the PR.
switch c, err := a.SCM(); {
switch c, err := a.SCM(tc); {
case errors.Is(err, ErrNoSCM):
case err != nil:
return err
Expand Down Expand Up @@ -737,7 +737,7 @@ func (a *Actions) SchemaPlan(ctx context.Context) error {
return fmt.Errorf("failed to generate schema plan comment: %w", err)
}
a.AddStepSummary(summary)
switch c, err := a.SCM(); {
switch c, err := a.SCM(tc); {
case errors.Is(err, ErrNoSCM):
case err != nil:
return err
Expand Down Expand Up @@ -1097,6 +1097,26 @@ func (a *Actions) RequiredInputs(input ...string) error {
return nil
}

// SCM returns a SCMClient.
func (a *Actions) SCM(tc *TriggerContext) (SCMClient, error) {
switch tc.SCM.Type {
case atlasexec.SCMTypeGithub:
token := a.Getenv("GITHUB_TOKEN")
if token == "" {
a.Warningf("GITHUB_TOKEN is not set, the action may not have all the permissions")
}
return githubClient(tc.Repo, tc.SCM.APIURL, token), nil
case atlasexec.SCMTypeGitlab:
token := a.Getenv("GITLAB_TOKEN")
if token == "" {
a.Warningf("GITLAB_TOKEN is not set, the action may not have all the permissions")
}
return gitlabClient(a.Getenv("CI_PROJECT_ID"), tc.SCM.APIURL, token), nil
default:
return nil, ErrNoSCM // Not implemented yet.
}
}

// addChecks runs annotations to the trigger event pull request for the given payload.
func (a *Actions) addChecks(lint *atlasexec.SummaryReport) error {
// Get the directory path from the lint report.
Expand Down
5 changes: 0 additions & 5 deletions atlasaction/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,4 @@ func (a *bbPipe) SetOutput(name, value string) {

func (a *bbPipe) AddStepSummary(string) {}

// SCM implements Action.
func (a *bbPipe) SCM() (SCMClient, error) {
return nil, ErrNoSCM // Not implemented yet.
}

var _ Action = (*bbPipe)(nil)
13 changes: 0 additions & 13 deletions atlasaction/circleci_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"io"
"net/http"
"os"
"strings"
"time"

Expand Down Expand Up @@ -121,15 +120,3 @@ func (a *circleCIOrb) GetTriggerContext() (*TriggerContext, error) {
func (a *circleCIOrb) AddStepSummary(summary string) {
// unsupported
}

func (a *circleCIOrb) SCM() (SCMClient, error) {
tc, err := a.GetTriggerContext()
if err != nil {
return nil, err
}
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
a.Warningf("GITHUB_TOKEN is not set, the action may not have all the permissions")
}
return githubClient(tc.Repo, tc.SCM.APIURL, token), nil
}
13 changes: 0 additions & 13 deletions atlasaction/gh_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"io"
"net/http"
"os"
"time"

"golang.org/x/oauth2"
Expand Down Expand Up @@ -84,18 +83,6 @@ func (a *ghAction) GetTriggerContext() (*TriggerContext, error) {
return tc, nil
}

func (a *ghAction) SCM() (SCMClient, error) {
tc, err := a.GetTriggerContext()
if err != nil {
return nil, err
}
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
a.Warningf("GITHUB_TOKEN is not set, the action may not have all the permissions")
}
return githubClient(tc.Repo, tc.SCM.APIURL, token), nil
}

// githubClient returns a new GitHub client for the given repository.
// If the GITHUB_TOKEN is set, it will be used for authentication.
func githubClient(repo, baseURL string, token string) *githubAPI {
Expand Down
32 changes: 12 additions & 20 deletions atlasaction/gitlab_ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,32 +95,24 @@ func (t *gitlabTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return http.DefaultTransport.RoundTrip(req)
}

func (g *gitlabCI) SCM() (SCMClient, error) {
tc, err := g.GetTriggerContext()
if err != nil {
return nil, err
}
httpClient := &http.Client{Timeout: time.Second * 30}
if token := g.getenv("GITLAB_TOKEN"); token != "" {
httpClient.Transport = &gitlabTransport{
Token: token,
}
} else {
g.Warningf("GITLAB_TOKEN is not set, the action may not have all the permissions")
}
return &gitlabAPI{
baseURL: tc.SCM.APIURL,
project: g.getenv("CI_PROJECT_ID"),
client: httpClient,
}, nil
}

type gitlabAPI struct {
baseURL string
project string
client *http.Client
}

func gitlabClient(project, baseURL, token string) *gitlabAPI {
httpClient := &http.Client{Timeout: time.Second * 30}
if token != "" {
httpClient.Transport = &gitlabTransport{Token: token}
}
return &gitlabAPI{
baseURL: baseURL,
project: project,
client: httpClient,
}
}

type GitlabComment struct {
ID int `json:"id"`
Body string `json:"body"`
Expand Down
27 changes: 14 additions & 13 deletions atlasaction/gitlab_ci_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package atlasaction_test

import (
"ariga.io/atlas-action/atlasaction"
"ariga.io/atlas-go-sdk/atlasexec"
"encoding/json"
"github.com/gorilla/mux"
"github.com/rogpeppe/go-internal/testscript"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strconv"
"testing"

"ariga.io/atlas-action/atlasaction"
"ariga.io/atlas-go-sdk/atlasexec"
"github.com/gorilla/mux"
"github.com/rogpeppe/go-internal/testscript"
)

func newMockHandler(dir string) http.Handler {
Expand Down Expand Up @@ -89,23 +90,23 @@ func newMockHandler(dir string) http.Handler {
func TestGitlabCI(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/gitlab",
Setup: func(env *testscript.Env) error {
commentsDir := filepath.Join(env.WorkDir, "comments")
Setup: func(e *testscript.Env) error {
commentsDir := filepath.Join(e.WorkDir, "comments")
srv := httptest.NewServer(newMockHandler(commentsDir))
if err := os.Mkdir(commentsDir, os.ModePerm); err != nil {
return err
}
env.Defer(srv.Close)
env.Setenv("GITLAB_CI", "true")
env.Setenv("CI_PROJECT_ID", "1")
env.Setenv("CI_API_V4_URL", srv.URL)
env.Setenv("GITLAB_TOKEN", "token")
c, err := atlasexec.NewClient(env.WorkDir, "atlas")
e.Defer(srv.Close)
e.Setenv("CI_API_V4_URL", srv.URL)
e.Setenv("CI_PROJECT_ID", "1")
e.Setenv("GITLAB_CI", "true")
e.Setenv("GITLAB_TOKEN", "token")
c, err := atlasexec.NewClient(e.WorkDir, "atlas")
if err != nil {
return err
}
// Create a new actions for each test.
env.Values[atlasKey{}] = &atlasClient{c}
e.Values[atlasKey{}] = &atlasClient{c}
return nil
},
Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){
Expand Down
Loading