Skip to content

Commit f526e48

Browse files
Copilotpelikhan
andauthored
Consolidate ghhelper into pkg/workflow and use go-gh/v2 (#3992)
* Initial plan * Clean up ghhelper: move to pkg/workflow and use go-gh/v2 - Moved ghhelper functionality from pkg/ghhelper to pkg/workflow - Updated ExecGH() to use go-gh/v2 library - Added ExecGHWithOutput() convenience wrapper - Updated all imports across CLI and parser packages - Deleted old pkg/ghhelper directory - Tests pass and code compiles successfully Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
1 parent 6417277 commit f526e48

11 files changed

Lines changed: 86 additions & 242 deletions

File tree

pkg/cli/audit.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/githubnext/gh-aw/pkg/cli/fileutil"
1313
"github.com/githubnext/gh-aw/pkg/console"
1414
"github.com/githubnext/gh-aw/pkg/constants"
15-
"github.com/githubnext/gh-aw/pkg/ghhelper"
1615
"github.com/githubnext/gh-aw/pkg/logger"
1716
"github.com/githubnext/gh-aw/pkg/parser"
1817
"github.com/githubnext/gh-aw/pkg/timeutil"
@@ -393,7 +392,7 @@ func fetchWorkflowRunMetadata(runInfo RunURLInfo, verbose bool) (WorkflowRun, er
393392
fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("Executing: gh %s", strings.Join(args, " "))))
394393
}
395394

