Skip to content

Commit

Permalink
add fetch all steps functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
zLukas committed Jan 28, 2025
1 parent f41e147 commit a3688b4
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 10 deletions.
27 changes: 27 additions & 0 deletions src/agent/cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cmd

import (
"fmt"

"github.com/GHA-Monitor/agent/pkg/api"
"github.com/GHA-Monitor/agent/pkg/credentials"
)

func GitHubInit() (*api.Git, error) {

creds := credentials.Credentials{}
var err error
var git *api.Git

err = creds.Set()
if err != nil {
fmt.Printf("Error seting credentials: %s \n", err)
return nil, err
}
git, err = api.NewClient(creds.Owner, creds.Repo, creds.GetToken())
if err != nil {
fmt.Println("failed to init github client ")
return nil, err
}
return git, nil
}
58 changes: 58 additions & 0 deletions src/agent/cmd/runs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cmd

import (
"context"
"fmt"

"github.com/GHA-Monitor/agent/pkg/api"
)

func FetchAllWorkflows(git *api.Git) ([]api.Workflow, error) {
if git == nil {
return nil, fmt.Errorf("client is nil")
}

workflows, err := api.FetchWorkflows(context.TODO(), git)
if err != nil {
return nil, fmt.Errorf("failed to fetch workflows: %w", err)
}

return fetchRuns(workflows, context.TODO(), git)
}

func fetchRuns(workflows []api.Workflow, ctx context.Context, client *api.Git) ([]api.Workflow, error) {
type result struct {
index int
runs []api.Run
err error
}

runCh := make(chan result, len(workflows))
for i, workflow := range workflows {
go func(i int, workflow api.Workflow) {
runs, err := api.FetchWorkflowRuns(ctx, client, workflow.ID)
if err != nil {
runCh <- result{i, nil, fmt.Errorf("failed to fetch runs for workflow %s: %w", workflow.Name, err)}
return
}
for j, run := range runs {
steps, err := api.FetchRunSteps(ctx, client, run.ID)
if err != nil {
runCh <- result{i, nil, fmt.Errorf("failed to fetch steps for run %s: %w", run.ID, err)}
return
}
runs[j].Steps = steps
}
runCh <- result{i, runs, nil}
}(i, workflow)
}

for range workflows {
res := <-runCh
if res.err != nil {
return nil, res.err
}
workflows[res.index].Runs = res.runs
}
return workflows, nil
}
13 changes: 4 additions & 9 deletions src/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,23 @@ import (
"fmt"
"os"

"github.com/GHA-Monitor/agent/cmd"
"github.com/GHA-Monitor/agent/pkg/api"
"github.com/GHA-Monitor/agent/pkg/credentials"
)

func main() {
creds := credentials.Credentials{}
var err error
var git *api.Git

err = creds.Set()
git, err = cmd.GitHubInit()
if err != nil {
fmt.Printf("Error seting credentials: %s \n", err)
os.Exit(1)
}
git, err = api.NewClient(creds.Owner, creds.Repo, creds.GetToken())
wf, err := api.FetchWorkflows(context.TODO(), git)
if err != nil {
fmt.Println("failed to init github client ")
fmt.Printf("Error fetching workflows: %s \n", err)
os.Exit(1)
}

wf, err := api.FetchWorkflows(context.TODO(), git)
fmt.Printf("printing workflows for %s repo \n", creds.Repo)
for _, w := range wf {
fmt.Println(w.Name)
}
Expand Down
7 changes: 7 additions & 0 deletions src/agent/pkg/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ type Git struct {
type Workflow struct {
ID int64
Name string
Runs []Run
}

type Run struct {
ID int64
Status string
Conclusion string
CreatedAt time.Time
Steps []Step
}

type Step struct {
Name string
Status string
}

func NewClient(owner, repo, token string) (*Git, error) {
Expand Down
25 changes: 24 additions & 1 deletion src/agent/pkg/api/workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package api
import (
"context"
"fmt"
"net/http"

"github.com/google/go-github/v68/github"
)

func FetchWorkflows(ctx context.Context, client *Git) ([]Workflow, error) {
rawWorkflows, _, err := client.GitClient.Actions.ListWorkflows(ctx, client.owner, client.repo, nil)
rawWorkflows, resp, err := client.GitClient.Actions.ListWorkflows(ctx, client.owner, client.repo, nil)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http error: %v ", resp.Status)
}
if err != nil {
return nil, fmt.Errorf("error getting workflows: %v", err)
}
Expand Down Expand Up @@ -44,3 +48,22 @@ func FetchWorkflowRuns(ctx context.Context, client *Git, workflowID int64) ([]Ru

return workflowRuns, nil
}

func FetchRunSteps(ctx context.Context, client *Git, runID int64) ([]Step, error) {
jobs, _, err := client.GitClient.Actions.ListWorkflowJobs(ctx, client.owner, client.repo, runID, nil)
if err != nil {
return nil, fmt.Errorf("error getting workflow jobs: %v", err)
}

var steps []Step
for _, job := range jobs.Jobs {
for _, step := range job.Steps {
steps = append(steps, Step{
Name: step.GetName(),
Status: step.GetStatus(),
})
}
}

return steps, nil
}

0 comments on commit a3688b4

Please sign in to comment.