about summary refs log tree commit diff
path: root/tools/gerrit-hook/main.go
blob: 5461490d739e37bf37a26083b81936d048919d88 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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)
	}

	switch cmd {
	case "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)
	case "post-command":
		postCommand(cfg)
	default:
		log.Info(fmt.Sprintf("`%s' is not a supported command", cmd))
		os.Exit(1)
	}
}