Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenlu committed Feb 18, 2024
1 parent 2e55f53 commit 933901b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 49 deletions.
74 changes: 30 additions & 44 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"ariga.io/atlas-go-sdk/atlasexec"
"ariga.io/atlas/sql/sqlcheck"
"github.com/mitchellh/mapstructure"
"github.com/sethvargo/go-githubactions"
)
Expand Down Expand Up @@ -125,7 +126,8 @@ func MigrateLint(ctx context.Context, client *atlasexec.Client, act *githubactio
Web: true,
Writer: &resp,
})
if err != nil && !errors.Is(err, atlasexec.LintErr) {
isLintErr := err != nil && errors.Is(err, atlasexec.LintErr)
if err != nil && !isLintErr {
return err
}
if err := json.NewDecoder(&resp).Decode(&payload); err != nil {
Expand Down Expand Up @@ -161,7 +163,7 @@ func MigrateLint(ctx context.Context, client *atlasexec.Client, act *githubactio
if err := ghClient.addSuggestions(act, &payload); err != nil {
return err
}
if errors.Is(err, atlasexec.LintErr) {
if isLintErr {
return fmt.Errorf("lint completed with errors, see report: %s", payload.URL)
}
return nil
Expand Down Expand Up @@ -283,43 +285,13 @@ func (g *githubAPI) addSuggestions(act *githubactions.Action, payload *atlasexec
filePath := path.Join(payload.Env.Dir, file.Name)
for _, report := range file.Reports {
for _, s := range report.SuggestedFixes {
prComment := pullRequestComment{
Body: fmt.Sprintf("```suggestion\n%s\n```", s.TextEdit.NewText),
Path: filePath,
CommitID: ghContext.SHA,
}
if s.TextEdit.End <= s.TextEdit.Line {
prComment.Line = s.TextEdit.Line
} else {
prComment.StartLine = s.TextEdit.Line
prComment.Line = s.TextEdit.End
}
buf, err := json.Marshal(prComment)
if err != nil {
return err
}
if err := g.createPRComment(event.PullRequestNumber, bytes.NewReader(buf)); err != nil {
if err := g.commentSuggestion(event.PullRequestNumber, ghContext.SHA, filePath, s); err != nil {
return err
}
}
for _, d := range report.Diagnostics {
for _, s := range d.SuggestedFixes {
prComment := pullRequestComment{
Body: fmt.Sprintf("```suggestion\n%s\n```", s.TextEdit.NewText),
Path: filePath,
CommitID: ghContext.SHA,
}
if s.TextEdit.End <= s.TextEdit.Line {
prComment.Line = s.TextEdit.Line
} else {
prComment.StartLine = s.TextEdit.Line
prComment.Line = s.TextEdit.End
}
buf, err := json.Marshal(prComment)
if err != nil {
return err
}
if err := g.createPRComment(event.PullRequestNumber, bytes.NewReader(buf)); err != nil {
if err := g.commentSuggestion(event.PullRequestNumber, ghContext.SHA, filePath, s); err != nil {
return err
}
}
Expand All @@ -336,12 +308,11 @@ type (
}

pullRequestComment struct {
Body string `json:"body"`
Path string `json:"path"`
CommitID string `json:"commit_id,omitempty"`
StartLine int `json:"start_line,omitempty"`
Line int `json:"line,omitempty"`
SubjectType string `json:"subject_type,omitempty"`
Body string `json:"body"`
Path string `json:"path"`
CommitID string `json:"commit_id,omitempty"`
StartLine int `json:"start_line,omitempty"`
Line int `json:"line,omitempty"`
}

githubAPI struct {
Expand Down Expand Up @@ -432,10 +403,25 @@ func (g *githubAPI) updateComment(id int, content io.Reader) error {
return err
}

// createPullRequestComment creates a review comment on the pull request.
func (g *githubAPI) createPRComment(id int, content io.Reader) error {
url := fmt.Sprintf("%v/repos/%v/pulls/%v/comments", g.baseURL, g.repo, id)
req, err := http.NewRequest(http.MethodPost, url, content)
// commentSuggestion creates a suggestion comment on the pull request.
func (g *githubAPI) commentSuggestion(prID int, commitID, filePath string, suggestion sqlcheck.SuggestedFix) error {
prComment := pullRequestComment{
Body: fmt.Sprintf("```suggestion\n%s\n```", suggestion.TextEdit.NewText),
Path: filePath,
CommitID: commitID,
}
if suggestion.TextEdit.End <= suggestion.TextEdit.Line {
prComment.Line = suggestion.TextEdit.Line
} else {
prComment.StartLine = suggestion.TextEdit.Line
prComment.Line = suggestion.TextEdit.End
}
buf, err := json.Marshal(prComment)
if err != nil {
return err
}
url := fmt.Sprintf("%v/repos/%v/pulls/%v/comments", g.baseURL, g.repo, prID)
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(buf))
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions atlasaction/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ func TestMigrateLint(t *testing.T) {
var payload pullRequestComment
require.NoError(t, json.NewDecoder(request.Body).Decode(&payload))
require.Equal(t, "testdata/migrations_destructive/20230925192914.sql", payload.Path)
require.Equal(t, "```suggestion\nAdd a pre-migration check to ensure table \"t1\" is empty before dropping it\n```", payload.Body)
require.Equal(t, "```suggestion\n-- atlas:txtar\n\n-- checks/destructive.sql --\n-- atlas:assert DS102\nSELECT NOT EXISTS (SELECT 1 FROM `t1`) AS `is_empty`;\n\n-- migration.sql --\ndrop table t1;\n```", payload.Body)
require.Equal(t, 1, payload.Line)
writer.WriteHeader(http.StatusCreated)
}))
tt.env["GITHUB_API_URL"] = ghMock.URL
Expand All @@ -382,10 +383,8 @@ 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")
// Since we wrote suggestion comment, we should see no error in check output
require.Empty(t, tt.out.String())
})
t.Run("lint summary - with diagnostics", func(t *testing.T) {
tt := newT(t)
Expand Down

0 comments on commit 933901b

Please sign in to comment.