Skip to content

Commit

Permalink
atlasaction: allow actions run without SCM client
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm committed Dec 11, 2024
1 parent b2402e8 commit ebff49e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
55 changes: 30 additions & 25 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ func WithVersion(v string) Option {
return func(c *config) { c.version = v }
}

// ErrNoSCM is returned when no SCM client is found.
var ErrNoSCM = errors.New("atlasaction: no SCM client found")

type (
config struct {
getenv func(string) string
Expand Down Expand Up @@ -512,41 +515,42 @@ func (a *Actions) MigrateLint(ctx context.Context) error {
return err
}
a.AddStepSummary(summary)
switch {
case tc.PullRequest == nil && isLintErr:
return fmt.Errorf("`atlas migrate lint` completed with errors, see report: %s", payload.URL)
case tc.PullRequest == nil:
if tc.PullRequest == nil {
if isLintErr {
return fmt.Errorf("`atlas migrate lint` completed with errors, see report: %s", payload.URL)
}
return nil
}
// In case of a pull request, we need to add comments and suggestion to the PR.
switch c, err := a.SCM(); {
case errors.Is(err, ErrNoSCM):
case err != nil:
return err
default:
scm, err := a.SCM()
if err != nil {
return err
}
if err = scm.UpsertComment(ctx, tc.PullRequest, dirName, summary); err != nil {
if err = c.UpsertComment(ctx, tc.PullRequest, dirName, summary); err != nil {
a.Errorf("failed to comment on the pull request: %v", err)
}
switch files, err := scm.ListPullRequestFiles(ctx, tc.PullRequest); {
switch files, err := c.ListPullRequestFiles(ctx, tc.PullRequest); {
case err != nil:
a.Errorf("failed to list pull request files: %w", err)
default:
err = a.addSuggestions(&payload, func(s *Suggestion) error {
// Add suggestion only if the file is part of the pull request.
if slices.Contains(files, s.Path) {
return scm.UpsertSuggestion(ctx, tc.PullRequest, s)
return c.UpsertSuggestion(ctx, tc.PullRequest, s)
}
return nil
})
if err != nil {
a.Errorf("failed to add suggestion on the pull request: %v", err)
}
}
if isLintErr {
return fmt.Errorf("`atlas migrate lint` completed with errors, see report: %s", payload.URL)
}
a.Infof("`atlas migrate lint` completed successfully, no issues found")
return nil
}
if isLintErr {
return fmt.Errorf("`atlas migrate lint` completed with errors, see report: %s", payload.URL)
}
a.Infof("`atlas migrate lint` completed successfully, no issues found")
return nil
}

// MigrateTest runs the GitHub Action for "ariga/atlas-action/migrate/test"
Expand Down Expand Up @@ -726,16 +730,17 @@ func (a *Actions) SchemaPlan(ctx context.Context) error {
return fmt.Errorf("failed to generate schema plan comment: %w", err)
}
a.AddStepSummary(summary)
scm, err := a.SCM()
if err != nil {
switch c, err := a.SCM(); {
case errors.Is(err, ErrNoSCM):
case err != nil:
return err
}
// Comment on the PR
err = scm.UpsertComment(ctx, tc.PullRequest, plan.File.Name, summary)
if err != nil {
// Don't fail the action if the comment fails.
// It may be due to the missing permissions.
a.Errorf("failed to comment on the pull request: %v", err)
default:
err = c.UpsertComment(ctx, tc.PullRequest, plan.File.Name, summary)
if err != nil {
// Don't fail the action if the comment fails.
// It may be due to the missing permissions.
a.Errorf("failed to comment on the pull request: %v", err)
}
}
if plan.Lint != nil {
if errs := plan.Lint.Errors(); len(errs) > 0 {
Expand Down
3 changes: 1 addition & 2 deletions atlasaction/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package atlasaction

import (
"errors"
"fmt"
"io"
"net/url"
Expand Down Expand Up @@ -104,7 +103,7 @@ func (a *bbPipe) AddStepSummary(string) {}

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

var _ Action = (*bbPipe)(nil)

0 comments on commit ebff49e

Please sign in to comment.