Skip to content

Commit

Permalink
atlasaction: allow the action custom format of report
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm committed Dec 14, 2024
1 parent a769b0a commit 4b19a2f
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 52 deletions.
57 changes: 30 additions & 27 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ type (
SetOutput(string, string)
// GetTriggerContext returns the context of the trigger event.
GetTriggerContext() (*TriggerContext, error)
// AddStepSummary adds a summary to the action step.
AddStepSummary(string)
}

// Reporter is an interface for reporting the status of the actions.
Reporter interface {
MigrateApply(context.Context, *atlasexec.MigrateApply)
MigrateLint(context.Context, *atlasexec.SummaryReport)
SchemaPlan(context.Context, *atlasexec.SchemaPlan)
SchemaApply(context.Context, *atlasexec.SchemaApply)
}
// SCMClient contains methods for interacting with SCM platforms (GitHub, Gitlab etc...).
SCMClient interface {
// UpsertComment posts or updates a pull request comment.
Expand Down Expand Up @@ -352,11 +357,8 @@ func (a *Actions) MigrateApply(ctx context.Context) error {
return nil
}
for _, run := range runs {
switch summary, err := RenderTemplate("migrate-apply.tmpl", run); {
case err != nil:
a.Errorf("failed to create summary: %v", err)
default:
a.AddStepSummary(summary)
if r, ok := a.Action.(Reporter); ok {
r.MigrateApply(ctx, run)
}
if run.Error != "" {
a.SetOutput("error", run.Error)
Expand Down Expand Up @@ -515,11 +517,9 @@ func (a *Actions) MigrateLint(ctx context.Context) error {
if err := a.addChecks(&payload); err != nil {
return err
}
summary, err := RenderTemplate("migrate-lint.tmpl", &payload)
if err != nil {
return err
if r, ok := a.Action.(Reporter); ok {
r.MigrateLint(ctx, &payload)
}
a.AddStepSummary(summary)
if tc.PullRequest == nil {
if isLintErr {
return fmt.Errorf("`atlas migrate lint` completed with errors, see report: %s", payload.URL)
Expand All @@ -532,7 +532,11 @@ func (a *Actions) MigrateLint(ctx context.Context) error {
case err != nil:
return err
default:
if err = c.UpsertComment(ctx, tc.PullRequest, dirName, summary); err != nil {
comment, err := RenderTemplate("migrate-lint.tmpl", &payload)
if err != nil {
return err
}
if err = c.UpsertComment(ctx, tc.PullRequest, dirName, comment); err != nil {
a.Errorf("failed to comment on the pull request: %v", err)
}
if c, ok := c.(SCMSuggestions); ok {
Expand Down Expand Up @@ -727,22 +731,24 @@ func (a *Actions) SchemaPlan(ctx context.Context) error {
a.SetOutput("link", plan.File.Link)
a.SetOutput("plan", plan.File.URL)
a.SetOutput("status", plan.File.Status)
// Report the schema plan to the user and add a comment to the PR.
summary, err := RenderTemplate("schema-plan.tmpl", map[string]any{
"Plan": plan,
"EnvName": params.Env,
"RerunCommand": tc.RerunCmd,
})
if err != nil {
return fmt.Errorf("failed to generate schema plan comment: %w", err)
if r, ok := a.Action.(Reporter); ok {
r.SchemaPlan(ctx, plan)
}
a.AddStepSummary(summary)
switch c, err := a.SCM(tc); {
case errors.Is(err, ErrNoSCM):
case err != nil:
return err
default:
err = c.UpsertComment(ctx, tc.PullRequest, plan.File.Name, summary)
// Report the schema plan to the user and add a comment to the PR.
comment, err := RenderTemplate("schema-plan.tmpl", map[string]any{
"Plan": plan,
"EnvName": params.Env,
"RerunCommand": tc.RerunCmd,
})
if err != nil {
return fmt.Errorf("failed to generate schema plan comment: %w", err)
}
err = c.UpsertComment(ctx, tc.PullRequest, plan.File.Name, comment)
if err != nil {
// Don't fail the action if the comment fails.
// It may be due to the missing permissions.
Expand Down Expand Up @@ -834,11 +840,8 @@ func (a *Actions) SchemaApply(ctx context.Context) error {
results = mErr.Result
}
for _, result := range results {
switch summary, err := RenderTemplate("schema-apply.tmpl", result); {
case err != nil:
a.Errorf("failed to create summary: %v", err)
default:
a.AddStepSummary(summary)
if r, ok := a.Action.(Reporter); ok {
r.SchemaApply(ctx, result)
}
if result.Error != "" {
a.SetOutput("error", result.Error)
Expand Down
38 changes: 27 additions & 11 deletions atlasaction/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2211,7 +2211,7 @@ func TestSchemaPlan(t *testing.T) {
return a
}
require.ErrorContains(t, newActs().SchemaPlan(ctx), "found multiple schema plans, please approve or delete the existing plans")
require.Len(t, act.summary, 0, "Expected 1 summary")
require.Equal(t, 0, act.summary, "No summaries generated")
require.Equal(t, 0, commentCounter, "No more comments generated")
require.Equal(t, 0, commentEdited, "No comment should be edited")

Expand All @@ -2220,7 +2220,7 @@ func TestSchemaPlan(t *testing.T) {
planFiles = nil
act.resetOutputs()
require.NoError(t, newActs().SchemaPlan(ctx))
require.Len(t, act.summary, 0, "No summaries generated")
require.Equal(t, 0, act.summary, "No summaries generated")
require.Equal(t, 0, commentCounter, "Expected 1 comment generated")
require.Equal(t, 0, commentEdited, "No comment should be edited")

Expand All @@ -2229,7 +2229,7 @@ func TestSchemaPlan(t *testing.T) {
planFiles = nil
act.resetOutputs()
require.NoError(t, newActs().SchemaPlan(ctx))
require.Len(t, act.summary, 1, "Expected 1 summary")
require.Equal(t, 1, act.summary, "Expected 1 summary")
require.Equal(t, 1, commentCounter, "Expected 1 comment generated")
require.Equal(t, 0, commentEdited, "No comment should be edited")
require.EqualValues(t, map[string]string{
Expand All @@ -2241,15 +2241,15 @@ func TestSchemaPlan(t *testing.T) {
act.trigger.PullRequest.Body = "Text\n/atlas:txmode: none\nText"
act.resetOutputs()
require.NoError(t, newActs().SchemaPlan(ctx))
require.Len(t, act.summary, 2, "Expected 1 summary")
require.Equal(t, 2, act.summary, "Expected 2 summary")
require.Equal(t, []string{"atlas:txmode: none"}, planprams.Directives)
act.trigger.PullRequest.Body = ""

// Existing plan
planFiles = []atlasexec.SchemaPlanFile{*planFile}
act.resetOutputs()
require.NoError(t, newActs().SchemaPlan(ctx))
require.Len(t, act.summary, 3, "Expected 2 summaries")
require.Equal(t, 3, act.summary, "Expected 3 summaries")
require.Equal(t, 1, commentCounter, "No more comments generated")
require.Equal(t, 2, commentEdited, "Expected comment to be edited")
require.EqualValues(t, map[string]string{
Expand Down Expand Up @@ -2390,7 +2390,7 @@ type (
scm *mockSCM // scm client
inputs map[string]string // input values
output map[string]string // step's output
summary []string // step summaries
summary int // step summaries
logger *slog.Logger // logger
fatal bool // fatal called
}
Expand All @@ -2400,7 +2400,28 @@ type (
}
)

// MigrateApply implements atlasaction.Reporter.
func (m *mockAction) MigrateApply(context.Context, *atlasexec.MigrateApply) {
m.summary++
}

// MigrateLint implements atlasaction.Reporter.
func (m *mockAction) MigrateLint(context.Context, *atlasexec.SummaryReport) {
m.summary++
}

// SchemaApply implements atlasaction.Reporter.
func (m *mockAction) SchemaApply(context.Context, *atlasexec.SchemaApply) {
m.summary++
}

// SchemaPlan implements atlasaction.Reporter.
func (m *mockAction) SchemaPlan(context.Context, *atlasexec.SchemaPlan) {
m.summary++
}

var _ atlasaction.Action = (*mockAction)(nil)
var _ atlasaction.Reporter = (*mockAction)(nil)
var _ atlasaction.SCMClient = (*mockSCM)(nil)

func (m *mockAction) resetOutputs() {
Expand Down Expand Up @@ -2430,11 +2451,6 @@ func (m *mockAction) SetOutput(name, value string) {
m.output[name] = value
}

// AddStepSummary implements Action.
func (m *mockAction) AddStepSummary(s string) {
m.summary = append(m.summary, s)
}

// Infof implements Action.
func (m *mockAction) Infof(msg string, args ...interface{}) {
m.logger.Info(fmt.Sprintf(msg, args...))
Expand Down
2 changes: 0 additions & 2 deletions atlasaction/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,4 @@ func (a *bbPipe) SetOutput(name, value string) {
}
}

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

var _ Action = (*bbPipe)(nil)
5 changes: 0 additions & 5 deletions atlasaction/circleci_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,3 @@ func (a *circleCIOrb) GetTriggerContext() (*TriggerContext, error) {
}
return ctx, nil
}

// AddStepSummary implements the Action interface.
func (a *circleCIOrb) AddStepSummary(summary string) {
// unsupported
}
50 changes: 48 additions & 2 deletions atlasaction/gh_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package atlasaction

import (
"context"
"fmt"
"io"
"net/http"
Expand All @@ -19,8 +20,6 @@ import (

const defaultGHApiUrl = "https://api.github.com"

var _ Action = (*ghAction)(nil)

type (
// ghAction is an implementation of the Action interface for GitHub Actions.
ghAction struct {
Expand All @@ -34,6 +33,50 @@ type (
}
)

// MigrateApply implements Reporter.
func (a *ghAction) MigrateApply(_ context.Context, r *atlasexec.MigrateApply) {
summary, err := RenderTemplate("migrate-apply.tmpl", r)
if err != nil {
a.Errorf("failed to create summary: %v", err)
return
}
a.AddStepSummary(summary)
}

// MigrateLint implements Reporter.
func (a *ghAction) MigrateLint(_ context.Context, r *atlasexec.SummaryReport) {
summary, err := RenderTemplate("migrate-lint.tmpl", r)
if err != nil {
a.Errorf("failed to create summary: %v", err)
return
}
a.AddStepSummary(summary)
}

// SchemaApply implements Reporter.
func (a *ghAction) SchemaApply(_ context.Context, r *atlasexec.SchemaApply) {
summary, err := RenderTemplate("schema-apply.tmpl", r)
if err != nil {
a.Errorf("failed to create summary: %v", err)
return
}
a.AddStepSummary(summary)
}

// SchemaPlan implements Reporter.
func (a *ghAction) SchemaPlan(_ context.Context, r *atlasexec.SchemaPlan) {
summary, err := RenderTemplate("schema-plan.tmpl", map[string]any{
"Plan": r,
"EnvName": a.GetInput("env"),
"RerunCommand": fmt.Sprintf("gh run rerun %s", a.Getenv("GITHUB_RUN_ID")),
})
if err != nil {
a.Errorf("failed to create summary: %v", err)
return
}
a.AddStepSummary(summary)
}

// NewGHAction returns a new Action for GitHub Actions.
func NewGHAction(getenv func(string) string, w io.Writer) *ghAction {
return &ghAction{
Expand Down Expand Up @@ -83,6 +126,9 @@ func (a *ghAction) GetTriggerContext() (*TriggerContext, error) {
return tc, nil
}

var _ Action = (*ghAction)(nil)
var _ Reporter = (*ghAction)(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
5 changes: 0 additions & 5 deletions atlasaction/gitlab_ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ func (g *gitlabCI) GetTriggerContext() (*TriggerContext, error) {
return ctx, nil
}

// AddStepSummary implements the Action interface.
func (g *gitlabCI) AddStepSummary(summary string) {
// unsupported
}

type gitlabTransport struct {
Token string
}
Expand Down

0 comments on commit 4b19a2f

Please sign in to comment.