diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 615edc4..a217c3f 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 }}" | npx commitlint 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/cmd/create_comment.go b/cmd/create_comment.go index 6c28a87..6221af0 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" ) @@ -10,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") @@ -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..91f7491 100644 --- a/cmd/get_comment.go +++ b/cmd/get_comment.go @@ -1,15 +1,16 @@ package cmd import ( - commentGithub "github.com/gbh-tech/github-pr-commenter/src" + "fmt" + "github.com/charmbracelet/log" "github.com/spf13/cobra" ) 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") @@ -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..99d0e4e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,13 +3,24 @@ package cmd import ( "os" + comments "github.com/gbh-tech/github-pr-commenter/src/comments" + "github.com/charmbracelet/log" "github.com/spf13/cobra" ) +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 := comments.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..bcacca4 100644 --- a/cmd/update_comment.go +++ b/cmd/update_comment.go @@ -1,15 +1,14 @@ package cmd import ( - commentGithub "github.com/gbh-tech/github-pr-commenter/src" - + "github.com/charmbracelet/log" "github.com/spf13/cobra" ) 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") @@ -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/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" + } + } } } diff --git a/src/comments.go b/src/comments.go deleted file mode 100644 index 3c01819..0000000 --- a/src/comments.go +++ /dev/null @@ -1,88 +0,0 @@ -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" -) - -type GithubClient struct { - Ctx context.Context - Client *github.Client -} - -func DeclareClient(client *GithubClient) { - token := os.Getenv("GITHUB_TOKEN") - if token == "" { - log.Fatal("Unauthorized: No token present") - } - client.Ctx = context.Background() - client.Client = github.NewClient(nil).WithAuthToken(token) -} - -func GetUserComments(pull int, org, repo, content, filePath string) { - var client GithubClient - DeclareClient(&client) - - 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) - } - for _, comment := range comments { - if strings.Contains(*comment.Body, commentBody) { - fmt.Println(*comment.ID) - return - } - } - log.Error("Comment not found!") -} - -func CreateComment(pull int, org, repo, content, filePath string) { - var client GithubClient - DeclareClient(&client) - - commentBody := content - if filePath != "" { - commentBody = files.ParseFileContent(filePath) - } - - 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) - } - - fmt.Printf("Comment was successfully added!\nContent: %v\n", *commentResp.Body) -} - -func UpdateComment(commentID int64, org, repo, content, filePath string) { - var client GithubClient - DeclareClient(&client) - - 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) - } - - fmt.Printf("Comment was successfully Updated!\nContent: %v\n", *commentResp.Body) -} diff --git a/src/comments/comments.go b/src/comments/comments.go new file mode 100644 index 0000000..f469973 --- /dev/null +++ b/src/comments/comments.go @@ -0,0 +1,69 @@ +package comments + +import ( + "context" + "fmt" + "strings" + + files "github.com/gbh-tech/github-pr-commenter/src/utils" + + "github.com/charmbracelet/log" + "github.com/google/go-github/v61/github" +) + +type GithubClient struct { + Ctx context.Context + Client *github.Client +} + +// NewClient creates a new GithubClient with the provided token +func NewClient(token string, client *GithubClient) error { + if token == "" { + return fmt.Errorf("Unauthorized: GitHub Token not present.") + } + client.Ctx = context.Background() + client.Client = github.NewClient(nil).WithAuthToken(token) + return nil +} + +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("PR ID: %v\nError listing comments: %v", pull, err) + } + + commentBody := files.GetCommentBody(content, filePath) + for _, comment := range comments { + if strings.Contains(*comment.Body, commentBody) { + return *comment.ID, nil + } + } + + 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) { + commentBody := files.GetCommentBody(content, filePath) + comment := &github.IssueComment{Body: github.String(commentBody)} + + commentResp, _, err := client.Client.Issues.CreateComment(client.Ctx, org, repo, pull, comment) + if err != nil { + return nil, fmt.Errorf("PR ID: %v\nError creating comment: %v", pull, err) + } + log.Info("Comment Created successfully") + + return commentResp, nil +} + +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)} + + commentResp, _, err := client.Client.Issues.EditComment(client.Ctx, org, repo, commentID, comment) + if err != nil { + return nil, fmt.Errorf("Comment ID: %v\nError updating comment: %v", commentID, err) + } + log.Info("Comment updated successfully") + + return commentResp, nil +} diff --git a/src/utils/files.go b/src/utils/files.go new file mode 100644 index 0000000..4da1771 --- /dev/null +++ b/src/utils/files.go @@ -0,0 +1,23 @@ +package files + +import ( + "fmt" + "os" +) + +// Parse content from a file to string +func ParseFileContent(filePath string) string { + file, err := os.ReadFile(filePath) + if err != nil { + fmt.Print(err) + } + 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 +} diff --git a/utils/files/files.go b/utils/files/files.go deleted file mode 100644 index 8ca018d..0000000 --- a/utils/files/files.go +++ /dev/null @@ -1,14 +0,0 @@ -package files - -import ( - "fmt" - "os" -) - -func ParseFileContent(filePath string) string { - file, err := os.ReadFile(filePath) - if err != nil { - fmt.Print(err) - } - return string(file) -}