github.com/covergates/covergates@v0.2.2-0.20201009050117-42ef8a19fb95/modules/scm/hook.go (about)

     1  package scm
     2  
     3  import (
     4  	"errors"
     5  	"net/http"
     6  
     7  	"github.com/covergates/covergates/config"
     8  	"github.com/covergates/covergates/core"
     9  	"github.com/drone/go-scm/scm"
    10  )
    11  
    12  var errWebhookNotSuport = errors.New("webhook not support")
    13  
    14  type webhookService struct {
    15  	config *config.Config
    16  	client *scm.Client
    17  	scm    core.SCMProvider
    18  }
    19  
    20  func (service *webhookService) Parse(req *http.Request) (core.HookEvent, error) {
    21  	config := service.config
    22  	hook, err := service.client.Webhooks.Parse(req, func(webhook scm.Webhook) (string, error) {
    23  		return config.Server.Secret, nil
    24  	})
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	switch event := hook.(type) {
    30  	case *scm.PullRequestHook:
    31  		if event.Action == scm.ActionMerge {
    32  			return &core.PullRequestHook{
    33  				Number: event.PullRequest.Number,
    34  				Merged: true,
    35  				Commit: event.PullRequest.Sha,
    36  				Source: event.PullRequest.Source,
    37  				Target: event.PullRequest.Target,
    38  			}, nil
    39  		} else if service.scm == core.Gitea && event.Action == scm.ActionClose {
    40  			return &core.PullRequestHook{
    41  				Number: event.PullRequest.Number,
    42  				Merged: true,
    43  				Commit: event.PullRequest.Sha,
    44  				Source: event.PullRequest.Source,
    45  				Target: event.PullRequest.Target,
    46  			}, nil
    47  		}
    48  	}
    49  	return nil, errWebhookNotSuport
    50  }
    51  
    52  func (service *webhookService) IsWebhookNotSupport(err error) bool {
    53  	return err == errWebhookNotSuport
    54  }