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 dce6591
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 41 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
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 dce6591

Please sign in to comment.