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: allow the SCM client control the comment format #279

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
203 changes: 24 additions & 179 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"io"
"net/url"
"os"
"path"
"slices"
"strconv"
"strings"
Expand All @@ -36,7 +35,6 @@ type (
Version string
Atlas AtlasExec
CloudClient func(string, string, *atlasexec.Version) CloudClient
Getenv func(string) string
}

// Action interface for Atlas.
Expand All @@ -45,6 +43,8 @@ type (
// GetType returns the type of atlasexec trigger Type. e.g. "GITHUB_ACTION"
// The value is used to identify the type on CI-Run page in Atlas Cloud.
GetType() atlasexec.TriggerType
// Getenv returns the value of the environment variable with the given name.
Getenv(string) string
// GetInput returns the value of the input with the given name.
GetInput(string) string
// SetOutput sets the value of the output with the given name.
Expand All @@ -62,19 +62,11 @@ type (
}
// SCMClient contains methods for interacting with SCM platforms (GitHub, Gitlab etc...).
SCMClient interface {
// UpsertComment posts or updates a pull request comment.
UpsertComment(ctx context.Context, pr *PullRequest, id, comment string) error
// CommentLint comments on the pull request with the lint report.
CommentLint(context.Context, *TriggerContext, *atlasexec.SummaryReport) error
// CommentPlan comments on the pull request with the schema plan.
CommentPlan(context.Context, *TriggerContext, *atlasexec.SchemaPlan) error
}

// SCMSuggestions contains methods for interacting with SCM platforms (GitHub, Gitlab etc...)
// that support suggestions on the pull request.
SCMSuggestions interface {
// UpsertSuggestion posts or updates a pull request suggestion.
UpsertSuggestion(ctx context.Context, pr *PullRequest, s *Suggestion) error
// ListPullRequestFiles returns a list of files changed in a pull request.
ListPullRequestFiles(ctx context.Context, pr *PullRequest) ([]string, error)
}

Logger interface {
// Infof logs an info message.
Infof(string, ...interface{})
Expand Down Expand Up @@ -134,6 +126,7 @@ type (

// TriggerContext holds the context of the environment the action is running in.
TriggerContext struct {
Act Action // Act is the action that is running.
SCM SCM // SCM is the source control management system.
Repo string // Repo is the repository name. e.g. "ariga/atlas-action".
RepoURL string // RepoURL is full URL of the repository. e.g. "https://github.com/ariga/atlas-action".
Expand Down Expand Up @@ -197,7 +190,6 @@ func New(opts ...Option) (*Actions, error) {
Atlas: cfg.atlas,
CloudClient: cfg.cloudClient,
Version: cfg.version,
Getenv: cfg.getenv,
}, nil
}

Expand Down Expand Up @@ -517,46 +509,18 @@ func (a *Actions) MigrateLint(ctx context.Context) error {
if payload.URL != "" {
a.SetOutput("report-url", payload.URL)
}
if err := a.addChecks(&payload); err != nil {
return err
}
if r, ok := a.Action.(Reporter); ok {
r.MigrateLint(ctx, &payload)
}
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(tc); {
case errors.Is(err, ErrNoSCM):
case err != nil:
return err
default:
comment, err := RenderTemplate("migrate-lint.tmpl", &payload)
if err != nil {
if tc.PullRequest != nil {
// In case of a pull request, we need to add comments and suggestion to the PR.
switch c, err := tc.SCMClient(); {
case errors.Is(err, ErrNoSCM):
case 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 {
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 c.UpsertSuggestion(ctx, tc.PullRequest, s)
}
return nil
})
if err != nil {
a.Errorf("failed to add suggestion on the pull request: %v", err)
}
default:
if err = c.CommentLint(ctx, tc, &payload); err != nil {
a.Errorf("failed to comment on the pull request: %v", err)
}
}
}
Expand Down Expand Up @@ -737,21 +701,12 @@ func (a *Actions) SchemaPlan(ctx context.Context) error {
if r, ok := a.Action.(Reporter); ok {
r.SchemaPlan(ctx, plan)
}
switch c, err := a.SCM(tc); {
switch c, err := tc.SCMClient(); {
case errors.Is(err, ErrNoSCM):
case err != nil:
return err
default:
// 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)
err = c.CommentPlan(ctx, tc, plan)
if err != nil {
// Don't fail the action if the comment fails.
// It may be due to the missing permissions.
Expand Down Expand Up @@ -1083,62 +1038,26 @@ func (a *Actions) RequiredInputs(input ...string) error {
return nil
}

// SCM returns a SCMClient.
func (a *Actions) SCM(tc *TriggerContext) (SCMClient, error) {
// SCMClient returns a Client to interact with the SCM.
func (tc *TriggerContext) SCMClient() (SCMClient, error) {
switch tc.SCM.Type {
case atlasexec.SCMTypeGithub:
token := a.Getenv("GITHUB_TOKEN")
token := tc.Act.Getenv("GITHUB_TOKEN")
if token == "" {
a.Warningf("GITHUB_TOKEN is not set, the action may not have all the permissions")
tc.Act.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")
token := tc.Act.Getenv("GITLAB_TOKEN")
if token == "" {
a.Warningf("GITLAB_TOKEN is not set, the action may not have all the permissions")
tc.Act.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
return gitlabClient(tc.Act.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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why you removing this functionality?

can you move it to github SCM?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved it to the ghActions. Because Checks are GH's specific implement.

// Get the directory path from the lint report.
dir := path.Join(a.WorkingDir(), lint.Env.Dir)
for _, file := range lint.Files {
filePath := path.Join(dir, file.Name)
if file.Error != "" && len(file.Reports) == 0 {
a.WithFieldsMap(map[string]string{
"file": filePath,
"line": "1",
}).Errorf(file.Error)
continue
}
for _, report := range file.Reports {
for _, diag := range report.Diagnostics {
msg := diag.Text
if diag.Code != "" {
msg = fmt.Sprintf("%v (%v)\n\nDetails: https://atlasgo.io/lint/analyzers#%v", msg, diag.Code, diag.Code)
}
lines := strings.Split(file.Text[:diag.Pos], "\n")
logger := a.WithFieldsMap(map[string]string{
"file": filePath,
"line": strconv.Itoa(max(1, len(lines))),
"title": report.Text,
})
if file.Error != "" {
logger.Errorf(msg)
} else {
logger.Warningf(msg)
}
}
}
}
return nil
}

// GetRunContext returns the run context for the action.
func (tc *TriggerContext) GetRunContext() *atlasexec.RunContext {
rc := &atlasexec.RunContext{
Expand All @@ -1157,80 +1076,6 @@ func (tc *TriggerContext) GetRunContext() *atlasexec.RunContext {
return rc
}

type Suggestion struct {
ID string // Unique identifier for the suggestion.
Path string // File path.
StartLine int // Start line numbers for the suggestion.
Line int // End line number for the suggestion.
Comment string // Comment body.
}

// addSuggestions returns the suggestions from the lint report.
func (a *Actions) addSuggestions(lint *atlasexec.SummaryReport, fn func(*Suggestion) error) (err error) {
if !slices.ContainsFunc(lint.Files, func(f *atlasexec.FileReport) bool {
return len(f.Reports) > 0
}) {
// No reports to add suggestions.
return nil
}
dir := a.WorkingDir()
for _, file := range lint.Files {
filePath := path.Join(dir, lint.Env.Dir, file.Name)
for reportIdx, report := range file.Reports {
for _, f := range report.SuggestedFixes {
if f.TextEdit == nil {
continue
}
s := Suggestion{Path: filePath, ID: f.Message}
if f.TextEdit.End <= f.TextEdit.Line {
s.Line = f.TextEdit.Line
} else {
s.StartLine = f.TextEdit.Line
s.Line = f.TextEdit.End
}
s.Comment, err = RenderTemplate("suggestion.tmpl", map[string]any{
"Fix": f,
"Dir": lint.Env.Dir,
})
if err != nil {
return fmt.Errorf("failed to render suggestion: %w", err)
}
if err = fn(&s); err != nil {
return fmt.Errorf("failed to process suggestion: %w", err)
}
}
for diagIdx, d := range report.Diagnostics {
for _, f := range d.SuggestedFixes {
if f.TextEdit == nil {
continue
}
s := Suggestion{Path: filePath, ID: f.Message}
if f.TextEdit.End <= f.TextEdit.Line {
s.Line = f.TextEdit.Line
} else {
s.StartLine = f.TextEdit.Line
s.Line = f.TextEdit.End
}
s.Comment, err = RenderTemplate("suggestion.tmpl", map[string]any{
"Fix": f,
"Dir": lint.Env.Dir,
"File": file,
"Report": reportIdx,
"Diag": diagIdx,
})
if err != nil {
return fmt.Errorf("failed to render suggestion: %w", err)
}
if err = fn(&s); err != nil {
return fmt.Errorf("failed to process suggestion: %w", err)
}
}
}
}
}
return nil
}

func execTime(start, end time.Time) string {
return end.Sub(start).String()
}
Expand Down
45 changes: 40 additions & 5 deletions atlasaction/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,18 @@ func TestMigrateLint(t *testing.T) {
)
switch {
// List comments endpoint
case path == "/repos/test-owner/test-repository/issues/0/comments" && method == http.MethodGet:
b, err := json.Marshal(comments)
require.NoError(t, err)
_, err = writer.Write(b)
require.NoError(t, err)
return
case path == "/repos/test-owner/test-repository/issues/0/comments" && method == http.MethodPost:
var payload map[string]any
require.NoError(t, json.NewDecoder(request.Body).Decode(&payload))
payload["id"] = 123
writer.WriteHeader(http.StatusCreated)
return
case path == "/repos/test-owner/test-repository/pulls/0/comments" && method == http.MethodGet:
b, err := json.Marshal(comments)
require.NoError(t, err)
Expand Down Expand Up @@ -1051,6 +1063,18 @@ func TestMigrateLint(t *testing.T) {
)
switch {
// List comments endpoint
case path == "/repos/test-owner/test-repository/issues/0/comments" && method == http.MethodGet:
b, err := json.Marshal(comments)
require.NoError(t, err)
_, err = writer.Write(b)
require.NoError(t, err)
return
case path == "/repos/test-owner/test-repository/issues/0/comments" && method == http.MethodPost:
var payload map[string]any
require.NoError(t, json.NewDecoder(request.Body).Decode(&payload))
payload["id"] = 123
writer.WriteHeader(http.StatusCreated)
return
case path == "/repos/test-owner/test-repository/pulls/0/comments" && method == http.MethodGet:
b, err := json.Marshal(comments)
require.NoError(t, err)
Expand Down Expand Up @@ -2204,6 +2228,7 @@ func TestSchemaPlan(t *testing.T) {
// Multiple plans will fail with an error
planFiles = []atlasexec.SchemaPlanFile{*planFile, *planFile}
act.resetOutputs()
act.trigger.Act = act
newActs := func() *atlasaction.Actions {
t.Helper()
a, err := atlasaction.New(atlasaction.WithAction(act), atlasaction.WithAtlas(m))
Expand Down Expand Up @@ -2408,6 +2433,7 @@ func (m *mockAction) MigrateApply(context.Context, *atlasexec.MigrateApply) {
// MigrateLint implements atlasaction.Reporter.
func (m *mockAction) MigrateLint(context.Context, *atlasexec.SummaryReport) {
m.summary++

}

// SchemaApply implements atlasaction.Reporter.
Expand All @@ -2433,6 +2459,11 @@ func (m *mockAction) GetType() atlasexec.TriggerType {
return atlasexec.TriggerTypeGithubAction
}

// Getenv implements Action.
func (m *mockAction) Getenv(e string) string {
return os.Getenv(e)
}

// GetTriggerContext implements Action.
func (m *mockAction) GetTriggerContext(context.Context) (*atlasaction.TriggerContext, error) {
return m.trigger, nil
Expand Down Expand Up @@ -2492,15 +2523,19 @@ func (m *mockAction) SCM() (atlasaction.SCMClient, error) {
return m.scm, nil
}

func (m *mockSCM) ListPullRequestFiles(context.Context, *atlasaction.PullRequest) ([]string, error) {
return nil, nil
func (m *mockSCM) CommentLint(ctx context.Context, tc *atlasaction.TriggerContext, r *atlasexec.SummaryReport) error {
comment, err := atlasaction.RenderTemplate("migrate-lint.tmpl", r)
if err != nil {
return err
}
return m.comment(ctx, tc.PullRequest, tc.Act.GetInput("dir-name"), comment)
}

func (m *mockSCM) UpsertSuggestion(context.Context, *atlasaction.PullRequest, *atlasaction.Suggestion) error {
return nil
func (m *mockSCM) CommentPlan(ctx context.Context, tc *atlasaction.TriggerContext, p *atlasexec.SchemaPlan) error {
return m.comment(ctx, tc.PullRequest, p.File.Name, "")
}

func (m *mockSCM) UpsertComment(_ context.Context, _ *atlasaction.PullRequest, id string, _ string) error {
func (m *mockSCM) comment(_ context.Context, _ *atlasaction.PullRequest, id string, _ string) error {
var (
method = http.MethodPatch
urlPath = "/repos/ariga/atlas-action/issues/comments/1"
Expand Down
Loading
Loading