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

feat: add tag handler - RK-19248 #92

Merged
merged 5 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 12 additions & 2 deletions pkg/git_provider/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (c *GithubClientImpl) SetWebhook() error {
"content_type": "json",
"secret": c.cfg.GitProviderConfig.WebhookSecret,
},
Events: []string{"push", "pull_request"},
Events: []string{"push", "pull_request", "create"},
Active: github.Bool(true),
}
if c.cfg.GitProviderConfig.OrgLevelWebhook {
Expand Down Expand Up @@ -252,12 +252,22 @@ func (c *GithubClientImpl) HandlePayload(request *http.Request, secret []byte) (
Branch: e.GetPullRequest().GetHead().GetRef(),
Commit: e.GetPullRequest().GetHead().GetSHA(),
User: e.GetPullRequest().GetUser().GetLogin(),
UserEmail: e.GetPullRequest().GetUser().GetEmail(), // Not working. GitHub missing email for PR events in payload.
UserEmail: e.GetSender().GetEmail(), // e.GetPullRequest().GetUser().GetEmail() Not working. GitHub missing email for PR events in payload.
PullRequestTitle: e.GetPullRequest().GetTitle(),
PullRequestURL: e.GetPullRequest().GetURL(),
DestBranch: e.GetPullRequest().GetBase().GetRef(),
Labels: e.GetPullRequest().Labels,
}
case *github.CreateEvent:
webhookPayload = &WebhookPayload{
Event: "create",
Action: e.GetRefType(), // Possible values are: "repository", "branch", "tag".
Repo: e.GetRepo().GetName(),
Branch: e.GetRef(),
Commit: e.GetRef(),
User: e.GetSender().GetLogin(),
UserEmail: e.GetSender().GetEmail(),
}
}

return webhookPayload, nil
Expand Down
31 changes: 18 additions & 13 deletions pkg/webhook_handler/webhook_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (wh *WebhookHandlerImpl) PrepareBatchForMatchingTriggers(ctx *context.Conte
utils.AddPrefixToList(*trigger.OnStart, ".workflows/"),
)
if len(onStartFiles) == 0 {
return nil, fmt.Errorf("one or more of onStart: %s files found", *trigger.OnStart)
return nil, fmt.Errorf("one or more of onStart: %s files found in repo: %s branch %s", *trigger.OnStart, wh.Payload.Repo, wh.Payload.Branch)
}
if err != nil {
return nil, err
Expand All @@ -98,7 +98,7 @@ func (wh *WebhookHandlerImpl) PrepareBatchForMatchingTriggers(ctx *context.Conte
utils.AddPrefixToList(*trigger.OnExit, ".workflows/"),
)
if len(onExitFiles) == 0 {
log.Printf("onExist: %s files not found", *trigger.OnExit)
log.Printf("one or more of onExist: %s files not found in repo: %s branch %s", *trigger.OnExit, wh.Payload.Repo, wh.Payload.Branch)
}
if err != nil {
return nil, err
Expand All @@ -114,23 +114,28 @@ func (wh *WebhookHandlerImpl) PrepareBatchForMatchingTriggers(ctx *context.Conte
utils.AddPrefixToList(*trigger.Templates, ".workflows/"),
)
if len(templatesFiles) == 0 {
log.Printf("parameters: %s files not found", *trigger.Templates)
log.Printf("one or more of templates: %s files not found in repo: %s branch %s", *trigger.Templates, wh.Payload.Repo, wh.Payload.Branch)
}
if err != nil {
return nil, err
}
}

parameters, err := wh.clients.GitProvider.GetFile(
ctx,
wh.Payload.Repo,
wh.Payload.Branch,
".workflows/parameters.yaml",
)
if err != nil {
return nil, err
parameters := &git_provider.CommitFile{
Path: nil,
Content: nil,
}
if parameters == nil {
if IsFileExists(ctx, wh, ".workflows", "parameters.yaml") {
parameters, err = wh.clients.GitProvider.GetFile(
ctx,
wh.Payload.Repo,
wh.Payload.Branch,
".workflows/parameters.yaml",
)
if err != nil {
return nil, err
}
} else {
log.Printf("parameters.yaml not found in repo: %s branch %s", wh.Payload.Repo, wh.Payload.Branch)
}

Expand All @@ -145,7 +150,7 @@ func (wh *WebhookHandlerImpl) PrepareBatchForMatchingTriggers(ctx *context.Conte
}
}
if !triggered {
return nil, fmt.Errorf("no matching trigger found for %s in branch %s", wh.Payload.Event, wh.Payload.Branch)
return nil, fmt.Errorf("no matching trigger found for event: %s action: %s in branch :%s", wh.Payload.Event, wh.Payload.Action, wh.Payload.Branch)
}
return workflowBatches, nil
}
Expand Down
14 changes: 13 additions & 1 deletion pkg/webhook_handler/webhook_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/rookout/piper/pkg/utils"
assertion "github.com/stretchr/testify/assert"
"net/http"
"strings"
"testing"
)

Expand Down Expand Up @@ -65,7 +66,18 @@ func (m *MockGitProvider) GetFiles(ctx *context.Context, repo string, branch str
}

func (m *MockGitProvider) ListFiles(ctx *context.Context, repo string, branch string, path string) ([]string, error) {
return nil, nil
var files []string

fullPath := fmt.Sprintf("%s/%s/%s/", repo, branch, path)

for key := range commitFileMap {
if strings.Contains(key, fullPath) {
trimmed := strings.Replace(key, fullPath, "", -1)
files = append(files, trimmed)
}
}

return files, nil
}

func (m *MockGitProvider) SetWebhook() error {
Expand Down