Skip to content

Commit

Permalink
Merge pull request #16 from vincenthsh/fix-action-contains
Browse files Browse the repository at this point in the history
fix: Change detection for composite actions
  • Loading branch information
vincenthsh authored May 15, 2024
2 parents bcb3bd8 + 4a767ab commit f817781
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 106 deletions.
18 changes: 8 additions & 10 deletions cmd/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,13 @@ var Command = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
started := time.Now()

if workingDirectory == "the current working directory" { // this is the default value from the flag
wd, err := os.Getwd()
if err != nil {
logger.Fatalf("could not get current working directory: %v", err)
}

workingDirectory = wd
}
workingDirectory = utils.FetchWorkingDirectory(workingDirectory)

wd, od, err := utils.NormalizeDirectories(workingDirectory, outputDirectory)
nd, err := utils.NormalizeDirectories(workingDirectory, outputDirectory)
if err != nil {
logger.Fatal(err)
}
wd, od := nd[0], nd[1]

if err := os.RemoveAll(od); err != nil {
logger.Fatalf("could not remove output directory: %v", err)
Expand All @@ -46,7 +40,11 @@ var Command = &cobra.Command{
logger.Fatalf("could not create output directory: %v", err)
}

ws := workspace.New(wd, od, workspaceManifest)
ws := workspace.New(workspace.Properties{
WorkingDirectory: wd,
OutputDirectory: od,
WorkspaceManifest: workspaceManifest,
})

logger.Info("collecting actions")

Expand Down
25 changes: 8 additions & 17 deletions cmd/checkversions/checkversions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package checkversions

import (
"os"
"strings"
"time"

Expand All @@ -21,27 +20,17 @@ var workspaceManifest string
var Command = &cobra.Command{
Use: "check-versions",
Short: "Check versions of changed actions in the monorepo",
Long: `Finds all changed actions and verifies no tag exists for their current version.`,
Long: `Finds all changed actions and verifies their current version has no existing tag.`,
Run: func(_ *cobra.Command, _ []string) {
started := time.Now()

if workingDirectory == "the current working directory" { // this is the default value from the flag
wd, err := os.Getwd()
if err != nil {
logger.Fatalf("could not get current working directory: %v", err)
}

workingDirectory = wd
}

// TODO: clean this up
outputDirectory := "build" // ignored
wd, od, err := utils.NormalizeDirectories(workingDirectory, outputDirectory)
workingDirectory = utils.FetchWorkingDirectory(workingDirectory)
wda, err := utils.NormalizeDirectories(workingDirectory)
if err != nil {
logger.Fatal(err)
}

repo, err := git.New(wd)
repo, err := git.New(wda[0])
if err != nil {
logger.Fatal(err)
}
Expand All @@ -55,8 +44,10 @@ var Command = &cobra.Command{

logger.Infof("files changed [%s]", strings.Join(changed, ", "))

ws := workspace.New(wd, od, workspaceManifest)

ws := workspace.New(workspace.Properties{
WorkingDirectory: wda[0],
WorkspaceManifest: workspaceManifest,
})
logger.Info("collecting actions")

actions, err := ws.CollectActions()
Expand Down
20 changes: 9 additions & 11 deletions cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,13 @@ var Command = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
started := time.Now()

if workingDirectory == "the current working directory" { // this is the default value from the flag
wd, err := os.Getwd()
if err != nil {
logger.Fatalf("could not get current working directory: %v", err)
}

workingDirectory = wd
}
workingDirectory = utils.FetchWorkingDirectory(workingDirectory)

wd, od, err := utils.NormalizeDirectories(workingDirectory, outputDirectory)
nd, err := utils.NormalizeDirectories(workingDirectory, outputDirectory)
if err != nil {
logger.Fatal(err)
}
wd, od := nd[0], nd[1]

if err := os.RemoveAll(od); err != nil {
logger.Fatalf("could not remove output directory: %v", err)
Expand All @@ -64,7 +58,11 @@ var Command = &cobra.Command{

logger.Infof("files changed [%s]", strings.Join(changed, ", "))

ws := workspace.New(wd, od, workspaceManifest)
ws := workspace.New(workspace.Properties{
WorkingDirectory: wd,
OutputDirectory: od,
WorkspaceManifest: workspaceManifest,
})

logger.Info("collecting actions")

Expand Down Expand Up @@ -153,6 +151,6 @@ func init() {
Command.Flags().StringVarP(&outputDirectory, "output", "o", "build", "output directory")
Command.Flags().StringVarP(&workingDirectory, "directory", "d", "the current working directory", "directory containing the monorepo of actions")
Command.Flags().StringVarP(&workspaceManifest, "workspace", "w", "gamma-workspace.yml", "workspace manifest for non-javascript actions")
pushTags = Command.Flags().BoolP("push-tags", "t", false, "also the version of action as tag")
pushTags = Command.Flags().BoolP("push-tags", "t", false, "push the action version tags")
Command.Flags().StringArrayVarP(&assetPaths, "asset", "a", []string{}, "copy over an asset to each action")
}
86 changes: 43 additions & 43 deletions cmd/merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package merge

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
Expand All @@ -13,56 +12,33 @@ import (
"github.com/gravitational/gamma/internal/workspace"
)

const outputDirectory = "fake"

var workingDirectory string
var workspaceManifest string
var actionNames []string
var actionsMap map[string]action.Action

// Get workspace action names and indexed map
func getActions(actions []action.Action) ([]string, map[string]action.Action) {
var actionNames []string
actionsMap := make(map[string]action.Action)
for _, action := range actions {
actionNames = append(actionNames, action.Name())
actionsMap[action.Name()] = action
}

return actionNames, actionsMap
}

var Command = &cobra.Command{
Use: "merge action",
Short: "Merge target action to stdout",
Short: "Merge target action yaml to stdout",
Long: `Writes the merged action yaml for the action passed in to stdout.`,
Run: func(_ *cobra.Command, args []string) {
if workingDirectory == "the current working directory" { // this is the default value from the flag
wd, err := os.Getwd()
if err != nil {
logger.Fatalf("could not get current working directory: %v", err)
}

workingDirectory = wd
}

wd, od, err := utils.NormalizeDirectories(workingDirectory, outputDirectory)
if err != nil {
logger.Fatal(err)
}

ws := workspace.New(wd, od, workspaceManifest)

actions, err := ws.CollectActions()
if err != nil {
logger.Fatal(err)
if len(args) != 1 || actionsMap[args[0]] == nil {
allActions := strings.Join(actionNames, ", ")
logger.Fatalf("Must specify exactly 1 target action to merge, choose from [%s]", allActions)
}

if len(actions) == 0 {
logger.Fatal("could not find any actions")
}

var actionNames []string
var targetAction action.Action
for _, action := range actions {
actionNames = append(actionNames, action.Name())
if len(args) == 1 && action.Name() == args[0] {
targetAction = action
}
}
allActions := strings.Join(actionNames, ", ")
if len(args) != 1 {
logger.Fatalf("Must specify exectly 1 target action, choose from [%s]", allActions)
} else if len(args) == 1 && targetAction == nil {
logger.Fatalf("Target action %v not found in [%s]", args[0], allActions)
}

targetAction := actionsMap[args[0]]
s, err := targetAction.GetActionYAML()
if err != nil {
logger.Errorf("error merging action %s: %v", targetAction.Name(), err)
Expand All @@ -74,4 +50,28 @@ var Command = &cobra.Command{
func init() {
Command.Flags().StringVarP(&workingDirectory, "directory", "d", "the current working directory", "directory containing the monorepo of actions")
Command.Flags().StringVarP(&workspaceManifest, "workspace", "w", "gamma-workspace.yml", "workspace manifest for non-javascript actions")

workingDirectory = utils.FetchWorkingDirectory(workingDirectory)
wdArr, err := utils.NormalizeDirectories(workingDirectory)
if err != nil {
logger.Fatal(err)
}
ws := workspace.New(workspace.Properties{
WorkingDirectory: wdArr[0],
WorkspaceManifest: workspaceManifest,
})

actions, err := ws.CollectActions()
if err != nil {
logger.Fatal(err)
}
actionNames, actionsMap = getActions(actions)

Command.ValidArgsFunction = func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}

return actionNames, cobra.ShellCompDirectiveDefault
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/go-git/go-git/v5 v5.11.0
github.com/google/go-github/v48 v48.1.0
github.com/jedib0t/go-pretty/v6 v6.4.2
github.com/mitchellh/copystructure v1.2.0
github.com/spf13/cobra v1.6.1
golang.org/x/sync v0.3.0
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -29,6 +30,7 @@ require (
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI=
github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4=
github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI=
Expand Down
31 changes: 24 additions & 7 deletions internal/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import (

"github.com/gravitational/gamma/internal/node"
"github.com/gravitational/gamma/internal/schema"
"github.com/gravitational/gamma/internal/utils"
publicshema "github.com/gravitational/gamma/pkg/schema"
"github.com/mitchellh/copystructure"
)

type Kind int
Expand All @@ -32,7 +34,7 @@ type action struct {
kind Kind
name string
packageInfo *node.PackageInfo
workspaceInfo *publicshema.ActionInfo
actionInfo *publicshema.ActionInfo
outputDirectory string
workingDirectory string
owner string
Expand All @@ -44,7 +46,7 @@ type Config struct {
WorkingDirectory string
OutputDirectory string
PackageInfo *node.PackageInfo
WorkspaceInfo *publicshema.ActionInfo
ActionInfo *publicshema.ActionInfo
}

type Action interface {
Expand All @@ -63,13 +65,28 @@ func New(config *Config) (Action, error) {
var uriString string
var kind Kind

actionInfo := config.ActionInfo

switch {
case config.PackageInfo != nil && config.PackageInfo.Repository != nil:
kind = Javascript
uriString = config.PackageInfo.Repository.URL
case config.WorkspaceInfo != nil:
case actionInfo != nil:
kind = Composite
uriString = config.WorkspaceInfo.RepositoryURL
uriString = config.ActionInfo.RepositoryURL
// normalize ActionInfo.OutputDirectory to match PackageInfo.Path
oda, err := utils.NormalizeDirectories(config.ActionInfo.OutputDirectory)
if err != nil {
return nil, err
}

structCopy, err := copystructure.Copy(config.ActionInfo)
if err != nil {
return nil, fmt.Errorf("failed to copy workspace info: %v", err)
}

actionInfo = structCopy.(*publicshema.ActionInfo)
actionInfo.OutputDirectory = oda[0]
default:
return nil, errors.New("repository field missing in Action")
}
Expand All @@ -85,7 +102,7 @@ func New(config *Config) (Action, error) {
kind: kind,
name: config.Name,
packageInfo: config.PackageInfo,
workspaceInfo: config.WorkspaceInfo,
actionInfo: actionInfo,
outputDirectory: config.OutputDirectory,
workingDirectory: config.WorkingDirectory,
owner: parts[0],
Expand All @@ -107,7 +124,7 @@ func (a *action) Version() string {
case Javascript:
return a.packageInfo.Version
default:
return a.workspaceInfo.Version
return a.actionInfo.Version
}
}

Expand All @@ -116,7 +133,7 @@ func (a *action) Path() string {
case Javascript:
return a.packageInfo.Path
default:
return a.workspaceInfo.OutputDirectory
return a.actionInfo.OutputDirectory
}
}

Expand Down
Loading

0 comments on commit f817781

Please sign in to comment.