396-
cmd := ghhelper.ExecGH(args...)
395+
cmd := workflow.ExecGH(args...)
397396
output, err := cmd.CombinedOutput()
398397
if err != nil {
399398
if verbose {

pkg/cli/logs.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
"github.com/githubnext/gh-aw/pkg/console"
1616
"github.com/githubnext/gh-aw/pkg/constants"
17-
"github.com/githubnext/gh-aw/pkg/ghhelper"
1817
"github.com/githubnext/gh-aw/pkg/logger"
1918
"github.com/githubnext/gh-aw/pkg/timeutil"
2019
"github.com/githubnext/gh-aw/pkg/workflow"
@@ -139,7 +138,7 @@ func fetchJobStatuses(runID int64, verbose bool) (int, error) {
139138
fmt.Println(console.FormatVerboseMessage(fmt.Sprintf("Fetching job statuses for run %d", runID)))
140139
}
141140

142-
cmd := ghhelper.ExecGH("api", fmt.Sprintf("repos/{owner}/{repo}/actions/runs/%d/jobs", runID), "--jq", ".jobs[] | {name: .name, status: .status, conclusion: .conclusion}")
141+
cmd := workflow.ExecGH("api", fmt.Sprintf("repos/{owner}/{repo}/actions/runs/%d/jobs", runID), "--jq", ".jobs[] | {name: .name, status: .status, conclusion: .conclusion}")
143142
output, err := cmd.CombinedOutput()
144143
if err != nil {
145144
if verbose {
@@ -185,7 +184,7 @@ func fetchJobDetails(runID int64, verbose bool) ([]JobInfoWithDuration, error) {
185184
fmt.Fprintln(os.Stderr, console.FormatVerboseMessage(fmt.Sprintf("Fetching job details for run %d", runID)))
186185
}
187186

188-
cmd := ghhelper.ExecGH("api", fmt.Sprintf("repos/{owner}/{repo}/actions/runs/%d/jobs", runID), "--jq", ".jobs[] | {name: .name, status: .status, conclusion: .conclusion, started_at: .started_at, completed_at: .completed_at}")
187+
cmd := workflow.ExecGH("api", fmt.Sprintf("repos/{owner}/{repo}/actions/runs/%d/jobs", runID), "--jq", ".jobs[] | {name: .name, status: .status, conclusion: .conclusion, started_at: .started_at, completed_at: .completed_at}")
189188
output, err := cmd.CombinedOutput()
190189
if err != nil {
191190
if verbose {

pkg/cli/logs_download.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020

2121
"github.com/githubnext/gh-aw/pkg/cli/fileutil"
2222
"github.com/githubnext/gh-aw/pkg/console"
23-
"github.com/githubnext/gh-aw/pkg/ghhelper"
23+
"github.com/githubnext/gh-aw/pkg/workflow"
2424
)
2525

2626
// flattenSingleFileArtifacts checks artifact directories and flattens any that contain a single file
@@ -97,7 +97,7 @@ func downloadWorkflowRunLogs(runID int64, outputDir string, verbose bool) error
9797

9898
// Use gh api to download the logs zip file
9999
// The endpoint returns a 302 redirect to the actual zip file
100-
cmd := ghhelper.ExecGH("api", "repos/{owner}/{repo}/actions/runs/"+strconv.FormatInt(runID, 10)+"/logs")
100+
cmd := workflow.ExecGH("api", "repos/{owner}/{repo}/actions/runs/"+strconv.FormatInt(runID, 10)+"/logs")
101101
output, err := cmd.Output()
102102
if err != nil {
103103
// Check for authentication errors
@@ -268,7 +268,7 @@ func downloadRunArtifacts(runID int64, outputDir string, verbose bool) error {
268268
spinner.Start()
269269
}
270270

271-
cmd := ghhelper.ExecGH("run", "download", strconv.FormatInt(runID, 10), "--dir", outputDir)
271+
cmd := workflow.ExecGH("run", "download", strconv.FormatInt(runID, 10), "--dir", outputDir)
272272
output, err := cmd.CombinedOutput()
273273

274274
// Stop spinner

pkg/cli/pr_command.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"time"
1111

1212
"github.com/githubnext/gh-aw/pkg/console"
13-
"github.com/githubnext/gh-aw/pkg/ghhelper"
1413
"github.com/githubnext/gh-aw/pkg/parser"
14+
"github.com/githubnext/gh-aw/pkg/workflow"
1515
"github.com/spf13/cobra"
1616
)
1717

@@ -100,15 +100,15 @@ func parsePRURL(prURL string) (owner, repo string, prNumber int, err error) {
100100
// checkRepositoryAccess checks if the current user has write access to the target repository
101101
func checkRepositoryAccess(owner, repo string) (bool, error) {
102102
// Get current user
103-
cmd := ghhelper.ExecGH("api", "/user", "--jq", ".login")
103+
cmd := workflow.ExecGH("api", "/user", "--jq", ".login")
104104
output, err := cmd.Output()
105105
if err != nil {
106106
return false, fmt.Errorf("failed to get current user: %w", err)
107107
}
108108
username := strings.TrimSpace(string(output))
109109

110110
// Check user's permission level for the repository
111-
cmd = ghhelper.ExecGH("api", fmt.Sprintf("/repos/%s/%s/collaborators/%s/permission", owner, repo, username))
111+
cmd = workflow.ExecGH("api", fmt.Sprintf("/repos/%s/%s/collaborators/%s/permission", owner, repo, username))
112112
output, err = cmd.Output()
113113
if err != nil {
114114
// If we get an error, it likely means we don't have access or the repo doesn't exist
@@ -133,7 +133,7 @@ func checkRepositoryAccess(owner, repo string) (bool, error) {
133133
// createForkIfNeeded creates a fork of the target repository and returns the fork repo name
134134
func createForkIfNeeded(targetOwner, targetRepo string, verbose bool) (forkOwner, forkRepo string, err error) {
135135
// Get current user
136-
cmd := ghhelper.ExecGH("api", "/user", "--jq", ".login")
136+
cmd := workflow.ExecGH("api", "/user", "--jq", ".login")
137137
output, err := cmd.Output()
138138
if err != nil {
139139
return "", "", fmt.Errorf("failed to get current user: %w", err)
@@ -170,7 +170,7 @@ func createForkIfNeeded(targetOwner, targetRepo string, verbose bool) (forkOwner
170170
// fetchPRInfo fetches detailed information about a pull request
171171
func fetchPRInfo(owner, repo string, prNumber int) (*PRInfo, error) {
172172
// Fetch PR details using gh API
173-
cmd := ghhelper.ExecGH("api", fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repo, prNumber),
173+
cmd := workflow.ExecGH("api", fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repo, prNumber),
174174
"--jq", `{
175175
number: .number,
176176
title: .title,
@@ -268,7 +268,7 @@ func applyPatchToRepo(patchFile string, prInfo *PRInfo, targetOwner, targetRepo
268268
fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Getting default branch of target repository..."))
269269
}
270270

271-
defaultBranchCmd := ghhelper.ExecGH("api", fmt.Sprintf("/repos/%s/%s", targetOwner, targetRepo), "--jq", ".default_branch")
271+
defaultBranchCmd := workflow.ExecGH("api", fmt.Sprintf("/repos/%s/%s", targetOwner, targetRepo), "--jq", ".default_branch")
272272
defaultBranchOutput, err := defaultBranchCmd.Output()
273273
if err != nil {
274274
return "", fmt.Errorf("failed to get default branch: %w", err)

pkg/cli/trial_command.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/githubnext/gh-aw/pkg/cli/fileutil"
1414
"github.com/githubnext/gh-aw/pkg/console"
1515
"github.com/githubnext/gh-aw/pkg/constants"
16-
"github.com/githubnext/gh-aw/pkg/ghhelper"
16+
1717
"github.com/githubnext/gh-aw/pkg/logger"
1818
"github.com/githubnext/gh-aw/pkg/parser"
1919
"github.com/githubnext/gh-aw/pkg/workflow"
@@ -482,7 +482,7 @@ func RunWorkflowTrials(workflowSpecs []string, logicalRepoSpec string, cloneRepo
482482

483483
// getCurrentGitHubUsername gets the current GitHub username from gh CLI
484484
func getCurrentGitHubUsername() (string, error) {
485-
cmd := ghhelper.ExecGH("api", "user", "--jq", ".login")
485+
cmd := workflow.ExecGH("api", "user", "--jq", ".login")
486486
output, err := cmd.Output()
487487
if err != nil {
488488
return "", fmt.Errorf("failed to get GitHub username: %w", err)

pkg/cli/update_command.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010

1111
"github.com/githubnext/gh-aw/pkg/console"
1212
"github.com/githubnext/gh-aw/pkg/constants"
13-
"github.com/githubnext/gh-aw/pkg/ghhelper"
1413
"github.com/githubnext/gh-aw/pkg/parser"
14+
"github.com/githubnext/gh-aw/pkg/workflow"
1515
"github.com/spf13/cobra"
1616
)
1717

@@ -429,7 +429,7 @@ func resolveLatestRelease(repo, currentRef string, allowMajor, verbose bool) (st
429429
}
430430

431431
// Use gh CLI to get releases
432-
cmd := ghhelper.ExecGH("api", fmt.Sprintf("/repos/%s/releases", repo), "--jq", ".[].tag_name")
432+
cmd := workflow.ExecGH("api", fmt.Sprintf("/repos/%s/releases", repo), "--jq", ".[].tag_name")
433433
output, err := cmd.Output()
434434
if err != nil {
435435
return "", fmt.Errorf("failed to fetch releases: %w", err)
@@ -486,7 +486,7 @@ func resolveLatestRelease(repo, currentRef string, allowMajor, verbose bool) (st
486486
// isBranchRef checks if a ref is a branch in the repository
487487
func isBranchRef(repo, ref string) (bool, error) {
488488
// Use gh CLI to list branches
489-
cmd := ghhelper.ExecGH("api", fmt.Sprintf("/repos/%s/branches", repo), "--jq", ".[].name")
489+
cmd := workflow.ExecGH("api", fmt.Sprintf("/repos/%s/branches", repo), "--jq", ".[].name")
490490
output, err := cmd.Output()
491491
if err != nil {
492492
return false, err
@@ -509,7 +509,7 @@ func resolveBranchHead(repo, branch string, verbose bool) (string, error) {
509509
}
510510

511511
// Use gh CLI to get branch info
512-
cmd := ghhelper.ExecGH("api", fmt.Sprintf("/repos/%s/branches/%s", repo, branch), "--jq", ".commit.sha")
512+
cmd := workflow.ExecGH("api", fmt.Sprintf("/repos/%s/branches/%s", repo, branch), "--jq", ".commit.sha")
513513
output, err := cmd.Output()
514514
if err != nil {
515515
return "", fmt.Errorf("failed to fetch branch info: %w", err)
@@ -530,7 +530,7 @@ func resolveDefaultBranchHead(repo string, verbose bool) (string, error) {
530530
}
531531

532532
// First get the default branch name
533-
cmd := ghhelper.ExecGH("api", fmt.Sprintf("/repos/%s", repo), "--jq", ".default_branch")
533+
cmd := workflow.ExecGH("api", fmt.Sprintf("/repos/%s", repo), "--jq", ".default_branch")
534534
output, err := cmd.Output()
535535
if err != nil {
536536
return "", fmt.Errorf("failed to fetch repository info: %w", err)
@@ -692,7 +692,7 @@ func downloadWorkflowContent(repo, path, ref string, verbose bool) ([]byte, erro
692692
}
693693

694694
// Use gh CLI to download the file
695-
cmd := ghhelper.ExecGH("api", fmt.Sprintf("/repos/%s/contents/%s?ref=%s", repo, path, ref), "--jq", ".content")
695+
cmd := workflow.ExecGH("api", fmt.Sprintf("/repos/%s/contents/%s?ref=%s", repo, path, ref), "--jq", ".content")
696696
output, err := cmd.Output()
697697
if err != nil {
698698
return nil, fmt.Errorf("failed to fetch file content: %w", err)

pkg/ghhelper/gh_helper.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

pkg/ghhelper/gh_helper_test.go

Lines changed: 0 additions & 157 deletions
This file was deleted.

0 commit comments

Comments
 (0)