Skip to content

Commit

Permalink
refactor: (DSP-2004) Apply good practice to code
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercm1410 committed Jul 15, 2024
1 parent 5a4c160 commit f0f3eb7
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 51 deletions.
8 changes: 5 additions & 3 deletions cmd/create_comment.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package cmd

import (
commentGithub "github.com/gbh-tech/github-pr-commenter/src"

"github.com/charmbracelet/log"
"github.com/spf13/cobra"
)

Expand All @@ -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)
}
},
}

Expand Down
9 changes: 7 additions & 2 deletions cmd/get_comment.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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)
},
}

Expand Down
11 changes: 11 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 5 additions & 3 deletions cmd/update_comment.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package cmd

import (
commentGithub "github.com/gbh-tech/github-pr-commenter/src"

"github.com/charmbracelet/log"
"github.com/spf13/cobra"
)

Expand All @@ -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)
}
},
}

Expand Down
64 changes: 21 additions & 43 deletions src/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
9 changes: 9 additions & 0 deletions utils/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ import (
"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
}

0 comments on commit f0f3eb7

Please sign in to comment.