Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenlu committed Feb 15, 2024
1 parent dd8b8bc commit 495d388
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 63 deletions.
126 changes: 63 additions & 63 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,63 +263,63 @@ func addChecks(act *githubactions.Action, payload *atlasexec.SummaryReport) erro

// addSuggestions comments on the pull request for the given payload.
func addSuggestions(act *githubactions.Action, payload *atlasexec.SummaryReport) error {
ghContext, err := act.Context()
if err != nil {
return err
}
event, err := triggerEvent(ghContext)
if err != nil {
return err
}
ghClient := githubAPI{
baseURL: ghContext.APIURL,
repo: ghContext.Repository,
client: &http.Client{
Transport: &roundTripper{
authToken: act.Getenv("GITHUB_TOKEN"),
},
Timeout: time.Second * 30,
},
}
for _, file := range payload.Files {
filePath := path.Join(payload.Env.Dir, file.Name)
for _, report := range file.Reports {
for _, s := range report.SuggestedFixes {
buf, err := json.Marshal(pullRequestComment{
Body: s.Message,
Path: filePath,
CommitID: ghContext.SHA,
StartLine: 1,
Line: 1,
})
if err != nil {
return err
}
if err := ghClient.createPRComment(event.PullRequestNumber, bytes.NewReader(buf)); err != nil {
return err
}
}
for _, d := range report.Diagnostics {
fmt.Printf("suggestion fixes length: %d for diag: %v\n", len(d.SuggestedFixes), d)
for _, s := range d.SuggestedFixes {
buf, err := json.Marshal(pullRequestComment{
Body: s.Message,
Path: filePath,
CommitID: ghContext.SHA,
StartLine: 1,
Line: 1,
})
if err != nil {
return err
}
if err := ghClient.createPRComment(event.PullRequestNumber, bytes.NewReader(buf)); err != nil {
return err
}
}
}
}
}
return nil
ghContext, err := act.Context()
if err != nil {
return err
}
event, err := triggerEvent(ghContext)
if err != nil {
return err
}
ghClient := githubAPI{
baseURL: ghContext.APIURL,
repo: ghContext.Repository,
client: &http.Client{
Transport: &roundTripper{
authToken: act.Getenv("GITHUB_TOKEN"),
},
Timeout: time.Second * 30,
},
}
for _, file := range payload.Files {
filePath := path.Join(payload.Env.Dir, file.Name)
for _, report := range file.Reports {
for _, s := range report.SuggestedFixes {
buf, err := json.Marshal(pullRequestComment{
Body: fmt.Sprintf("```suggestion\n%s\n```", s.Message),
Path: filePath,
CommitID: ghContext.SHA,
StartLine: 1,
Line: 1,

})
if err != nil {
return err
}
if err := ghClient.createPRComment(event.PullRequestNumber, bytes.NewReader(buf)); err != nil {
return err
}
}
for _, d := range report.Diagnostics {
for _, s := range d.SuggestedFixes {
buf, err := json.Marshal(pullRequestComment{
Body: fmt.Sprintf("```suggestion\n%s\n```", s.Message),
Path: filePath,
CommitID: ghContext.SHA,
StartLine: 1,
Line: 1,
})
if err != nil {
return err
}
if err := ghClient.createPRComment(event.PullRequestNumber, bytes.NewReader(buf)); err != nil {
return err
}
}
}
}
}
return nil
}

type (
Expand All @@ -329,12 +329,12 @@ type (
}

pullRequestComment struct {
Body string `json:"body"`
Path string `json:"path"`
CommitID string `json:"commit_id"`
StartLine int `json:"start_line"`
Line int `json:"line"`
StartSide string `json:"start_side" default:"RIGHT"`
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"`
}

githubAPI struct {
Expand Down
20 changes: 20 additions & 0 deletions atlasaction/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,14 @@ func TestMigrateLint(t *testing.T) {
})
t.Run("lint with error", func(t *testing.T) {
tt := newT(t)
ghMock := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path == "/repos/test-owner/test-repository/pulls/0/comments" {
writer.WriteHeader(http.StatusCreated)
return
}
}))
tt.env["GITHUB_API_URL"] = ghMock.URL
tt.env["GITHUB_REPOSITORY"] = "test-owner/test-repository"
tt.setupConfigWithLogin(t, srv.URL, token)
tt.setInput("dev-url", "sqlite://file?mode=memory")
tt.setInput("dir", "file://testdata/migrations_destructive")
Expand All @@ -369,6 +377,14 @@ func TestMigrateLint(t *testing.T) {
})
t.Run("lint summary - lint error", func(t *testing.T) {
tt := newT(t)
ghMock := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path == "/repos/test-owner/test-repository/pulls/0/comments" {
writer.WriteHeader(http.StatusCreated)
return
}
}))
tt.env["GITHUB_API_URL"] = ghMock.URL
tt.env["GITHUB_REPOSITORY"] = "test-owner/test-repository"
tt.setupConfigWithLogin(t, srv.URL, token)
tt.setInput("dev-url", "sqlite://file?mode=memory")
tt.setInput("dir", "file://testdata/migrations_destructive")
Expand Down Expand Up @@ -430,6 +446,10 @@ func TestMigrateLint(t *testing.T) {
var ghPayloads []ghPayload
commentRegex := regexp.MustCompile("<!-- generated by ariga/atlas-action for [a-zA-Z-]* -->")
ghMock := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path == "/repos/test-owner/test-repository/pulls/42/comments" {
writer.WriteHeader(http.StatusCreated)
return
}
var payload struct {
Body string `json:"body"`
}
Expand Down

0 comments on commit 495d388

Please sign in to comment.