From 3b9b56c3319e93c69dc48c9c24061ff9ea5c8c13 Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Thu, 15 Jun 2023 11:18:02 +0800 Subject: [PATCH] feature(main): add release for branch (#33) * feature(main): add release for branch Signed-off-by: cuisongliu * feature(main): add release for branch Signed-off-by: cuisongliu --------- Signed-off-by: cuisongliu --- README.md | 7 ++++++- pkg/gh/consts.go | 1 + pkg/gh/tag.go | 9 ++++++++- pkg/utils/shell.go | 5 +++++ pkg/workflow/release.go | 10 +++++++--- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 04c9485..1f70bab 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,12 @@ message: - [x] 支持release `SEALOS_TYPE: "/comment"` # 评论触发 - + example: + ```markdown + /release v1.2.1 + /release v1.2.3 release-v1.2 + ``` + > 目标分支为`release-v1.2`,如果没有则默认为`main`分支,该功能v0.0.7-rc1支持 - [x] 支持PR文本替换回复 - `SEALOS_TYPE: "pr_comment"` # PR文本替换回复 - `SEALOS_filename: "README.md"` # PR文本替换回复文件位置 diff --git a/pkg/gh/consts.go b/pkg/gh/consts.go index e25a77a..408aa07 100644 --- a/pkg/gh/consts.go +++ b/pkg/gh/consts.go @@ -31,6 +31,7 @@ const ( gitCommit = "cd /tmp/fork-sealos-repo && git commit -am '%s' -s" gitRelease = "cd /tmp/fork-sealos-repo && git describe --abbrev=0 --tags" gitTag = "cd /tmp/fork-sealos-repo && git fetch --tags && git tag -l" + gitCheck = "cd /tmp/fork-sealos-repo && git checkout fork/%s" gitNewTag = "cd /tmp/fork-sealos-repo && git tag %s" gitAddRemote = "cd /tmp/fork-sealos-repo && git remote add fork https://%s:%s@github.com/%s.git" gitPushRemote = "cd /tmp/fork-sealos-repo && git push -f fork %s" diff --git a/pkg/gh/tag.go b/pkg/gh/tag.go index d01cc63..61bd19d 100644 --- a/pkg/gh/tag.go +++ b/pkg/gh/tag.go @@ -22,15 +22,22 @@ import ( "github.com/pkg/errors" ) -func Tag(tag string) error { +func Tag(tag, branch string) error { if err := setPreGithub(); err != nil { return err } + var firstShell any + if branch == "" { + firstShell = utils.SkipShell("") + } else { + firstShell = fmt.Sprintf(gitCheck, branch) + } if ok, err := checkRemoteTagExists(tag); err != nil { return err } else { if !ok { shells := []any{ + firstShell, fmt.Sprintf(gitNewTag, tag), fmt.Sprintf(gitPushRemote, tag), } diff --git a/pkg/utils/shell.go b/pkg/utils/shell.go index 63c0f86..2ce1ce3 100644 --- a/pkg/utils/shell.go +++ b/pkg/utils/shell.go @@ -29,6 +29,7 @@ import ( type RetryShell string type RetrySecretShell string type SecretShell string +type SkipShell string func ExecShellForAny(secrets ...string) func(shells []any) error { return func(shells []any) error { @@ -55,11 +56,15 @@ func ExecShellForAny(secrets ...string) func(shells []any) error { return err } } + if _, ok := sh.(SkipShell); ok { + continue + } if s, ok := sh.(string); ok { if err := RunCommand("bash", "-c", s); err != nil { return err } } + } return nil } diff --git a/pkg/workflow/release.go b/pkg/workflow/release.go index a9f6e55..9744d5c 100644 --- a/pkg/workflow/release.go +++ b/pkg/workflow/release.go @@ -34,8 +34,12 @@ func (c *release) Run() error { return c.sender.sendMsgToIssue("permission_error") } data := strings.Split(c.Body, " ") - if len(data) == 2 && utils.ValidateVersion(data[1]) { - err := gh.Tag(data[1]) + if (2 == len(data) || 3 == len(data)) && utils.ValidateVersion(data[1]) { + var newBranch string + if len(data) == 3 { + newBranch = data[2] + } + err := gh.Tag(data[1], newBranch) if err != nil { c.sender.Error = err.Error() return c.sender.sendMsgToIssue("release_error") @@ -53,7 +57,7 @@ func (c *release) Run() error { } return nil } else { - logger.Error("command format is error: %s ex. /{prefix}_release {tag}", c.Body) + logger.Error("command format is error: %s ex. /{prefix}_release {tag} {branch}", c.Body) return c.sender.sendMsgToIssue("format_error") } }