From 83a38a6da9ef99bc6596f6cfb53395a89f0165c7 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Mon, 30 May 2022 13:32:43 -0700 Subject: feat(gerrit-hook): a small tool to act as a dispatcher for gerrit When a patchset is created, gerrit will call this tool with a number of arguments. This hook triggers a build with buildKite for the given patchset, and add a comment to gerrit with a link to the build. We do not wait for the build to be successful to update gerrit. This will be done by another hook which the buildKite agents will call once they are done with the build. Change-Id: Iaa221765f3c52875ec37c5d282ba0557291eb5a4 Reviewed-on: https://cl.fcuny.net/c/world/+/171 Reviewed-by: Franck Cuny --- tools/gerrit-hook/main.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tools/gerrit-hook/main.go (limited to 'tools/gerrit-hook/main.go') diff --git a/tools/gerrit-hook/main.go b/tools/gerrit-hook/main.go new file mode 100644 index 0000000..f8ed687 --- /dev/null +++ b/tools/gerrit-hook/main.go @@ -0,0 +1,64 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log/syslog" + "os" + "path" +) + +// config represents the configuration for the gerrit hook +type config struct { + GerritUrl string `json:"gerritUrl"` + GerritUser string `json:"gerritUser"` + GerritPassword string `json:"gerritPassword"` + BuildKiteToken string `json:"buildKiteToken"` + BuildKiteOrganization string `json:"buildKiteOrganization"` +} + +func loadConfig() (*config, error) { + configPath := "/var/run/agenix/gerrit/hooks" + + configJson, err := ioutil.ReadFile(configPath) + if err != nil { + return nil, fmt.Errorf("failed to read configuration file %s: %v", configPath, err) + } + + var cfg config + err = json.Unmarshal(configJson, &cfg) + if err != nil { + return nil, fmt.Errorf("failed to unmarshall configuration: %v", err) + } + + return &cfg, nil +} + +func main() { + log, err := syslog.New(syslog.LOG_INFO|syslog.LOG_USER, "gerrit-hook") + if err != nil { + fmt.Fprintf(os.Stderr, "failed to open syslog: %s\n", err) + } + + log.Info(fmt.Sprintf("`gerrit-hook' called with arguments: %v\n", os.Args)) + + cmd := path.Base(os.Args[0]) + + cfg, err := loadConfig() + if err != nil { + os.Exit(1) + } + + if cmd == "patchset-created" { + trigger, err := triggerForPatchsetCreated() + if err != nil { + log.Crit(fmt.Sprintf("failed to create a trigger: %s", err)) + os.Exit(1) + } + gerritHookMain(cfg, log, trigger) + } else { + log.Info(fmt.Sprintf("`%s' is not a supported command", cmd)) + os.Exit(1) + } +} -- cgit v1.2.3