Skip to content

Commit

Permalink
docs, remove unused code, better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
frantjc committed Nov 2, 2024
1 parent cfe3135 commit 1e9016a
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 39 deletions.
4 changes: 2 additions & 2 deletions azuredevops/task_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func (r *TaskReference) IsRemote() bool {
func (r *TaskReference) String() string {
ref := r.Path
if v := r.Version; v != "" {
ref = ref + "@" + v
ref = fmt.Sprintf("%s@%s", ref, v)
}
return ref
}

// TODO regexp.
// Parse parses a reference to a Azure DevOps Task.
func Parse(ref string) (*TaskReference, error) {
r := &TaskReference{}

Expand Down
2 changes: 1 addition & 1 deletion cloudbuild/workspace.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package cloudbuild

var WorkspacePath = "/workspace"
const WorkspacePath = "/workspace"
2 changes: 1 addition & 1 deletion command/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func hookAttach(cmd *cobra.Command, workingDir string, stdoutUsed ...bool) func(
return func(ctx context.Context, c forge.Container) {
var (
streams = commandStreams(cmd, stdoutUsed...)
_, _ = fmt.Fprintln(streams.Out, "detach with "+forge.DefaultDetachKeys)
_, _ = fmt.Fprintln(streams.Out, "detach with", forge.DefaultDetachKeys)
)

streams, restore, err := forge.TerminalStreams(streams.In, streams.Out, streams.Err)
Expand Down
2 changes: 1 addition & 1 deletion forgeactions/env_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (m *Mapping) SetGlobalContextFromEnvFiles(ctx context.Context, globalContex
}

for k, v := range outputs {
globalContext.EnvContext["STATE_"+k] = v
globalContext.EnvContext[fmt.Sprintf("STATE_%s", k)] = v
}
case strings.HasSuffix(m.GitHubEnvPath, header.Name):
env, err := githubactions.ParseEnvFile(r)
Expand Down
12 changes: 6 additions & 6 deletions githubactions/download_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ func DownloadAction(ctx context.Context, u *Uses) (*Metadata, io.ReadCloser, err
go func() {
defer close(shaC)

if ref, _, err := client.Git.GetRef(ctx, u.GetOwner(), u.GetRepository(), "tags/"+u.Version); err == nil {
if ref, _, err := client.Git.GetRef(ctx, u.GetOwner(), u.GetRepository(), fmt.Sprintf("tags/%s", u.Version)); err == nil {
shaC <- ref.GetObject().GetSHA()
} else {
if ref, _, err := client.Git.GetRef(ctx, u.GetOwner(), u.GetRepository(), "heads/"+u.Version); err == nil {
if ref, _, err := client.Git.GetRef(ctx, u.GetOwner(), u.GetRepository(), fmt.Sprintf("heads/%s", u.Version)); err == nil {
shaC <- ref.GetObject().GetSHA()
} else {
shaC <- u.Version
Expand All @@ -59,7 +59,7 @@ func DownloadAction(ctx context.Context, u *Uses) (*Metadata, io.ReadCloser, err
}()

for _, filename := range ActionYAMLFilenames {
rc, _, err := client.Repositories.DownloadContents(ctx, u.GetOwner(), u.GetRepository(), u.GetActionPath()+"/"+filename, &github.RepositoryContentGetOptions{
rc, _, err := client.Repositories.DownloadContents(ctx, u.GetOwner(), u.GetRepository(), fmt.Sprintf("%s/%s", u.GetActionPath(), filename), &github.RepositoryContentGetOptions{
Ref: u.Version,
})
if err != nil {
Expand All @@ -78,7 +78,7 @@ func DownloadAction(ctx context.Context, u *Uses) (*Metadata, io.ReadCloser, err
}

if metadata == nil {
return nil, nil, ErrNotAnAction
return nil, nil, fmt.Errorf("action.yaml/action.yml not found at %s", u)
}

link, _, err := client.Repositories.GetArchiveLink(
Expand Down Expand Up @@ -108,7 +108,7 @@ func DownloadAction(ctx context.Context, u *Uses) (*Metadata, io.ReadCloser, err
if matched, err := regexp.MatchString("[0-9a-f]{40}", sha); err != nil {
return nil, nil, err
} else if !matched {
return nil, nil, fmt.Errorf("get action sha")
return nil, nil, fmt.Errorf("get action sha returned something that does not look like a sha: %s", sha)
}

r, err := gzip.NewReader(res.Body)
Expand All @@ -117,5 +117,5 @@ func DownloadAction(ctx context.Context, u *Uses) (*Metadata, io.ReadCloser, err
}

// sha is guaranteed to be a 40 character string by the above regexp.
return metadata, xtar.Subdir(tar.NewReader(r), u.GetOwner()+"-"+u.GetRepository()+"-"+sha[0:7]+"/"), nil
return metadata, xtar.Subdir(tar.NewReader(r), fmt.Sprintf("%s-%s-%s/", u.GetOwner(), u.GetRepository(), sha[0:7])), nil
}
16 changes: 0 additions & 16 deletions githubactions/errors.go

This file was deleted.

5 changes: 1 addition & 4 deletions githubactions/metadata.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package githubactions

import (
"errors"
"fmt"
"io"
"strings"
Expand All @@ -18,8 +17,6 @@ const (
RunsUsingNode20 = "node20"
)

var ErrMissingRequiredInput = errors.New("required input missing")

func NewMetadataFromReader(r io.Reader) (*Metadata, error) {
m := &Metadata{}
return m, yaml.NewDecoder(r).Decode(m)
Expand All @@ -40,7 +37,7 @@ func (m *Metadata) InputsFromWith(with map[string]string) (map[string]string, er
case input.Default != "":
inputs[name] = fmt.Sprint(input.Default)
case input.Required:
return nil, ErrMissingRequiredInput
return nil, fmt.Errorf("required input %s is missing", name)
}
}
return inputs, nil
Expand Down
12 changes: 10 additions & 2 deletions githubactions/uses.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (u *Uses) IsRemote() bool {
func (u *Uses) String() string {
uses := u.Path
if v := u.Version; v != "" {
uses = uses + "@" + v
uses = fmt.Sprintf("%s@%s", uses, v)
}
return uses
}
Expand Down Expand Up @@ -64,7 +64,15 @@ func (u *Uses) GoString() string {
return "&Uses{" + u.String() + "}"
}

// TODO regexp.
// Parse parses a reference to a GitHub Action that would appear as the value
// of `uses` in a GitHub Actions Workflow Step, such as:
//
// steps:
// - uses: frantjc/forge@v0
// - uses: ./
// - uses: ./my/local/action
//
// Also supports the special case ".".
func Parse(uses string) (*Uses, error) {
r := &Uses{}

Expand Down
48 changes: 48 additions & 0 deletions githubactions/uses_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package githubactions_test

import (
"testing"

"github.com/frantjc/forge/githubactions"
)

func TestParse(t *testing.T) {
for _, s := range []struct {
uses string
expectedPath string
expectedVersion string
expectedLocal bool
}{
{
uses: "./frantjc/forge",
expectedPath: "./frantjc/forge",
expectedVersion: "",
expectedLocal: true,
},
{
uses: ".",
expectedPath: ".",
expectedVersion: "",
expectedLocal: true,
},
{
uses: "frantjc/forge@v0",
expectedPath: "frantjc/forge",
expectedVersion: "v0",
},
} {
if actual, err := githubactions.Parse(s.uses); err != nil {
t.Error(err)
t.FailNow()
} else if actual.Path != s.expectedPath {
t.Error("was", actual.Path, "but expected", s.expectedPath)
t.FailNow()
} else if actual.Version != s.expectedVersion {
t.Error("was", actual.Version, "but expected", s.expectedVersion)
t.FailNow()
} else if local := actual.IsLocal(); local != s.expectedLocal {
t.Error("was", local, "but expected", s.expectedLocal)
t.FailNow()
}
}
}
3 changes: 3 additions & 0 deletions githubactions/workflow_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func (c *WorkflowCommand) GoString() string {
return "&WorkflowCommand{" + c.String() + "}"
}

// GetName returns the value of the name parameter from the workflow command.
// Useful for set-env workflow commands as they require it to specify the name
// of the environment variable.
func (c *WorkflowCommand) GetName() string {
if c.Parameters != nil {
if name, ok := c.Parameters["name"]; ok {
Expand Down
2 changes: 1 addition & 1 deletion githubactions/workflow_command_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (w *WorkflowCommandWriter) handleCommand(wc *WorkflowCommand) []byte {
case CommandStopCommands:
w.StopCommandsTokens[wc.Value] = true
case CommandSaveState:
w.GlobalContext.EnvContext["STATE_"+wc.GetName()] = wc.Value
w.GlobalContext.EnvContext[fmt.Sprintf("STATE_%s", wc.GetName())] = wc.Value

if !w.saveStateDeprecationWarned {
return []byte("[" + CommandWarning + "] The `" + wc.Command + "` command is deprecated and will be disabled soon. Please upgrade to using Environment Files. For more information see: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/")
Expand Down
20 changes: 15 additions & 5 deletions githubactions/workflow_commands.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package githubactions

import "strings"
import (
"fmt"
"strings"
)

const (
CommandDebug = "debug"
Expand All @@ -17,21 +20,26 @@ const (
CommandStopCommands = "stop-commands"
)

// TODO regexp.
// ParseWorkflowCommandString parses a workflow command from a string such as:
//
// ::set-env name=HELLO::there
//
// This supports a deprecated GitHub Actions function that used such strings written
// to stdout to send commands from a GitHub Action up to GitHub Actions.
func ParseWorkflowCommandString(workflowCommand string) (*WorkflowCommand, error) {
if !strings.HasPrefix(workflowCommand, "::") {
return nil, ErrNotAWorkflowCommand
return nil, fmt.Errorf("not a workflow command: %s", workflowCommand)
}

a := strings.Split(workflowCommand, "::")
if len(a) < 2 {
return nil, ErrNotAWorkflowCommand
return nil, fmt.Errorf("not a workflow command: %s", workflowCommand)
}

cmdAndParams := a[1]
b := strings.Split(cmdAndParams, " ")
if len(b) < 1 {
return nil, ErrNotAWorkflowCommand
return nil, fmt.Errorf("not a workflow command: %s", workflowCommand)
}

cmd := b[0]
Expand All @@ -57,6 +65,8 @@ func ParseWorkflowCommandString(workflowCommand string) (*WorkflowCommand, error
}, nil
}

// ParseWorkflowCommand parses a workflow command from bytes.
// See ParseWorkflowCommandString for more details.
func ParseWorkflowCommand(b []byte) (*WorkflowCommand, error) {
return ParseWorkflowCommandString(string(b))
}

0 comments on commit 1e9016a

Please sign in to comment.