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
import (
"fmt"
"net/http"
gitsource "github.com/sorintlab/agola/internal/gitsources"
"github.com/sorintlab/agola/internal/gitsources/agolagit"
"github.com/sorintlab/agola/internal/services/common"
csapi "github.com/sorintlab/agola/internal/services/configstore/api"
"github.com/sorintlab/agola/internal/services/gateway/action"
@ -51,109 +48,67 @@ func (h *webhooksHandler) handleWebhook(r *http.Request) (int, string, error) {
ctx := r.Context()
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 == "" {
runType = types.RunTypeUser
return http.StatusBadRequest, "", errors.Errorf("bad webhook url %q. Missing projectid", r.URL)
}
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)
if err != nil {
return http.StatusBadRequest, "", errors.Errorf("failed to get project %s: %w", projectID, err)
}
project = csProject.Project
user, _, err := h.configstoreClient.GetUserByLinkedAccount(ctx, project.LinkedAccountID)
if err != nil {
return http.StatusInternalServerError, "", errors.Errorf("failed to get user by linked account %q: %w", project.LinkedAccountID, err)
}
la := user.LinkedAccounts[project.LinkedAccountID]
h.log.Infof("la: %s", util.Dump(la))
if la == nil {
return http.StatusInternalServerError, "", errors.Errorf("linked account %q in user %q doesn't exist", project.LinkedAccountID, user.Name)
}
rs, _, err := h.configstoreClient.GetRemoteSource(ctx, la.RemoteSourceID)
if err != nil {
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)
if err != nil {
return http.StatusInternalServerError, "", errors.Errorf("failed to create gitea client: %w", err)
}
sshPrivKey = project.SSHPrivateKey
sshHostKey = rs.SSHHostKey
// use remotesource skipSSHHostKeyCheck config and override with project config if set to true there
skipSSHHostKeyCheck = rs.SkipSSHHostKeyCheck
if project.SkipSSHHostKeyCheck {
skipSSHHostKeyCheck = project.SkipSSHHostKeyCheck
}
runType = types.RunTypeProject
webhookData, err = gitSource.ParseWebhook(r, project.WebhookSecret)
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
}
webhookData.ProjectID = projectID
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
csProject, _, err := h.configstoreClient.GetProject(ctx, projectID)
if err != nil {
return http.StatusBadRequest, "", errors.Errorf("failed to get project %s: %w", projectID, err)
}
project := csProject.Project
user, _, err := h.configstoreClient.GetUserByLinkedAccount(ctx, project.LinkedAccountID)
if err != nil {
return http.StatusInternalServerError, "", errors.Errorf("failed to get user by linked account %q: %w", project.LinkedAccountID, err)
}
la := user.LinkedAccounts[project.LinkedAccountID]
h.log.Infof("la: %s", util.Dump(la))
if la == nil {
return http.StatusInternalServerError, "", errors.Errorf("linked account %q in user %q doesn't exist", project.LinkedAccountID, user.Name)
}
rs, _, err := h.configstoreClient.GetRemoteSource(ctx, la.RemoteSourceID)
if err != nil {
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)
if err != nil {
return http.StatusInternalServerError, "", errors.Errorf("failed to create gitea client: %w", err)
}
sshPrivKey := project.SSHPrivateKey
sshHostKey := rs.SSHHostKey
// use remotesource skipSSHHostKeyCheck config and override with project config if set to true there
skipSSHHostKeyCheck := rs.SkipSSHHostKeyCheck
if project.SkipSSHHostKeyCheck {
skipSSHHostKeyCheck = project.SkipSSHHostKeyCheck
}
webhookData, err := gitSource.ParseWebhook(r, project.WebhookSecret)
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
}
cloneURL := webhookData.SSHURL
h.log.Infof("webhookData: %s", util.Dump(webhookData))
req := &action.CreateRunRequest{
RunType: runType,
RunType: types.RunTypeProject,
RefType: common.WebHookEventToRunRefType(webhookData.Event),
RunCreationTrigger: types.RunCreationTriggerTypeWebhook,
Project: project,
User: user,
User: nil,
RepoPath: webhookData.Repo.Path,
GitSource: gitSource,
CommitSHA: webhookData.CommitSHA,

View File

@ -23,9 +23,8 @@ const (
)
type WebhookData struct {
Event WebhookEvent `json:"event,omitempty"`
ProjectID string `json:"project_id,omitempty"`
SSHURL string `json:"ssh_url"`
Event WebhookEvent `json:"event,omitempty"`
SSHURL string `json:"ssh_url"`
CompareLink string `json:"compare_link,omitempty"` // Compare link to remote git source
CommitLink string `json:"commit_link,omitempty"` // Commit link to remote git source