-
Notifications
You must be signed in to change notification settings - Fork 1
feat(chatops-lark): add command /bump-tidbx-hotfix-tag #367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b150eda
feat(chatops-lark): add command `/bump-tidbx-hotfix-tag`
wuhuizuo 3047909
feat(chatops-lark): register commands only when configured
wuhuizuo fd2261f
chore(chatops-lark): remove unused types and constants
wuhuizuo e330bad
Extract hotfix bump-tag request/response types
wuhuizuo a574ceb
Remove debug log from audit handler
wuhuizuo 8f2701f
Update chatops-lark/pkg/events/handler/hotfix.go
wuhuizuo 7e2b105
Document hotfix API and audit fields in config example
wuhuizuo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package audit | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestNewLarkCardWithGoTemplate(t *testing.T) { | ||
| t.Run("success with original template", func(t *testing.T) { | ||
| result := "TiDB-X hotfix tag bumped successfully:\n• Repo: PingCAP-QE/ci\n• Commit: e2cc653b672029b78519a524847b341baf72c98f\n• Tag: v8.5.4-nextgen.202510.2" | ||
| info := &AuditInfo{ | ||
| UserEmail: "user@example.com", | ||
| Command: "/devbuild", | ||
| Args: []string{"--foo", "bar"}, | ||
| Result: &result, | ||
| } | ||
| str, err := newLarkCardWithGoTemplate(info) | ||
| t.Log(str) | ||
| if err != nil { | ||
| t.Fatalf("expected no error, got: %v", err) | ||
| } | ||
wuhuizuo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| package handler | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/go-resty/resty/v2" | ||
| "github.com/rs/zerolog/log" | ||
|
|
||
| "github.com/PingCAP-QE/ee-apps/chatops-lark/pkg/config" | ||
| ) | ||
|
|
||
| // ctx keys store hotfix service configuration | ||
| const hotfixCfgKey string = "hotfix.cfg" | ||
|
|
||
| type hotfixRuntimeConfig struct { | ||
| APIURL string | ||
| ActorEmail string | ||
| ActorGitHub *string | ||
| } | ||
|
|
||
| // construct hotfixBumpTagRequest payload according to tibuild v2 goa design | ||
| type hotfixBumpTagRequest struct { | ||
| Repo string `json:"repo"` | ||
| Author string `json:"author"` | ||
| Commit string `json:"commit,omitempty"` | ||
| // Branch is optional in design; we don't require it in command, leave empty | ||
| Branch string `json:"branch,omitempty"` | ||
| } | ||
|
|
||
| type hotfixBumpTagResponse struct { | ||
| Repo string `json:"repo"` | ||
| Commit string `json:"commit"` | ||
| Tag string `json:"tag"` | ||
| } | ||
|
|
||
| type hotfixAPIError struct { | ||
| Code int `json:"code"` | ||
| Message string `json:"message"` | ||
| } | ||
|
|
||
| // setupCtxHotfix prepares the runtime context for hotfix-related commands. | ||
| func setupCtxHotfix(ctx context.Context, cfg config.Config, actor *CommandActor) context.Context { | ||
| runtime := hotfixRuntimeConfig{ | ||
| APIURL: cfg.Hotfix.ApiURL, | ||
| ActorEmail: actor.Email, | ||
| ActorGitHub: actor.GitHubID, | ||
| } | ||
| return context.WithValue(ctx, hotfixCfgKey, &runtime) | ||
| } | ||
|
|
||
| type bumpTidbxParams struct { | ||
| repo string | ||
| commit string | ||
| help bool | ||
| } | ||
|
|
||
| func parseCommandHotfixBumpTidbx(args []string) (*bumpTidbxParams, string, error) { | ||
| fs := flag.NewFlagSet("/bump-tidbx-hotfix-tag", flag.ContinueOnError) | ||
| // silence default usage output | ||
| sink := new(strings.Builder) | ||
| fs.SetOutput(sink) | ||
|
|
||
| ret := &bumpTidbxParams{} | ||
|
|
||
| fs.StringVar(&ret.repo, "repo", "", "Full name of GitHub repository (e.g., pingcap/tidb)") | ||
| fs.StringVar(&ret.commit, "commit", "", "Short or full git commit SHA") | ||
| fs.BoolVar(&ret.help, "help", false, "Show help") | ||
|
|
||
| if err := fs.Parse(args); err != nil { | ||
| return nil, "", err | ||
| } | ||
|
|
||
| if ret.help { | ||
| return ret, hotfixHelpText(), NewSkipError("Help requested") | ||
| } | ||
|
|
||
| // validate required args | ||
| missing := []string{} | ||
| if ret.repo == "" { | ||
| missing = append(missing, "--repo") | ||
| } | ||
| if ret.commit == "" { | ||
| missing = append(missing, "--commit") | ||
| } | ||
| if len(missing) > 0 { | ||
| return nil, hotfixHelpText(), NewInformationError(fmt.Sprintf("Missing required argument(s): %s", strings.Join(missing, ", "))) | ||
| } | ||
|
|
||
| // strict repo format validation: must be <org>/<repo>, neither part empty, and only one slash | ||
| if strings.Count(ret.repo, "/") != 1 { | ||
| return nil, hotfixHelpText(), NewInformationError("Invalid --repo. Expected format: <org>/<repo> (e.g., pingcap/tidb)") | ||
| } | ||
| parts := strings.Split(ret.repo, "/") | ||
| if len(parts) != 2 || parts[0] == "" || parts[1] == "" { | ||
| return nil, hotfixHelpText(), NewInformationError("Invalid --repo. Expected format: <org>/<repo> (e.g., pingcap/tidb)") | ||
| } | ||
|
|
||
| // commit sanity | ||
| if len(ret.commit) < 7 || len(ret.commit) > 40 { | ||
| // sha length can vary, but common short SHA >=7, full 40 | ||
| // continue but inform the user | ||
| return ret, "", NewInformationError("The provided --commit looks unusual; ensure it's a valid short or full SHA") | ||
| } | ||
|
|
||
| return ret, "", nil | ||
| } | ||
|
Comment on lines
61
to
110
|
||
|
|
||
| func hotfixHelpText() string { | ||
| return `Usage: /bump-tidbx-hotfix-tag --repo <org>/<repo> --commit <commit-sha> | ||
|
|
||
| Description: | ||
| Bump TiDB-X style hotfix git tag for a GitHub repository by calling TiBuild v2 API. | ||
|
|
||
| Arguments: | ||
| --repo Full name of GitHub repository (e.g., pingcap/tidb) | ||
| --commit Short or full git commit SHA to tag | ||
|
|
||
| Examples: | ||
| /bump-tidbx-hotfix-tag --repo pingcap/tidb --commit abc123def | ||
|
|
||
| Notes: | ||
| The tag will be generated in TiDB-X style and created on the specified commit.` | ||
| } | ||
|
|
||
| // runCommandHotfixBumpTidbxTag handles `/bump-tidbx-hotfix-tag` command. | ||
| func runCommandHotfixBumpTidbxTag(ctx context.Context, args []string) (string, error) { | ||
| params, msg, err := parseCommandHotfixBumpTidbx(args) | ||
| if err != nil { | ||
| // return parsed message and error type to upper layer | ||
| return msg, err | ||
| } | ||
|
|
||
| runtime, ok := ctx.Value(hotfixCfgKey).(*hotfixRuntimeConfig) | ||
| if !ok || runtime == nil || runtime.APIURL == "" { | ||
| return "", fmt.Errorf("hotfix API URL is not configured") | ||
| } | ||
|
|
||
| reqBody := hotfixBumpTagRequest{ | ||
| Repo: params.repo, | ||
| Commit: params.commit, | ||
| Author: preferAuthor(runtime), | ||
| } | ||
|
|
||
| // POST to /bump-tag-for-tidbx | ||
| url := strings.TrimRight(runtime.APIURL, "/") + "/bump-tag-for-tidbx" | ||
|
|
||
| var res hotfixBumpTagResponse | ||
| var apiErr hotfixAPIError | ||
| client := resty.New().SetTimeout(20 * time.Second) | ||
| r, err := client.R(). | ||
| SetBody(reqBody). | ||
| SetResult(&res). | ||
| SetError(&apiErr). | ||
| Post(url) | ||
| if err != nil { | ||
| log.Err(err).Msg("hotfix API request failed") | ||
| return "", fmt.Errorf("hotfix API request failed: %w", err) | ||
| } | ||
| if !r.IsSuccess() { | ||
| if apiErr.Message != "" { | ||
| return "", fmt.Errorf("hotfix API error: %s (code: %d, http: %d)", apiErr.Message, apiErr.Code, r.StatusCode()) | ||
| } | ||
| return "", fmt.Errorf("hotfix API http status: %d", r.StatusCode()) | ||
| } | ||
|
|
||
| // build user-friendly message | ||
| lines := []string{ | ||
| "TiDB-X hotfix tag bumped successfully:", | ||
| fmt.Sprintf("• Repo: %s", res.Repo), | ||
| fmt.Sprintf("• Commit: %s", res.Commit), | ||
| fmt.Sprintf("• Tag: %s", res.Tag), | ||
| } | ||
| return strings.Join(lines, "\n"), nil | ||
| } | ||
|
|
||
| // preferAuthor picks a string representing the author for the API. | ||
| // If GitHubID is available, prefer it; otherwise fall back to email. | ||
| func preferAuthor(rt *hotfixRuntimeConfig) string { | ||
| if rt.ActorGitHub != nil { | ||
| if s := strings.TrimSpace(*rt.ActorGitHub); s != "" { | ||
| return s | ||
| } | ||
| } | ||
| return strings.TrimSpace(rt.ActorEmail) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.