Skip to content

Commit

Permalink
add code suggestion function
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenlu committed Feb 14, 2024
1 parent 3ba4263 commit b42ef76
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ func MigrateLint(ctx context.Context, client *atlasexec.Client, act *githubactio
if err := addChecks(act, &payload); err != nil {
return err
}
if err := addSuggestions(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 @@ -258,12 +261,65 @@ func addChecks(act *githubactions.Action, payload *atlasexec.SummaryReport) erro
return nil
}

// 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,
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 {
prComment := pullRequestComment{
Body: s.Message,
Path: filePath,
CommitID: ghContext.SHA,
StartLine: 1,
Line: 1,
}
buf, err := json.Marshal(prComment)
if err != nil {
return err
}
r := bytes.NewReader(buf)
if err := ghClient.createPullRequestComment(event.PullRequestNumber, r, ghContext.Repository); err != nil {
return err
}
}
}
}
return nil
}

type (
githubIssueComment struct {
ID int `json:"id"`
Body string `json:"body"`
}

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"`
}

githubAPI struct {
baseURL string
client *http.Client
Expand Down Expand Up @@ -350,6 +406,26 @@ func (g *githubAPI) updateComment(id int, content io.Reader, repo string) error
return err
}

func (g *githubAPI) createPullRequestComment(id int, content io.Reader, repo string) error {
url := fmt.Sprintf("%v/repos/%v/pulls/%v/comments", g.baseURL, repo, id)
req, err := http.NewRequest(http.MethodPost, url, content)
if err != nil {
return err
}
res, err := g.client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusCreated {
b, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("unexpected status code %v: unable to read body %v", res.StatusCode, err)
}
return fmt.Errorf("unexpected status code %v: with body: %v", res.StatusCode, string(b))
}
return err
}

func createRunContext(act *githubactions.Action) (*atlasexec.RunContext, error) {
ghContext, err := act.Context()
Expand Down

0 comments on commit b42ef76

Please sign in to comment.