github.com/fanux/shipyard@v0.0.0-20161009071005-6515ce223235/controller/api/webhook.go (about)

     1  package api
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"strings"
     7  
     8  	log "github.com/Sirupsen/logrus"
     9  	"github.com/gorilla/mux"
    10  	"github.com/shipyard/shipyard/dockerhub"
    11  )
    12  
    13  func (a *Api) hubWebhook(w http.ResponseWriter, r *http.Request) {
    14  	vars := mux.Vars(r)
    15  	id := vars["id"]
    16  	key, err := a.manager.WebhookKey(id)
    17  	if err != nil {
    18  		log.Errorf("invalid webook key: id=%s from %s", id, r.RemoteAddr)
    19  		http.Error(w, err.Error(), http.StatusNotFound)
    20  		return
    21  	}
    22  	var webhook *dockerhub.Webhook
    23  	if err := json.NewDecoder(r.Body).Decode(&webhook); err != nil {
    24  		log.Errorf("error parsing webhook: %s", err)
    25  		http.Error(w, err.Error(), http.StatusInternalServerError)
    26  		return
    27  	}
    28  	if strings.Index(webhook.Repository.RepoName, key.Image) == -1 {
    29  		log.Errorf("webhook key image does not match: repo=%s image=%s", webhook.Repository.RepoName, key.Image)
    30  		http.Error(w, "not found", http.StatusNotFound)
    31  		return
    32  	}
    33  	log.Infof("received webhook notification for %s", webhook.Repository.RepoName)
    34  	// TODO @ehazlett - redeploy containers
    35  }