share workflow content getting code

This commit is contained in:
vilmibm 2021-04-08 16:11:21 -05:00
parent 1b5eb30575
commit 48e162fa05
4 changed files with 31 additions and 52 deletions

View file

@ -2,13 +2,11 @@ package run
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"sort"
"strings"
@ -285,7 +283,7 @@ func runRun(opts *RunOptions) error {
}
if opts.Prompt {
yamlContent, err := getWorkflowContent(client, repo, workflow, ref)
yamlContent, err := shared.GetWorkflowContent(client, repo, *workflow, ref)
if err != nil {
return fmt.Errorf("unable to fetch workflow file content: %w", err)
}
@ -408,24 +406,3 @@ func findInputs(yamlContent []byte) (map[string]WorkflowInput, error) {
return out, nil
}
func getWorkflowContent(client *api.Client, repo ghrepo.Interface, workflow *shared.Workflow, ref string) ([]byte, error) {
path := fmt.Sprintf("repos/%s/contents/%s?ref=%s", ghrepo.FullName(repo), workflow.Path, url.QueryEscape(ref))
type Result struct {
Content string
}
var result Result
err := client.REST(repo.RepoHost(), "GET", path, nil, &result)
if err != nil {
return nil, err
}
decoded, err := base64.StdEncoding.DecodeString(result.Content)
if err != nil {
return nil, fmt.Errorf("failed to decode workflow file: %w", err)
}
return decoded, nil
}

View file

@ -1,8 +1,10 @@
package shared
import (
"encoding/base64"
"errors"
"fmt"
"net/url"
"path"
"strings"
@ -216,3 +218,28 @@ func ResolveWorkflow(io *iostreams.IOStreams, client *api.Client, repo ghrepo.In
return SelectWorkflow(workflows, "Which workflow do you mean?", states)
}
func GetWorkflowContent(client *api.Client, repo ghrepo.Interface, workflow Workflow, ref string) ([]byte, error) {
path := fmt.Sprintf("repos/%s/contents/%s", ghrepo.FullName(repo), workflow.Path)
if ref != "" {
q := fmt.Sprintf("?ref=%s", url.QueryEscape(ref))
path = path + q
}
type Result struct {
Content string
}
var result Result
err := client.REST(repo.RepoHost(), "GET", path, nil, &result)
if err != nil {
return nil, err
}
decoded, err := base64.StdEncoding.DecodeString(result.Content)
if err != nil {
return nil, fmt.Errorf("failed to decode workflow file: %w", err)
}
return decoded, nil
}

View file

@ -1,9 +1,7 @@
package view
import (
"encoding/base64"
"fmt"
"net/url"
"github.com/cli/cli/api"
"github.com/cli/cli/internal/ghrepo"
@ -16,31 +14,6 @@ type workflowRuns struct {
Runs []runShared.Run
}
func getWorkflowContent(client *api.Client, repo ghrepo.Interface, ref string, workflow *shared.Workflow) (string, error) {
path := fmt.Sprintf("repos/%s/contents/%s", ghrepo.FullName(repo), workflow.Path)
if ref != "" {
q := fmt.Sprintf("?ref=%s", url.QueryEscape(ref))
path = path + q
}
type Result struct {
Content string
}
var result Result
err := client.REST(repo.RepoHost(), "GET", path, nil, &result)
if err != nil {
return "", err
}
decoded, err := base64.StdEncoding.DecodeString(result.Content)
if err != nil {
return "", fmt.Errorf("failed to decode workflow file: %w", err)
}
return string(decoded), nil
}
func getWorkflowRuns(client *api.Client, repo ghrepo.Interface, workflow *shared.Workflow) (workflowRuns, error) {
var wr workflowRuns
var result runShared.RunsPayload

View file

@ -143,7 +143,7 @@ func viewWorkflowContent(opts *ViewOptions, client *api.Client, workflow *shared
}
opts.IO.StartProgressIndicator()
yaml, err := getWorkflowContent(client, repo, opts.Ref, workflow)
yamlBytes, err := shared.GetWorkflowContent(client, repo, *workflow, opts.Ref)
opts.IO.StopProgressIndicator()
if err != nil {
if s, ok := err.(api.HTTPError); ok && s.StatusCode == 404 {
@ -155,6 +155,8 @@ func viewWorkflowContent(opts *ViewOptions, client *api.Client, workflow *shared
return fmt.Errorf("could not get workflow file content: %w", err)
}
yaml := string(yamlBytes)
theme := opts.IO.DetectTerminalTheme()
markdownStyle := markdown.GetStyle(theme)
if err := opts.IO.StartPager(); err != nil {