about summary refs log tree commit diff
path: root/tools/gerrit-hook/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'tools/gerrit-hook/main.go')
-rw-r--r--tools/gerrit-hook/main.go64
1 files changed, 64 insertions, 0 deletions
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)
+	}
+}