fix webhook
This commit is contained in:
parent
4205922617
commit
b7d4b8b182
1 changed files with 37 additions and 6 deletions
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
@ -21,7 +22,7 @@ func (a *App) handleDeployWebhook(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
path := strings.TrimPrefix(r.URL.Path, "/api/webhooks/deploy/")
|
path := strings.Trim(strings.TrimPrefix(r.URL.Path, "/api/webhooks/deploy/"), "/")
|
||||||
parts := strings.Split(path, "/")
|
parts := strings.Split(path, "/")
|
||||||
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
|
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
|
||||||
http.Error(w, "bad path", http.StatusBadRequest)
|
http.Error(w, "bad path", http.StatusBadRequest)
|
||||||
|
|
@ -37,17 +38,22 @@ func (a *App) handleDeployWebhook(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var payload struct {
|
ref, err := parseWebhookRef(r)
|
||||||
Ref string `json:"ref"`
|
if err != nil {
|
||||||
}
|
http.Error(w, "invalid webhook payload", http.StatusBadRequest)
|
||||||
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
|
||||||
http.Error(w, "invalid json", http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if payload.Ref != "refs/heads/main" {
|
ref = strings.TrimSpace(ref)
|
||||||
|
if ref == "" {
|
||||||
|
writeJSON(w, http.StatusAccepted, map[string]string{"status": "ignored", "reason": "no ref in payload"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if ref != "refs/heads/main" && ref != "main" {
|
||||||
|
log.Printf("webhook ignored project_id=%d ref=%q", p.ID, ref)
|
||||||
writeJSON(w, http.StatusAccepted, map[string]string{"status": "ignored", "reason": "not main branch"})
|
writeJSON(w, http.StatusAccepted, map[string]string{"status": "ignored", "reason": "not main branch"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
log.Printf("webhook deploy accepted project_id=%d ref=%q", p.ID, ref)
|
||||||
if err := a.deployProjectFromMain(r.Context(), p); err != nil {
|
if err := a.deployProjectFromMain(r.Context(), p); err != nil {
|
||||||
log.Printf("deploy failed project_id=%d err=%v", p.ID, err)
|
log.Printf("deploy failed project_id=%d err=%v", p.ID, err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
|
@ -56,6 +62,31 @@ func (a *App) handleDeployWebhook(w http.ResponseWriter, r *http.Request) {
|
||||||
writeJSON(w, http.StatusOK, map[string]string{"status": "deployed"})
|
writeJSON(w, http.StatusOK, map[string]string{"status": "deployed"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseWebhookRef(r *http.Request) (string, error) {
|
||||||
|
rawBody, err := io.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
var payload struct {
|
||||||
|
Ref string `json:"ref"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(rawBody, &payload); err == nil {
|
||||||
|
return payload.Ref, nil
|
||||||
|
}
|
||||||
|
formVals, err := url.ParseQuery(string(rawBody))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
raw := strings.TrimSpace(formVals.Get("payload"))
|
||||||
|
if raw == "" {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal([]byte(raw), &payload); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return payload.Ref, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (a *App) getProjectForWebhook(ctx context.Context, projectID int64, token string) (Project, error) {
|
func (a *App) getProjectForWebhook(ctx context.Context, projectID int64, token string) (Project, error) {
|
||||||
var p Project
|
var p Project
|
||||||
err := a.db.QueryRowContext(ctx, `SELECT p.id, p.user_id, p.name, p.slug, p.description, p.repo_url, p.service_name, p.route_host, p.workspace_id, w.name, w.unix_user, p.deploy_token, p.created_at
|
err := a.db.QueryRowContext(ctx, `SELECT p.id, p.user_id, p.name, p.slug, p.description, p.repo_url, p.service_name, p.route_host, p.workspace_id, w.name, w.unix_user, p.deploy_token, p.created_at
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue