From 0f2731f9e8ab6d13ca25773189d124b91a31b276 Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Wed, 26 Mar 2025 22:58:52 +0100 Subject: [PATCH] fix(actions): respect running context Signed-off-by: Ettore Di Giacinto --- services/actions.go | 18 +++++++++--------- services/actions/githubissuecloser.go | 6 ++---- services/actions/githubissuecomment.go | 6 ++---- services/actions/githubissuelabeler.go | 6 ++---- services/actions/githubissueopener.go | 6 ++---- services/actions/githubissuereader.go | 6 ++---- services/actions/githubissuesearch.go | 6 ++---- .../githubrepositorycreateupdatecontent.go | 8 +++----- services/actions/githubrepositorygetcontent.go | 6 ++---- services/actions/githubrepositoryreadme.go | 6 ++---- webui/app.go | 2 +- 11 files changed, 29 insertions(+), 47 deletions(-) diff --git a/services/actions.go b/services/actions.go index 1ff5252..173cff5 100644 --- a/services/actions.go +++ b/services/actions.go @@ -94,23 +94,23 @@ func Action(name string, config map[string]string, pool *state.AgentPool) (types case ActionSearch: a = actions.NewSearch(config) case ActionGithubIssueLabeler: - a = actions.NewGithubIssueLabeler(context.Background(), config) + a = actions.NewGithubIssueLabeler(config) case ActionGithubIssueOpener: - a = actions.NewGithubIssueOpener(context.Background(), config) + a = actions.NewGithubIssueOpener(config) case ActionGithubIssueCloser: - a = actions.NewGithubIssueCloser(context.Background(), config) + a = actions.NewGithubIssueCloser(config) case ActionGithubIssueSearcher: - a = actions.NewGithubIssueSearch(context.Background(), config) + a = actions.NewGithubIssueSearch(config) case ActionGithubIssueReader: - a = actions.NewGithubIssueReader(context.Background(), config) + a = actions.NewGithubIssueReader(config) case ActionGithubIssueCommenter: - a = actions.NewGithubIssueCommenter(context.Background(), config) + a = actions.NewGithubIssueCommenter(config) case ActionGithubRepositoryGet: - a = actions.NewGithubRepositoryGetContent(context.Background(), config) + a = actions.NewGithubRepositoryGetContent(config) case ActionGithubRepositoryCreateOrUpdate: - a = actions.NewGithubRepositoryCreateOrUpdateContent(context.Background(), config) + a = actions.NewGithubRepositoryCreateOrUpdateContent(config) case ActionGithubREADME: - a = actions.NewGithubRepositoryREADME(context.Background(), config) + a = actions.NewGithubRepositoryREADME(config) case ActionScraper: a = actions.NewScraper(config) case ActionWikipedia: diff --git a/services/actions/githubissuecloser.go b/services/actions/githubissuecloser.go index fd74df1..ba94a71 100644 --- a/services/actions/githubissuecloser.go +++ b/services/actions/githubissuecloser.go @@ -11,11 +11,10 @@ import ( type GithubIssuesCloser struct { token, repository, owner, customActionName string - context context.Context client *github.Client } -func NewGithubIssueCloser(ctx context.Context, config map[string]string) *GithubIssuesCloser { +func NewGithubIssueCloser(config map[string]string) *GithubIssuesCloser { client := github.NewClient(nil).WithAuthToken(config["token"]) return &GithubIssuesCloser{ client: client, @@ -23,7 +22,6 @@ func NewGithubIssueCloser(ctx context.Context, config map[string]string) *Github repository: config["repository"], owner: config["owner"], customActionName: config["customActionName"], - context: ctx, } } @@ -60,7 +58,7 @@ func (g *GithubIssuesCloser) Run(ctx context.Context, params types.ActionParams) // return "", err // } - _, _, err = g.client.Issues.Edit(g.context, result.Owner, result.Repository, result.IssueNumber, &github.IssueRequest{ + _, _, err = g.client.Issues.Edit(ctx, result.Owner, result.Repository, result.IssueNumber, &github.IssueRequest{ State: github.String("closed"), }) diff --git a/services/actions/githubissuecomment.go b/services/actions/githubissuecomment.go index 1350a99..42497e8 100644 --- a/services/actions/githubissuecomment.go +++ b/services/actions/githubissuecomment.go @@ -11,11 +11,10 @@ import ( type GithubIssuesCommenter struct { token, repository, owner, customActionName string - context context.Context client *github.Client } -func NewGithubIssueCommenter(ctx context.Context, config map[string]string) *GithubIssuesCommenter { +func NewGithubIssueCommenter(config map[string]string) *GithubIssuesCommenter { client := github.NewClient(nil).WithAuthToken(config["token"]) return &GithubIssuesCommenter{ @@ -24,7 +23,6 @@ func NewGithubIssueCommenter(ctx context.Context, config map[string]string) *Git customActionName: config["customActionName"], repository: config["repository"], owner: config["owner"], - context: ctx, } } @@ -45,7 +43,7 @@ func (g *GithubIssuesCommenter) Run(ctx context.Context, params types.ActionPara result.Owner = g.owner } - _, _, err = g.client.Issues.CreateComment(g.context, result.Owner, result.Repository, result.IssueNumber, + _, _, err = g.client.Issues.CreateComment(ctx, result.Owner, result.Repository, result.IssueNumber, &github.IssueComment{ Body: &result.Comment, }) diff --git a/services/actions/githubissuelabeler.go b/services/actions/githubissuelabeler.go index 43e8422..055ae51 100644 --- a/services/actions/githubissuelabeler.go +++ b/services/actions/githubissuelabeler.go @@ -14,11 +14,10 @@ import ( type GithubIssuesLabeler struct { token, repository, owner, customActionName string availableLabels []string - context context.Context client *github.Client } -func NewGithubIssueLabeler(ctx context.Context, config map[string]string) *GithubIssuesLabeler { +func NewGithubIssueLabeler(config map[string]string) *GithubIssuesLabeler { client := github.NewClient(nil).WithAuthToken(config["token"]) // Get available labels @@ -34,7 +33,6 @@ func NewGithubIssueLabeler(ctx context.Context, config map[string]string) *Githu customActionName: config["customActionName"], repository: config["repository"], owner: config["owner"], - context: ctx, availableLabels: availableLabels, } } @@ -56,7 +54,7 @@ func (g *GithubIssuesLabeler) Run(ctx context.Context, params types.ActionParams result.Owner = g.owner } - labels, _, err := g.client.Issues.AddLabelsToIssue(g.context, result.Owner, result.Repository, result.IssueNumber, []string{result.Label}) + labels, _, err := g.client.Issues.AddLabelsToIssue(ctx, result.Owner, result.Repository, result.IssueNumber, []string{result.Label}) //labelsNames := []string{} for _, l := range labels { xlog.Info("Label added", "label", l.Name) diff --git a/services/actions/githubissueopener.go b/services/actions/githubissueopener.go index 615290a..bbff91d 100644 --- a/services/actions/githubissueopener.go +++ b/services/actions/githubissueopener.go @@ -11,11 +11,10 @@ import ( type GithubIssuesOpener struct { token, repository, owner, customActionName string - context context.Context client *github.Client } -func NewGithubIssueOpener(ctx context.Context, config map[string]string) *GithubIssuesOpener { +func NewGithubIssueOpener(config map[string]string) *GithubIssuesOpener { client := github.NewClient(nil).WithAuthToken(config["token"]) return &GithubIssuesOpener{ @@ -24,7 +23,6 @@ func NewGithubIssueOpener(ctx context.Context, config map[string]string) *Github repository: config["repository"], owner: config["owner"], customActionName: config["customActionName"], - context: ctx, } } @@ -53,7 +51,7 @@ func (g *GithubIssuesOpener) Run(ctx context.Context, params types.ActionParams) } resultString := "" - createdIssue, _, err := g.client.Issues.Create(g.context, result.Owner, result.Repository, issue) + createdIssue, _, err := g.client.Issues.Create(ctx, result.Owner, result.Repository, issue) if err != nil { resultString = fmt.Sprintf("Error creating issue: %v", err) } else { diff --git a/services/actions/githubissuereader.go b/services/actions/githubissuereader.go index 0f8d5ce..2f6168c 100644 --- a/services/actions/githubissuereader.go +++ b/services/actions/githubissuereader.go @@ -11,11 +11,10 @@ import ( type GithubIssuesReader struct { token, repository, owner, customActionName string - context context.Context client *github.Client } -func NewGithubIssueReader(ctx context.Context, config map[string]string) *GithubIssuesReader { +func NewGithubIssueReader(config map[string]string) *GithubIssuesReader { client := github.NewClient(nil).WithAuthToken(config["token"]) return &GithubIssuesReader{ @@ -24,7 +23,6 @@ func NewGithubIssueReader(ctx context.Context, config map[string]string) *Github customActionName: config["customActionName"], repository: config["repository"], owner: config["owner"], - context: ctx, } } @@ -45,7 +43,7 @@ func (g *GithubIssuesReader) Run(ctx context.Context, params types.ActionParams) result.Owner = g.owner } - issue, _, err := g.client.Issues.Get(g.context, result.Owner, result.Repository, result.IssueNumber) + issue, _, err := g.client.Issues.Get(ctx, result.Owner, result.Repository, result.IssueNumber) if err == nil && issue != nil { return types.ActionResult{ Result: fmt.Sprintf( diff --git a/services/actions/githubissuesearch.go b/services/actions/githubissuesearch.go index aecdc9e..daa5358 100644 --- a/services/actions/githubissuesearch.go +++ b/services/actions/githubissuesearch.go @@ -12,11 +12,10 @@ import ( type GithubIssueSearch struct { token, repository, owner, customActionName string - context context.Context client *github.Client } -func NewGithubIssueSearch(ctx context.Context, config map[string]string) *GithubIssueSearch { +func NewGithubIssueSearch(config map[string]string) *GithubIssueSearch { client := github.NewClient(nil).WithAuthToken(config["token"]) return &GithubIssueSearch{ @@ -25,7 +24,6 @@ func NewGithubIssueSearch(ctx context.Context, config map[string]string) *Github repository: config["repository"], owner: config["owner"], customActionName: config["customActionName"], - context: ctx, } } @@ -49,7 +47,7 @@ func (g *GithubIssueSearch) Run(ctx context.Context, params types.ActionParams) query := fmt.Sprintf("%s in:%s user:%s", result.Query, result.Repository, result.Owner) resultString := "" - issues, _, err := g.client.Search.Issues(g.context, query, &github.SearchOptions{ + issues, _, err := g.client.Search.Issues(ctx, query, &github.SearchOptions{ ListOptions: github.ListOptions{PerPage: 5}, Order: "desc", //Sort: "created", diff --git a/services/actions/githubrepositorycreateupdatecontent.go b/services/actions/githubrepositorycreateupdatecontent.go index 82fd1a3..2a8398d 100644 --- a/services/actions/githubrepositorycreateupdatecontent.go +++ b/services/actions/githubrepositorycreateupdatecontent.go @@ -11,11 +11,10 @@ import ( type GithubRepositoryCreateOrUpdateContent struct { token, repository, owner, customActionName, defaultBranch, commitAuthor, commitMail string - context context.Context client *github.Client } -func NewGithubRepositoryCreateOrUpdateContent(ctx context.Context, config map[string]string) *GithubRepositoryCreateOrUpdateContent { +func NewGithubRepositoryCreateOrUpdateContent(config map[string]string) *GithubRepositoryCreateOrUpdateContent { client := github.NewClient(nil).WithAuthToken(config["token"]) return &GithubRepositoryCreateOrUpdateContent{ @@ -27,7 +26,6 @@ func NewGithubRepositoryCreateOrUpdateContent(ctx context.Context, config map[st commitAuthor: config["commitAuthor"], commitMail: config["commitMail"], defaultBranch: config["defaultBranch"], - context: ctx, } } @@ -65,12 +63,12 @@ func (g *GithubRepositoryCreateOrUpdateContent) Run(ctx context.Context, params } var sha *string - c, _, _, _ := g.client.Repositories.GetContents(g.context, result.Owner, result.Repository, result.Path, nil) + c, _, _, _ := g.client.Repositories.GetContents(ctx, result.Owner, result.Repository, result.Path, nil) if c != nil { sha = c.SHA } - fileContent, _, err := g.client.Repositories.CreateFile(g.context, result.Owner, result.Repository, result.Path, &github.RepositoryContentFileOptions{ + fileContent, _, err := g.client.Repositories.CreateFile(ctx, result.Owner, result.Repository, result.Path, &github.RepositoryContentFileOptions{ Message: &result.CommitMessage, SHA: sha, Committer: &github.CommitAuthor{ diff --git a/services/actions/githubrepositorygetcontent.go b/services/actions/githubrepositorygetcontent.go index 733c145..61c39d5 100644 --- a/services/actions/githubrepositorygetcontent.go +++ b/services/actions/githubrepositorygetcontent.go @@ -11,11 +11,10 @@ import ( type GithubRepositoryGetContent struct { token, repository, owner, customActionName string - context context.Context client *github.Client } -func NewGithubRepositoryGetContent(ctx context.Context, config map[string]string) *GithubRepositoryGetContent { +func NewGithubRepositoryGetContent(config map[string]string) *GithubRepositoryGetContent { client := github.NewClient(nil).WithAuthToken(config["token"]) return &GithubRepositoryGetContent{ @@ -24,7 +23,6 @@ func NewGithubRepositoryGetContent(ctx context.Context, config map[string]string repository: config["repository"], owner: config["owner"], customActionName: config["customActionName"], - context: ctx, } } @@ -46,7 +44,7 @@ func (g *GithubRepositoryGetContent) Run(ctx context.Context, params types.Actio result.Owner = g.owner } - fileContent, directoryContent, _, err := g.client.Repositories.GetContents(g.context, result.Owner, result.Repository, result.Path, nil) + fileContent, directoryContent, _, err := g.client.Repositories.GetContents(ctx, result.Owner, result.Repository, result.Path, nil) if err != nil { resultString := fmt.Sprintf("Error getting content : %v", err) return types.ActionResult{Result: resultString}, err diff --git a/services/actions/githubrepositoryreadme.go b/services/actions/githubrepositoryreadme.go index 5bd194f..03f0be8 100644 --- a/services/actions/githubrepositoryreadme.go +++ b/services/actions/githubrepositoryreadme.go @@ -11,18 +11,16 @@ import ( type GithubRepositoryREADME struct { token, customActionName string - context context.Context client *github.Client } -func NewGithubRepositoryREADME(ctx context.Context, config map[string]string) *GithubRepositoryREADME { +func NewGithubRepositoryREADME(config map[string]string) *GithubRepositoryREADME { client := github.NewClient(nil).WithAuthToken(config["token"]) return &GithubRepositoryREADME{ client: client, token: config["token"], customActionName: config["customActionName"], - context: ctx, } } @@ -37,7 +35,7 @@ func (g *GithubRepositoryREADME) Run(ctx context.Context, params types.ActionPar return types.ActionResult{}, err } - fileContent, _, err := g.client.Repositories.GetReadme(g.context, result.Owner, result.Repository, &github.RepositoryContentGetOptions{}) + fileContent, _, err := g.client.Repositories.GetReadme(ctx, result.Owner, result.Repository, &github.RepositoryContentGetOptions{}) if err != nil { resultString := fmt.Sprintf("Error getting content : %v", err) return types.ActionResult{Result: resultString}, err diff --git a/webui/app.go b/webui/app.go index 9f8fd3f..53a593d 100644 --- a/webui/app.go +++ b/webui/app.go @@ -322,7 +322,7 @@ func (a *App) ExecuteAction(pool *state.AgentPool) func(c *fiber.Ctx) error { return errorJSONMessage(c, err.Error()) } - ctx, cancel := context.WithTimeout(context.Background(), 200*time.Second) + ctx, cancel := context.WithTimeout(c.Context(), 200*time.Second) defer cancel() res, err := a.Run(ctx, payload.Params)