Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set repo/owner in different fields for webhookinfo #19

Merged
merged 2 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions vcsutils/webhookparser/bitbucketcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"net/http"
"strings"
"time"

"github.com/jfrog/froggit-go/vcsutils"
Expand Down Expand Up @@ -55,22 +56,32 @@ func (webhook *BitbucketCloudWebhook) parseIncomingWebhook(payload []byte) (*Web

func (webhook *BitbucketCloudWebhook) parsePushEvent(bitbucketCloudWebHook *bitbucketCloudWebHook) *WebhookInfo {
return &WebhookInfo{
Repository: bitbucketCloudWebHook.Repository.FullName,
Branch: bitbucketCloudWebHook.Push.Changes[0].New.Name,
Timestamp: bitbucketCloudWebHook.Push.Changes[0].New.Target.Date.UTC().Unix(),
Event: vcsutils.Push,
TargetRepositoryDetails: webhook.parseRepoFullName(bitbucketCloudWebHook.Repository.FullName),
Branch: bitbucketCloudWebHook.Push.Changes[0].New.Name,
Timestamp: bitbucketCloudWebHook.Push.Changes[0].New.Target.Date.UTC().Unix(),
Event: vcsutils.Push,
}
}

func (webhook *BitbucketCloudWebhook) parsePrEvents(bitbucketCloudWebHook *bitbucketCloudWebHook, event vcsutils.WebhookEvent) *WebhookInfo {
return &WebhookInfo{
PullRequestId: bitbucketCloudWebHook.PullRequest.Id,
Repository: bitbucketCloudWebHook.PullRequest.Destination.Repository.FullName,
Branch: bitbucketCloudWebHook.PullRequest.Destination.Branch.Name,
SourceRepository: bitbucketCloudWebHook.PullRequest.Source.Repository.FullName,
SourceBranch: bitbucketCloudWebHook.PullRequest.Source.Branch.Name,
Timestamp: bitbucketCloudWebHook.PullRequest.UpdatedOn.UTC().Unix(),
Event: event,
PullRequestId: bitbucketCloudWebHook.PullRequest.Id,
TargetRepositoryDetails: webhook.parseRepoFullName(bitbucketCloudWebHook.PullRequest.Destination.Repository.FullName),
Branch: bitbucketCloudWebHook.PullRequest.Destination.Branch.Name,
SourceRepositoryDetails: webhook.parseRepoFullName(bitbucketCloudWebHook.PullRequest.Source.Repository.FullName),
SourceBranch: bitbucketCloudWebHook.PullRequest.Source.Branch.Name,
Timestamp: bitbucketCloudWebHook.PullRequest.UpdatedOn.UTC().Unix(),
Event: event,
}
}

func (webhook *BitbucketCloudWebhook) parseRepoFullName(fullName string) WebHookInfoRepoDetails {
// From https://support.atlassian.com/bitbucket-cloud/docs/event-payloads/#Repository
// "full_name : The workspace and repository slugs joined with a '/'."
split := strings.Split(fullName, "/")
return WebHookInfoRepoDetails{
Name: split[1],
Owner: split[0],
}
}

Expand Down
15 changes: 10 additions & 5 deletions vcsutils/webhookparser/bitbucketcloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func TestBitbucketCloudParseIncomingPushWebhook(t *testing.T) {
assert.NoError(t, err)

// Check values
assert.Equal(t, expectedRepoName, actual.Repository)
assert.Equal(t, expectedRepoName, actual.TargetRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.TargetRepositoryDetails.Owner)
assert.Equal(t, expectedBranch, actual.Branch)
assert.Equal(t, bitbucketCloudPushExpectedTime, actual.Timestamp)
assert.Equal(t, vcsutils.Push, actual.Event)
Expand All @@ -52,10 +53,12 @@ func TestBitbucketCloudParseIncomingPrCreateWebhook(t *testing.T) {

// Check values
assert.Equal(t, bitbucketCloudExpectedPrId, actual.PullRequestId)
assert.Equal(t, expectedRepoName, actual.Repository)
assert.Equal(t, expectedRepoName, actual.TargetRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.TargetRepositoryDetails.Owner)
assert.Equal(t, expectedBranch, actual.Branch)
assert.Equal(t, bitbucketCloudPrCreateExpectedTime, actual.Timestamp)
assert.Equal(t, expectedRepoName, actual.SourceRepository)
assert.Equal(t, expectedRepoName, actual.SourceRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.SourceRepositoryDetails.Owner)
assert.Equal(t, expectedSourceBranch, actual.SourceBranch)
assert.Equal(t, vcsutils.PrCreated, actual.Event)
}
Expand All @@ -75,10 +78,12 @@ func TestBitbucketCloudParseIncomingPrUpdateWebhook(t *testing.T) {

// Check values
assert.Equal(t, bitbucketCloudExpectedPrId, actual.PullRequestId)
assert.Equal(t, expectedRepoName, actual.Repository)
assert.Equal(t, expectedRepoName, actual.TargetRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.TargetRepositoryDetails.Owner)
assert.Equal(t, expectedBranch, actual.Branch)
assert.Equal(t, bitbucketCloudPrUpdateExpectedTime, actual.Timestamp)
assert.Equal(t, expectedRepoName, actual.SourceRepository)
assert.Equal(t, expectedRepoName, actual.SourceRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.SourceRepositoryDetails.Owner)
assert.Equal(t, expectedSourceBranch, actual.SourceBranch)
assert.Equal(t, vcsutils.PrEdited, actual.Event)
}
Expand Down
34 changes: 19 additions & 15 deletions vcsutils/webhookparser/bitbucketserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,35 @@ func (webhook *BitbucketServerWebhook) parsePushEvent(bitbucketCloudWebHook *bit
if err != nil {
return nil, err
}
repository := bitbucketCloudWebHook.Repository
return &WebhookInfo{
Repository: getFullRepositoryName(bitbucketCloudWebHook.Repository),
Branch: strings.TrimPrefix(bitbucketCloudWebHook.Changes[0].RefId, "refs/heads/"),
Timestamp: eventTime.UTC().Unix(),
Event: vcsutils.Push,
TargetRepositoryDetails: webhook.getRepositoryDetails(repository),
Branch: strings.TrimPrefix(bitbucketCloudWebHook.Changes[0].RefId, "refs/heads/"),
Timestamp: eventTime.UTC().Unix(),
Event: vcsutils.Push,
}, nil
}

func (webhook *BitbucketServerWebhook) getRepositoryDetails(repository bitbucketv1.Repository) WebHookInfoRepoDetails {
return WebHookInfoRepoDetails{
Name: repository.Slug,
Owner: strings.TrimPrefix(strings.ToLower(repository.Project.Key), "~"),
}
}

func (webhook *BitbucketServerWebhook) parsePrEvents(bitbucketCloudWebHook *bitbucketServerWebHook, event vcsutils.WebhookEvent) (*WebhookInfo, error) {
eventTime, err := time.Parse("2006-01-02T15:04:05-0700", bitbucketCloudWebHook.Date)
if err != nil {
return nil, err
}
return &WebhookInfo{
PullRequestId: bitbucketCloudWebHook.PullRequest.ID,
Repository: getFullRepositoryName(bitbucketCloudWebHook.PullRequest.ToRef.Repository),
Branch: strings.TrimPrefix(bitbucketCloudWebHook.PullRequest.ToRef.ID, "refs/heads/"),
SourceRepository: getFullRepositoryName(bitbucketCloudWebHook.PullRequest.FromRef.Repository),
SourceBranch: strings.TrimPrefix(bitbucketCloudWebHook.PullRequest.FromRef.ID, "refs/heads/"),
Timestamp: eventTime.UTC().Unix(),
Event: event,
PullRequestId: bitbucketCloudWebHook.PullRequest.ID,
TargetRepositoryDetails: webhook.getRepositoryDetails(bitbucketCloudWebHook.PullRequest.ToRef.Repository),
Branch: strings.TrimPrefix(bitbucketCloudWebHook.PullRequest.ToRef.ID, "refs/heads/"),
SourceRepositoryDetails: webhook.getRepositoryDetails(bitbucketCloudWebHook.PullRequest.FromRef.Repository),
SourceBranch: strings.TrimPrefix(bitbucketCloudWebHook.PullRequest.FromRef.ID, "refs/heads/"),
Timestamp: eventTime.UTC().Unix(),
Event: event,
}, nil
}

Expand All @@ -107,7 +115,3 @@ type bitbucketServerWebHook struct {
RefId string `json:"refId,omitempty"`
} `json:"changes,omitempty"`
}

func getFullRepositoryName(repository bitbucketv1.Repository) string {
return strings.ToLower(repository.Project.Key) + "/" + repository.Name
}
15 changes: 10 additions & 5 deletions vcsutils/webhookparser/bitbucketserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func TestBitbucketServerParseIncomingPushWebhook(t *testing.T) {
assert.NoError(t, err)

// Check values
assert.Equal(t, "~"+expectedRepoName, actual.Repository)
assert.Equal(t, expectedRepoName, actual.TargetRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.TargetRepositoryDetails.Owner)
assert.Equal(t, expectedBranch, actual.Branch)
assert.Equal(t, bitbucketServerPushExpectedTime, actual.Timestamp)
assert.Equal(t, vcsutils.Push, actual.Event)
Expand All @@ -60,10 +61,12 @@ func TestBitbucketServerParseIncomingPrCreateWebhook(t *testing.T) {

// Check values
assert.Equal(t, bitbucketServerExpectedPrId, actual.PullRequestId)
assert.Equal(t, "~"+expectedRepoName, actual.Repository)
assert.Equal(t, expectedRepoName, actual.TargetRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.TargetRepositoryDetails.Owner)
assert.Equal(t, expectedBranch, actual.Branch)
assert.Equal(t, bitbucketServerPrCreateExpectedTime, actual.Timestamp)
assert.Equal(t, "~"+expectedRepoName, actual.SourceRepository)
assert.Equal(t, expectedRepoName, actual.SourceRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.SourceRepositoryDetails.Owner)
assert.Equal(t, expectedSourceBranch, actual.SourceBranch)
assert.Equal(t, vcsutils.PrCreated, actual.Event)
}
Expand All @@ -84,10 +87,12 @@ func TestBitbucketServerParseIncomingPrUpdateWebhook(t *testing.T) {

// Check values
assert.Equal(t, bitbucketServerExpectedPrId, actual.PullRequestId)
assert.Equal(t, "~"+expectedRepoName, actual.Repository)
assert.Equal(t, expectedRepoName, actual.TargetRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.TargetRepositoryDetails.Owner)
assert.Equal(t, expectedBranch, actual.Branch)
assert.Equal(t, bitbucketServerPrUpdateExpectedTime, actual.Timestamp)
assert.Equal(t, "~"+expectedRepoName, actual.SourceRepository)
assert.Equal(t, expectedRepoName, actual.SourceRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.SourceRepositoryDetails.Owner)
assert.Equal(t, expectedSourceBranch, actual.SourceBranch)
assert.Equal(t, vcsutils.PrEdited, actual.Event)
}
Expand Down
3 changes: 2 additions & 1 deletion vcsutils/webhookparser/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package webhookparser
import "os"

const (
expectedRepoName = "yahavi/hello-world"
expectedOwner = "yahavi"
expectedRepoName = "hello-world"
expectedBranch = "main"
expectedSourceBranch = "dev"
)
Expand Down
31 changes: 20 additions & 11 deletions vcsutils/webhookparser/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ func (webhook *GitHubWebhook) parseIncomingWebhook(payload []byte) (*WebhookInfo

func (webhook *GitHubWebhook) parsePushEvent(event *github.PushEvent) *WebhookInfo {
return &WebhookInfo{
Repository: event.GetRepo().GetFullName(),
Branch: strings.TrimPrefix(event.GetRef(), "refs/heads/"),
Timestamp: event.GetHeadCommit().GetTimestamp().UTC().Unix(),
Event: vcsutils.Push,
TargetRepositoryDetails: WebHookInfoRepoDetails{
Name: *event.GetRepo().Name,
Owner: *event.GetRepo().Owner.Login,
},
Branch: strings.TrimPrefix(event.GetRef(), "refs/heads/"),
Timestamp: event.GetHeadCommit().GetTimestamp().UTC().Unix(),
Event: vcsutils.Push,
}
}

Expand All @@ -66,12 +69,18 @@ func (webhook *GitHubWebhook) parsePrEvents(event *github.PullRequestEvent) *Web
return nil
}
return &WebhookInfo{
PullRequestId: event.GetPullRequest().GetNumber(),
Repository: event.GetPullRequest().GetBase().GetRepo().GetFullName(),
Branch: event.GetPullRequest().GetBase().GetRef(),
SourceRepository: event.GetPullRequest().GetHead().GetRepo().GetFullName(),
SourceBranch: event.GetPullRequest().GetHead().GetRef(),
Timestamp: event.GetPullRequest().GetUpdatedAt().UTC().Unix(),
Event: webhookEvent,
PullRequestId: event.GetPullRequest().GetNumber(),
TargetRepositoryDetails: WebHookInfoRepoDetails{
Name: *event.GetPullRequest().GetBase().GetRepo().Name,
Owner: *event.GetPullRequest().GetBase().GetRepo().Owner.Login,
},
Branch: event.GetPullRequest().GetBase().GetRef(),
SourceRepositoryDetails: WebHookInfoRepoDetails{
Name: *event.GetPullRequest().GetHead().GetRepo().Name,
Owner: *event.GetPullRequest().GetHead().GetRepo().Owner.Login,
},
SourceBranch: event.GetPullRequest().GetHead().GetRef(),
Timestamp: event.GetPullRequest().GetUpdatedAt().UTC().Unix(),
Event: webhookEvent,
}
}
15 changes: 10 additions & 5 deletions vcsutils/webhookparser/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func TestGitHubParseIncomingPushWebhook(t *testing.T) {
assert.NoError(t, err)

// Check values
assert.Equal(t, expectedRepoName, actual.Repository)
assert.Equal(t, expectedRepoName, actual.TargetRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.TargetRepositoryDetails.Owner)
assert.Equal(t, expectedBranch, actual.Branch)
assert.Equal(t, githubPushExpectedTime, actual.Timestamp)
assert.Equal(t, vcsutils.Push, actual.Event)
Expand All @@ -66,10 +67,12 @@ func TestGitHubParseIncomingPrCreateWebhook(t *testing.T) {

// Check values
assert.Equal(t, gitHubExpectedPrId, actual.PullRequestId)
assert.Equal(t, expectedRepoName, actual.Repository)
assert.Equal(t, expectedRepoName, actual.TargetRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.TargetRepositoryDetails.Owner)
assert.Equal(t, expectedBranch, actual.Branch)
assert.Equal(t, githubPrCreateExpectedTime, actual.Timestamp)
assert.Equal(t, expectedRepoName, actual.SourceRepository)
assert.Equal(t, expectedRepoName, actual.SourceRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.SourceRepositoryDetails.Owner)
assert.Equal(t, expectedSourceBranch, actual.SourceBranch)
assert.Equal(t, vcsutils.PrCreated, actual.Event)
}
Expand All @@ -91,10 +94,12 @@ func TestGitHubParseIncomingPrUpdateWebhook(t *testing.T) {

// Check values
assert.Equal(t, gitHubExpectedPrId, actual.PullRequestId)
assert.Equal(t, expectedRepoName, actual.Repository)
assert.Equal(t, expectedRepoName, actual.TargetRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.TargetRepositoryDetails.Owner)
assert.Equal(t, expectedBranch, actual.Branch)
assert.Equal(t, githubPrUpdateExpectedTime, actual.Timestamp)
assert.Equal(t, expectedRepoName, actual.SourceRepository)
assert.Equal(t, expectedRepoName, actual.SourceRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.SourceRepositoryDetails.Owner)
assert.Equal(t, expectedSourceBranch, actual.SourceBranch)
assert.Equal(t, vcsutils.PrEdited, actual.Event)
}
Expand Down
30 changes: 19 additions & 11 deletions vcsutils/webhookparser/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,18 @@ func (webhook *GitLabWebhook) parseIncomingWebhook(payload []byte) (*WebhookInfo

func (webhook *GitLabWebhook) parsePushEvent(event *gitlab.PushEvent) *WebhookInfo {
return &WebhookInfo{
Repository: event.Project.PathWithNamespace,
Branch: strings.TrimPrefix(event.Ref, "refs/heads/"),
Timestamp: event.Commits[0].Timestamp.Local().Unix(),
Event: vcsutils.Push,
TargetRepositoryDetails: webhook.parseRepoDetails(event.Project.PathWithNamespace),
Branch: strings.TrimPrefix(event.Ref, "refs/heads/"),
Timestamp: event.Commits[0].Timestamp.Local().Unix(),
Event: vcsutils.Push,
}
}

func (webhook *GitLabWebhook) parseRepoDetails(pathWithNamespace string) WebHookInfoRepoDetails {
split := strings.Split(pathWithNamespace, "/")
return WebHookInfoRepoDetails{
Name: split[1],
Owner: split[0],
}
}

Expand All @@ -72,12 +80,12 @@ func (webhook *GitLabWebhook) parsePrEvents(event *gitlab.MergeEvent) (*WebhookI
return nil, err
}
return &WebhookInfo{
PullRequestId: event.ObjectAttributes.IID,
SourceRepository: event.ObjectAttributes.Source.PathWithNamespace,
SourceBranch: event.ObjectAttributes.SourceBranch,
Repository: event.ObjectAttributes.Target.PathWithNamespace,
Branch: event.ObjectAttributes.TargetBranch,
Timestamp: eventTime.UTC().Unix(),
Event: webhookEvent,
PullRequestId: event.ObjectAttributes.IID,
SourceRepositoryDetails: webhook.parseRepoDetails(event.ObjectAttributes.Source.PathWithNamespace),
SourceBranch: event.ObjectAttributes.SourceBranch,
TargetRepositoryDetails: webhook.parseRepoDetails(event.ObjectAttributes.Target.PathWithNamespace),
Branch: event.ObjectAttributes.TargetBranch,
Timestamp: eventTime.UTC().Unix(),
Event: webhookEvent,
}, nil
}
15 changes: 10 additions & 5 deletions vcsutils/webhookparser/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func TestGitLabParseIncomingPushWebhook(t *testing.T) {
assert.NoError(t, err)

// Check values
assert.Equal(t, expectedRepoName, actual.Repository)
assert.Equal(t, expectedRepoName, actual.TargetRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.TargetRepositoryDetails.Owner)
assert.Equal(t, expectedBranch, actual.Branch)
assert.Equal(t, gitlabPushExpectedTime, actual.Timestamp)
assert.Equal(t, vcsutils.Push, actual.Event)
Expand All @@ -55,10 +56,12 @@ func TestGitLabParseIncomingPrCreateWebhook(t *testing.T) {

// Check values
assert.Equal(t, gitlabExpectedPrId, actual.PullRequestId)
assert.Equal(t, expectedRepoName, actual.Repository)
assert.Equal(t, expectedRepoName, actual.TargetRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.TargetRepositoryDetails.Owner)
assert.Equal(t, expectedBranch, actual.Branch)
assert.Equal(t, gitlabPrCreateExpectedTime, actual.Timestamp)
assert.Equal(t, expectedRepoName, actual.SourceRepository)
assert.Equal(t, expectedRepoName, actual.SourceRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.SourceRepositoryDetails.Owner)
assert.Equal(t, expectedSourceBranch, actual.SourceBranch)
assert.Equal(t, vcsutils.PrCreated, actual.Event)
}
Expand All @@ -79,10 +82,12 @@ func TestGitLabParseIncomingPrUpdateWebhook(t *testing.T) {

// Check values
assert.Equal(t, gitlabExpectedPrId, actual.PullRequestId)
assert.Equal(t, expectedRepoName, actual.Repository)
assert.Equal(t, expectedRepoName, actual.TargetRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.TargetRepositoryDetails.Owner)
assert.Equal(t, expectedBranch, actual.Branch)
assert.Equal(t, gitlabPrUpdateExpectedTime, actual.Timestamp)
assert.Equal(t, expectedRepoName, actual.SourceRepository)
assert.Equal(t, expectedRepoName, actual.SourceRepositoryDetails.Name)
assert.Equal(t, expectedOwner, actual.SourceRepositoryDetails.Owner)
assert.Equal(t, expectedSourceBranch, actual.SourceBranch)
assert.Equal(t, vcsutils.PrEdited, actual.Event)
}
Expand Down
9 changes: 7 additions & 2 deletions vcsutils/webhookparser/webhookparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ const EventHeaderKey = "X-Event-Key"
// This struct is used for parsing an incoming webhook request from the VCS provider.
type WebhookInfo struct {
// The target repository for pull requests and push
Repository string `json:"repository,omitempty"`
TargetRepositoryDetails WebHookInfoRepoDetails `json:"target_repository_details,omitempty"`
// The target branch for pull requests and push
Branch string `json:"branch,omitempty"`
RemiBou marked this conversation as resolved.
Show resolved Hide resolved
// Pull request id
PullRequestId int `json:"pull_request_id,omitempty"`
// The source repository for pull requests
SourceRepository string `json:"source_repository,omitempty"`
SourceRepositoryDetails WebHookInfoRepoDetails `json:"source_repository_details,omitempty"`
// The source branch for pull requests
SourceBranch string `json:"source_branch,omitempty"`
// Seconds from epoch
Expand All @@ -26,6 +26,11 @@ type WebhookInfo struct {
Event vcsutils.WebhookEvent `json:"event,omitempty"`
}

type WebHookInfoRepoDetails struct {
Name string `json:"name,omitempty"`
Owner string `json:"owner,omitempty"`
}

type WebhookParser interface {
// Validate the webhook payload with the expected token and return the payload
validatePayload(token []byte) ([]byte, error)
Expand Down