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

Commit b61fe47

Browse files
authored
add report (#35)
* feature(main): add issue_renew action Signed-off-by: cuisongliu <cuisongliu@qq.com> * feature(main): add issue_renew action Signed-off-by: cuisongliu <cuisongliu@qq.com> * feature(main): add issue_renew action Signed-off-by: cuisongliu <cuisongliu@qq.com> * feature(main): add issue_renew action Signed-off-by: cuisongliu <cuisongliu@qq.com> * feature(main): add issue_renew action Signed-off-by: cuisongliu <cuisongliu@qq.com> * feature(main): add issue_renew action Signed-off-by: cuisongliu <cuisongliu@qq.com> * feature(main): add issue_renew action Signed-off-by: cuisongliu <cuisongliu@qq.com> * feature(main): add issue_renew action Signed-off-by: cuisongliu <cuisongliu@qq.com> * feature(main): add issue_renew action Signed-off-by: cuisongliu <cuisongliu@qq.com> --------- Signed-off-by: cuisongliu <cuisongliu@qq.com>
1 parent e6fd358 commit b61fe47

File tree

7 files changed

+233
-2
lines changed

7 files changed

+233
-2
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Github Rebot for Sealos test issue renew
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches: [ "*" ]
7+
jobs:
8+
linkChecker:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Set up Go 1.x
12+
uses: actions/setup-go@v2
13+
with:
14+
go-version: 1.20.x
15+
- uses: actions/checkout@v3
16+
- name: Build file
17+
run: |
18+
make install
19+
- run: |
20+
gh-rebot action
21+
env:
22+
SEALOS_TYPE: "issue_renew"
23+
SEALOS_ISSUE_TITLE: "[DaylyReport] Auto build for sealos"
24+
SEALOS_ISSUE_BODY: "xxxx"
25+
SEALOS_ISSUE_BODYFILE: "README.md"
26+
SEALOS_ISSUE_LABEL: "dayly-report"
27+
SEALOS_ISSUE_TYPE: "day"
28+
GH_TOKEN: "${{ secrets.GH_PAT }}"

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,20 @@ message:
133133
> 该功能v0.0.7-rc2支持
134134
135135
- `SEALOS_TYPE: "issue_comment_reply"` # issue创建comment,支持回复
136-
- `SEALOS_FILENAME: "README.md"` # issue内容如果多可以写文件
136+
- `SEALOS_FILENAME: "README.md"` # issue回复内容如果多可以写文件
137137
- `SEALOS_COMMENT: "/xxxx"` # comment的内容
138138
- `SEALOS_ISREPLY: "true"` # 是否回复,根据当前的comment的内容追加
139139

140+
- [x] issue自动创建
141+
> 该功能v0.0.7-rc2支持
142+
143+
- `SEALOS_TYPE: "issue_renew"` # issue创建comment,支持回复
144+
- `SEALOS_ISSUE_TITLE: "dxxxx"` # issue的title
145+
- `SEALOS_ISSUE_BODY: "xxxx"` # issue内容
146+
- `SEALOS_ISSUE_BODYFILE: "README.md"` # issue内容如果多可以写文件
147+
- `SEALOS_ISSUE_LABEL: "dayly-report"` # 新增issue的label
148+
- `SEALOS_ISSUE_TYPE: "day"` # day和week , 会在titles上自动加上日期
149+
140150
## Roadmap
141151

142152
- [ ] 支持label操作

cmd/action.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/cuisongliu/logger"
99
"github.com/labring/gh-rebot/pkg/action"
1010
"github.com/labring/gh-rebot/pkg/setup"
11+
"github.com/labring/gh-rebot/pkg/types"
12+
"github.com/labring/gh-rebot/pkg/utils"
1113
"os"
1214

1315
"github.com/spf13/cobra"
@@ -23,13 +25,21 @@ var actionCmd = &cobra.Command{
2325
os.Exit(1)
2426
}
2527
logger.Debug("action type is ", actionType)
28+
if !utils.In([]string{"issue_renew"}, actionType) {
29+
if err := types.ValidateIssueOrPRNumber(); err != nil {
30+
logger.Error(err)
31+
os.Exit(1)
32+
}
33+
}
2634
switch actionType {
2735
case "/comment":
2836
err = action.CommentEngine()
2937
case "pr_comment":
3038
err = action.PRComment()
3139
case "issue_comment_reply":
3240
err = action.CommentReply()
41+
case "issue_renew":
42+
err = action.IssueRenew()
3343
default:
3444
err = fmt.Errorf("not support action type")
3545
}

pkg/action/action_issue_renew.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
"github.com/cuisongliu/logger"
22+
"github.com/google/go-github/v39/github"
23+
github_go "github.com/labring/gh-rebot/pkg/github-go"
24+
"github.com/labring/gh-rebot/pkg/types"
25+
"github.com/labring/gh-rebot/pkg/utils"
26+
"os"
27+
"time"
28+
)
29+
30+
// IssueRenew is new a issue
31+
func IssueRenew() error {
32+
issueTitle, err := GetEnvFromAction("issue_title")
33+
if err != nil {
34+
return err
35+
}
36+
label, _ := GetEnvFromAction("issue_label")
37+
if err != nil {
38+
return err
39+
}
40+
body, _ := GetEnvFromAction("issue_body")
41+
bodyfile, _ := GetEnvFromAction("issue_bodyfile")
42+
if bodyfile != "" {
43+
bodyBytes, _ := os.ReadFile(bodyfile)
44+
body = string(bodyBytes)
45+
}
46+
issueType, err := GetEnvFromAction("issue_type")
47+
if err != nil {
48+
return err
49+
}
50+
switch issueType {
51+
case "day":
52+
issueTitle = issueTitle + " " + utils.FormatDay(time.Now())
53+
case "week":
54+
start, end := utils.FormatWeek(time.Now())
55+
issueTitle = issueTitle + " " + start + " to " + end
56+
default:
57+
issueTitle = issueTitle + " " + utils.FormatDay(time.Now())
58+
}
59+
60+
owner, repo, err := getRepo()
61+
if err != nil {
62+
return err
63+
}
64+
ctx := context.Background()
65+
client := github_go.GithubClient(ctx)
66+
67+
issues, _, err := client.Issues.ListByRepo(ctx, owner, repo, &github.IssueListByRepoOptions{
68+
Creator: types.GlobalsBotConfig.Bot.Username,
69+
})
70+
logger.Info("repo:%s, issueTitle: %s, owner: %s, create: %s", repo, issueTitle, owner, types.GlobalsBotConfig.Bot.Username)
71+
if err != nil {
72+
return err
73+
}
74+
hasIssue := false
75+
for _, issue := range issues {
76+
logger.Debug("issue: %s, state: %s, id: %d", issue.GetTitle(), issue.GetState(), issue.GetID())
77+
if issue.GetTitle() == issueTitle && issue.GetState() != "closed" {
78+
logger.Info("issue already exist, issue: %s", issue.GetTitle())
79+
hasIssue = true
80+
return nil
81+
} else {
82+
state := "closed"
83+
issueRequest := &github.IssueRequest{
84+
State: &state,
85+
}
86+
_, _, _ = client.Issues.Edit(ctx, owner, repo, issue.GetNumber(), issueRequest)
87+
logger.Info("close issue: %s", issue.GetTitle())
88+
}
89+
}
90+
logger.Warn("issue not exist, issue: %s", issueTitle)
91+
if !hasIssue {
92+
issueRequest := &github.IssueRequest{
93+
Title: &issueTitle,
94+
Body: &body,
95+
Labels: &[]string{
96+
label,
97+
},
98+
}
99+
_, _, _ = client.Issues.Create(ctx, owner, repo, issueRequest)
100+
logger.Info("create issue: %s", issueTitle)
101+
}
102+
103+
return nil
104+
}

