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

Commit 348acd1

Browse files
authored
Issue renew (#37)
* feature(main): add issue_renew action for env Signed-off-by: cuisongliu <cuisongliu@qq.com> * feature(main): add issue_renew action for env Signed-off-by: cuisongliu <cuisongliu@qq.com> * feature(main): add issue_renew action for env Signed-off-by: cuisongliu <cuisongliu@qq.com> --------- Signed-off-by: cuisongliu <cuisongliu@qq.com>
1 parent b61fe47 commit 348acd1

File tree

6 files changed

+82
-14
lines changed

6 files changed

+82
-14
lines changed

.github/workflows/test-issue-renew.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ jobs:
2626
SEALOS_ISSUE_LABEL: "dayly-report"
2727
SEALOS_ISSUE_TYPE: "day"
2828
GH_TOKEN: "${{ secrets.GH_PAT }}"
29+
- run: |
30+
echo "output is ${{ env.SEALOS_ISSUE_NUMBER }}"

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,20 @@ message:
138138
- `SEALOS_ISREPLY: "true"` # 是否回复,根据当前的comment的内容追加
139139

140140
- [x] issue自动创建
141-
> 该功能v0.0.7-rc2支持
141+
> 该功能v0.0.7-rc3支持
142+
143+
入参:
142144

143145
- `SEALOS_TYPE: "issue_renew"` # issue创建comment,支持回复
144146
- `SEALOS_ISSUE_TITLE: "dxxxx"` # issue的title
145147
- `SEALOS_ISSUE_BODY: "xxxx"` # issue内容
146148
- `SEALOS_ISSUE_BODYFILE: "README.md"` # issue内容如果多可以写文件
147149
- `SEALOS_ISSUE_LABEL: "dayly-report"` # 新增issue的label
148150
- `SEALOS_ISSUE_TYPE: "day"` # day和week , 会在titles上自动加上日期
151+
152+
返回参数:
153+
154+
- env.SEALOS_ISSUE_NUMBER # issue的number
149155

150156
## Roadmap
151157

pkg/action/GITHUB_ENV

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
aa=bb
2+
cc=dd

pkg/action/action.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,13 @@ func getRepo() (string, string, error) {
7272
}
7373
return split[0], types.ActionConfigJSON.RepoName, nil
7474
}
75+
76+
func writeGithubEnv(key, value string) error {
77+
file, err := os.OpenFile(os.Getenv("GITHUB_ENV"), os.O_APPEND|os.O_WRONLY, 0644)
78+
if err != nil {
79+
return err
80+
}
81+
defer file.Close()
82+
_, err = file.WriteString(fmt.Sprintf("%s=%s\n", key, value))
83+
return err
84+
}

pkg/action/action_issue_renew.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"github.com/labring/gh-rebot/pkg/types"
2525
"github.com/labring/gh-rebot/pkg/utils"
2626
"os"
27+
"strconv"
28+
"strings"
2729
"time"
2830
)
2931

@@ -72,19 +74,28 @@ func IssueRenew() error {
7274
return err
7375
}
7476
hasIssue := false
77+
issueNumber := ""
78+
defer func() {
79+
writeGithubEnv("SEALOS_ISSUE_NUMBER", issueNumber)
80+
logger.Info("add env SEALOS_ISSUE_NUMBER: %s", issueNumber)
81+
}()
82+
issueOldTitle, _ := GetEnvFromAction("issue_title")
7583
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,
84+
if strings.HasPrefix(issue.GetTitle(), issueOldTitle) {
85+
logger.Debug("issue: %s, state: %s, id: %d", issue.GetTitle(), issue.GetState(), issue.GetID())
86+
if issue.GetTitle() == issueTitle && issue.GetState() != "closed" {
87+
logger.Info("issue already exist, issue: %s", issue.GetTitle())
88+
hasIssue = true
89+
issueNumber = strconv.Itoa(issue.GetNumber())
90+
return nil
91+
} else {
92+
state := "closed"
93+
issueRequest := &github.IssueRequest{
94+
State: &state,
95+
}
96+
_, _, _ = client.Issues.Edit(ctx, owner, repo, issue.GetNumber(), issueRequest)
97+
logger.Info("close issue: %s", issue.GetTitle())
8598
}
86-
_, _, _ = client.Issues.Edit(ctx, owner, repo, issue.GetNumber(), issueRequest)
87-
logger.Info("close issue: %s", issue.GetTitle())
8899
}
89100
}
90101
logger.Warn("issue not exist, issue: %s", issueTitle)
@@ -96,8 +107,9 @@ func IssueRenew() error {
96107
label,
97108
},
98109
}
99-
_, _, _ = client.Issues.Create(ctx, owner, repo, issueRequest)
100-
logger.Info("create issue: %s", issueTitle)
110+
issue, _, _ := client.Issues.Create(ctx, owner, repo, issueRequest)
111+
issueNumber = strconv.Itoa(issue.GetNumber())
112+
logger.Info("create issue: %s, number: %d", issueTitle, issue.GetNumber())
101113
}
102114

103115
return nil

pkg/action/action_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
"os"
21+
"testing"
22+
)
23+
24+
func Test_writeGithubEnv(t *testing.T) {
25+
os.Setenv("GITHUB_ENV", "GITHUB_ENV")
26+
err := writeGithubEnv("aa", "bb")
27+
if err != nil {
28+
t.Error(err)
29+
return
30+
}
31+
err = writeGithubEnv("cc", "dd")
32+
if err != nil {
33+
t.Error(err)
34+
return
35+
}
36+
}

0 commit comments

Comments
 (0)