// Handle is an HTTP handler which processes a GitHub webhook to verify the
// identity of a commit author.
Handle is an HTTP handler which processes a GitHub webhook to verify the
identity of a commit author.
// Handle is an HTTP handler which processes a GitHub webhook to verify the // identity of a commit author .
func (w *Webhook) Handle(wr http.ResponseWriter, req *http.Request) {
	b, err := ioutil.ReadAll(req.Body)
	if err != nil {
		log.TraceR(req, fmt.Sprintf("error reading request body: %s", err), nil)
		wr.WriteHeader(http.StatusBadRequest)
		return
	}
	defer req.Body.Close()
	var p payload
	if err = json.Unmarshal(b, &p); err != nil {
		log.TraceR(req, fmt.Sprintf("error unmarshaling payload: %s", err), nil)
		wr.WriteHeader(http.StatusBadRequest)
		return
	}
	v, err := identity.IsValid(p.Repository.Owner.Name, p.Repository.Name, p.Commit.ID)
	if err != nil {
		log.Debug(fmt.Sprintf("error validating signature: %s", err), nil)
		wr.WriteHeader(http.StatusInternalServerError)
		return
	}
	if v {
		log.Debug(fmt.Sprintf("valid signature from %s for %s:%s", p.Commit.Author.Username, p.Repository.Name, p.Commit.ID), nil)
		wr.WriteHeader(http.StatusOK)
		return
	}
	log.Debug(fmt.Sprintf("invalid signature from %s for %s:%s", p.Commit.Author.Username, p.Repository.Name, p.Commit.ID), nil)
	message := &slackshot.Payload{
		Colour: "danger",
		Message: &slackshot.Message{
			Headline: fmt.Sprintf("*Unverified commit from %s*", p.Commit.Author.Username),
			Summary:  fmt.Sprintf("_<%s>_", p.Commit.URL),
		},
	}
	if err := (&slackshot.Webhook{Endpoint: w.SlackURL}).Send(message); err != nil {
		log.Error(fmt.Errorf("error sending notification to Slack: %s", err), nil)
		wr.WriteHeader(http.StatusInternalServerError)
		return
	}
	wr.WriteHeader(http.StatusBadRequest)
}
func ( w * Webhook ) Handle ( wr http . ResponseWriter , req * http . Request ) { b , err := ioutil . ReadAll ( req . Body ) if err != nil { log . TraceR ( req , fmt . Sprintf ( " " , err ) , nil ) wr . WriteHeader ( http . StatusBadRequest ) return } defer req . Body . Close ( ) var p payload if err = json . Unmarshal ( b , & p ) ; err != nil { log . TraceR ( req , fmt . Sprintf ( " " , err ) , nil ) wr . WriteHeader ( http . StatusBadRequest ) return } v , err := identity . IsValid ( p . Repository . Owner . Name , p . Repository . Name , p . Commit . ID ) if err != nil { log . Debug ( fmt . Sprintf ( " " , err ) , nil ) wr . WriteHeader ( http . StatusInternalServerError ) return } if v { log . Debug ( fmt . Sprintf ( " " , p . Commit . Author . Username , p . Repository . Name , p . Commit . ID ) , nil ) wr . WriteHeader ( http . StatusOK ) return } log . Debug ( fmt . Sprintf ( " " , p . Commit . Author . Username , p . Repository . Name , p . Commit . ID ) , nil ) message := & slackshot . Payload { Colour : " " , Message : & slackshot . Message { Headline : fmt . Sprintf ( " " , p . Commit . Author . Username ) , Summary : fmt . Sprintf ( " " , p . Commit . URL ) , } , } if err := ( & slackshot . Webhook { Endpoint : w . SlackURL } ) . Send ( message ) ; err != nil { log . Error ( fmt . Errorf ( " " , err ) , nil ) wr . WriteHeader ( http . StatusInternalServerError ) return } wr . WriteHeader ( http . StatusBadRequest ) }
Handle is an HTTP handler which processes a GitHub webhook to verify the
identity of a commit author.
Handle is an HTTP handler which processes a GitHub webhook to verify the identity of a commit author .