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.
|
|
|
|
|
2019-06-11 15:07:08 +00:00
|
|
|
package api
|
2019-02-21 16:58:25 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/services/common"
|
|
|
|
"agola.io/agola/internal/services/gateway/action"
|
|
|
|
"agola.io/agola/internal/services/types"
|
|
|
|
"agola.io/agola/internal/util"
|
2019-07-31 13:39:07 +00:00
|
|
|
csclient "agola.io/agola/services/configstore/client"
|
|
|
|
rsclient "agola.io/agola/services/runservice/client"
|
2019-02-21 16:58:25 +00:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2019-05-23 09:23:14 +00:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-02-21 16:58:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type webhooksHandler struct {
|
|
|
|
log *zap.SugaredLogger
|
2019-05-03 21:48:49 +00:00
|
|
|
ah *action.ActionHandler
|
2019-07-31 13:39:07 +00:00
|
|
|
configstoreClient *csclient.Client
|
|
|
|
runserviceClient *rsclient.Client
|
2019-02-21 16:58:25 +00:00
|
|
|
apiExposedURL string
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:39:07 +00:00
|
|
|
func NewWebhooksHandler(logger *zap.Logger, ah *action.ActionHandler, configstoreClient *csclient.Client, runserviceClient *rsclient.Client, apiExposedURL string) *webhooksHandler {
|
2019-06-11 15:07:08 +00:00
|
|
|
return &webhooksHandler{
|
|
|
|
log: logger.Sugar(),
|
|
|
|
ah: ah,
|
|
|
|
configstoreClient: configstoreClient,
|
|
|
|
runserviceClient: runserviceClient,
|
|
|
|
apiExposedURL: apiExposedURL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
func (h *webhooksHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-06-11 15:07:08 +00:00
|
|
|
err := h.handleWebhook(r)
|
|
|
|
if httpError(w, err) {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := httpResponse(w, http.StatusOK, nil); err != nil {
|
2019-02-21 16:58:25 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 15:07:08 +00:00
|
|
|
func (h *webhooksHandler) handleWebhook(r *http.Request) error {
|
2019-02-21 16:58:25 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
projectID := r.URL.Query().Get("projectid")
|
2019-02-21 22:01:17 +00:00
|
|
|
if projectID == "" {
|
2019-06-11 15:07:08 +00:00
|
|
|
return util.NewErrBadRequest(errors.Errorf("bad webhook url %q. Missing projectid", r.URL))
|
2019-02-21 22:01:17 +00:00
|
|
|
}
|
2019-02-21 16:58:25 +00:00
|
|
|
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
2019-06-11 13:17:08 +00:00
|
|
|
csProject, _, err := h.configstoreClient.GetProject(ctx, projectID)
|
|
|
|
if err != nil {
|
2019-06-11 15:07:08 +00:00
|
|
|
return util.NewErrBadRequest(errors.Errorf("failed to get project %s: %w", projectID, err))
|
2019-06-11 13:17:08 +00:00
|
|
|
}
|
|
|
|
project := csProject.Project
|
|
|
|
|
|
|
|
user, _, err := h.configstoreClient.GetUserByLinkedAccount(ctx, project.LinkedAccountID)
|
|
|
|
if err != nil {
|
2019-06-11 15:07:08 +00:00
|
|
|
return util.NewErrInternal(errors.Errorf("failed to get user by linked account %q: %w", project.LinkedAccountID, err))
|
2019-06-11 13:17:08 +00:00
|
|
|
}
|
|
|
|
la := user.LinkedAccounts[project.LinkedAccountID]
|
|
|
|
if la == nil {
|
2019-06-11 15:07:08 +00:00
|
|
|
return util.NewErrInternal(errors.Errorf("linked account %q in user %q doesn't exist", project.LinkedAccountID, user.Name))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
2019-06-11 13:17:08 +00:00
|
|
|
rs, _, err := h.configstoreClient.GetRemoteSource(ctx, la.RemoteSourceID)
|
|
|
|
if err != nil {
|
2019-06-11 15:07:08 +00:00
|
|
|
return util.NewErrInternal(errors.Errorf("failed to get remote source %q: %w", la.RemoteSourceID, err))
|
2019-06-11 13:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gitSource, err := h.ah.GetGitSource(ctx, rs, user.Name, la)
|
|
|
|
if err != nil {
|
2019-06-11 15:07:08 +00:00
|
|
|
return util.NewErrInternal(errors.Errorf("failed to create gitea client: %w", err))
|
2019-06-11 13:17:08 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
webhookData, err := gitSource.ParseWebhook(r, project.WebhookSecret)
|
|
|
|
if err != nil {
|
2019-06-11 15:07:08 +00:00
|
|
|
return util.NewErrBadRequest(errors.Errorf("failed to parse webhook: %w", err))
|
2019-06-11 13:17:08 +00:00
|
|
|
}
|
|
|
|
// skip nil webhook data
|
|
|
|
// TODO(sgotti) report the reason of the skip
|
|
|
|
if webhookData == nil {
|
|
|
|
h.log.Infof("skipping webhook")
|
2019-06-11 15:07:08 +00:00
|
|
|
return nil
|
2019-06-11 13:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cloneURL := webhookData.SSHURL
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-06-11 07:31:12 +00:00
|
|
|
req := &action.CreateRunRequest{
|
2019-06-11 13:17:08 +00:00
|
|
|
RunType: types.RunTypeProject,
|
2019-06-11 07:31:12 +00:00
|
|
|
RefType: common.WebHookEventToRunRefType(webhookData.Event),
|
|
|
|
RunCreationTrigger: types.RunCreationTriggerTypeWebhook,
|
|
|
|
|
|
|
|
Project: project,
|
2019-06-11 13:17:08 +00:00
|
|
|
User: nil,
|
2019-06-11 07:31:12 +00:00
|
|
|
RepoPath: webhookData.Repo.Path,
|
|
|
|
GitSource: gitSource,
|
|
|
|
CommitSHA: webhookData.CommitSHA,
|
|
|
|
Message: webhookData.Message,
|
|
|
|
Branch: webhookData.Branch,
|
|
|
|
Tag: webhookData.Tag,
|
|
|
|
PullRequestID: webhookData.PullRequestID,
|
2020-01-25 11:33:53 +00:00
|
|
|
PRFromSameRepo: webhookData.PRFromSameRepo,
|
2019-06-11 07:31:12 +00:00
|
|
|
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 {
|
2019-06-11 15:07:08 +00:00
|
|
|
return util.NewErrInternal(errors.Errorf("failed to create run: %w", err))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 15:07:08 +00:00
|
|
|
return nil
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|