From 6e4b0f3cef56443a9780c4ff73466556e4915513 Mon Sep 17 00:00:00 2001 From: Åsmund Østvold Date: Fri, 7 Jan 2022 11:37:48 +0100 Subject: feat(ops/besadii): make text 'cl' posted BuildKite configurable Some companies do not know the 'cl' term. They do know of 'change' and would maybe not like to introduce one more synonym. This cl introduce an optional entry 'gerritChangeName' in besadii.json. The string has to match `^[a-z0-9]+$` for readability. Change-Id: Id70fcb1e45158869f88bf37669be49b8b8a3b295 Reviewed-on: https://cl.tvl.fyi/c/depot/+/4825 Tested-by: BuildkiteCI Reviewed-by: tazjin Autosubmit: asmundo --- ops/besadii/main.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/ops/besadii/main.go b/ops/besadii/main.go index c02c2fbc0d0c..e22dbb1e484e 100644 --- a/ops/besadii/main.go +++ b/ops/besadii/main.go @@ -34,6 +34,13 @@ import ( // Regular expression to extract change ID out of a URL var changeIdRegexp = regexp.MustCompile(`^.*/(\d+)$`) +// Regular expression to check if gerritChangeName valid. The +// limitation could be what is allowed for a git branch name. For now +// we want to have a stricter limitation for readability and ease of +// use. +var gerritChangeNameRegexp = `^[a-z0-9]+$` +var gerritChangeNameCheck = regexp.MustCompile(gerritChangeNameRegexp) + // besadii configuration file structure type config struct { // Required configuration for Buildkite<>Gerrit monorepo @@ -47,6 +54,7 @@ type config struct { BuildkiteOrg string `json:"buildkiteOrg"` BuildkiteProject string `json:"buildkiteProject"` BuildkiteToken string `json:"buildkiteToken"` + GerritChangeName string `json:"gerritChangeName"` // Optional configuration for Sourcegraph trigger updates. SourcegraphUrl string `json:"sourcegraphUrl"` @@ -138,6 +146,14 @@ func loadConfig() (*config, error) { cfg.GerritLabel = "Verified" } + // The default text referring to a Gerrit Change in BuildKite. + if cfg.GerritChangeName == "" { + cfg.GerritChangeName = "cl" + } + if !gerritChangeNameCheck.MatchString(cfg.GerritChangeName) { + return nil, fmt.Errorf("invalid 'gerritChangeName': %s", cfg.GerritChangeName) + } + // Rudimentary config validation logic if cfg.SourcegraphUrl != "" && cfg.SourcegraphToken == "" { return nil, fmt.Errorf("'SourcegraphToken' must be set if 'SourcegraphUrl' is set") @@ -180,7 +196,7 @@ func updateGerrit(cfg *config, review reviewInput, changeId, patchset string) { resp, err := http.DefaultClient.Do(req) if err != nil { - fmt.Errorf("failed to update CL on Gerrit: %w", err) + fmt.Errorf("failed to update %s on %s: %w", cfg.GerritChangeName, cfg.GerritUrl, err) } defer resp.Body.Close() @@ -210,8 +226,8 @@ func triggerBuild(cfg *config, log *syslog.Writer, trigger *buildTrigger) error headBuild = false // The branch doesn't have to be a real ref (it's just used to - // group builds), so make it the identifier for the CL - branch = fmt.Sprintf("cl/%v", strings.Split(trigger.ref, "/")[3]) + // group builds), so make it the identifier for the CL. + branch = fmt.Sprintf("%s/%v", cfg.GerritChangeName, strings.Split(trigger.ref, "/")[3]) } build := Build{ @@ -459,7 +475,7 @@ func postCommandMain(cfg *config) { // If these variables are unset, but the hook was invoked, the // build was most likely for a branch and not for a CL - no status // needs to be reported back to Gerrit! - fmt.Println("This isn't a CL build, nothing to do. Have a nice day!") + fmt.Println("This isn't a %s build, nothing to do. Have a nice day!", cfg.GerritChangeName) return } -- cgit 1.4.1