From d9e3fbb4f99d7f2fc99e552bc0de615183b75509 Mon Sep 17 00:00:00 2001 From: silviu-dinu Date: Sat, 1 Feb 2025 23:16:05 +0400 Subject: [PATCH 1/4] feat: filter GitHub workflows via query parameter for better queue count accuracy Signed-off-by: silviu-dinu --- pkg/scalers/github_runner_scaler.go | 32 ++++++++++++----------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/pkg/scalers/github_runner_scaler.go b/pkg/scalers/github_runner_scaler.go index 026c363709e..571bf409bf4 100644 --- a/pkg/scalers/github_runner_scaler.go +++ b/pkg/scalers/github_runner_scaler.go @@ -588,18 +588,6 @@ func getGithubRequest(ctx context.Context, url string, metadata *githubRunnerMet return b, r.StatusCode, nil } -func stripDeadRuns(allWfrs []WorkflowRuns) []WorkflowRun { - var filtered []WorkflowRun - for _, wfrs := range allWfrs { - for _, wfr := range wfrs.WorkflowRuns { - if wfr.Status == "queued" || wfr.Status == "in_progress" { - filtered = append(filtered, wfr) - } - } - } - return filtered -} - // getWorkflowRunJobs returns a list of jobs for a given workflow run func (s *githubRunnerScaler) getWorkflowRunJobs(ctx context.Context, workflowRunID int64, repoName string) ([]Job, error) { url := fmt.Sprintf("%s/repos/%s/%s/actions/runs/%d/jobs", s.metadata.githubAPIURL, s.metadata.owner, repoName, workflowRunID) @@ -618,8 +606,8 @@ func (s *githubRunnerScaler) getWorkflowRunJobs(ctx context.Context, workflowRun } // getWorkflowRuns returns a list of workflow runs for a given repository -func (s *githubRunnerScaler) getWorkflowRuns(ctx context.Context, repoName string) (*WorkflowRuns, error) { - url := fmt.Sprintf("%s/repos/%s/%s/actions/runs", s.metadata.githubAPIURL, s.metadata.owner, repoName) +func (s *githubRunnerScaler) getWorkflowRuns(ctx context.Context, repoName string, status string) (*WorkflowRuns, error) { + url := fmt.Sprintf("%s/repos/%s/%s/actions/runs?status=%s", s.metadata.githubAPIURL, s.metadata.owner, repoName, status) body, statusCode, err := getGithubRequest(ctx, url, s.metadata, s.httpClient) if err != nil && statusCode == 404 { return nil, nil @@ -669,21 +657,27 @@ func (s *githubRunnerScaler) GetWorkflowQueueLength(ctx context.Context) (int64, return -1, err } - var allWfrs []WorkflowRuns + var wfrs []WorkflowRuns for _, repo := range repos { - wfrs, err := s.getWorkflowRuns(ctx, repo) + wfrsQueued, err := s.getWorkflowRuns(ctx, repo, "queued") + if err != nil { + return -1, err + } + if wfrsQueued != nil { + wfrs = append(wfrs, *wfrsQueued) + } + wfrsInProgress, err := s.getWorkflowRuns(ctx, repo, "in_progress") if err != nil { return -1, err } - if wfrs != nil { - allWfrs = append(allWfrs, *wfrs) + if wfrsInProgress != nil { + wfrs = append(wfrs, *wfrsInProgress) } } var queueCount int64 - wfrs := stripDeadRuns(allWfrs) for _, wfr := range wfrs { jobs, err := s.getWorkflowRunJobs(ctx, wfr.ID, wfr.Repository.Name) if err != nil { From 63bf4077e6ecb9dc13211c45c926707f1711db8c Mon Sep 17 00:00:00 2001 From: silviu-dinu Date: Sun, 2 Feb 2025 00:04:34 +0400 Subject: [PATCH 2/4] Update CHANGELOG.md Signed-off-by: silviu-dinu --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1874189a34a..19bfc9f120e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,6 +72,7 @@ Here is an overview of all new **experimental** features: ### Improvements - **IBMMQ Scaler**: Handling StatusNotFound in IBMMQ scaler ([#6472](https://github.com/kedacore/keda/pull/6472)) +- **GitHub Scaler**: Filter workflows via query parameter for improved queue count accuracy ([#6519](https://github.com/kedacore/keda/pull/6519)) ### Fixes From 8a88221fde6d576cf33d32d833714c16954a75db Mon Sep 17 00:00:00 2001 From: silviu-dinu Date: Sun, 2 Feb 2025 00:12:56 +0400 Subject: [PATCH 3/4] Update CHANGELOG.md Signed-off-by: silviu-dinu --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19bfc9f120e..7c32845584f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -71,8 +71,8 @@ Here is an overview of all new **experimental** features: ### Improvements -- **IBMMQ Scaler**: Handling StatusNotFound in IBMMQ scaler ([#6472](https://github.com/kedacore/keda/pull/6472)) - **GitHub Scaler**: Filter workflows via query parameter for improved queue count accuracy ([#6519](https://github.com/kedacore/keda/pull/6519)) +- **IBMMQ Scaler**: Handling StatusNotFound in IBMMQ scaler ([#6472](https://github.com/kedacore/keda/pull/6472)) ### Fixes From b50b7bbe60949cbf0f93c47e425fb45c446fa139 Mon Sep 17 00:00:00 2001 From: silviu-dinu Date: Sun, 2 Feb 2025 00:34:58 +0400 Subject: [PATCH 4/4] Update github_runner_scaler.go Signed-off-by: silviu-dinu --- pkg/scalers/github_runner_scaler.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/pkg/scalers/github_runner_scaler.go b/pkg/scalers/github_runner_scaler.go index 571bf409bf4..a69039fb8f6 100644 --- a/pkg/scalers/github_runner_scaler.go +++ b/pkg/scalers/github_runner_scaler.go @@ -588,6 +588,18 @@ func getGithubRequest(ctx context.Context, url string, metadata *githubRunnerMet return b, r.StatusCode, nil } +func stripDeadRuns(allWfrs []WorkflowRuns) []WorkflowRun { + var filtered []WorkflowRun + for _, wfrs := range allWfrs { + for _, wfr := range wfrs.WorkflowRuns { + if wfr.Status == "queued" || wfr.Status == "in_progress" { + filtered = append(filtered, wfr) + } + } + } + return filtered +} + // getWorkflowRunJobs returns a list of jobs for a given workflow run func (s *githubRunnerScaler) getWorkflowRunJobs(ctx context.Context, workflowRunID int64, repoName string) ([]Job, error) { url := fmt.Sprintf("%s/repos/%s/%s/actions/runs/%d/jobs", s.metadata.githubAPIURL, s.metadata.owner, repoName, workflowRunID) @@ -657,7 +669,7 @@ func (s *githubRunnerScaler) GetWorkflowQueueLength(ctx context.Context) (int64, return -1, err } - var wfrs []WorkflowRuns + var allWfrs []WorkflowRuns for _, repo := range repos { wfrsQueued, err := s.getWorkflowRuns(ctx, repo, "queued") @@ -665,19 +677,20 @@ func (s *githubRunnerScaler) GetWorkflowQueueLength(ctx context.Context) (int64, return -1, err } if wfrsQueued != nil { - wfrs = append(wfrs, *wfrsQueued) + allWfrs = append(allWfrs, *wfrsQueued) } wfrsInProgress, err := s.getWorkflowRuns(ctx, repo, "in_progress") if err != nil { return -1, err } if wfrsInProgress != nil { - wfrs = append(wfrs, *wfrsInProgress) + allWfrs = append(allWfrs, *wfrsInProgress) } } var queueCount int64 + wfrs := stripDeadRuns(allWfrs) for _, wfr := range wfrs { jobs, err := s.getWorkflowRunJobs(ctx, wfr.ID, wfr.Repository.Name) if err != nil {