2019-02-21 15:06:34 +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 gitea
|
|
|
|
|
|
|
|
import (
|
2019-05-07 16:29:31 +00:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2019-02-21 15:06:34 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-05-07 16:29:31 +00:00
|
|
|
"io/ioutil"
|
2019-02-21 15:06:34 +00:00
|
|
|
"net/http"
|
2019-04-03 13:01:21 +00:00
|
|
|
"path"
|
2019-02-21 15:06:34 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/services/types"
|
2019-02-21 15:06:34 +00:00
|
|
|
|
2019-05-23 09:23:14 +00:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-02-21 15:06:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-05-07 16:29:31 +00:00
|
|
|
hookEvent = "X-Gitea-Event"
|
|
|
|
signatureHeader = "X-Gitea-Signature"
|
2019-02-21 15:06:34 +00:00
|
|
|
|
|
|
|
hookPush = "push"
|
|
|
|
hookPullRequest = "pull_request"
|
|
|
|
|
|
|
|
prStateOpen = "open"
|
|
|
|
|
|
|
|
prActionOpen = "opened"
|
|
|
|
prActionSync = "synchronized"
|
|
|
|
)
|
|
|
|
|
2019-05-07 16:29:31 +00:00
|
|
|
func (c *Client) ParseWebhook(r *http.Request, secret string) (*types.WebhookData, error) {
|
|
|
|
data, err := ioutil.ReadAll(io.LimitReader(r.Body, 10*1024*1024))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify signature
|
2019-05-22 09:39:27 +00:00
|
|
|
signature := r.Header.Get(signatureHeader)
|
|
|
|
// old versions of gitea doesn't provide a signature
|
|
|
|
if secret != "" && signature != "" {
|
2019-05-07 16:29:31 +00:00
|
|
|
ds, err := hex.DecodeString(signature)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Errorf("wrong webhook signature")
|
|
|
|
}
|
|
|
|
h := hmac.New(sha256.New, []byte(secret))
|
2019-07-02 14:10:39 +00:00
|
|
|
if _, err := h.Write(data); err != nil {
|
|
|
|
return nil, errors.Errorf("failed to calculate webhook signature")
|
|
|
|
}
|
2019-05-07 16:29:31 +00:00
|
|
|
cs := h.Sum(nil)
|
|
|
|
if !hmac.Equal(cs, ds) {
|
|
|
|
return nil, errors.Errorf("wrong webhook signature")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 15:06:34 +00:00
|
|
|
switch r.Header.Get(hookEvent) {
|
|
|
|
case hookPush:
|
2019-05-07 16:29:31 +00:00
|
|
|
return parsePushHook(data)
|
2019-02-21 15:06:34 +00:00
|
|
|
case hookPullRequest:
|
2019-05-07 16:29:31 +00:00
|
|
|
return parsePullRequestHook(data)
|
2019-02-21 15:06:34 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unknown webhook event type: %q", r.Header.Get(hookEvent))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:29:31 +00:00
|
|
|
func parsePushHook(data []byte) (*types.WebhookData, error) {
|
2019-02-21 15:06:34 +00:00
|
|
|
push := new(pushHook)
|
2019-05-07 16:29:31 +00:00
|
|
|
err := json.Unmarshal(data, push)
|
2019-02-21 15:06:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return webhookDataFromPush(push)
|
|
|
|
}
|
|
|
|
|
2019-05-07 16:29:31 +00:00
|
|
|
func parsePullRequestHook(data []byte) (*types.WebhookData, error) {
|
|
|
|
prhook := new(pullRequestHook)
|
|
|
|
err := json.Unmarshal(data, prhook)
|
2019-02-21 15:06:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip non open pull requests
|
|
|
|
if prhook.PullRequest.State != prStateOpen {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
// only accept actions that have new commits
|
|
|
|
if prhook.Action != prActionOpen && prhook.Action != prActionSync {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return webhookDataFromPullRequest(prhook), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func webhookDataFromPush(hook *pushHook) (*types.WebhookData, error) {
|
|
|
|
sender := hook.Sender.Username
|
|
|
|
if sender == "" {
|
|
|
|
sender = hook.Sender.Login
|
|
|
|
}
|
|
|
|
|
|
|
|
// common data
|
|
|
|
whd := &types.WebhookData{
|
|
|
|
CommitSHA: hook.After,
|
2019-04-11 14:49:07 +00:00
|
|
|
SSHURL: hook.Repo.SSHURL,
|
2019-02-21 15:06:34 +00:00
|
|
|
Ref: hook.Ref,
|
|
|
|
CompareLink: hook.Compare,
|
|
|
|
CommitLink: fmt.Sprintf("%s/commit/%s", hook.Repo.URL, hook.After),
|
|
|
|
Sender: sender,
|
|
|
|
|
|
|
|
Repo: types.WebhookDataRepo{
|
2019-04-03 13:01:21 +00:00
|
|
|
Path: path.Join(hook.Repo.Owner.Username, hook.Repo.Name),
|
|
|
|
WebURL: hook.Repo.URL,
|
2019-02-21 15:06:34 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(hook.Ref, "refs/heads/"):
|
|
|
|
whd.Event = types.WebhookEventPush
|
|
|
|
whd.Branch = strings.TrimPrefix(hook.Ref, "refs/heads/")
|
|
|
|
whd.BranchLink = fmt.Sprintf("%s/src/branch/%s", hook.Repo.URL, whd.Branch)
|
|
|
|
if len(hook.Commits) > 0 {
|
|
|
|
whd.Message = hook.Commits[0].Message
|
|
|
|
}
|
|
|
|
case strings.HasPrefix(hook.Ref, "refs/tags/"):
|
|
|
|
whd.Event = types.WebhookEventTag
|
|
|
|
whd.Tag = strings.TrimPrefix(hook.Ref, "refs/tags/")
|
|
|
|
whd.TagLink = fmt.Sprintf("%s/src/tag/%s", hook.Repo.URL, whd.Tag)
|
|
|
|
whd.Message = fmt.Sprintf("Tag %s", whd.Tag)
|
|
|
|
default:
|
|
|
|
// ignore received webhook since it doesn't have a ref we're interested in
|
2019-04-03 13:01:21 +00:00
|
|
|
return nil, fmt.Errorf("unsupported webhook ref %q", hook.Ref)
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return whd, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function that extracts the Build data from a Gitea pull_request hook
|
|
|
|
func webhookDataFromPullRequest(hook *pullRequestHook) *types.WebhookData {
|
|
|
|
sender := hook.Sender.Username
|
|
|
|
if sender == "" {
|
|
|
|
sender = hook.Sender.Login
|
|
|
|
}
|
2019-04-11 14:49:07 +00:00
|
|
|
whd := &types.WebhookData{
|
2019-02-21 15:06:34 +00:00
|
|
|
Event: types.WebhookEventPullRequest,
|
|
|
|
CommitSHA: hook.PullRequest.Head.Sha,
|
2019-04-11 14:49:07 +00:00
|
|
|
SSHURL: hook.Repo.SSHURL,
|
2019-02-21 15:06:34 +00:00
|
|
|
Ref: fmt.Sprintf("refs/pull/%d/head", hook.Number),
|
|
|
|
CommitLink: fmt.Sprintf("%s/commit/%s", hook.Repo.URL, hook.PullRequest.Head.Sha),
|
|
|
|
Branch: hook.PullRequest.Base.Ref,
|
|
|
|
Message: hook.PullRequest.Title,
|
|
|
|
Sender: sender,
|
|
|
|
PullRequestID: strconv.FormatInt(hook.PullRequest.ID, 10),
|
|
|
|
PullRequestLink: hook.PullRequest.URL,
|
|
|
|
|
|
|
|
Repo: types.WebhookDataRepo{
|
2019-04-03 13:01:21 +00:00
|
|
|
Path: path.Join(hook.Repo.Owner.Username, hook.Repo.Name),
|
|
|
|
WebURL: hook.Repo.URL,
|
2019-02-21 15:06:34 +00:00
|
|
|
},
|
|
|
|
}
|
2019-04-11 14:49:07 +00:00
|
|
|
return whd
|
2019-02-21 15:06:34 +00:00
|
|
|
}
|