-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
120 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters