From f0f3eb72d0eb4f637ec0ee2727c92eb0583378a6 Mon Sep 17 00:00:00 2001 From: Javier Carrillo Date: Mon, 15 Jul 2024 10:30:23 -0400 Subject: [PATCH 1/8] refactor: (DSP-2004) Apply good practice to code --- cmd/create_comment.go | 8 ++++-- cmd/get_comment.go | 9 ++++-- cmd/root.go | 11 ++++++++ cmd/update_comment.go | 8 ++++-- src/comments.go | 64 ++++++++++++++----------------------------- utils/files/files.go | 9 ++++++ 6 files changed, 58 insertions(+), 51 deletions(-) diff --git a/cmd/create_comment.go b/cmd/create_comment.go index 6c28a87..922f794 100644 --- a/cmd/create_comment.go +++ b/cmd/create_comment.go @@ -1,8 +1,7 @@ package cmd import ( - commentGithub "github.com/gbh-tech/github-pr-commenter/src" - + "github.com/charmbracelet/log" "github.com/spf13/cobra" ) @@ -18,7 +17,10 @@ var createCommentPrCmd = &cobra.Command{ content, _ := cmd.Flags().GetString("content") filePath, _ := cmd.Flags().GetString("filePath") - commentGithub.CreateComment(pull, org, repo, content, filePath) + _, err := GithubClient.CreateComment(pull, org, repo, content, filePath) + if err != nil { + log.Fatalf("Error: %v\n", err) + } }, } diff --git a/cmd/get_comment.go b/cmd/get_comment.go index 342045c..53af8dd 100644 --- a/cmd/get_comment.go +++ b/cmd/get_comment.go @@ -1,8 +1,9 @@ package cmd import ( - commentGithub "github.com/gbh-tech/github-pr-commenter/src" + "fmt" + "github.com/charmbracelet/log" "github.com/spf13/cobra" ) @@ -17,7 +18,11 @@ var getCommentPrCmd = &cobra.Command{ content, _ := cmd.Flags().GetString("content") filePath, _ := cmd.Flags().GetString("filePath") - commentGithub.GetUserComments(pull, org, repo, content, filePath) + commentID, err := GithubClient.GetUserComments(pull, org, repo, content, filePath) + if err != nil { + log.Fatalf("Error: %v\n", err) + } + fmt.Printf("%v", commentID) }, } diff --git a/cmd/root.go b/cmd/root.go index 104ebc6..e8ec019 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,13 +3,24 @@ package cmd import ( "os" + commentGithub "github.com/gbh-tech/github-pr-commenter/src" + "github.com/charmbracelet/log" "github.com/spf13/cobra" ) +var GithubClient commentGithub.GithubClient + var RootCmd = &cobra.Command{ Use: "commenter", Short: "A CLI to perform operation on GitHub PR issues.", + PersistentPreRun: func(cmd *cobra.Command, args []string) { + token := os.Getenv("GITHUB_TOKEN") + err := commentGithub.NewClient(token, &GithubClient) + if err != nil { + log.Fatalf("Error initializing GitHub client: %v", err) + } + }, } func init() { diff --git a/cmd/update_comment.go b/cmd/update_comment.go index fd47bb1..645da52 100644 --- a/cmd/update_comment.go +++ b/cmd/update_comment.go @@ -1,8 +1,7 @@ package cmd import ( - commentGithub "github.com/gbh-tech/github-pr-commenter/src" - + "github.com/charmbracelet/log" "github.com/spf13/cobra" ) @@ -17,7 +16,10 @@ var updateCommentPrCmd = &cobra.Command{ filePath, _ := cmd.Flags().GetString("filePath") commentID, _ := cmd.Flags().GetInt64("commentID") - commentGithub.UpdateComment(commentID, org, repo, content, filePath) + _, err := GithubClient.UpdateComment(commentID, org, repo, content, filePath) + if err != nil { + log.Fatalf("Error: %v\n", err) + } }, } diff --git a/src/comments.go b/src/comments.go index 3c01819..7fd18de 100644 --- a/src/comments.go +++ b/src/comments.go @@ -3,12 +3,10 @@ package githubComments import ( "context" "fmt" - "os" "strings" "github.com/gbh-tech/github-pr-commenter/utils/files" - "github.com/charmbracelet/log" "github.com/google/go-github/v61/github" ) @@ -17,72 +15,52 @@ type GithubClient struct { Client *github.Client } -func DeclareClient(client *GithubClient) { - token := os.Getenv("GITHUB_TOKEN") +// NewClient creates a new GithubClient with the provided token +func NewClient(token string, client *GithubClient) error { if token == "" { - log.Fatal("Unauthorized: No token present") + return fmt.Errorf("unauthorized: no token present") } client.Ctx = context.Background() client.Client = github.NewClient(nil).WithAuthToken(token) + return nil } -func GetUserComments(pull int, org, repo, content, filePath string) { - var client GithubClient - DeclareClient(&client) - +func (client *GithubClient) GetUserComments(pull int, org, repo, content, filePath string) (int64, error) { comments, _, err := client.Client.Issues.ListComments(client.Ctx, org, repo, pull, nil) if err != nil { - log.Fatalf("Error: %v\n", err) - } - commentBody := content - if filePath != "" { - commentBody = files.ParseFileContent(filePath) + return 0, fmt.Errorf("error listing comments: %v", err) } + + commentBody := files.GetCommentBody(content, filePath) for _, comment := range comments { if strings.Contains(*comment.Body, commentBody) { - fmt.Println(*comment.ID) - return + return *comment.ID, nil } } - log.Error("Comment not found!") -} -func CreateComment(pull int, org, repo, content, filePath string) { - var client GithubClient - DeclareClient(&client) + return 0, fmt.Errorf("comment not found") +} - commentBody := content - if filePath != "" { - commentBody = files.ParseFileContent(filePath) - } +func (client *GithubClient) CreateComment(pull int, org, repo, content, filePath string) (*github.IssueComment, error) { + commentBody := files.GetCommentBody(content, filePath) + comment := &github.IssueComment{Body: github.String(commentBody)} - comment := &github.IssueComment{ - Body: github.String(commentBody), - } commentResp, _, err := client.Client.Issues.CreateComment(client.Ctx, org, repo, pull, comment) if err != nil { - log.Fatalf("Error: %v\n", err) + return nil, fmt.Errorf("error creating comment: %v", err) } - fmt.Printf("Comment was successfully added!\nContent: %v\n", *commentResp.Body) + return commentResp, nil } -func UpdateComment(commentID int64, org, repo, content, filePath string) { - var client GithubClient - DeclareClient(&client) +func (client *GithubClient) UpdateComment(commentID int64, org, repo, content, filePath string) (*github.IssueComment, error) { + commentBody := files.GetCommentBody(content, filePath) + comment := &github.IssueComment{Body: github.String(commentBody)} - commentBody := content - if filePath != "" { - commentBody = files.ParseFileContent(filePath) - } - - comment := &github.IssueComment{ - Body: github.String(commentBody), - } commentResp, _, err := client.Client.Issues.EditComment(client.Ctx, org, repo, commentID, comment) if err != nil { - log.Fatalf("Error: %v\n", err) + return nil, fmt.Errorf("error updating comment: %v", err) } - fmt.Printf("Comment was successfully Updated!\nContent: %v\n", *commentResp.Body) + return commentResp, nil } diff --git a/utils/files/files.go b/utils/files/files.go index 8ca018d..4da1771 100644 --- a/utils/files/files.go +++ b/utils/files/files.go @@ -5,6 +5,7 @@ import ( "os" ) +// Parse content from a file to string func ParseFileContent(filePath string) string { file, err := os.ReadFile(filePath) if err != nil { @@ -12,3 +13,11 @@ func ParseFileContent(filePath string) string { } return string(file) } + +// getCommentBody returns the comment body from either content or filePath +func GetCommentBody(content, filePath string) string { + if filePath != "" { + return ParseFileContent(filePath) + } + return content +} From 8a0ff3c83f8802cdc871a4cdce061cdc9b6f8729 Mon Sep 17 00:00:00 2001 From: Javier Carrillo Date: Mon, 15 Jul 2024 10:56:54 -0400 Subject: [PATCH 2/8] ci: (DSO-2004) Add Validate PR title to CI --- .github/workflows/lint.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 615edc4..9d2a7c9 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -51,3 +51,6 @@ jobs: --from ${{ github.event.pull_request.head.sha }}~${{ github.event.pull_request.commits }} --to ${{ github.event.pull_request.head.sha }} --verbose + + - name: 🔎 Validate PR title with commitlint + run: echo "${{ github.event.pull_request.title }}" | np From b614c76f7d37e354baa8c68477593517d602fc8b Mon Sep 17 00:00:00 2001 From: Javier Carrillo Date: Mon, 15 Jul 2024 11:36:46 -0400 Subject: [PATCH 3/8] chore: (DSO-2004) Move js config to package.json --- .release-it.json | 71 ---------------------------------------- commitlint.config.js | 1 - package.json | 77 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 72 deletions(-) delete mode 100644 .release-it.json delete mode 100644 commitlint.config.js diff --git a/.release-it.json b/.release-it.json deleted file mode 100644 index f0b70bb..0000000 --- a/.release-it.json +++ /dev/null @@ -1,71 +0,0 @@ -{ - "github": { - "release": false, - "releaseName": "v${version}" - }, - "git": { - "commitMessage": "chore: release v${version}", - "tagMatch": "v[0-9]*\\.[0-9]*\\.[0-9]*", - "tagName": "v${version}", - "getLatestTagFromAllRefs": true, - "tagExclude": "*[-]*", - "push": true, - "release": true, - "pushArgs": ["--no-verify", "--follow-tags", "--force"], - "commitArgs": ["--no-verify"] - }, - "plugins": { - "@release-it/conventional-changelog": { - "preset": { - "name": "conventionalcommits", - "types": [ - { - "type": "feat", - "section": "Features" - }, - { - "type": "fix", - "section": "Bug Fixes" - }, - { - "type": "chore", - "section": "Miscellaneous" - }, - { - "type": "docs", - "section": "Miscellaneous" - }, - { - "type": "style", - "section": "Miscellaneous" - }, - { - "type": "refactor", - "section": "Miscellaneous" - }, - { - "type": "perf", - "section": "Miscellaneous" - }, - { - "type": "test", - "section": "Miscellaneous" - }, - { - "type": "build", - "section": "Miscellaneous" - }, - { - "type": "revert", - "section": "Miscellaneous" - }, - { - "type": "ci", - "section": "Miscellaneous" - } - ] - }, - "infile": "CHANGELOG.md" - } - } -} diff --git a/commitlint.config.js b/commitlint.config.js deleted file mode 100644 index 422b194..0000000 --- a/commitlint.config.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = { extends: ['@commitlint/config-conventional'] }; diff --git a/package.json b/package.json index 8f833b2..303ada6 100644 --- a/package.json +++ b/package.json @@ -12,5 +12,82 @@ "commitlint": "^19.2.1", "husky": "^9.0.11", "release-it": "^17.3.0" + }, + "commitlint": { + "extends": [ + "@commitlint/config-conventional" + ], + "defaultIgnores": true + }, + "release-it": { + "github": { + "release": false, + "releaseName": "v${version}" + }, + "git": { + "commitMessage": "chore: release v${version}", + "tagMatch": "v[0-9]*\\.[0-9]*\\.[0-9]*", + "tagName": "v${version}", + "getLatestTagFromAllRefs": true, + "tagExclude": "*[-]*", + "push": true, + "release": true, + "pushArgs": ["--no-verify", "--follow-tags", "--force"], + "commitArgs": ["--no-verify"] + }, + "plugins": { + "@release-it/conventional-changelog": { + "preset": { + "name": "conventionalcommits", + "types": [ + { + "type": "feat", + "section": "Features" + }, + { + "type": "fix", + "section": "Bug Fixes" + }, + { + "type": "chore", + "section": "Miscellaneous" + }, + { + "type": "docs", + "section": "Miscellaneous" + }, + { + "type": "style", + "section": "Miscellaneous" + }, + { + "type": "refactor", + "section": "Miscellaneous" + }, + { + "type": "perf", + "section": "Miscellaneous" + }, + { + "type": "test", + "section": "Miscellaneous" + }, + { + "type": "build", + "section": "Miscellaneous" + }, + { + "type": "revert", + "section": "Miscellaneous" + }, + { + "type": "ci", + "section": "Miscellaneous" + } + ] + }, + "infile": "CHANGELOG.md" + } + } } } From e6ed3bf8dc375443df214740a9e5509dd453588c Mon Sep 17 00:00:00 2001 From: Javier Carrillo Date: Mon, 15 Jul 2024 11:40:13 -0400 Subject: [PATCH 4/8] chore: (DSO-2004) Dir structure updated --- cmd/root.go | 2 +- src/{ => comments}/comments.go | 2 +- {utils/files => src/utils}/files.go | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename src/{ => comments}/comments.go (96%) rename {utils/files => src/utils}/files.go (100%) diff --git a/cmd/root.go b/cmd/root.go index e8ec019..b44149d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,7 +3,7 @@ package cmd import ( "os" - commentGithub "github.com/gbh-tech/github-pr-commenter/src" + commentGithub "github.com/gbh-tech/github-pr-commenter/src/comments" "github.com/charmbracelet/log" "github.com/spf13/cobra" diff --git a/src/comments.go b/src/comments/comments.go similarity index 96% rename from src/comments.go rename to src/comments/comments.go index 7fd18de..553aed1 100644 --- a/src/comments.go +++ b/src/comments/comments.go @@ -5,7 +5,7 @@ import ( "fmt" "strings" - "github.com/gbh-tech/github-pr-commenter/utils/files" + files "github.com/gbh-tech/github-pr-commenter/src/utils" "github.com/google/go-github/v61/github" ) diff --git a/utils/files/files.go b/src/utils/files.go similarity index 100% rename from utils/files/files.go rename to src/utils/files.go From 7a22e49b27845d18782e4fe7260c4327c0b9ef11 Mon Sep 17 00:00:00 2001 From: Javier Carrillo Date: Mon, 15 Jul 2024 11:56:35 -0400 Subject: [PATCH 5/8] chore: (DSO-2004) Improve debug experience on githubComments package --- src/comments/comments.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/comments/comments.go b/src/comments/comments.go index 553aed1..2221711 100644 --- a/src/comments/comments.go +++ b/src/comments/comments.go @@ -7,6 +7,7 @@ import ( files "github.com/gbh-tech/github-pr-commenter/src/utils" + "github.com/charmbracelet/log" "github.com/google/go-github/v61/github" ) @@ -18,7 +19,7 @@ type GithubClient struct { // NewClient creates a new GithubClient with the provided token func NewClient(token string, client *GithubClient) error { if token == "" { - return fmt.Errorf("unauthorized: no token present") + return fmt.Errorf("Unauthorized: GitHub Token not present.") } client.Ctx = context.Background() client.Client = github.NewClient(nil).WithAuthToken(token) @@ -28,7 +29,7 @@ func NewClient(token string, client *GithubClient) error { func (client *GithubClient) GetUserComments(pull int, org, repo, content, filePath string) (int64, error) { comments, _, err := client.Client.Issues.ListComments(client.Ctx, org, repo, pull, nil) if err != nil { - return 0, fmt.Errorf("error listing comments: %v", err) + return 0, fmt.Errorf("PR ID: %v\nError listing comments: %v", pull, err) } commentBody := files.GetCommentBody(content, filePath) @@ -38,7 +39,7 @@ func (client *GithubClient) GetUserComments(pull int, org, repo, content, filePa } } - return 0, fmt.Errorf("comment not found") + return 0, fmt.Errorf("PR ID: %v\nComment not found", pull) } func (client *GithubClient) CreateComment(pull int, org, repo, content, filePath string) (*github.IssueComment, error) { @@ -47,8 +48,9 @@ func (client *GithubClient) CreateComment(pull int, org, repo, content, filePath commentResp, _, err := client.Client.Issues.CreateComment(client.Ctx, org, repo, pull, comment) if err != nil { - return nil, fmt.Errorf("error creating comment: %v", err) + return nil, fmt.Errorf("PR ID: %v\nError creating comment: %v", pull, err) } + log.Info("Comment Created successfully") return commentResp, nil } @@ -59,8 +61,9 @@ func (client *GithubClient) UpdateComment(commentID int64, org, repo, content, f commentResp, _, err := client.Client.Issues.EditComment(client.Ctx, org, repo, commentID, comment) if err != nil { - return nil, fmt.Errorf("error updating comment: %v", err) + return nil, fmt.Errorf("Comment ID: %v\nError updating comment: %v", commentID, err) } + log.Info("Comment updated successfully") return commentResp, nil } From 2db4134aaa4d5a0f8c893ea405c90874eaf3ceb5 Mon Sep 17 00:00:00 2001 From: Javier Carrillo Date: Mon, 15 Jul 2024 14:12:53 -0400 Subject: [PATCH 6/8] ci: (DSO-2004) Fix PR title validation with commitlint --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 9d2a7c9..a217c3f 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -53,4 +53,4 @@ jobs: --verbose - name: 🔎 Validate PR title with commitlint - run: echo "${{ github.event.pull_request.title }}" | np + run: echo "${{ github.event.pull_request.title }}" | npx commitlint From f432af977a118da987a87b14797a30254ff12a5a Mon Sep 17 00:00:00 2001 From: Javier Carrillo Date: Mon, 26 Aug 2024 17:01:04 -0400 Subject: [PATCH 7/8] chore: (DSO-2004) Update comments descriptions --- cmd/create_comment.go | 2 +- cmd/get_comment.go | 2 +- cmd/update_comment.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/create_comment.go b/cmd/create_comment.go index 922f794..6221af0 100644 --- a/cmd/create_comment.go +++ b/cmd/create_comment.go @@ -9,7 +9,7 @@ var createCommentPrCmd = &cobra.Command{ Use: "create", Aliases: []string{"new"}, Short: "Create new comment on PR", - Example: "atlas comment create [flags]", + Example: "commenter create [flags]", Run: func(cmd *cobra.Command, args []string) { pull, _ := cmd.Flags().GetInt("pull") repo, _ := cmd.Flags().GetString("repo") diff --git a/cmd/get_comment.go b/cmd/get_comment.go index 53af8dd..91f7491 100644 --- a/cmd/get_comment.go +++ b/cmd/get_comment.go @@ -10,7 +10,7 @@ import ( var getCommentPrCmd = &cobra.Command{ Use: "get", Short: "Get message ID based on text", - Example: "atlas comment get [flags]", + Example: "commenter get [flags]", Run: func(cmd *cobra.Command, args []string) { pull, _ := cmd.Flags().GetInt("pull") repo, _ := cmd.Flags().GetString("repo") diff --git a/cmd/update_comment.go b/cmd/update_comment.go index 645da52..bcacca4 100644 --- a/cmd/update_comment.go +++ b/cmd/update_comment.go @@ -8,7 +8,7 @@ import ( var updateCommentPrCmd = &cobra.Command{ Use: "update", Short: "Update comment on PR", - Example: "atlas comment update [flags]", + Example: "commenter update [flags]", Run: func(cmd *cobra.Command, args []string) { repo, _ := cmd.Flags().GetString("repo") org, _ := cmd.Flags().GetString("org") From 5fdbb8893f1fd35d2f513cd821ee408bd1ae7295 Mon Sep 17 00:00:00 2001 From: Javier Carrillo Date: Mon, 26 Aug 2024 17:04:26 -0400 Subject: [PATCH 8/8] chore: (DSO-2004) Update commentGithub package name --- cmd/root.go | 6 +++--- src/comments/comments.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index b44149d..99d0e4e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,20 +3,20 @@ package cmd import ( "os" - commentGithub "github.com/gbh-tech/github-pr-commenter/src/comments" + comments "github.com/gbh-tech/github-pr-commenter/src/comments" "github.com/charmbracelet/log" "github.com/spf13/cobra" ) -var GithubClient commentGithub.GithubClient +var GithubClient comments.GithubClient var RootCmd = &cobra.Command{ Use: "commenter", Short: "A CLI to perform operation on GitHub PR issues.", PersistentPreRun: func(cmd *cobra.Command, args []string) { token := os.Getenv("GITHUB_TOKEN") - err := commentGithub.NewClient(token, &GithubClient) + err := comments.NewClient(token, &GithubClient) if err != nil { log.Fatalf("Error initializing GitHub client: %v", err) } diff --git a/src/comments/comments.go b/src/comments/comments.go index 2221711..f469973 100644 --- a/src/comments/comments.go +++ b/src/comments/comments.go @@ -1,4 +1,4 @@ -package githubComments +package comments import ( "context"