mirror of
https://github.com/bapung/gitea-runner-operator.git
synced 2026-06-21 23:48:43 +00:00
add user scope
This commit is contained in:
@@ -38,6 +38,7 @@ type Client interface {
|
||||
authToken string,
|
||||
scope v1alpha1.RunnerGroupScope,
|
||||
org string,
|
||||
user string,
|
||||
repo string,
|
||||
labels []string,
|
||||
) (*RunnerStats, error)
|
||||
@@ -118,6 +119,7 @@ func (c *HTTPClient) GetRunnerStats(
|
||||
authToken string,
|
||||
scope v1alpha1.RunnerGroupScope,
|
||||
org string,
|
||||
user string,
|
||||
repo string,
|
||||
labels []string,
|
||||
) (*RunnerStats, error) {
|
||||
@@ -126,6 +128,8 @@ func (c *HTTPClient) GetRunnerStats(
|
||||
return c.getRunnerStatsForRepo(ctx, giteaURL, authToken, org, repo, labels)
|
||||
case v1alpha1.RunnerGroupScopeOrg:
|
||||
return c.getRunnerStatsForOrg(ctx, giteaURL, authToken, org, labels)
|
||||
case v1alpha1.RunnerGroupScopeUser:
|
||||
return c.getRunnerStatsForUser(ctx, giteaURL, authToken, user, labels)
|
||||
case v1alpha1.RunnerGroupScopeGlobal:
|
||||
return c.getRunnerStatsGlobal(ctx, giteaURL, authToken, labels)
|
||||
default:
|
||||
@@ -145,6 +149,28 @@ func (c *HTTPClient) getRunnerStatsForOrg(ctx context.Context, giteaURL, authTok
|
||||
return c.fetchRunnerStats(ctx, endpoint, authToken, labels)
|
||||
}
|
||||
|
||||
// getRunnerStatsForUser fetches queued runs for all repos owned by a user
|
||||
func (c *HTTPClient) getRunnerStatsForUser(ctx context.Context, giteaURL, authToken, user string, labels []string) (*RunnerStats, error) {
|
||||
repos, err := c.fetchReposForUser(ctx, giteaURL, authToken, user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var allQueuedJobs []ActionWorkflowJob
|
||||
for _, repo := range repos {
|
||||
endpoint := fmt.Sprintf("%s/api/v1/repos/%s/%s/actions/jobs", strings.TrimSuffix(giteaURL, "/"), repo.Owner.Login, repo.Name)
|
||||
stats, err := c.fetchRunnerStats(ctx, endpoint, authToken, labels)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
allQueuedJobs = append(allQueuedJobs, stats.QueuedJobs...)
|
||||
}
|
||||
|
||||
return &RunnerStats{
|
||||
QueuedJobs: allQueuedJobs,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// getRunnerStatsGlobal fetches queued runs using admin-level API for global scope
|
||||
func (c *HTTPClient) getRunnerStatsGlobal(ctx context.Context, giteaURL, authToken string, labels []string) (*RunnerStats, error) {
|
||||
endpoint := fmt.Sprintf("%s/api/v1/admin/actions/jobs", strings.TrimSuffix(giteaURL, "/"))
|
||||
@@ -475,6 +501,70 @@ func (c *HTTPClient) fetchUserRepos(ctx context.Context, giteaURL, authToken str
|
||||
return allRepos, nil
|
||||
}
|
||||
|
||||
// fetchReposForUser fetches all repositories owned by a specific user with pagination
|
||||
func (c *HTTPClient) fetchReposForUser(ctx context.Context, giteaURL, authToken, username string) ([]Repository, error) {
|
||||
var allRepos []Repository
|
||||
page := 1
|
||||
limit := 50
|
||||
|
||||
for {
|
||||
endpoint := fmt.Sprintf("%s/api/v1/users/%s/repos", strings.TrimSuffix(giteaURL, "/"), username)
|
||||
u, err := url.Parse(endpoint)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
q := u.Query()
|
||||
q.Set("page", fmt.Sprintf("%d", page))
|
||||
q.Set("limit", fmt.Sprintf("%d", limit))
|
||||
u.RawQuery = q.Encode()
|
||||
|
||||
fmt.Printf("DEBUG: Fetching repos for user %s from %s\n", username, u.String())
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", u.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "token "+authToken)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
fmt.Printf("DEBUG: Request failed: %v\n", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Printf("DEBUG: Response status: %s\n", resp.Status)
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
fmt.Printf("DEBUG: Error body: %s\n", string(body))
|
||||
return nil, c.handleHTTPError(resp.StatusCode, body, "fetch user repos")
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
// fmt.Printf("DEBUG: Response body: %s\n", string(body))
|
||||
|
||||
var repos []Repository
|
||||
if err := json.Unmarshal(body, &repos); err != nil {
|
||||
fmt.Printf("DEBUG: Failed to decode response: %v\n", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
allRepos = append(allRepos, repos...)
|
||||
|
||||
if len(repos) < limit {
|
||||
break
|
||||
}
|
||||
|
||||
page++
|
||||
}
|
||||
|
||||
return allRepos, nil
|
||||
}
|
||||
|
||||
// filterQueuedJobs filters workflow jobs by labels
|
||||
func (c *HTTPClient) filterQueuedJobs(jobs []ActionWorkflowJob, runnerLabels []string) []ActionWorkflowJob {
|
||||
var matched []ActionWorkflowJob
|
||||
|
||||
@@ -32,6 +32,7 @@ func TestHTTPClient_GetRunnerStats(t *testing.T) {
|
||||
name string
|
||||
scope v1alpha1.RunnerGroupScope
|
||||
org string
|
||||
user string
|
||||
repo string
|
||||
labels []string
|
||||
mockResponse ActionWorkflowJobsResponse
|
||||
@@ -87,12 +88,43 @@ func TestHTTPClient_GetRunnerStats(t *testing.T) {
|
||||
expectedQueued: 2,
|
||||
expectedError: false,
|
||||
},
|
||||
{
|
||||
name: "user scope",
|
||||
scope: v1alpha1.RunnerGroupScopeUser,
|
||||
user: "testuser",
|
||||
labels: []string{"linux"},
|
||||
mockResponse: ActionWorkflowJobsResponse{
|
||||
TotalCount: 1,
|
||||
Jobs: []ActionWorkflowJob{
|
||||
{ID: 1, Status: "queued", Labels: []string{"linux"}},
|
||||
},
|
||||
},
|
||||
expectedQueued: 1,
|
||||
expectedError: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Create mock server
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
// Handle User Repos call for User Scope
|
||||
if tt.scope == v1alpha1.RunnerGroupScopeUser && strings.Contains(r.URL.Path, "/repos") && !strings.Contains(r.URL.Path, "/actions/jobs") {
|
||||
repos := []Repository{
|
||||
{
|
||||
Name: "testrepo",
|
||||
Owner: struct {
|
||||
Login string `json:"login"`
|
||||
}{Login: tt.user},
|
||||
FullName: tt.user + "/testrepo",
|
||||
},
|
||||
}
|
||||
json.NewEncoder(w).Encode(repos)
|
||||
return
|
||||
}
|
||||
|
||||
// Verify correct endpoint is called
|
||||
expectedPath := ""
|
||||
switch tt.scope {
|
||||
@@ -102,6 +134,8 @@ func TestHTTPClient_GetRunnerStats(t *testing.T) {
|
||||
expectedPath = "/api/v1/orgs/testorg/actions/jobs"
|
||||
case v1alpha1.RunnerGroupScopeGlobal:
|
||||
expectedPath = "/api/v1/admin/actions/jobs"
|
||||
case v1alpha1.RunnerGroupScopeUser:
|
||||
expectedPath = "/api/v1/repos/" + tt.user + "/testrepo/actions/jobs"
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(r.URL.Path, expectedPath) {
|
||||
@@ -114,8 +148,6 @@ func TestHTTPClient_GetRunnerStats(t *testing.T) {
|
||||
t.Errorf("Expected Authorization header to start with 'token ', got %s", authHeader)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
// Only return jobs for 'queued' status to simplify counting
|
||||
if r.URL.Query().Get("status") == "queued" {
|
||||
json.NewEncoder(w).Encode(tt.mockResponse)
|
||||
@@ -132,6 +164,7 @@ func TestHTTPClient_GetRunnerStats(t *testing.T) {
|
||||
"test-token",
|
||||
tt.scope,
|
||||
tt.org,
|
||||
tt.user,
|
||||
tt.repo,
|
||||
tt.labels,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user