Skip to content

Commit

Permalink
Refactor GitHub repository and workflow handling
Browse files Browse the repository at this point in the history
- Remove branch parameter from ListWorkflowRuns.
- Removed unused types and methods related to workflow run logs and outputs.
- Enhanced error handling in workflow triggering and rerunning functions.

These changes improve the clarity and usability of the GitHub repository interface.
  • Loading branch information
canack committed Jan 6, 2025
1 parent f567e8c commit 2dfbb32
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 108 deletions.
3 changes: 1 addition & 2 deletions internal/github/repository/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ type Repository interface {
GetAuthUser(ctx context.Context) (*GithubUser, error)
GetRepository(ctx context.Context, repository string) (*GithubRepository, error)
ListBranches(ctx context.Context, repository string) ([]GithubBranch, error)
ListWorkflowRuns(ctx context.Context, repository string, branch string) (*WorkflowRuns, error)
ListWorkflowRuns(ctx context.Context, repository string) (*WorkflowRuns, error)
TriggerWorkflow(ctx context.Context, repository string, branch string, workflowName string, workflow any) error
GetWorkflows(ctx context.Context, repository string) ([]Workflow, error)
GetTriggerableWorkflows(ctx context.Context, repository string) ([]Workflow, error)
InspectWorkflowContent(ctx context.Context, repository string, branch string, workflowFile string) ([]byte, error)
GetWorkflowRunLogs(ctx context.Context, repository string, runId int64) (GithubWorkflowRunLogs, error)
ReRunFailedJobs(ctx context.Context, repository string, runId int64) error
ReRunWorkflow(ctx context.Context, repository string, runId int64) error
CancelWorkflow(ctx context.Context, repository string, runId int64) error
Expand Down
19 changes: 1 addition & 18 deletions internal/github/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,12 @@ func (r *Repo) GetRepository(ctx context.Context, repository string) (*GithubRep
return &repo, nil
}

func (r *Repo) ListWorkflowRuns(ctx context.Context, repository string, branch string) (*WorkflowRuns, error) {
func (r *Repo) ListWorkflowRuns(ctx context.Context, repository string) (*WorkflowRuns, error) {
// List workflow runs for the given repository and branch
var workflowRuns WorkflowRuns
err := r.do(ctx, nil, &workflowRuns, requestOptions{
method: http.MethodGet,
paths: []string{"repos", repository, "actions", "runs"},
queryParams: map[string]string{
"branch": branch,
},
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -291,20 +288,6 @@ func (r *Repo) getWorkflowFile(ctx context.Context, repository string, path stri
return string(decodedContent), nil
}

func (r *Repo) GetWorkflowRunLogs(ctx context.Context, repository string, runID int64) (GithubWorkflowRunLogs, error) {
// Get the logs for a given workflow run
var workflowRunLogs GithubWorkflowRunLogs
err := r.do(ctx, nil, &workflowRunLogs, requestOptions{
method: http.MethodGet,
paths: []string{"repos", repository, "actions", "runs", strconv.FormatInt(runID, 10), "logs"},
})
if err != nil {
return GithubWorkflowRunLogs{}, err
}

return workflowRunLogs, nil
}

func (r *Repo) ReRunFailedJobs(ctx context.Context, repository string, runID int64) error {
// Re-run failed jobs for a given workflow run
err := r.do(ctx, nil, nil, requestOptions{
Expand Down
8 changes: 1 addition & 7 deletions internal/github/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,8 @@ func TestRepo_ListWorkflowRuns(t *testing.T) {
repo := newRepo(ctx)

targetRepositoryName := "canack/tc"
targetRepository, err := repo.GetRepository(ctx, targetRepositoryName)
if err != nil {
t.Error(err)
}

defaultBranch := targetRepository.DefaultBranch

workflowRuns, err := repo.ListWorkflowRuns(ctx, targetRepositoryName, defaultBranch)
workflowRuns, err := repo.ListWorkflowRuns(ctx, targetRepositoryName)
if err != nil {
t.Error(err)
}
Expand Down
14 changes: 0 additions & 14 deletions internal/github/repository/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ import (
"time"
)

type GithubConfig struct {
Token string
}

type InitializeOptions struct {
HTTPTimeout time.Duration
}

type GithubRepository struct {
Id int `json:"id"`
NodeId string `json:"node_id"`
Expand Down Expand Up @@ -116,12 +108,6 @@ type Actor struct {
AvatarUrl string `json:"avatar_url"`
}

type GithubWorkflowRunLogs struct {
TotalSize int `json:"total_size"`
Url string `json:"url"`
Download string `json:"download_url"`
}

type GithubUser struct {
Login string `json:"login"` // username
ID int `json:"id"`
Expand Down
8 changes: 4 additions & 4 deletions internal/github/usecase/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ type UseCase interface {
GetWorkflowHistory(ctx context.Context, input GetWorkflowHistoryInput) (*GetWorkflowHistoryOutput, error)
GetTriggerableWorkflows(ctx context.Context, input GetTriggerableWorkflowsInput) (*GetTriggerableWorkflowsOutput, error)
InspectWorkflow(ctx context.Context, input InspectWorkflowInput) (*InspectWorkflowOutput, error)
TriggerWorkflow(ctx context.Context, input TriggerWorkflowInput) (*TriggerWorkflowOutput, error)
ReRunFailedJobs(ctx context.Context, input ReRunFailedJobsInput) (*ReRunFailedJobsOutput, error)
ReRunWorkflow(ctx context.Context, input ReRunWorkflowInput) (*ReRunWorkflowOutput, error)
CancelWorkflow(ctx context.Context, input CancelWorkflowInput) (*CancelWorkflowOutput, error)
TriggerWorkflow(ctx context.Context, input TriggerWorkflowInput) error
ReRunFailedJobs(ctx context.Context, input ReRunFailedJobsInput) error
ReRunWorkflow(ctx context.Context, input ReRunWorkflowInput) error
CancelWorkflow(ctx context.Context, input CancelWorkflowInput) error
}
14 changes: 0 additions & 14 deletions internal/github/usecase/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,6 @@ type TriggerWorkflowInput struct {
Content string // workflow content in json format
}

type TriggerWorkflowOutput struct {
// Return workflow information
// Like status url etc.
}

// ------------------------------------------------------------

type GetTriggerableWorkflowsInput struct {
Expand All @@ -143,25 +138,16 @@ type ReRunFailedJobsInput struct {
WorkflowID int64
}

type ReRunFailedJobsOutput struct {
}

// ------------------------------------------------------------

type ReRunWorkflowInput struct {
Repository string
WorkflowID int64
}

type ReRunWorkflowOutput struct {
}

// ------------------------------------------------------------

type CancelWorkflowInput struct {
Repository string
WorkflowID int64
}

type CancelWorkflowOutput struct {
}
40 changes: 9 additions & 31 deletions internal/github/usecase/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,8 @@ func (u useCase) workerListRepositories(ctx context.Context, repository gr.Githu

func (u useCase) GetWorkflowHistory(ctx context.Context, input GetWorkflowHistoryInput) (*GetWorkflowHistoryOutput, error) {
var targetRepositoryName = input.Repository
var targetBranch = input.Branch
if targetBranch == "" {
repository, err := u.githubRepository.GetRepository(ctx, targetRepositoryName)
if err != nil {
return nil, err
}
targetBranch = repository.DefaultBranch
}

workflowRuns, err := u.githubRepository.ListWorkflowRuns(ctx, targetRepositoryName, targetBranch)
workflowRuns, err := u.githubRepository.ListWorkflowRuns(ctx, targetRepositoryName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -214,34 +206,20 @@ func (u useCase) InspectWorkflow(ctx context.Context, input InspectWorkflowInput
}, nil
}

func (u useCase) TriggerWorkflow(ctx context.Context, input TriggerWorkflowInput) (*TriggerWorkflowOutput, error) {
err := u.githubRepository.TriggerWorkflow(ctx, input.Repository, input.Branch, input.WorkflowFile, input.Content)
if err != nil {
return nil, err
}

return &TriggerWorkflowOutput{}, nil
func (u useCase) TriggerWorkflow(ctx context.Context, input TriggerWorkflowInput) error {
return u.githubRepository.TriggerWorkflow(ctx, input.Repository, input.Branch, input.WorkflowFile, input.Content)
}

func (u useCase) ReRunFailedJobs(ctx context.Context, input ReRunFailedJobsInput) (*ReRunFailedJobsOutput, error) {
if err := u.githubRepository.ReRunFailedJobs(ctx, input.Repository, input.WorkflowID); err != nil {
return nil, err
}
return &ReRunFailedJobsOutput{}, nil
func (u useCase) ReRunFailedJobs(ctx context.Context, input ReRunFailedJobsInput) error {
return u.githubRepository.ReRunFailedJobs(ctx, input.Repository, input.WorkflowID)
}

func (u useCase) ReRunWorkflow(ctx context.Context, input ReRunWorkflowInput) (*ReRunWorkflowOutput, error) {
if err := u.githubRepository.ReRunWorkflow(ctx, input.Repository, input.WorkflowID); err != nil {
return nil, err
}
return &ReRunWorkflowOutput{}, nil
func (u useCase) ReRunWorkflow(ctx context.Context, input ReRunWorkflowInput) error {
return u.githubRepository.ReRunWorkflow(ctx, input.Repository, input.WorkflowID)
}

func (u useCase) CancelWorkflow(ctx context.Context, input CancelWorkflowInput) (*CancelWorkflowOutput, error) {
if err := u.githubRepository.CancelWorkflow(ctx, input.Repository, input.WorkflowID); err != nil {
return nil, err
}
return &CancelWorkflowOutput{}, nil
func (u useCase) CancelWorkflow(ctx context.Context, input CancelWorkflowInput) error {
return u.githubRepository.CancelWorkflow(ctx, input.Repository, input.WorkflowID)
}

func (u useCase) timeToString(t time.Time) string {
Expand Down
3 changes: 1 addition & 2 deletions internal/github/usecase/usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestUseCase_TriggerWorkflow(t *testing.T) {
t.Error(err)
}

trigger, err := githubUseCase.TriggerWorkflow(ctx, TriggerWorkflowInput{
err = githubUseCase.TriggerWorkflow(ctx, TriggerWorkflowInput{
WorkflowFile: ".github/workflows/dispatch_test.yaml",
Repository: "canack/tc",
Branch: "master",
Expand All @@ -94,5 +94,4 @@ func TestUseCase_TriggerWorkflow(t *testing.T) {
if err != nil {
t.Error(err)
}
t.Log(trigger)
}
6 changes: 2 additions & 4 deletions internal/terminal/handler/ghtrigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,14 +511,12 @@ func (m *ModelGithubTrigger) triggerWorkflow() {
return
}

_, err = m.github.TriggerWorkflow(context.Background(), gu.TriggerWorkflowInput{
if err := m.github.TriggerWorkflow(context.Background(), gu.TriggerWorkflowInput{
Repository: m.selectedRepository.RepositoryName,
Branch: m.selectedRepository.BranchName,
WorkflowFile: m.selectedWorkflow,
Content: content,
})

if err != nil {
}); err != nil {
m.status.SetError(err)
m.status.SetErrorMessage("Workflow cannot be triggered")
return
Expand Down
18 changes: 6 additions & 12 deletions internal/terminal/handler/ghworkflowhistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,10 @@ func (m *ModelGithubWorkflowHistory) openInBrowser() {
func (m *ModelGithubWorkflowHistory) rerunFailedJobs() {
m.status.SetProgressMessage("Re-running failed jobs...")

_, err := m.github.ReRunFailedJobs(context.Background(), gu.ReRunFailedJobsInput{
if err := m.github.ReRunFailedJobs(context.Background(), gu.ReRunFailedJobsInput{
Repository: m.selectedRepository.RepositoryName,
WorkflowID: m.selectedWorkflowID,
})

if err != nil {
}); err != nil {
m.status.SetError(err)
m.status.SetErrorMessage("Failed to re-run failed jobs")
return
Expand All @@ -515,12 +513,10 @@ func (m *ModelGithubWorkflowHistory) rerunWorkflow() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

_, err := m.github.ReRunWorkflow(ctx, gu.ReRunWorkflowInput{
if err := m.github.ReRunWorkflow(ctx, gu.ReRunWorkflowInput{
Repository: m.selectedRepository.RepositoryName,
WorkflowID: m.selectedWorkflowID,
})

if err != nil {
}); err != nil {
if errors.Is(err, context.DeadlineExceeded) {
m.status.SetErrorMessage("Workflow re-run request timed out")
} else {
Expand All @@ -541,12 +537,10 @@ func (m *ModelGithubWorkflowHistory) rerunWorkflow() {
func (m *ModelGithubWorkflowHistory) cancelWorkflow() {
m.status.SetProgressMessage("Canceling workflow...")

_, err := m.github.CancelWorkflow(context.Background(), gu.CancelWorkflowInput{
if err := m.github.CancelWorkflow(context.Background(), gu.CancelWorkflowInput{
Repository: m.selectedRepository.RepositoryName,
WorkflowID: m.selectedWorkflowID,
})

if err != nil {
}); err != nil {
m.status.SetError(err)
m.status.SetErrorMessage("Failed to cancel workflow")
return
Expand Down

0 comments on commit 2dfbb32

Please sign in to comment.