X Tutup
Skip to content

Commit 48e162f

Browse files
author
vilmibm
committed
share workflow content getting code
1 parent 1b5eb30 commit 48e162f

File tree

4 files changed

+31
-52
lines changed

4 files changed

+31
-52
lines changed

pkg/cmd/workflow/run/run.go

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ package run
22

33
import (
44
"bytes"
5-
"encoding/base64"
65
"encoding/json"
76
"errors"
87
"fmt"
98
"io/ioutil"
109
"net/http"
11-
"net/url"
1210
"reflect"
1311
"sort"
1412
"strings"
@@ -285,7 +283,7 @@ func runRun(opts *RunOptions) error {
285283
}
286284

287285
if opts.Prompt {
288-
yamlContent, err := getWorkflowContent(client, repo, workflow, ref)
286+
yamlContent, err := shared.GetWorkflowContent(client, repo, *workflow, ref)
289287
if err != nil {
290288
return fmt.Errorf("unable to fetch workflow file content: %w", err)
291289
}
@@ -408,24 +406,3 @@ func findInputs(yamlContent []byte) (map[string]WorkflowInput, error) {
408406

409407
return out, nil
410408
}
411-
412-
func getWorkflowContent(client *api.Client, repo ghrepo.Interface, workflow *shared.Workflow, ref string) ([]byte, error) {
413-
path := fmt.Sprintf("repos/%s/contents/%s?ref=%s", ghrepo.FullName(repo), workflow.Path, url.QueryEscape(ref))
414-
415-
type Result struct {
416-
Content string
417-
}
418-
419-
var result Result
420-
err := client.REST(repo.RepoHost(), "GET", path, nil, &result)
421-
if err != nil {
422-
return nil, err
423-
}
424-
425-
decoded, err := base64.StdEncoding.DecodeString(result.Content)
426-
if err != nil {
427-
return nil, fmt.Errorf("failed to decode workflow file: %w", err)
428-
}
429-
430-
return decoded, nil
431-
}

pkg/cmd/workflow/shared/shared.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package shared
22

33
import (
4+
"encoding/base64"
45
"errors"
56
"fmt"
7+
"net/url"
68
"path"
79
"strings"
810

@@ -216,3 +218,28 @@ func ResolveWorkflow(io *iostreams.IOStreams, client *api.Client, repo ghrepo.In
216218

217219
return SelectWorkflow(workflows, "Which workflow do you mean?", states)
218220
}
221+
222+
func GetWorkflowContent(client *api.Client, repo ghrepo.Interface, workflow Workflow, ref string) ([]byte, error) {
223+
path := fmt.Sprintf("repos/%s/contents/%s", ghrepo.FullName(repo), workflow.Path)
224+
if ref != "" {
225+
q := fmt.Sprintf("?ref=%s", url.QueryEscape(ref))
226+
path = path + q
227+
}
228+
229+
type Result struct {
230+
Content string
231+
}
232+
233+
var result Result
234+
err := client.REST(repo.RepoHost(), "GET", path, nil, &result)
235+
if err != nil {
236+
return nil, err
237+
}
238+
239+
decoded, err := base64.StdEncoding.DecodeString(result.Content)
240+
if err != nil {
241+
return nil, fmt.Errorf("failed to decode workflow file: %w", err)
242+
}
243+
244+
return decoded, nil
245+
}

pkg/cmd/workflow/view/http.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package view
22

33
import (
4-
"encoding/base64"
54
"fmt"
6-
"net/url"
75

86
"github.com/cli/cli/api"
97
"github.com/cli/cli/internal/ghrepo"
@@ -16,31 +14,6 @@ type workflowRuns struct {
1614
Runs []runShared.Run
1715
}
1816

19-
func getWorkflowContent(client *api.Client, repo ghrepo.Interface, ref string, workflow *shared.Workflow) (string, error) {
20-
path := fmt.Sprintf("repos/%s/contents/%s", ghrepo.FullName(repo), workflow.Path)
21-
if ref != "" {
22-
q := fmt.Sprintf("?ref=%s", url.QueryEscape(ref))
23-
path = path + q
24-
}
25-
26-
type Result struct {
27-
Content string
28-
}
29-
30-
var result Result
31-
err := client.REST(repo.RepoHost(), "GET", path, nil, &result)
32-
if err != nil {
33-
return "", err
34-
}
35-
36-
decoded, err := base64.StdEncoding.DecodeString(result.Content)
37-
if err != nil {
38-
return "", fmt.Errorf("failed to decode workflow file: %w", err)
39-
}
40-
41-
return string(decoded), nil
42-
}
43-
4417
func getWorkflowRuns(client *api.Client, repo ghrepo.Interface, workflow *shared.Workflow) (workflowRuns, error) {
4518
var wr workflowRuns
4619
var result runShared.RunsPayload

pkg/cmd/workflow/view/view.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func viewWorkflowContent(opts *ViewOptions, client *api.Client, workflow *shared
143143
}
144144

145145
opts.IO.StartProgressIndicator()
146-
yaml, err := getWorkflowContent(client, repo, opts.Ref, workflow)
146+
yamlBytes, err := shared.GetWorkflowContent(client, repo, *workflow, opts.Ref)
147147
opts.IO.StopProgressIndicator()
148148
if err != nil {
149149
if s, ok := err.(api.HTTPError); ok && s.StatusCode == 404 {
@@ -155,6 +155,8 @@ func viewWorkflowContent(opts *ViewOptions, client *api.Client, workflow *shared
155155
return fmt.Errorf("could not get workflow file content: %w", err)
156156
}
157157

158+
yaml := string(yamlBytes)
159+
158160
theme := opts.IO.DetectTerminalTheme()
159161
markdownStyle := markdown.GetStyle(theme)
160162
if err := opts.IO.StartPager(); err != nil {

0 commit comments

Comments
 (0)
X Tutup