gateway webhook: remove handling of user direct runs

This commit is contained in:
Simone Gotti 2019-06-11 15:17:08 +02:00
parent a27721c5b1
commit 369f116bfc
2 changed files with 49 additions and 95 deletions

View File

@ -15,11 +15,8 @@
package gateway package gateway
import ( import (
"fmt"
"net/http" "net/http"
gitsource "github.com/sorintlab/agola/internal/gitsources"
"github.com/sorintlab/agola/internal/gitsources/agolagit"
"github.com/sorintlab/agola/internal/services/common" "github.com/sorintlab/agola/internal/services/common"
csapi "github.com/sorintlab/agola/internal/services/configstore/api" csapi "github.com/sorintlab/agola/internal/services/configstore/api"
"github.com/sorintlab/agola/internal/services/gateway/action" "github.com/sorintlab/agola/internal/services/gateway/action"
@ -51,33 +48,17 @@ func (h *webhooksHandler) handleWebhook(r *http.Request) (int, string, error) {
ctx := r.Context() ctx := r.Context()
projectID := r.URL.Query().Get("projectid") projectID := r.URL.Query().Get("projectid")
userID := r.URL.Query().Get("userid")
if projectID == "" && userID == "" {
return http.StatusBadRequest, "", errors.Errorf("bad webhook url %q. Missing projectid or userid", r.URL)
}
runType := types.RunTypeProject
if projectID == "" { if projectID == "" {
runType = types.RunTypeUser return http.StatusBadRequest, "", errors.Errorf("bad webhook url %q. Missing projectid", r.URL)
} }
defer r.Body.Close() defer r.Body.Close()
var project *types.Project
var user *types.User
var webhookData *types.WebhookData
var sshPrivKey string
var cloneURL string
var sshHostKey string
var skipSSHHostKeyCheck bool
var gitSource gitsource.GitSource
if runType == types.RunTypeProject {
csProject, _, err := h.configstoreClient.GetProject(ctx, projectID) csProject, _, err := h.configstoreClient.GetProject(ctx, projectID)
if err != nil { if err != nil {
return http.StatusBadRequest, "", errors.Errorf("failed to get project %s: %w", projectID, err) return http.StatusBadRequest, "", errors.Errorf("failed to get project %s: %w", projectID, err)
} }
project = csProject.Project project := csProject.Project
user, _, err := h.configstoreClient.GetUserByLinkedAccount(ctx, project.LinkedAccountID) user, _, err := h.configstoreClient.GetUserByLinkedAccount(ctx, project.LinkedAccountID)
if err != nil { if err != nil {
@ -93,20 +74,20 @@ func (h *webhooksHandler) handleWebhook(r *http.Request) (int, string, error) {
return http.StatusInternalServerError, "", errors.Errorf("failed to get remote source %q: %w", la.RemoteSourceID, err) return http.StatusInternalServerError, "", errors.Errorf("failed to get remote source %q: %w", la.RemoteSourceID, err)
} }
gitSource, err = h.ah.GetGitSource(ctx, rs, user.Name, la) gitSource, err := h.ah.GetGitSource(ctx, rs, user.Name, la)
if err != nil { if err != nil {
return http.StatusInternalServerError, "", errors.Errorf("failed to create gitea client: %w", err) return http.StatusInternalServerError, "", errors.Errorf("failed to create gitea client: %w", err)
} }
sshPrivKey = project.SSHPrivateKey sshPrivKey := project.SSHPrivateKey
sshHostKey = rs.SSHHostKey sshHostKey := rs.SSHHostKey
// use remotesource skipSSHHostKeyCheck config and override with project config if set to true there // use remotesource skipSSHHostKeyCheck config and override with project config if set to true there
skipSSHHostKeyCheck = rs.SkipSSHHostKeyCheck skipSSHHostKeyCheck := rs.SkipSSHHostKeyCheck
if project.SkipSSHHostKeyCheck { if project.SkipSSHHostKeyCheck {
skipSSHHostKeyCheck = project.SkipSSHHostKeyCheck skipSSHHostKeyCheck = project.SkipSSHHostKeyCheck
} }
runType = types.RunTypeProject
webhookData, err = gitSource.ParseWebhook(r, project.WebhookSecret) webhookData, err := gitSource.ParseWebhook(r, project.WebhookSecret)
if err != nil { if err != nil {
return http.StatusBadRequest, "", errors.Errorf("failed to parse webhook: %w", err) return http.StatusBadRequest, "", errors.Errorf("failed to parse webhook: %w", err)
} }
@ -117,43 +98,17 @@ func (h *webhooksHandler) handleWebhook(r *http.Request) (int, string, error) {
return 0, "", nil return 0, "", nil
} }
webhookData.ProjectID = projectID cloneURL := webhookData.SSHURL
cloneURL = webhookData.SSHURL
} else {
gitSource = agolagit.New(h.apiExposedURL + "/repos")
var err error
webhookData, err = gitSource.ParseWebhook(r, "")
if err != nil {
return http.StatusBadRequest, "", errors.Errorf("failed to parse webhook: %w", err)
}
// skip nil webhook data
// TODO(sgotti) report the reason of the skip
if webhookData == nil {
h.log.Infof("skipping webhook")
return 0, "", nil
}
user, _, err = h.configstoreClient.GetUser(ctx, userID)
if err != nil {
return http.StatusBadRequest, "", errors.Errorf("failed to get user with id %q: %w", userID, err)
}
h.log.Debugf("user: %s", util.Dump(user))
cloneURL = fmt.Sprintf("%s/%s", h.apiExposedURL+"/repos", webhookData.Repo.Path)
runType = types.RunTypeUser
}
h.log.Infof("webhookData: %s", util.Dump(webhookData)) h.log.Infof("webhookData: %s", util.Dump(webhookData))
req := &action.CreateRunRequest{ req := &action.CreateRunRequest{
RunType: runType, RunType: types.RunTypeProject,
RefType: common.WebHookEventToRunRefType(webhookData.Event), RefType: common.WebHookEventToRunRefType(webhookData.Event),
RunCreationTrigger: types.RunCreationTriggerTypeWebhook, RunCreationTrigger: types.RunCreationTriggerTypeWebhook,
Project: project, Project: project,
User: user, User: nil,
RepoPath: webhookData.Repo.Path, RepoPath: webhookData.Repo.Path,
GitSource: gitSource, GitSource: gitSource,
CommitSHA: webhookData.CommitSHA, CommitSHA: webhookData.CommitSHA,

View File

@ -24,7 +24,6 @@ const (
type WebhookData struct { type WebhookData struct {
Event WebhookEvent `json:"event,omitempty"` Event WebhookEvent `json:"event,omitempty"`
ProjectID string `json:"project_id,omitempty"`
SSHURL string `json:"ssh_url"` SSHURL string `json:"ssh_url"`
CompareLink string `json:"compare_link,omitempty"` // Compare link to remote git source CompareLink string `json:"compare_link,omitempty"` // Compare link to remote git source