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

Commit e6fd358

Browse files
authored
add comment feature (#34)
* feature(main): add markdown utils Signed-off-by: cuisongliu <cuisongliu@qq.com> * feature(main): add issue_comment_reply action Signed-off-by: cuisongliu <cuisongliu@qq.com> * feature(main): add issue_comment_reply action Signed-off-by: cuisongliu <cuisongliu@qq.com> * feature(main): add issue_comment_reply action Signed-off-by: cuisongliu <cuisongliu@qq.com> --------- Signed-off-by: cuisongliu <cuisongliu@qq.com>
1 parent 3b9b56c commit e6fd358

File tree

9 files changed

+181
-27
lines changed

9 files changed

+181
-27
lines changed

.github/gh-bot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: v1
22
debug: true
33
type: action
44
action:
5-
printConfig: false
5+
printConfig: true
66
release:
77
retry: 15s
88
actionName: Release
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Github Rebot for Sealos test issue reply
2+
3+
on:
4+
issue_comment:
5+
types:
6+
- created
7+
8+
jobs:
9+
linkChecker:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Set up Go 1.x
13+
uses: actions/setup-go@v2
14+
with:
15+
go-version: 1.20.x
16+
- uses: actions/checkout@v3
17+
- name: Build file
18+
run: |
19+
make install
20+
- if: startswith(github.event.comment.body, '/comment')
21+
run: |
22+
gh-rebot action
23+
env:
24+
SEALOS_TYPE: "issue_comment_reply"
25+
# SEALOS_FILENAME: "templates/template.md"
26+
SEALOS_COMMENT: "dfffdjh fdhjfhj gfrjghjf"
27+
GH_TOKEN: "${{ secrets.GH_PAT }}"
28+
- name: using file
29+
if: startswith(github.event.comment.body, '/comment')
30+
run: |
31+
echo "somethings.....default" >> templates/template.md
32+
- run: |
33+
gh-rebot action
34+
if: startswith(github.event.comment.body, '/comment')
35+
env:
36+
SEALOS_TYPE: "issue_comment_reply"
37+
SEALOS_FILENAME: "templates/template.md"
38+
GH_TOKEN: "${{ secrets.GH_PAT }}"
39+
- run: |
40+
gh-rebot action
41+
if: startswith(github.event.comment.body, '/reply')
42+
env:
43+
SEALOS_TYPE: "issue_comment_reply"
44+
SEALOS_ISREPLY: "true"
45+
SEALOS_FILENAME: "templates/template.md"
46+
GH_TOKEN: "${{ secrets.GH_PAT }}"

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,31 @@ message:
111111
SEALOS_TYPE: "/comment"
112112
GH_TOKEN: "${{ secrets.GH_PAT }}"
113113
```
114-
**版本支持**: 支持v0.0.6-rc6及以上版本
114+
**版本支持**:
115115

116116
- [x] 支持release
117+
> 目标分支为`release-v1.2`,如果没有则默认为`main`分支,该功能v0.0.7-rc1支持
118+
117119
`SEALOS_TYPE: "/comment"` # 评论触发
118120
example:
119121
```markdown
120122
/release v1.2.1
121123
/release v1.2.3 release-v1.2
122124
```
123-
> 目标分支为`release-v1.2`,如果没有则默认为`main`分支,该功能v0.0.7-rc1支持
125+
124126
- [x] 支持PR文本替换回复
127+
> 该功能v0.0.6 支持
125128
- `SEALOS_TYPE: "pr_comment"` # PR文本替换回复
126-
- `SEALOS_filename: "README.md"` # PR文本替换回复文件位置
127-
- `SEALOS_replace_tag: "TAG"` # 寻找标记,根据这个标记进行替换
129+
- `SEALOS_FILENAME: "README.md"` # PR文本替换回复文件位置
130+
- `SEALOS_REPLACE_TAG: "TAG"` # 寻找标记,根据这个标记进行替换
131+
132+
- [x] issue创建comment
133+
> 该功能v0.0.7-rc2支持
134+
135+
- `SEALOS_TYPE: "issue_comment_reply"` # issue创建comment,支持回复
136+
- `SEALOS_FILENAME: "README.md"` # issue内容如果多可以写文件
137+
- `SEALOS_COMMENT: "/xxxx"` # comment的内容
138+
- `SEALOS_ISREPLY: "true"` # 是否回复,根据当前的comment的内容追加
128139

129140
## Roadmap
130141

cmd/action.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ var actionCmd = &cobra.Command{
2828
err = action.CommentEngine()
2929
case "pr_comment":
3030
err = action.PRComment()
31+
case "issue_comment_reply":
32+
err = action.CommentReply()
3133
default:
3234
err = fmt.Errorf("not support action type")
3335
}

pkg/action/action.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ limitations under the License.
1717
package action
1818

1919
import (
20+
"fmt"
2021
"github.com/cuisongliu/logger"
2122
"github.com/labring/gh-rebot/pkg/types"
2223
"github.com/labring/gh-rebot/pkg/workflow"
2324
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25+
"os"
2426
"strings"
2527
)
2628

@@ -49,3 +51,24 @@ func CommentEngine() error {
4951
}
5052
return nil
5153
}
54+
55+
func GetEnvFromAction(key string) (string, error) {
56+
allKey := strings.ToUpper("sealos_" + key)
57+
val, _ := os.LookupEnv(allKey)
58+
if val == "" {
59+
return "", fmt.Errorf("not found %s", allKey)
60+
}
61+
return val, nil
62+
}
63+
64+
func getRepo() (string, string, error) {
65+
repo := os.Getenv("GITHUB_REPOSITORY") // 获取环境变量GITHUB_REF
66+
if repo == "" {
67+
return "", "", fmt.Errorf("not found GITHUB_REPOSITORY")
68+
}
69+
split := strings.Split(repo, "/")
70+
if len(split) != 2 {
71+
return "", "", fmt.Errorf("GITHUB_REPOSITORY format error")
72+
}
73+
return split[0], types.ActionConfigJSON.RepoName, nil
74+
}

pkg/action/action_comment_reply.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright 2023 cuisongliu@qq.com.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package action
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"github.com/cuisongliu/logger"
23+
"github.com/google/go-github/v39/github"
24+
github_go "github.com/labring/gh-rebot/pkg/github-go"
25+
"github.com/labring/gh-rebot/pkg/types"
26+
"github.com/labring/gh-rebot/pkg/utils"
27+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
28+
"os"
29+
"strings"
30+
)
31+
32+
func CommentReply() error {
33+
issueNumber := int(types.ActionConfigJSON.IssueOrPRNumber)
34+
fileName, _ := GetEnvFromAction("filename")
35+
comment, _ := GetEnvFromAction("comment")
36+
isReply, _ := GetEnvFromAction("isReply")
37+
if fileName == "" && comment == "" {
38+
return fmt.Errorf("filename or comment is empty")
39+
}
40+
41+
if fileName != "" {
42+
fileContent, err := os.ReadFile(fileName)
43+
if err != nil {
44+
return err
45+
}
46+
comment = string(fileContent)
47+
}
48+
49+
if isReply == "true" {
50+
replyBody, _, _ := unstructured.NestedString(types.ActionConfigJSON.Data, "comment", "body")
51+
replyBody = utils.QuoteReply(replyBody)
52+
comment = strings.Join([]string{replyBody, comment}, "\r\n\r\n")
53+
}
54+
55+
owner, repo, err := getRepo()
56+
if err != nil {
57+
return err
58+
}
59+
logger.Info("repo:%s, issueNumber: %d", repo, issueNumber)
60+
logger.Debug("comment: %s", comment)
61+
ctx := context.Background()
62+
client := github_go.GithubClient(ctx)
63+
githubComment := &github.IssueComment{Body: github.String(comment)}
64+
_, _, err = client.Issues.CreateComment(ctx, owner, repo, issueNumber, githubComment)
65+
return err
66+
}

pkg/action/action_pr_comment.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,6 @@ import (
2727
"strings"
2828
)
2929

30-
func GetEnvFromAction(key string) (string, error) {
31-
allKey := strings.ToUpper("sealos_" + key)
32-
val, _ := os.LookupEnv(allKey)
33-
if val == "" {
34-
return "", fmt.Errorf("not found %s", allKey)
35-
}
36-
return val, nil
37-
}
38-
39-
func getRepo() (string, string, error) {
40-
repo := os.Getenv("GITHUB_REPOSITORY") // 获取环境变量GITHUB_REF
41-
if repo == "" {
42-
return "", "", fmt.Errorf("not found GITHUB_REPOSITORY")
43-
}
44-
split := strings.Split(repo, "/")
45-
if len(split) != 2 {
46-
return "", "", fmt.Errorf("GITHUB_REPOSITORY format error")
47-
}
48-
return split[0], types.ActionConfigJSON.RepoName, nil
49-
}
50-
5130
// PRComment is a action to comment on PR
5231
func PRComment() error {
5332
fileName, err := GetEnvFromAction("filename")

pkg/types/load.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func ParseConfig(filePath string) (*Config, error) {
3838
if err != nil {
3939
return nil, err
4040
}
41-
if token, ok := os.LookupEnv("GH_TOKEN"); ok {
41+
if token, ok := os.LookupEnv("GH_TOKEN"); ok && token != "" {
4242
config.Token = token
4343
} else {
4444
return nil, errors.New("error: GH_TOKEN is not set. Please set the GH_TOKEN environment variable to enable authentication and access to the GitHub API")

pkg/utils/markdown.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright 2023 cuisongliu@qq.com.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package utils
18+
19+
import "strings"
20+
21+
func QuoteReply(text string) string {
22+
lines := strings.Split(text, "\r\n")
23+
for i, line := range lines {
24+
lines[i] = "> " + line
25+
}
26+
return strings.Join(lines, "\r\n")
27+
}

0 commit comments

Comments
 (0)