From 6ab34be69a604075b39cf3a05d694e2f1e76c332 Mon Sep 17 00:00:00 2001 From: "Giau. Tran Minh" Date: Tue, 17 Dec 2024 21:02:12 +0700 Subject: [PATCH] atlasaction: allow the SCM client control the comment format --- atlasaction/action.go | 24 ++--- atlasaction/action_test.go | 14 ++- atlasaction/bitbucket.go | 5 +- atlasaction/comments/schema-plan.tmpl | 2 +- atlasaction/gh_action.go | 97 +++++++++++-------- atlasaction/gitlab_ci.go | 93 +++++++++++------- atlasaction/testdata/gitlab/schema-plan.txtar | 2 +- 7 files changed, 140 insertions(+), 97 deletions(-) diff --git a/atlasaction/action.go b/atlasaction/action.go index f4d058e8..ee88357b 100644 --- a/atlasaction/action.go +++ b/atlasaction/action.go @@ -62,8 +62,10 @@ type ( } // SCMClient contains methods for interacting with SCM platforms (GitHub, Gitlab etc...). SCMClient interface { - // UpsertComment posts or updates a pull request comment. - UpsertComment(ctx context.Context, pr *PullRequest, id, comment string) error + // CommentLint comments on the pull request with the lint report. + CommentLint(context.Context, *TriggerContext, *atlasexec.SummaryReport) error + // CommentPlan comments on the pull request with the schema plan. + CommentPlan(context.Context, *TriggerContext, *atlasexec.SchemaPlan) error } // SCMSuggestions contains methods for interacting with SCM platforms (GitHub, Gitlab etc...) @@ -134,6 +136,7 @@ type ( // TriggerContext holds the context of the environment the action is running in. TriggerContext struct { + Act Action // Act is the action that is running. SCM SCM // SCM is the source control management system. Repo string // Repo is the repository name. e.g. "ariga/atlas-action". RepoURL string // RepoURL is full URL of the repository. e.g. "https://github.com/ariga/atlas-action". @@ -535,11 +538,7 @@ func (a *Actions) MigrateLint(ctx context.Context) error { case err != nil: return err default: - comment, err := RenderTemplate("migrate-lint.tmpl", &payload) - if err != nil { - return err - } - if err = c.UpsertComment(ctx, tc.PullRequest, dirName, comment); err != nil { + if err = c.CommentLint(ctx, tc, &payload); err != nil { a.Errorf("failed to comment on the pull request: %v", err) } if c, ok := c.(SCMSuggestions); ok { @@ -742,16 +741,7 @@ func (a *Actions) SchemaPlan(ctx context.Context) error { case err != nil: return err default: - // Report the schema plan to the user and add a comment to the PR. - comment, err := RenderTemplate("schema-plan.tmpl", map[string]any{ - "Plan": plan, - "EnvName": params.Env, - "RerunCommand": tc.RerunCmd, - }) - if err != nil { - return fmt.Errorf("failed to generate schema plan comment: %w", err) - } - err = c.UpsertComment(ctx, tc.PullRequest, plan.File.Name, comment) + err = c.CommentPlan(ctx, tc, plan) if err != nil { // Don't fail the action if the comment fails. // It may be due to the missing permissions. diff --git a/atlasaction/action_test.go b/atlasaction/action_test.go index aba0b3d1..a8fb6501 100644 --- a/atlasaction/action_test.go +++ b/atlasaction/action_test.go @@ -2500,7 +2500,19 @@ func (m *mockSCM) UpsertSuggestion(context.Context, *atlasaction.PullRequest, *a return nil } -func (m *mockSCM) UpsertComment(_ context.Context, _ *atlasaction.PullRequest, id string, _ string) error { +func (m *mockSCM) CommentLint(ctx context.Context, tc *atlasaction.TriggerContext, r *atlasexec.SummaryReport) error { + comment, err := atlasaction.RenderTemplate("migrate-lint.tmpl", r) + if err != nil { + return err + } + return m.comment(ctx, tc.PullRequest, tc.Act.GetInput("dir-name"), comment) +} + +func (m *mockSCM) CommentPlan(ctx context.Context, tc *atlasaction.TriggerContext, p *atlasexec.SchemaPlan) error { + return m.comment(ctx, tc.PullRequest, p.File.Name, "") +} + +func (m *mockSCM) comment(_ context.Context, _ *atlasaction.PullRequest, id string, _ string) error { var ( method = http.MethodPatch urlPath = "/repos/ariga/atlas-action/issues/comments/1" diff --git a/atlasaction/bitbucket.go b/atlasaction/bitbucket.go index ecbce214..1ca52e11 100644 --- a/atlasaction/bitbucket.go +++ b/atlasaction/bitbucket.go @@ -25,7 +25,7 @@ type bbPipe struct { } // NewBitBucketPipe returns a new Action for BitBucket. -func NewBitBucketPipe(getenv func(string) string, w io.Writer) Action { +func NewBitBucketPipe(getenv func(string) string, w io.Writer) *bbPipe { // Disable color output for testing, // but enable it for non-testing environments. color.NoColor = testing.Testing() @@ -33,13 +33,14 @@ func NewBitBucketPipe(getenv func(string) string, w io.Writer) Action { } // GetType implements Action. -func (a *bbPipe) GetType() atlasexec.TriggerType { +func (*bbPipe) GetType() atlasexec.TriggerType { return atlasexec.TriggerTypeBitbucket } // GetTriggerContext implements Action. func (a *bbPipe) GetTriggerContext(context.Context) (*TriggerContext, error) { tc := &TriggerContext{ + Act: a, Branch: a.getenv("BITBUCKET_BRANCH"), Commit: a.getenv("BITBUCKET_COMMIT"), Repo: a.getenv("BITBUCKET_REPO_FULL_NAME"), diff --git a/atlasaction/comments/schema-plan.tmpl b/atlasaction/comments/schema-plan.tmpl index 68069019..a9d72c44 100644 --- a/atlasaction/comments/schema-plan.tmpl +++ b/atlasaction/comments/schema-plan.tmpl @@ -24,7 +24,7 @@ the database with the desired state. Otherwise, Atlas will report a schema drift 3\. Push the updated plan to the registry using the following command: ```bash -atlas schema plan push --pending --env {{ .EnvName }} --file {{ .Plan.File.Name }}.plan.hcl +atlas schema plan push --pending --file {{ .Plan.File.Name }}.plan.hcl ``` {{- if .RerunCommand }} diff --git a/atlasaction/gh_action.go b/atlasaction/gh_action.go index 09a22867..a639e048 100644 --- a/atlasaction/gh_action.go +++ b/atlasaction/gh_action.go @@ -71,7 +71,6 @@ func (a *ghAction) SchemaApply(_ context.Context, r *atlasexec.SchemaApply) { func (a *ghAction) SchemaPlan(_ context.Context, r *atlasexec.SchemaPlan) { summary, err := RenderTemplate("schema-plan.tmpl", map[string]any{ "Plan": r, - "EnvName": a.GetInput("env"), "RerunCommand": fmt.Sprintf("gh run rerun %s", a.Getenv("GITHUB_RUN_ID")), }) if err != nil { @@ -82,7 +81,7 @@ func (a *ghAction) SchemaPlan(_ context.Context, r *atlasexec.SchemaPlan) { } // GetType implements the Action interface. -func (a *ghAction) GetType() atlasexec.TriggerType { +func (*ghAction) GetType() atlasexec.TriggerType { return atlasexec.TriggerTypeGithubAction } @@ -97,6 +96,7 @@ func (a *ghAction) GetTriggerContext(context.Context) (*TriggerContext, error) { return nil, err } tc := &TriggerContext{ + Act: a, SCM: SCM{Type: atlasexec.SCMTypeGithub, APIURL: ctx.APIURL}, Repo: ctx.Repository, Branch: ctx.HeadRef, @@ -176,8 +176,29 @@ type ( } ) -func (g *githubAPI) UpsertComment(ctx context.Context, pr *PullRequest, id, comment string) error { - comments, err := g.getIssueComments(ctx, pr) +// CommentLint implements SCMClient. +func (c *githubAPI) CommentLint(ctx context.Context, tc *TriggerContext, r *atlasexec.SummaryReport) error { + comment, err := RenderTemplate("migrate-lint.tmpl", r) + if err != nil { + return err + } + return c.comment(ctx, tc.PullRequest, tc.Act.GetInput("dir-name"), comment) +} + +// CommentPlan implements SCMClient. +func (c *githubAPI) CommentPlan(ctx context.Context, tc *TriggerContext, p *atlasexec.SchemaPlan) error { + // Report the schema plan to the user and add a comment to the PR. + comment, err := RenderTemplate("schema-plan.tmpl", map[string]any{ + "Plan": p, + }) + if err != nil { + return err + } + return c.comment(ctx, tc.PullRequest, p.File.Name, comment) +} + +func (c *githubAPI) comment(ctx context.Context, pr *PullRequest, id, comment string) error { + comments, err := c.getIssueComments(ctx, pr) if err != nil { return err } @@ -188,43 +209,43 @@ func (g *githubAPI) UpsertComment(ctx context.Context, pr *PullRequest, id, comm if found := slices.IndexFunc(comments, func(c githubIssueComment) bool { return strings.Contains(c.Body, marker) }); found != -1 { - return g.updateIssueComment(ctx, comments[found].ID, body) + return c.updateIssueComment(ctx, comments[found].ID, body) } - return g.createIssueComment(ctx, pr, body) + return c.createIssueComment(ctx, pr, body) } -func (g *githubAPI) getIssueComments(ctx context.Context, pr *PullRequest) ([]githubIssueComment, error) { - url := fmt.Sprintf("%v/repos/%v/issues/%v/comments", g.baseURL, g.repo, pr.Number) +func (c *githubAPI) getIssueComments(ctx context.Context, pr *PullRequest) ([]githubIssueComment, error) { + url := fmt.Sprintf("%v/repos/%v/issues/%v/comments", c.baseURL, c.repo, pr.Number) req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return nil, err } - res, err := g.client.Do(req) + res, err := c.client.Do(req) if err != nil { - return nil, fmt.Errorf("error querying github comments with %v/%v, %w", g.repo, pr.Number, err) + return nil, fmt.Errorf("error querying github comments with %v/%v, %w", c.repo, pr.Number, err) } defer res.Body.Close() buf, err := io.ReadAll(res.Body) if err != nil { - return nil, fmt.Errorf("error reading PR issue comments from %v/%v, %v", g.repo, pr.Number, err) + return nil, fmt.Errorf("error reading PR issue comments from %v/%v, %v", c.repo, pr.Number, err) } if res.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected status code %v when calling GitHub API", res.StatusCode) } var comments []githubIssueComment if err = json.Unmarshal(buf, &comments); err != nil { - return nil, fmt.Errorf("error parsing github comments with %v/%v from %v, %w", g.repo, pr.Number, string(buf), err) + return nil, fmt.Errorf("error parsing github comments with %v/%v from %v, %w", c.repo, pr.Number, string(buf), err) } return comments, nil } -func (g *githubAPI) createIssueComment(ctx context.Context, pr *PullRequest, content io.Reader) error { - url := fmt.Sprintf("%v/repos/%v/issues/%v/comments", g.baseURL, g.repo, pr.Number) +func (c *githubAPI) createIssueComment(ctx context.Context, pr *PullRequest, content io.Reader) error { + url := fmt.Sprintf("%v/repos/%v/issues/%v/comments", c.baseURL, c.repo, pr.Number) req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, content) if err != nil { return err } - res, err := g.client.Do(req) + res, err := c.client.Do(req) if err != nil { return err } @@ -240,13 +261,13 @@ func (g *githubAPI) createIssueComment(ctx context.Context, pr *PullRequest, con } // updateIssueComment updates issue comment with the given id. -func (g *githubAPI) updateIssueComment(ctx context.Context, id int, content io.Reader) error { - url := fmt.Sprintf("%v/repos/%v/issues/comments/%v", g.baseURL, g.repo, id) +func (c *githubAPI) updateIssueComment(ctx context.Context, id int, content io.Reader) error { + url := fmt.Sprintf("%v/repos/%v/issues/comments/%v", c.baseURL, c.repo, id) req, err := http.NewRequestWithContext(ctx, http.MethodPatch, url, content) if err != nil { return err } - res, err := g.client.Do(req) + res, err := c.client.Do(req) if err != nil { return err } @@ -262,11 +283,11 @@ func (g *githubAPI) updateIssueComment(ctx context.Context, id int, content io.R } // UpsertSuggestion creates or updates a suggestion review comment on trigger event pull request. -func (g *githubAPI) UpsertSuggestion(ctx context.Context, pr *PullRequest, s *Suggestion) error { +func (c *githubAPI) UpsertSuggestion(ctx context.Context, pr *PullRequest, s *Suggestion) error { marker := commentMarker(s.ID) body := fmt.Sprintf("%s\n%s", s.Comment, marker) // TODO: Listing the comments only once and updating the comment in the same call. - comments, err := g.listReviewComments(ctx, pr) + comments, err := c.listReviewComments(ctx, pr) if err != nil { return err } @@ -277,7 +298,7 @@ func (g *githubAPI) UpsertSuggestion(ctx context.Context, pr *PullRequest, s *Su return c.Path == s.Path && strings.Contains(c.Body, marker) }) if found != -1 { - if err := g.updateReviewComment(ctx, comments[found].ID, body); err != nil { + if err := c.updateReviewComment(ctx, comments[found].ID, body); err != nil { return err } return nil @@ -292,12 +313,12 @@ func (g *githubAPI) UpsertSuggestion(ctx context.Context, pr *PullRequest, s *Su if err != nil { return err } - url := fmt.Sprintf("%v/repos/%v/pulls/%v/comments", g.baseURL, g.repo, pr.Number) + url := fmt.Sprintf("%v/repos/%v/pulls/%v/comments", c.baseURL, c.repo, pr.Number) req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(buf)) if err != nil { return err } - res, err := g.client.Do(req) + res, err := c.client.Do(req) if err != nil { return err } @@ -313,13 +334,13 @@ func (g *githubAPI) UpsertSuggestion(ctx context.Context, pr *PullRequest, s *Su } // listReviewComments for the trigger event pull request. -func (g *githubAPI) listReviewComments(ctx context.Context, pr *PullRequest) ([]pullRequestComment, error) { - url := fmt.Sprintf("%v/repos/%v/pulls/%v/comments", g.baseURL, g.repo, pr.Number) +func (c *githubAPI) listReviewComments(ctx context.Context, pr *PullRequest) ([]pullRequestComment, error) { + url := fmt.Sprintf("%v/repos/%v/pulls/%v/comments", c.baseURL, c.repo, pr.Number) req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return nil, err } - res, err := g.client.Do(req) + res, err := c.client.Do(req) if err != nil { return nil, err } @@ -339,7 +360,7 @@ func (g *githubAPI) listReviewComments(ctx context.Context, pr *PullRequest) ([] } // updateReviewComment updates the review comment with the given id. -func (g *githubAPI) updateReviewComment(ctx context.Context, id int, body string) error { +func (c *githubAPI) updateReviewComment(ctx context.Context, id int, body string) error { type pullRequestUpdate struct { Body string `json:"body"` } @@ -347,12 +368,12 @@ func (g *githubAPI) updateReviewComment(ctx context.Context, id int, body string if err != nil { return err } - url := fmt.Sprintf("%v/repos/%v/pulls/comments/%v", g.baseURL, g.repo, id) + url := fmt.Sprintf("%v/repos/%v/pulls/comments/%v", c.baseURL, c.repo, id) req, err := http.NewRequestWithContext(ctx, http.MethodPatch, url, bytes.NewReader(b)) if err != nil { return err } - res, err := g.client.Do(req) + res, err := c.client.Do(req) if err != nil { return err } @@ -368,13 +389,13 @@ func (g *githubAPI) updateReviewComment(ctx context.Context, id int, body string } // ListPullRequestFiles return paths of the files in the trigger event pull request. -func (g *githubAPI) ListPullRequestFiles(ctx context.Context, pr *PullRequest) ([]string, error) { - url := fmt.Sprintf("%v/repos/%v/pulls/%v/files", g.baseURL, g.repo, pr.Number) +func (c *githubAPI) ListPullRequestFiles(ctx context.Context, pr *PullRequest) ([]string, error) { + url := fmt.Sprintf("%v/repos/%v/pulls/%v/files", c.baseURL, c.repo, pr.Number) req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return nil, err } - res, err := g.client.Do(req) + res, err := c.client.Do(req) if err != nil { return nil, err } @@ -398,19 +419,19 @@ func (g *githubAPI) ListPullRequestFiles(ctx context.Context, pr *PullRequest) ( } // OpeningPullRequest returns the latest open pull request for the given branch. -func (g *githubAPI) OpeningPullRequest(ctx context.Context, branch string) (*PullRequest, error) { - owner, _, err := g.ownerRepo() +func (c *githubAPI) OpeningPullRequest(ctx context.Context, branch string) (*PullRequest, error) { + owner, _, err := c.ownerRepo() if err != nil { return nil, err } // Get open pull requests for the branch. url := fmt.Sprintf("%s/repos/%s/pulls?state=open&head=%s:%s&sort=created&direction=desc&per_page=1&page=1", - g.baseURL, g.repo, owner, branch) + c.baseURL, c.repo, owner, branch) req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return nil, err } - res, err := g.client.Do(req) + res, err := c.client.Do(req) if err != nil { return nil, fmt.Errorf("error calling GitHub API: %w", err) } @@ -442,8 +463,8 @@ func (g *githubAPI) OpeningPullRequest(ctx context.Context, branch string) (*Pul } } -func (g *githubAPI) ownerRepo() (string, string, error) { - s := strings.Split(g.repo, "/") +func (c *githubAPI) ownerRepo() (string, string, error) { + s := strings.Split(c.repo, "/") if len(s) != 2 { return "", "", fmt.Errorf("GITHUB_REPOSITORY must be in the format of 'owner/repo'") } diff --git a/atlasaction/gitlab_ci.go b/atlasaction/gitlab_ci.go index 04bc387d..60e098ff 100644 --- a/atlasaction/gitlab_ci.go +++ b/atlasaction/gitlab_ci.go @@ -25,25 +25,23 @@ type gitlabCI struct { getenv func(string) string } -var _ Action = (*gitlabCI)(nil) - // NewGitlabCI returns a new Action for Gitlab CI. -func NewGitlabCI(getenv func(string) string, w io.Writer) Action { +func NewGitlabCI(getenv func(string) string, w io.Writer) *gitlabCI { return &gitlabCI{getenv: getenv, coloredLogger: &coloredLogger{w}} } // GetType implements the Action interface. -func (g *gitlabCI) GetType() atlasexec.TriggerType { - return "GITLAB_CI" +func (*gitlabCI) GetType() atlasexec.TriggerType { + return atlasexec.TriggerTypeGitlab } // GetInput implements the Action interface. -func (g *gitlabCI) GetInput(name string) string { - return strings.TrimSpace(g.getenv(toEnvVar("ATLAS_INPUT_" + name))) +func (a *gitlabCI) GetInput(name string) string { + return strings.TrimSpace(a.getenv(toEnvVar("ATLAS_INPUT_" + name))) } // SetOutput implements the Action interface. -func (g *gitlabCI) SetOutput(name, value string) { +func (a *gitlabCI) SetOutput(name, value string) { f, err := os.OpenFile(".env", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) if err != nil { return @@ -53,33 +51,33 @@ func (g *gitlabCI) SetOutput(name, value string) { } // GetTriggerContext implements the Action interface. -func (g *gitlabCI) GetTriggerContext(context.Context) (*TriggerContext, error) { +func (a *gitlabCI) GetTriggerContext(context.Context) (*TriggerContext, error) { ctx := &TriggerContext{ - SCM: SCM{ - Type: atlasexec.SCMTypeGitlab, - APIURL: g.getenv("CI_API_V4_URL"), - }, - Repo: g.getenv("CI_PROJECT_NAME"), - RepoURL: g.getenv("CI_PROJECT_URL"), - Branch: g.getenv("CI_COMMIT_REF_NAME"), - Commit: g.getenv("CI_COMMIT_SHA"), - Actor: &Actor{Name: g.getenv("GITLAB_USER_NAME"), ID: g.getenv("GITLAB_USER_ID")}, - } - if mr := g.getenv("CI_MERGE_REQUEST_IID"); mr != "" { + Act: a, + SCM: SCM{Type: atlasexec.SCMTypeGitlab, APIURL: a.getenv("CI_API_V4_URL")}, + Repo: a.getenv("CI_PROJECT_NAME"), + RepoURL: a.getenv("CI_PROJECT_URL"), + Branch: a.getenv("CI_COMMIT_REF_NAME"), + Commit: a.getenv("CI_COMMIT_SHA"), + Actor: &Actor{Name: a.getenv("GITLAB_USER_NAME"), ID: a.getenv("GITLAB_USER_ID")}, + } + if mr := a.getenv("CI_MERGE_REQUEST_IID"); mr != "" { num, err := strconv.Atoi(mr) if err != nil { return nil, err } ctx.PullRequest = &PullRequest{ - Commit: g.getenv("CI_COMMIT_SHA"), + Commit: a.getenv("CI_COMMIT_SHA"), Number: num, - URL: g.getenv("CI_MERGE_REQUEST_REF_PATH"), - Body: g.getenv("CI_MERGE_REQUEST_DESCRIPTION"), + URL: a.getenv("CI_MERGE_REQUEST_REF_PATH"), + Body: a.getenv("CI_MERGE_REQUEST_DESCRIPTION"), } } return ctx, nil } +var _ Action = (*gitlabCI)(nil) + type gitlabTransport struct { Token string } @@ -115,28 +113,49 @@ type GitlabComment struct { var _ SCMClient = (*gitlabAPI)(nil) -func (g *gitlabAPI) UpsertComment(ctx context.Context, pr *PullRequest, id, comment string) error { - url := fmt.Sprintf("%v/projects/%v/merge_requests/%v/notes", g.baseURL, g.project, pr.Number) +// CommentLint implements SCMClient. +func (c *gitlabAPI) CommentLint(ctx context.Context, tc *TriggerContext, r *atlasexec.SummaryReport) error { + comment, err := RenderTemplate("migrate-lint.tmpl", r) + if err != nil { + return err + } + return c.comment(ctx, tc.PullRequest, tc.Act.GetInput("dir-name"), comment) +} + +// CommentPlan implements SCMClient. +func (c *gitlabAPI) CommentPlan(ctx context.Context, tc *TriggerContext, p *atlasexec.SchemaPlan) error { + // Report the schema plan to the user and add a comment to the PR. + comment, err := RenderTemplate("schema-plan.tmpl", map[string]any{ + "Plan": p, + }) + if err != nil { + return fmt.Errorf("failed to generate schema plan comment: %w", err) + } + return c.comment(ctx, tc.PullRequest, p.File.Name, comment) +} + +func (c *gitlabAPI) comment(ctx context.Context, pr *PullRequest, id, comment string) error { + url := fmt.Sprintf("%v/projects/%v/merge_requests/%v/notes", c.baseURL, c.project, pr.Number) req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return err } req.Header.Set("Content-Type", "application/json") - res, err := g.client.Do(req) + res, err := c.client.Do(req) if err != nil { - return fmt.Errorf("error querying gitlab comments with %v/%v, %w", g.project, pr.Number, err) + return fmt.Errorf("error querying gitlab comments with %v/%v, %w", c.project, pr.Number, err) } defer res.Body.Close() buf, err := io.ReadAll(res.Body) if err != nil { - return fmt.Errorf("error reading PR issue comments from %v/%v, %v", g.project, pr.Number, err) + return fmt.Errorf("error reading PR issue comments from %v/%v, %v", c.project, pr.Number, err) } if res.StatusCode != http.StatusOK { return fmt.Errorf("unexpected status code %v when calling Gitlab API. body: %s", res.StatusCode, string(buf)) } var comments []GitlabComment if err = json.Unmarshal(buf, &comments); err != nil { - return fmt.Errorf("error parsing gitlab notes with %v/%v from %v, %w", g.project, pr.Number, string(buf), err) + return fmt.Errorf("error parsing gitlab notes with %v/%v from %v, %w", c.project, pr.Number, string(buf), err) } var ( marker = commentMarker(id) @@ -145,20 +164,20 @@ func (g *gitlabAPI) UpsertComment(ctx context.Context, pr *PullRequest, id, comm if found := slices.IndexFunc(comments, func(c GitlabComment) bool { return !c.System && strings.Contains(c.Body, marker) }); found != -1 { - return g.updateComment(ctx, pr, comments[found].ID, body) + return c.updateComment(ctx, pr, comments[found].ID, body) } - return g.createComment(ctx, pr, comment) + return c.createComment(ctx, pr, comment) } -func (g *gitlabAPI) createComment(ctx context.Context, pr *PullRequest, comment string) error { +func (c *gitlabAPI) createComment(ctx context.Context, pr *PullRequest, comment string) error { body := strings.NewReader(fmt.Sprintf(`{"body": %q}`, comment)) - url := fmt.Sprintf("%v/projects/%v/merge_requests/%v/notes", g.baseURL, g.project, pr.Number) + url := fmt.Sprintf("%v/projects/%v/merge_requests/%v/notes", c.baseURL, c.project, pr.Number) req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, body) if err != nil { return err } req.Header.Set("Content-Type", "application/json") - res, err := g.client.Do(req) + res, err := c.client.Do(req) if err != nil { return err } @@ -173,15 +192,15 @@ func (g *gitlabAPI) createComment(ctx context.Context, pr *PullRequest, comment return err } -func (g *gitlabAPI) updateComment(ctx context.Context, pr *PullRequest, NoteId int, comment string) error { +func (c *gitlabAPI) updateComment(ctx context.Context, pr *PullRequest, NoteId int, comment string) error { body := strings.NewReader(fmt.Sprintf(`{"body": %q}`, comment)) - url := fmt.Sprintf("%v/projects/%v/merge_requests/%v/notes/%v", g.baseURL, g.project, pr.Number, NoteId) + url := fmt.Sprintf("%v/projects/%v/merge_requests/%v/notes/%v", c.baseURL, c.project, pr.Number, NoteId) req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, body) if err != nil { return err } req.Header.Set("Content-Type", "application/json") - res, err := g.client.Do(req) + res, err := c.client.Do(req) if err != nil { return err } diff --git a/atlasaction/testdata/gitlab/schema-plan.txtar b/atlasaction/testdata/gitlab/schema-plan.txtar index d075e45a..396a09ac 100644 --- a/atlasaction/testdata/gitlab/schema-plan.txtar +++ b/atlasaction/testdata/gitlab/schema-plan.txtar @@ -150,7 +150,7 @@ the database with the desired state. Otherwise, Atlas will report a schema drift 3\. Push the updated plan to the registry using the following command: ```bash -atlas schema plan push --pending --env --file 20241010143904.plan.hcl +atlas schema plan push --pending --file 20241010143904.plan.hcl ``` \ No newline at end of file