Skip to content

Commit

Permalink
Initial webhook implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nlewo committed Mar 21, 2023
1 parent 818d7a5 commit 110717b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/nlewo/comin/config"
"github.com/nlewo/comin/deploy"
"github.com/nlewo/comin/webhook"
"github.com/nlewo/comin/worker"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -30,6 +31,7 @@ var pollCmd = &cobra.Command{

wk := worker.NewWorker(deployer.Deploy)
go worker.Scheduler(wk, config.Poller.Period)
go webhook.Run(wk, config.Webhook)
wk.Run()
},
}
Expand Down
14 changes: 14 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ func Read(path string) (config types.Configuration, err error) {
config.Remotes[i].Auth.AccessToken = string(content)
}
}

if config.Webhook.Address == "" {
config.Webhook.Address = "127.0.0.1"
}
if config.Webhook.Port == 0 {
config.Webhook.Port = 4242
}
if config.Webhook.SecretPath != "" {
content, err := ioutil.ReadFile(config.Webhook.SecretPath)
if err != nil {
return config, err
}
config.Webhook.Secret = string(content)
}
logrus.Debugf("Config is '%#v'", config)
return
}
Expand Down
8 changes: 8 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,18 @@ type Poller struct {
Period int `yaml:"period"`
}

type Webhook struct {
Address string `yaml:"address"`
Port int `yaml:"port"`
Secret string
SecretPath string `yaml:"secret_path"`
}

type Configuration struct {
Hostname string `yaml:"hostname"`
StateDir string `yaml:"state_dir"`
Remotes []Remote `yaml:"remotes"`
Branches Branches `yaml:"branches"`
Poller Poller `yaml:"poller"`
Webhook Webhook `yaml:"webhook"`
}
47 changes: 47 additions & 0 deletions webhook/webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package webhook

import (
"fmt"
"github.com/nlewo/comin/types"
"github.com/nlewo/comin/worker"
"github.com/sirupsen/logrus"
"io"
"net/http"
"os"
)

func Run(worker worker.Worker, cfg types.Webhook) {
handler := func(w http.ResponseWriter, r *http.Request) {
var secret string
logrus.Infof("Getting webhook request %s from %s", r.URL, r.RemoteAddr)
if cfg.Secret != "" {
secret = r.Header.Get("X-Gitlab-Token")
if secret == "" {
logrus.Infof("Webhook called from %s without the X-Gitlab-Token header", r.RemoteAddr)
w.WriteHeader(http.StatusUnauthorized)
io.WriteString(w, "The header X-Gitlab-Token is required\n")
return
}
if secret != cfg.Secret {
logrus.Infof("Webhook called from %s with the invalid secret %s", r.RemoteAddr, secret)
w.WriteHeader(http.StatusUnauthorized)
io.WriteString(w, "Invalid X-Gitlab-Token header value\n")
return
}
}
if worker.Beat() {
w.WriteHeader(http.StatusOK)
io.WriteString(w, "A deployment has been triggered\n")
} else {
w.WriteHeader(http.StatusConflict)
io.WriteString(w, "A deployment is already running\n")
}
}
http.HandleFunc("/deploy", handler)
url := fmt.Sprintf("%s:%d", cfg.Address, cfg.Port)
logrus.Infof("Starting the webhook server on %s", url)
if err := http.ListenAndServe(url, nil); err != nil {
logrus.Errorf("Error while running the webhook server: %s", err)
os.Exit(1)
}
}

0 comments on commit 110717b

Please sign in to comment.