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: add code suggestion comments to migrate lint action #119

Merged
merged 18 commits into from
Feb 19, 2024
Prev Previous commit
Next Next commit
small refactor of code
ronenlu committed Feb 14, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 3ba4263b204d046c6cbd99f41eb5f59e01389dc4
54 changes: 33 additions & 21 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ import (
"strconv"
"strings"
"text/template"
"time"

"ariga.io/atlas-go-sdk/atlasexec"
"github.com/mitchellh/mapstructure"
@@ -138,7 +139,7 @@ func MigrateLint(ctx context.Context, client *atlasexec.Client, act *githubactio
if err := comment.Execute(&summary, &payload); err != nil {
return err
}
if err := publish(act, dirName, summary.String()); err != nil {
if err := addSummary(act, dirName, summary.String()); err != nil {
return err
}
if err := addChecks(act, &payload); err != nil {
@@ -172,10 +173,10 @@ var (
)
)

// publish writes a comment and summary to the pull request for dirName. It adds a marker
// addSummary writes a summary to the pull request for dirName. It adds a marker
// HTML comment to the end of the comment body to identify the comment as one created by
// this action.
func publish(act *githubactions.Action, dirName, summary string) error {
func addSummary(act *githubactions.Action, dirName, summary string) error {
ghContext, err := act.Context()
if err != nil {
return err
@@ -187,13 +188,18 @@ func publish(act *githubactions.Action, dirName, summary string) error {
act.AddStepSummary(summary)
g := githubAPI{
baseURL: ghContext.APIURL,
client: &http.Client{
Transport: &roundTripper{
authToken: act.Getenv("GITHUB_TOKEN"),
},
Timeout: time.Second * 30,
},
}
prNumber := event.PullRequestNumber
if prNumber == 0 {
return nil
}
ghToken := act.Getenv("GITHUB_TOKEN")
comments, err := g.getIssueComments(prNumber, ghContext.Repository, ghToken)
comments, err := g.getIssueComments(prNumber, ghContext.Repository)
if err != nil {
return err
}
@@ -212,9 +218,9 @@ func publish(act *githubactions.Action, dirName, summary string) error {
return strings.Contains(c.Body, marker)
})
if found != -1 {
return g.updateComment(comments[found].ID, r, ghContext.Repository, ghToken)
return g.updateComment(comments[found].ID, r, ghContext.Repository)
}
return g.createIssueComment(prNumber, r, ghContext.Repository, ghToken)
return g.createIssueComment(prNumber, r, ghContext.Repository)
}

// addChecks runs to the pull request for the given payload.
@@ -260,17 +266,30 @@ type (

githubAPI struct {
baseURL string
client *http.Client
}

// roundTripper is a http.RoundTripper that adds the Authorization header.
roundTripper struct {
authToken string
}
)

func (g *githubAPI) getIssueComments(id int, repo, authToken string) ([]githubIssueComment, error) {
// RoundTrip implements http.RoundTripper.
func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("Accept", "application/vnd.github+json")
req.Header.Add("Authorization", "Bearer "+r.authToken)
req.Header.Add("X-GitHub-Api-Version", "2022-11-28")
return http.DefaultTransport.RoundTrip(req)
}

func (g *githubAPI) getIssueComments(id int, repo string) ([]githubIssueComment, error) {
url := fmt.Sprintf("%v/repos/%v/issues/%v/comments", g.baseURL, repo, id)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
addHeaders(req, authToken)
res, err := http.DefaultClient.Do(req)
res, err := g.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error querying github comments with %v/%v, %w", repo, id, err)
}
@@ -289,14 +308,13 @@ func (g *githubAPI) getIssueComments(id int, repo, authToken string) ([]githubIs
return comments, nil
}

func (g *githubAPI) createIssueComment(id int, content io.Reader, repo, authToken string) error {
func (g *githubAPI) createIssueComment(id int, content io.Reader, repo string) error {
url := fmt.Sprintf("%v/repos/%v/issues/%v/comments", g.baseURL, repo, id)
req, err := http.NewRequest(http.MethodPost, url, content)
if err != nil {
return err
}
addHeaders(req, authToken)
res, err := http.DefaultClient.Do(req)
res, err := g.client.Do(req)
if err != nil {
return err
}
@@ -311,14 +329,13 @@ func (g *githubAPI) createIssueComment(id int, content io.Reader, repo, authToke
return err
}

func (g *githubAPI) updateComment(id int, content io.Reader, repo, authToken string) error {
func (g *githubAPI) updateComment(id int, content io.Reader, repo string) error {
url := fmt.Sprintf("%v/repos/%v/issues/comments/%v", g.baseURL, repo, id)
req, err := http.NewRequest(http.MethodPatch, url, content)
if err != nil {
return err
}
addHeaders(req, authToken)
res, err := http.DefaultClient.Do(req)
res, err := g.client.Do(req)
if err != nil {
return err
}
@@ -333,11 +350,6 @@ func (g *githubAPI) updateComment(id int, content io.Reader, repo, authToken str
return err
}

func addHeaders(req *http.Request, authToken string) {
req.Header.Add("Accept", "application/vnd.github+json")
req.Header.Add("Authorization", "Bearer "+authToken)
req.Header.Add("X-GitHub-Api-Version", "2022-11-28")
}

func createRunContext(act *githubactions.Action) (*atlasexec.RunContext, error) {
ghContext, err := act.Context()
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
ariga.io/atlas v0.14.3-0.20231010104048-0c071bfc9161 h1:xZS2wAf1AzRNA/8iD2LTAXtIZuIDYDXsZRlYBAyBu0A=
ariga.io/atlas v0.14.3-0.20231010104048-0c071bfc9161/go.mod h1:isZrlzJ5cpoCoKFoY9knZug7Lq4pP1cm8g3XciLZ0Pw=
ariga.io/atlas v0.19.1-0.20240214103610-1a087ae06f34 h1:TfSSEbpoPfxB1o4X9NrLqjel+gTH8y1p3xOVnnq8HGA=
ariga.io/atlas v0.19.1-0.20240214103610-1a087ae06f34/go.mod h1:uj3pm+hUTVN/X5yfdBexHlZv+1Xu5u5ZbZx7+CDavNU=
ariga.io/atlas-go-sdk v0.3.0 h1:19hxr3Fm/Jg/pNSLq3mpTQs1k1sPNhQXpLgVtIDQ0J8=
ariga.io/atlas-go-sdk v0.3.0/go.mod h1:owkEEXw6jqne5KPVDfKsYB7cwMiMk3jtOiAAeKxS/yU=
ariga.io/atlas-go-sdk v0.4.1-0.20240214121042-62a4fc93b7e6 h1:QLaRJn/MI6IYlGpxBnrYqAq5TlXjDS3Uny5ErZCu0c4=
ariga.io/atlas-go-sdk v0.4.1-0.20240214121042-62a4fc93b7e6/go.mod h1:MbFqaKJ21TGy+KZMH2bKk79BY1UeJq09h57skyatU9E=
ariga.io/atlas-go-sdk v0.5.0 h1:VCbfLpKMYwbAfXyKPYC4dzhnyTfSvqyNzqUyRLWvgD4=
ariga.io/atlas-go-sdk v0.5.0/go.mod h1:MbFqaKJ21TGy+KZMH2bKk79BY1UeJq09h57skyatU9E=
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
@@ -25,8 +20,7 @@ github.com/go-openapi/inflect v0.19.0 h1:9jCH9scKIbHeV9m12SmPilScz6krDxKRasNNSNP
github.com/go-openapi/inflect v0.19.0/go.mod h1:lHpZVlpIQqLyKwJ4N+YSc9hchQy/i12fJykb83CRBH4=
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/hcl/v2 v2.18.1 h1:6nxnOJFku1EuSawSD81fuviYUV8DxFr3fp2dUi3ZYSo=
github.com/hashicorp/hcl/v2 v2.18.1/go.mod h1:ThLC89FV4p9MPW804KVbe/cEXoQ8NZEh+JtMeeGErHE=