agola/internal/services/gateway/webhook.go

182 lines
6.0 KiB
Go
Raw Normal View History

2019-02-21 16:58:25 +00:00
// Copyright 2019 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package gateway
import (
"fmt"
"net/http"
gitsource "github.com/sorintlab/agola/internal/gitsources"
2019-02-21 22:01:17 +00:00
"github.com/sorintlab/agola/internal/gitsources/agolagit"
"github.com/sorintlab/agola/internal/services/common"
2019-02-21 16:58:25 +00:00
csapi "github.com/sorintlab/agola/internal/services/configstore/api"
"github.com/sorintlab/agola/internal/services/gateway/action"
rsapi "github.com/sorintlab/agola/internal/services/runservice/api"
2019-02-21 16:58:25 +00:00
"github.com/sorintlab/agola/internal/services/types"
"github.com/sorintlab/agola/internal/util"
"go.uber.org/zap"
errors "golang.org/x/xerrors"
2019-02-21 16:58:25 +00:00
)
type webhooksHandler struct {
log *zap.SugaredLogger
ah *action.ActionHandler
2019-02-21 16:58:25 +00:00
configstoreClient *csapi.Client
runserviceClient *rsapi.Client
apiExposedURL string
}
func (h *webhooksHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
code, userErr, err := h.handleWebhook(r)
if err != nil {
h.log.Errorf("err: %+v", err)
http.Error(w, userErr, code)
}
}
func (h *webhooksHandler) handleWebhook(r *http.Request) (int, string, error) {
ctx := r.Context()
projectID := r.URL.Query().Get("projectid")
2019-04-03 13:11:50 +00:00
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
2019-02-21 22:01:17 +00:00
if projectID == "" {
runType = types.RunTypeUser
2019-02-21 22:01:17 +00:00
}
2019-02-21 16:58:25 +00:00
defer r.Body.Close()
var project *types.Project
var user *types.User
2019-02-21 22:01:17 +00:00
var webhookData *types.WebhookData
var sshPrivKey string
var cloneURL string
var sshHostKey string
2019-02-21 22:01:17 +00:00
var skipSSHHostKeyCheck bool
2019-02-21 16:58:25 +00:00
var gitSource gitsource.GitSource
if runType == types.RunTypeProject {
csProject, _, err := h.configstoreClient.GetProject(ctx, projectID)
2019-02-21 22:01:17 +00:00
if err != nil {
return http.StatusBadRequest, "", errors.Errorf("failed to get project %s: %w", projectID, err)
2019-02-21 22:01:17 +00:00
}
project = csProject.Project
2019-02-21 16:58:25 +00:00
2019-02-21 22:01:17 +00:00
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)
2019-02-21 22:01:17 +00:00
}
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)
2019-02-21 22:01:17 +00:00
}
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)
2019-02-21 22:01:17 +00:00
}
2019-02-21 16:58:25 +00:00
gitSource, err = h.ah.GetGitSource(ctx, rs, user.Name, la)
2019-02-21 22:01:17 +00:00
if err != nil {
return http.StatusInternalServerError, "", errors.Errorf("failed to create gitea client: %w", err)
2019-02-21 22:01:17 +00:00
}
2019-02-21 16:58:25 +00:00
2019-02-21 22:01:17 +00:00
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
}
2019-02-21 22:01:17 +00:00
runType = types.RunTypeProject
webhookData, err = gitSource.ParseWebhook(r, project.WebhookSecret)
2019-02-21 22:01:17 +00:00
if err != nil {
return http.StatusBadRequest, "", errors.Errorf("failed to parse webhook: %w", err)
2019-02-21 22:01:17 +00:00
}
2019-04-03 15:05:50 +00:00
// skip nil webhook data
// TODO(sgotti) report the reason of the skip
if webhookData == nil {
h.log.Infof("skipping webhook")
return 0, "", nil
}
2019-02-21 22:01:17 +00:00
webhookData.ProjectID = projectID
cloneURL = webhookData.SSHURL
2019-02-21 22:01:17 +00:00
} else {
gitSource = agolagit.New(h.apiExposedURL + "/repos")
var err error
webhookData, err = gitSource.ParseWebhook(r, "")
2019-02-21 22:01:17 +00:00
if err != nil {
return http.StatusBadRequest, "", errors.Errorf("failed to parse webhook: %w", err)
2019-02-21 22:01:17 +00:00
}
2019-04-03 15:05:50 +00:00
// skip nil webhook data
// TODO(sgotti) report the reason of the skip
if webhookData == nil {
h.log.Infof("skipping webhook")
return 0, "", nil
}
2019-02-21 22:01:17 +00:00
user, _, err = h.configstoreClient.GetUser(ctx, userID)
2019-02-21 22:01:17 +00:00
if err != nil {
return http.StatusBadRequest, "", errors.Errorf("failed to get user with id %q: %w", userID, err)
2019-02-21 22:01:17 +00:00
}
h.log.Debugf("user: %s", util.Dump(user))
cloneURL = fmt.Sprintf("%s/%s", h.apiExposedURL+"/repos", webhookData.Repo.Path)
2019-02-21 22:01:17 +00:00
runType = types.RunTypeUser
2019-02-21 16:58:25 +00:00
}
h.log.Infof("webhookData: %s", util.Dump(webhookData))
req := &action.CreateRunRequest{
RunType: runType,
RefType: common.WebHookEventToRunRefType(webhookData.Event),
RunCreationTrigger: types.RunCreationTriggerTypeWebhook,
Project: project,
User: user,
RepoPath: webhookData.Repo.Path,
GitSource: gitSource,
CommitSHA: webhookData.CommitSHA,
Message: webhookData.Message,
Branch: webhookData.Branch,
Tag: webhookData.Tag,
PullRequestID: webhookData.PullRequestID,
Ref: webhookData.Ref,
SSHPrivKey: sshPrivKey,
SSHHostKey: sshHostKey,
SkipSSHHostKeyCheck: skipSSHHostKeyCheck,
CloneURL: cloneURL,
CommitLink: webhookData.CommitLink,
BranchLink: webhookData.BranchLink,
TagLink: webhookData.TagLink,
PullRequestLink: webhookData.PullRequestLink,
CompareLink: webhookData.CompareLink,
}
if err := h.ah.CreateRuns(ctx, req); err != nil {
return http.StatusInternalServerError, "", errors.Errorf("failed to create run: %w", err)
2019-02-21 16:58:25 +00:00
}
return 0, "", nil
}