pkg/types/validate.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ func (t *GithubVar) validate() error {
5252
if t.RepoFullName == "" {
5353
return fmt.Errorf("error: not found repository.full_name in github event")
5454
}
55-
if t.IssueOrPRNumber == 0 {
55+
return nil
56+
}
57+
58+
func ValidateIssueOrPRNumber() error {
59+
if ActionConfigJSON.IssueOrPRNumber == 0 {
5660
return fmt.Errorf("error: not found issue.number or pull_request.number in github event")
5761
}
5862
return nil

pkg/utils/date.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 "time"
20+
21+
func startOfWeek(t time.Time) time.Time {
22+
daysFromMonday := (int)(t.Weekday() - time.Monday)
23+
if daysFromMonday < 0 {
24+
daysFromMonday = 6
25+
}
26+
27+
return t.AddDate(0, 0, -daysFromMonday).Truncate(24 * time.Hour)
28+
}
29+
30+
func endOfWeek(t time.Time) time.Time {
31+
daysToSunday := (int)(7 - t.Weekday())
32+
if daysToSunday == 7 {
33+
daysToSunday = 0
34+
}
35+
36+
return t.AddDate(0, 0, daysToSunday).Truncate(24 * time.Hour)
37+
}
38+
39+
func FormatDay(t time.Time) string {
40+
return t.Format("2006/1/2")
41+
}
42+
func FormatWeek(t time.Time) (string, string) {
43+
start := startOfWeek(t)
44+
end := endOfWeek(t)
45+
46+
return FormatDay(start), FormatDay(end)
47+
}

pkg/utils/date_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 (
20+
"testing"
21+
"time"
22+
)
23+
24+
func TestFormat(t *testing.T) {
25+
t.Logf("date: %s", FormatDay(time.Now()))
26+
start, end := FormatWeek(time.Now())
27+
t.Logf("week date: %s - %s", start, end)
28+
}

0 commit comments

Comments
 (0)