From dce659197ed88d1e0b5def01f6d1c63c2f74ad07 Mon Sep 17 00:00:00 2001 From: "Giau. Tran Minh" Date: Sun, 15 Dec 2024 00:21:52 +0700 Subject: [PATCH] atlasaction: allow the action custom format of report --- atlasaction/action.go | 57 ++++++++++++++++++---------------- atlasaction/bitbucket.go | 2 -- atlasaction/circleci_action.go | 5 --- atlasaction/gh_action.go | 50 +++++++++++++++++++++++++++-- atlasaction/gitlab_ci.go | 5 --- 5 files changed, 78 insertions(+), 41 deletions(-) diff --git a/atlasaction/action.go b/atlasaction/action.go index 8c6936b1..5693b9d2 100644 --- a/atlasaction/action.go +++ b/atlasaction/action.go @@ -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. @@ -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) @@ -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) @@ -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 { @@ -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. @@ -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) diff --git a/atlasaction/bitbucket.go b/atlasaction/bitbucket.go index e48001f3..39b2f396 100644 --- a/atlasaction/bitbucket.go +++ b/atlasaction/bitbucket.go @@ -99,6 +99,4 @@ func (a *bbPipe) SetOutput(name, value string) { } } -func (a *bbPipe) AddStepSummary(string) {} - var _ Action = (*bbPipe)(nil) diff --git a/atlasaction/circleci_action.go b/atlasaction/circleci_action.go index ce517cd2..89dff181 100644 --- a/atlasaction/circleci_action.go +++ b/atlasaction/circleci_action.go @@ -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 -} diff --git a/atlasaction/gh_action.go b/atlasaction/gh_action.go index 449c975d..a7719c07 100644 --- a/atlasaction/gh_action.go +++ b/atlasaction/gh_action.go @@ -5,6 +5,7 @@ package atlasaction import ( + "context" "fmt" "io" "net/http" @@ -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 { @@ -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{ @@ -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 { diff --git a/atlasaction/gitlab_ci.go b/atlasaction/gitlab_ci.go index b4328893..d0d2415b 100644 --- a/atlasaction/gitlab_ci.go +++ b/atlasaction/gitlab_ci.go @@ -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 }