Skip to content

Commit

Permalink
atlasaction: add check runs comment in migrate lint action (#118)
Browse files Browse the repository at this point in the history
* atlasaction: add check runs comment in migrate lint action

* update tests
  • Loading branch information
ronenlu authored Feb 14, 2024
1 parent 275a596 commit 35678a7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
39 changes: 39 additions & 0 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"fmt"
"io"
"net/http"
"path"
"slices"
"strconv"
"strings"
Expand Down Expand Up @@ -140,6 +141,9 @@ func MigrateLint(ctx context.Context, client *atlasexec.Client, act *githubactio
if err := publish(act, dirName, summary.String()); err != nil {
return err
}
if err := addChecks(act, &payload); err != nil {
return err
}
if errors.Is(err, atlasexec.LintErr) {
return fmt.Errorf("lint completed with errors, see report: %s", payload.URL)
}
Expand Down Expand Up @@ -213,6 +217,41 @@ func publish(act *githubactions.Action, dirName, summary string) error {
return g.createIssueComment(prNumber, r, ghContext.Repository, ghToken)
}

// addChecks runs to the pull request for the given payload.
func addChecks(act *githubactions.Action, payload *atlasexec.SummaryReport) error {
dir := payload.Env.Dir
for _, file := range payload.Files {
filePath := path.Join(dir, file.Name)
if file.Error != "" && len(file.Reports) == 0 {
act.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")
act = act.WithFieldsMap(map[string]string{
"file": filePath,
"line": strconv.Itoa(max(1, len(lines))),
"title": report.Text,
})
if file.Error != "" {
act.Errorf(msg)
} else {
act.Noticef(msg)
}
}
}
}
return nil
}

type (
githubIssueComment struct {
ID int `json:"id"`
Expand Down
8 changes: 8 additions & 0 deletions atlasaction/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,10 @@ func TestMigrateLint(t *testing.T) {
require.Contains(t, sum, "2 new migration files detected")
require.Contains(t, sum, "1 error found")
require.Contains(t, sum, `<a href="https://migration-lint-report-url" target="_blank">`)
out := tt.out.String()
require.Contains(t, out, "error file=testdata/migrations_destructive/20230925192914.sql")
require.Contains(t, out, "destructive changes detected")
require.Contains(t, out, "Details: https://atlasgo.io/lint/analyzers#DS102")
})
t.Run("lint summary - with diagnostics", func(t *testing.T) {
tt := newT(t)
Expand All @@ -396,6 +400,10 @@ func TestMigrateLint(t *testing.T) {
require.Contains(t, sum, "2 new migration files detected")
require.Contains(t, sum, "1 issue found")
require.Contains(t, sum, `<a href="https://migration-lint-report-url" target="_blank">`)
out := tt.out.String()
require.Contains(t, out, "notice file=testdata/diagnostics/20231016114135_add_not_null.sql")
require.Contains(t, out, "data dependent changes detected")
require.Contains(t, out, "Details: https://atlasgo.io/lint/analyzers#MF103")
})
t.Run("lint summary - lint success", func(t *testing.T) {
tt := newT(t)
Expand Down

0 comments on commit 35678a7

Please sign in to comment.