Skip to content

Commit

Permalink
feat: add tag handler - RK-19248 (#92)
Browse files Browse the repository at this point in the history
* feat: add tag handler

Signed-off-by: Gosha <[email protected]>

* fix: improve logs verbosity

Signed-off-by: Gosha <[email protected]>

* fix: fix missing parameters.yaml file error

Signed-off-by: Gosha <[email protected]>

* fix: tag branch is the tag

Signed-off-by: Gosha <[email protected]>

* fix: add ListFiles mock

Signed-off-by: Gosha <[email protected]>

---------

Signed-off-by: Gosha <[email protected]>
  • Loading branch information
gosharo authored Jul 10, 2023
1 parent af763d4 commit 925dbc5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
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

0 comments on commit 925dbc5

Please sign in to comment.