// ServeHTTP implements the http.Handler interface.
ServeHTTP implements the http.Handler interface.
// ServeHTTP implements the http . Handler interface .
func (h *GitLabWebhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	extSvc, err := h.getExternalServiceFromRawID(r.Context(), r.FormValue(extsvc.IDParam))
	if err == errExternalServiceNotFound {
		respond(w, http.StatusUnauthorized, err)
		return
	} else if err != nil {
		respond(w, http.StatusInternalServerError, errors.Wrap(err, "getting external service"))
		return
	}
	if ok, err := validateGitLabSecret(extSvc, r.Header.Get(webhooks.TokenHeaderName)); err != nil {
		respond(w, http.StatusInternalServerError, errors.Wrap(err, "validating the shared secret"))
		return
	} else if !ok {
		respond(w, http.StatusUnauthorized, "shared secret is incorrect")
		return
	}
	if r.Body == nil {
		respond(w, http.StatusBadRequest, "missing request body")
		return
	}
	payload, err := ioutil.ReadAll(r.Body)
	if err != nil {
		respond(w, http.StatusInternalServerError, errors.Wrap(err, "reading payload"))
		return
	}
	event, err := webhooks.UnmarshalEvent(payload)
	if err != nil {
		if errors.Is(err, webhooks.ErrObjectKindUnknown) {
			log15.Debug("unknown object kind", "err", err)
			w.Header().Set("Content-Type", "text/plain; charset=utf-8")
			w.WriteHeader(http.StatusNoContent)
			fmt.Fprintf(w, "%v", err)
		} else {
			respond(w, http.StatusInternalServerError, errors.Wrap(err, "unmarshalling payload"))
		}
		return
	}
	if err := h.handleEvent(r.Context(), extSvc, event); err != nil {
		respond(w, err.code, err)
	} else {
		respond(w, http.StatusNoContent, nil)
	}
}
func ( h * GitLabWebhook ) ServeHTTP ( w http . ResponseWriter , r * http . Request ) { extSvc , err := h . getExternalServiceFromRawID ( r . Context ( ) , r . FormValue ( extsvc . IDParam ) ) if err == errExternalServiceNotFound { respond ( w , http . StatusUnauthorized , err ) return } else if err != nil { respond ( w , http . StatusInternalServerError , errors . Wrap ( err , " " ) ) return } if ok , err := validateGitLabSecret ( extSvc , r . Header . Get ( webhooks . TokenHeaderName ) ) ; err != nil { respond ( w , http . StatusInternalServerError , errors . Wrap ( err , " " ) ) return } else if ! ok { respond ( w , http . StatusUnauthorized , " " ) return } if r . Body == nil { respond ( w , http . StatusBadRequest , " " ) return } payload , err := ioutil . ReadAll ( r . Body ) if err != nil { respond ( w , http . StatusInternalServerError , errors . Wrap ( err , " " ) ) return } event , err := webhooks . UnmarshalEvent ( payload ) if err != nil { if errors . Is ( err , webhooks . ErrObjectKindUnknown ) { log15 . Debug ( " " , " " , err ) w . Header ( ) . Set ( " " , " " ) w . WriteHeader ( http . StatusNoContent ) fmt . Fprintf ( w , " " , err ) } else { respond ( w , http . StatusInternalServerError , errors . Wrap ( err , " " ) ) } return } if err := h . handleEvent ( r . Context ( ) , extSvc , event ) ; err != nil { respond ( w , err . code , err ) } else { respond ( w , http . StatusNoContent , nil ) } }
ServeHTTP implements the http.Handler interface.
ServeHTTP implements the http . Handler interface .
// Look up the external service. // 🚨 SECURITY: Verify the shared secret against the GitLab external service // configuration. If there isn't a webhook defined in the service with this // secret, or the header is empty, then we return a 401 to the client. // Parse the event proper. // We don't want to return a non-2XX status code and have GitLab // retry the webhook, so we'll log that we don't know what to do // and return 204. // We don't use respond() here so that we don't log an error, since // this really isn't one. // Route the request based on the event type.