Skip to content
This repository has been archived by the owner on Aug 26, 2023. It is now read-only.

Commit

Permalink
feature(main): add issue comment (#45)
Browse files Browse the repository at this point in the history
Signed-off-by: cuisongliu <cuisongliu@qq.com>
  • Loading branch information
cuisongliu authored Jun 27, 2023
1 parent 34fd6ca commit 03a9bf9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 107 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test-issue-reply.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
run: |
gh-rebot action
env:
SEALOS_TYPE: "issue_comment_reply"
SEALOS_TYPE: "issue_comment"
# SEALOS_FILENAME: "templates/template.md"
SEALOS_COMMENT: "dfffdjh fdhjfhj gfrjghjf"
GH_TOKEN: "${{ secrets.GH_PAT }}"
Expand All @@ -33,14 +33,14 @@ jobs:
gh-rebot action
if: startswith(github.event.comment.body, '/comment')
env:
SEALOS_TYPE: "issue_comment_reply"
SEALOS_TYPE: "issue_comment"
SEALOS_FILENAME: "templates/template.md"
GH_TOKEN: "${{ secrets.GH_PAT }}"
- run: |
gh-rebot action
if: startswith(github.event.comment.body, '/reply')
env:
SEALOS_TYPE: "issue_comment_reply"
SEALOS_TYPE: "issue_comment"
SEALOS_ISREPLY: "true"
SEALOS_FILENAME: "templates/template.md"
GH_TOKEN: "${{ secrets.GH_PAT }}"
4 changes: 2 additions & 2 deletions .github/workflows/test-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
run: |
gh-rebot action
env:
SEALOS_TYPE: "pr_comment"
SEALOS_TYPE: "issue_comment"
SEALOS_FILENAME: "templates/template.md"
SEALOS_REPLACE_TAG: "DEFAULT"
GH_TOKEN: "${{ secrets.GH_PAT }}"
Expand All @@ -34,7 +34,7 @@ jobs:
run: |
gh-rebot action
env:
SEALOS_TYPE: "pr_comment"
SEALOS_TYPE: "issue_comment"
SEALOS_FILENAME: "templates/template.md"
SEALOS_REPLACE_TAG: "DEFAULT"
GH_TOKEN: "${{ secrets.GH_PAT }}"
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,11 @@ message:
```

- [x] 支持PR文本替换回复
> 该功能v0.0.6 支持
> 该功能v0.0.8-rc2 支持
- `SEALOS_TYPE: "pr_comment"` # PR文本替换回复
- `SEALOS_FILENAME: "README.md"` # PR文本替换回复文件位置
- `SEALOS_COMMENT: "/xxxx"` # comment的内容
- `SEALOS_REPLACE_TAG: "TAG"` # 寻找标记,根据这个标记进行替换

- [x] issue创建comment
> 该功能v0.0.7-rc2支持

- `SEALOS_TYPE: "issue_comment_reply"` # issue创建comment,支持回复
- `SEALOS_FILENAME: "README.md"` # issue回复内容如果多可以写文件
- `SEALOS_COMMENT: "/xxxx"` # comment的内容
- `SEALOS_ISREPLY: "true"` # 是否回复,根据当前的comment的内容追加

- [x] issue自动创建
Expand Down
6 changes: 2 additions & 4 deletions cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ var actionCmd = &cobra.Command{
switch actionType {
case "/comment":
err = action.CommentEngine()
case "pr_comment":
err = action.PRComment()
case "issue_comment_reply":
err = action.CommentReply()
case "issue_comment":
err = action.IssueComment()
case "issue_renew":
err = action.IssueRenew()
default:
Expand Down
66 changes: 0 additions & 66 deletions pkg/action/action_comment_reply.go

This file was deleted.

50 changes: 26 additions & 24 deletions pkg/action/action_pr_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,21 @@ import (
"github.com/google/go-github/v39/github"
github_go "github.com/labring/gh-rebot/pkg/github-go"
"github.com/labring/gh-rebot/pkg/types"
"github.com/labring/gh-rebot/pkg/utils"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"os"
"strings"
)

// PRComment is a action to comment on PR
func PRComment() error {
// IssueComment is a action to comment on PR
func IssueComment() error {
fileName, _ := GetEnvFromAction("filename")
commentBody, _ := GetEnvFromAction("comment")
if fileName == "" && commentBody == "" {
return fmt.Errorf("filename or comment is empty")
}
replaceTag, err := GetEnvFromAction("replace_tag")
if err != nil {
return err
}

replaceTag, _ := GetEnvFromAction("replace_tag")
prNumber := int(types.ActionConfigJSON.IssueOrPRNumber)
if err != nil {
return err
}

if fileName != "" {
fileContent, err := os.ReadFile(fileName)
Expand All @@ -51,6 +46,17 @@ func PRComment() error {
}
commentBody = string(fileContent)
}
hiddenReplace := fmt.Sprintf("<!-- %s -->", replaceTag)
if replaceTag != "" {
commentBody = commentBody + "\n" + hiddenReplace
}

isReply, _ := GetEnvFromAction("isReply")
if isReply == "true" {
replyBody, _, _ := unstructured.NestedString(types.ActionConfigJSON.Data, "comment", "body")
replyBody = utils.QuoteReply(replyBody)
commentBody = strings.Join([]string{replyBody, commentBody}, "\r\n\r\n")
}

owner, repo, err := getRepo("")
if err != nil {
Expand All @@ -63,27 +69,23 @@ func PRComment() error {
if err != nil {
return fmt.Errorf("Issues.ListComments returned error: %v", err)
}
hiddenReplace := fmt.Sprintf("<!-- %s -->", replaceTag)
content := commentBody + "\n" + hiddenReplace

// Checks existing comments, edits if match found
for _, comment := range comments {
if comment.Body != nil && comment.ID != nil {
if *comment.Body == content {
logger.Debug("The comment %d has been already added to the pull request. Skipping...", *comment.ID)
return nil
} else if hiddenReplace != "" && strings.LastIndex(*comment.Body, hiddenReplace) != -1 {
_, _, err = client.Issues.EditComment(ctx, owner, repo, *comment.ID, &github.IssueComment{Body: github.String(content)})
if err != nil {
return fmt.Errorf("Issues.EditComment returned error: %v", err)
if replaceTag != "" {
for _, comment := range comments {
if comment.Body != nil && comment.ID != nil {
if hiddenReplace != "" && strings.LastIndex(*comment.Body, hiddenReplace) != -1 {
_, _, err = client.Issues.EditComment(ctx, owner, repo, *comment.ID, &github.IssueComment{Body: github.String(commentBody)})
if err != nil {
return fmt.Errorf("Issues.EditComment returned error: %v", err)
}
return nil
}
return nil
}
}
}

// Creates new comment
_, _, err = client.Issues.CreateComment(ctx, owner, repo, prNumber, &github.IssueComment{Body: github.String(content)})
_, _, err = client.Issues.CreateComment(ctx, owner, repo, prNumber, &github.IssueComment{Body: github.String(commentBody)})
if err != nil {
return fmt.Errorf("Issues.CreateComment returned error: %v", err)
}
Expand Down

0 comments on commit 03a9bf9

Please sign in to comment.