Skip to content

Commit

Permalink
fix: remove context propogation as pointer (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
GoshaDo authored Jan 11, 2025
1 parent e7d9578 commit 075cbf2
Show file tree
Hide file tree
Showing 25 changed files with 153 additions and 152 deletions.
2 changes: 1 addition & 1 deletion pkg/event_handler/github_event_notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewGithubEventNotifier(cfg *conf.GlobalConfig, clients *clients.Clients) Ev
}
}

func (gn *githubNotifier) Notify(ctx *context.Context, workflow *v1alpha1.Workflow) error {
func (gn *githubNotifier) Notify(ctx context.Context, workflow *v1alpha1.Workflow) error {
fmt.Printf("Notifing workflow, %s\n", workflow.GetName())

repo, ok := workflow.GetLabels()["repo"]
Expand Down
18 changes: 9 additions & 9 deletions pkg/event_handler/github_event_notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,35 @@ import (

type mockGitProvider struct{}

func (m *mockGitProvider) GetFile(ctx *context.Context, repo string, branch string, path string) (*git_provider.CommitFile, error) {
func (m *mockGitProvider) GetFile(ctx context.Context, repo string, branch string, path string) (*git_provider.CommitFile, error) {
return nil, nil
}

func (m *mockGitProvider) GetFiles(ctx *context.Context, repo string, branch string, paths []string) ([]*git_provider.CommitFile, error) {
func (m *mockGitProvider) GetFiles(ctx context.Context, repo string, branch string, paths []string) ([]*git_provider.CommitFile, error) {
return nil, nil
}

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

func (m *mockGitProvider) SetWebhook(ctx *context.Context, repo *string) (*git_provider.HookWithStatus, error) {
func (m *mockGitProvider) SetWebhook(ctx context.Context, repo *string) (*git_provider.HookWithStatus, error) {
return nil, nil
}

func (m *mockGitProvider) UnsetWebhook(ctx *context.Context, hook *git_provider.HookWithStatus) error {
func (m *mockGitProvider) UnsetWebhook(ctx context.Context, hook *git_provider.HookWithStatus) error {
return nil
}

func (m *mockGitProvider) HandlePayload(ctx *context.Context, request *http.Request, secret []byte) (*git_provider.WebhookPayload, error) {
func (m *mockGitProvider) HandlePayload(ctx context.Context, request *http.Request, secret []byte) (*git_provider.WebhookPayload, error) {
return nil, nil
}

func (m *mockGitProvider) SetStatus(ctx *context.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error {
func (m *mockGitProvider) SetStatus(ctx context.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error {
return nil
}

func (m *mockGitProvider) PingHook(ctx *context.Context, hook *git_provider.HookWithStatus) error {
func (m *mockGitProvider) PingHook(ctx context.Context, hook *git_provider.HookWithStatus) error {
return nil
}

Expand Down Expand Up @@ -202,7 +202,7 @@ func TestNotify(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Call the function being tested
err := gn.Notify(&ctx, test.workflow)
err := gn.Notify(ctx, test.workflow)

// Use assert to check the equality of the error
if test.wantedError != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/event_handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func Start(ctx context.Context, stop context.CancelFunc, cfg *conf.GlobalConfig,
Operator: metav1.LabelSelectorOpExists},
},
}
watcher, err := clients.Workflows.Watch(&ctx, labelSelector)
watcher, err := clients.Workflows.Watch(ctx, labelSelector)
if err != nil {
log.Printf("[event handler] Failed to watch workflow error:%s", err)
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/event_handler/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ type EventHandler interface {
}

type EventNotifier interface {
Notify(ctx *context.Context, workflow *v1alpha1.Workflow) error
Notify(ctx context.Context, workflow *v1alpha1.Workflow) error
}
4 changes: 2 additions & 2 deletions pkg/event_handler/workflow_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ func (weh *workflowEventHandler) Handle(ctx context.Context, event *watch.Event)
return nil
}

err := weh.Notifier.Notify(&ctx, workflow)
err := weh.Notifier.Notify(ctx, workflow)
if err != nil {
return fmt.Errorf("failed to Notify workflow to git provider, error:%s\n", err)
}

err = weh.Clients.Workflows.UpdatePiperWorkflowLabel(&ctx, workflow.GetName(), "notified", string(workflow.Status.Phase))
err = weh.Clients.Workflows.UpdatePiperWorkflowLabel(ctx, workflow.GetName(), "notified", string(workflow.Status.Phase))
if err != nil {
return fmt.Errorf("error in workflow %s status patch: %s", workflow.GetName(), err)
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/git_provider/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package git_provider

import (
"bytes"
context2 "context"
"encoding/json"
"fmt"
"github.com/ktrysmt/go-bitbucket"
"github.com/quickube/piper/pkg/conf"
"github.com/quickube/piper/pkg/utils"
"github.com/tidwall/gjson"
"golang.org/x/net/context"
"io"
"log"
"net/http"
Expand Down Expand Up @@ -36,7 +36,7 @@ func NewBitbucketServerClient(cfg *conf.GlobalConfig) (Client, error) {
}, err
}

func (b BitbucketClientImpl) ListFiles(ctx *context.Context, repo string, branch string, path string) ([]string, error) {
func (b BitbucketClientImpl) ListFiles(ctx context2.Context, repo string, branch string, path string) ([]string, error) {
var filesList []string
fileOptions := bitbucket.RepositoryFilesOptions{
Owner: b.cfg.GitProviderConfig.OrgName,
Expand All @@ -58,7 +58,7 @@ func (b BitbucketClientImpl) ListFiles(ctx *context.Context, repo string, branch
return filesList, nil
}

func (b BitbucketClientImpl) GetFile(ctx *context.Context, repo string, branch string, path string) (*CommitFile, error) {
func (b BitbucketClientImpl) GetFile(ctx context2.Context, repo string, branch string, path string) (*CommitFile, error) {
fileOptions := bitbucket.RepositoryFilesOptions{
Owner: b.cfg.GitProviderConfig.OrgName,
RepoSlug: repo,
Expand All @@ -78,7 +78,7 @@ func (b BitbucketClientImpl) GetFile(ctx *context.Context, repo string, branch s
}, nil
}

func (b BitbucketClientImpl) GetFiles(ctx *context.Context, repo string, branch string, paths []string) ([]*CommitFile, error) {
func (b BitbucketClientImpl) GetFiles(ctx context2.Context, repo string, branch string, paths []string) ([]*CommitFile, error) {
var commitFiles []*CommitFile
for _, path := range paths {
file, err := b.GetFile(ctx, repo, branch, path)
Expand All @@ -94,7 +94,7 @@ func (b BitbucketClientImpl) GetFiles(ctx *context.Context, repo string, branch
return commitFiles, nil
}

func (b BitbucketClientImpl) SetWebhook(ctx *context.Context, repo *string) (*HookWithStatus, error) {
func (b BitbucketClientImpl) SetWebhook(ctx context2.Context, repo *string) (*HookWithStatus, error) {
webhookOptions := &bitbucket.WebhooksOptions{
Owner: b.cfg.GitProviderConfig.OrgName,
RepoSlug: *repo,
Expand Down Expand Up @@ -139,12 +139,12 @@ func (b BitbucketClientImpl) SetWebhook(ctx *context.Context, repo *string) (*Ho
}, nil
}

func (b BitbucketClientImpl) UnsetWebhook(ctx *context.Context, hook *HookWithStatus) error {
func (b BitbucketClientImpl) UnsetWebhook(ctx context2.Context, hook *HookWithStatus) error {
//TODO implement me
panic("implement me")
}

func (b BitbucketClientImpl) HandlePayload(ctx *context.Context, request *http.Request, secret []byte) (*WebhookPayload, error) {
func (b BitbucketClientImpl) HandlePayload(ctx context2.Context, request *http.Request, secret []byte) (*WebhookPayload, error) {
var webhookPayload *WebhookPayload
var buf bytes.Buffer

Expand Down Expand Up @@ -205,7 +205,7 @@ func (b BitbucketClientImpl) HandlePayload(ctx *context.Context, request *http.R
return webhookPayload, nil
}

func (b BitbucketClientImpl) SetStatus(ctx *context.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error {
func (b BitbucketClientImpl) SetStatus(ctx context2.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error {
commitOptions := bitbucket.CommitsOptions{
Owner: b.cfg.GitProviderConfig.OrgName,
RepoSlug: *repo,
Expand All @@ -225,7 +225,7 @@ func (b BitbucketClientImpl) SetStatus(ctx *context.Context, repo *string, commi
return nil
}

func (b BitbucketClientImpl) PingHook(ctx *context.Context, hook *HookWithStatus) error {
func (b BitbucketClientImpl) PingHook(ctx context2.Context, hook *HookWithStatus) error {
//TODO implement me
panic("implement me")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/git_provider/bitbucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestBitbucketListFiles(t *testing.T) {
ctx := context.Background()

// Execute
actualContent, err := c.ListFiles(&ctx, "test-repo1", "branch1", ".workflows")
actualContent, err := c.ListFiles(ctx, "test-repo1", "branch1", ".workflows")
expectedContent := []string{"exit.yaml", "main.yaml"}

// Assert
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestBitbucketSetStatus(t *testing.T) {
t.Run(test.name, func(t *testing.T) {

// Call the function being tested
err := c.SetStatus(&ctx, test.repo, test.commit, test.linkURL, test.status, test.message)
err := c.SetStatus(ctx, test.repo, test.commit, test.linkURL, test.status, test.message)

// Use assert to check the equality of the error
if test.wantedError != nil {
Expand Down
46 changes: 23 additions & 23 deletions pkg/git_provider/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ func NewGithubClient(cfg *conf.GlobalConfig) (Client, error) {
}, err
}

func (c *GithubClientImpl) ListFiles(ctx *context.Context, repo string, branch string, path string) ([]string, error) {
func (c *GithubClientImpl) ListFiles(ctx context.Context, repo string, branch string, path string) ([]string, error) {
var files []string

opt := &github.RepositoryContentGetOptions{Ref: branch}
_, directoryContent, resp, err := c.client.Repositories.GetContents(*ctx, c.cfg.GitProviderConfig.OrgName, repo, path, opt)
_, directoryContent, resp, err := c.client.Repositories.GetContents(ctx, c.cfg.GitProviderConfig.OrgName, repo, path, opt)
if err != nil {
return nil, err
}
Expand All @@ -67,11 +67,11 @@ func (c *GithubClientImpl) ListFiles(ctx *context.Context, repo string, branch s
return files, nil
}

func (c *GithubClientImpl) GetFile(ctx *context.Context, repo string, branch string, path string) (*CommitFile, error) {
func (c *GithubClientImpl) GetFile(ctx context.Context, repo string, branch string, path string) (*CommitFile, error) {
var commitFile CommitFile

opt := &github.RepositoryContentGetOptions{Ref: branch}
fileContent, _, resp, err := c.client.Repositories.GetContents(*ctx, c.cfg.GitProviderConfig.OrgName, repo, path, opt)
fileContent, _, resp, err := c.client.Repositories.GetContents(ctx, c.cfg.GitProviderConfig.OrgName, repo, path, opt)
if err != nil {
return &commitFile, err
}
Expand All @@ -96,7 +96,7 @@ func (c *GithubClientImpl) GetFile(ctx *context.Context, repo string, branch str
return &commitFile, nil
}

func (c *GithubClientImpl) GetFiles(ctx *context.Context, repo string, branch string, paths []string) ([]*CommitFile, error) {
func (c *GithubClientImpl) GetFiles(ctx context.Context, repo string, branch string, paths []string) ([]*CommitFile, error) {
var commitFiles []*CommitFile
for _, path := range paths {
file, err := c.GetFile(ctx, repo, branch, path)
Expand All @@ -112,7 +112,7 @@ func (c *GithubClientImpl) GetFiles(ctx *context.Context, repo string, branch st
return commitFiles, nil
}

func (c *GithubClientImpl) SetWebhook(ctx *context.Context, repo *string) (*HookWithStatus, error) {
func (c *GithubClientImpl) SetWebhook(ctx context.Context, repo *string) (*HookWithStatus, error) {
if c.cfg.OrgLevelWebhook && repo != nil {
return nil, fmt.Errorf("trying to set repo scope. repo: %s", *repo)
}
Expand All @@ -128,10 +128,10 @@ func (c *GithubClientImpl) SetWebhook(ctx *context.Context, repo *string) (*Hook
}

if repo == nil {
respHook, ok := isOrgWebhookEnabled(*ctx, c)
respHook, ok := isOrgWebhookEnabled(ctx, c)
if !ok {
createdHook, resp, err := c.client.Organizations.CreateHook(
*ctx,
ctx,
c.cfg.GitProviderConfig.OrgName,
hookConf,
)
Expand All @@ -146,7 +146,7 @@ func (c *GithubClientImpl) SetWebhook(ctx *context.Context, repo *string) (*Hook
return &HookWithStatus{HookID: hookID, HealthStatus: true, RepoName: repo}, nil
} else {
updatedHook, resp, err := c.client.Organizations.EditHook(
*ctx,
ctx,
c.cfg.GitProviderConfig.OrgName,
respHook.GetID(),
hookConf,
Expand All @@ -166,9 +166,9 @@ func (c *GithubClientImpl) SetWebhook(ctx *context.Context, repo *string) (*Hook
return &HookWithStatus{HookID: hookID, HealthStatus: true, RepoName: repo}, nil
}
} else {
respHook, ok := isRepoWebhookEnabled(*ctx, c, *repo)
respHook, ok := isRepoWebhookEnabled(ctx, c, *repo)
if !ok {
createdHook, resp, err := c.client.Repositories.CreateHook(*ctx, c.cfg.GitProviderConfig.OrgName, *repo, hookConf)
createdHook, resp, err := c.client.Repositories.CreateHook(ctx, c.cfg.GitProviderConfig.OrgName, *repo, hookConf)
if err != nil {
return nil, err
}
Expand All @@ -180,7 +180,7 @@ func (c *GithubClientImpl) SetWebhook(ctx *context.Context, repo *string) (*Hook
hookID := createdHook.GetID()
return &HookWithStatus{HookID: hookID, HealthStatus: true, RepoName: repo}, nil
} else {
updatedHook, resp, err := c.client.Repositories.EditHook(*ctx, c.cfg.GitProviderConfig.OrgName, *repo, respHook.GetID(), hookConf)
updatedHook, resp, err := c.client.Repositories.EditHook(ctx, c.cfg.GitProviderConfig.OrgName, *repo, respHook.GetID(), hookConf)
if err != nil {
return nil, err
}
Expand All @@ -195,11 +195,11 @@ func (c *GithubClientImpl) SetWebhook(ctx *context.Context, repo *string) (*Hook
}
}

func (c *GithubClientImpl) UnsetWebhook(ctx *context.Context, hook *HookWithStatus) error {
func (c *GithubClientImpl) UnsetWebhook(ctx context.Context, hook *HookWithStatus) error {

if hook.RepoName == nil {

resp, err := c.client.Organizations.DeleteHook(*ctx, c.cfg.GitProviderConfig.OrgName, hook.HookID)
resp, err := c.client.Organizations.DeleteHook(ctx, c.cfg.GitProviderConfig.OrgName, hook.HookID)

if err != nil {
return err
Expand All @@ -210,7 +210,7 @@ func (c *GithubClientImpl) UnsetWebhook(ctx *context.Context, hook *HookWithStat
}
log.Printf("removed org webhook, hookID :%d\n", hook.HookID) // INFO
} else {
resp, err := c.client.Repositories.DeleteHook(*ctx, c.cfg.GitProviderConfig.OrgName, *hook.RepoName, hook.HookID)
resp, err := c.client.Repositories.DeleteHook(ctx, c.cfg.GitProviderConfig.OrgName, *hook.RepoName, hook.HookID)

if err != nil {
return fmt.Errorf("failed to delete repo level webhhok for %s, API call returned %d. %s", *hook.RepoName, resp.StatusCode, err)
Expand All @@ -225,7 +225,7 @@ func (c *GithubClientImpl) UnsetWebhook(ctx *context.Context, hook *HookWithStat
return nil
}

func (c *GithubClientImpl) HandlePayload(ctx *context.Context, request *http.Request, secret []byte) (*WebhookPayload, error) {
func (c *GithubClientImpl) HandlePayload(ctx context.Context, request *http.Request, secret []byte) (*WebhookPayload, error) {
var webhookPayload *WebhookPayload

payload, err := github.ValidatePayload(request, secret)
Expand Down Expand Up @@ -307,7 +307,7 @@ func (c *GithubClientImpl) HandlePayload(ctx *context.Context, request *http.Req

}

func (c *GithubClientImpl) SetStatus(ctx *context.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error {
func (c *GithubClientImpl) SetStatus(ctx context.Context, repo *string, commit *string, linkURL *string, status *string, message *string) error {
if !utils.ValidateHTTPFormat(*linkURL) {
return fmt.Errorf("invalid linkURL")
}
Expand All @@ -319,7 +319,7 @@ func (c *GithubClientImpl) SetStatus(ctx *context.Context, repo *string, commit
AvatarURL: utils.SPtr("https://argoproj.github.io/argo-workflows/assets/logo.png"),
}

_, resp, err := c.client.Repositories.CreateStatus(*ctx, c.cfg.OrgName, *repo, *commit, repoStatus)
_, resp, err := c.client.Repositories.CreateStatus(ctx, c.cfg.OrgName, *repo, *commit, repoStatus)
if err != nil {
return err
}
Expand All @@ -332,12 +332,12 @@ func (c *GithubClientImpl) SetStatus(ctx *context.Context, repo *string, commit
return nil
}

func (c *GithubClientImpl) PingHook(ctx *context.Context, hook *HookWithStatus) error {
func (c *GithubClientImpl) PingHook(ctx context.Context, hook *HookWithStatus) error {
if c.cfg.OrgLevelWebhook && hook.RepoName != nil {
return fmt.Errorf("trying to ping repo scope webhook while configured for org level webhook. repo: %s", *hook.RepoName)
}
if hook.RepoName == nil {
resp, err := c.client.Organizations.PingHook(*ctx, c.cfg.OrgName, hook.HookID)
resp, err := c.client.Organizations.PingHook(ctx, c.cfg.OrgName, hook.HookID)
if err != nil {
return err
}
Expand All @@ -346,7 +346,7 @@ func (c *GithubClientImpl) PingHook(ctx *context.Context, hook *HookWithStatus)
return fmt.Errorf("unable to find organization webhook for hookID: %d", hook.HookID)
}
} else {
resp, err := c.client.Repositories.PingHook(*ctx, c.cfg.GitProviderConfig.OrgName, *hook.RepoName, hook.HookID)
resp, err := c.client.Repositories.PingHook(ctx, c.cfg.GitProviderConfig.OrgName, *hook.RepoName, hook.HookID)
if err != nil {
return err
}
Expand All @@ -359,8 +359,8 @@ func (c *GithubClientImpl) PingHook(ctx *context.Context, hook *HookWithStatus)
return nil
}

func (c *GithubClientImpl) refToSHA(ctx *context.Context, ref string, repo string) (*string, error) {
respSHA, resp, err := c.client.Repositories.GetCommitSHA1(*ctx, c.cfg.OrgName, repo, ref, "")
func (c *GithubClientImpl) refToSHA(ctx context.Context, ref string, repo string) (*string, error) {
respSHA, resp, err := c.client.Repositories.GetCommitSHA1(ctx, c.cfg.OrgName, repo, ref, "")
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 075cbf2

Please sign in to comment.