From ebea9ebc163a87f9e546ffb66153cc39b5d631b7 Mon Sep 17 00:00:00 2001 From: Rajat Jindal Date: Sat, 5 Oct 2019 08:26:38 +0530 Subject: [PATCH] getting there --- cmd/root.go | 24 ++++++---- pkg/actions/actions.go | 42 ++++++++++++++++-- pkg/actions/{payload.go => fake_actions.go} | 49 ++++++++++++++++++--- pkg/actions/inputs.go | 12 ----- pkg/actions/release_info.go | 4 +- pkg/krew/krew.go | 43 +++--------------- 6 files changed, 106 insertions(+), 68 deletions(-) rename pkg/actions/{payload.go => fake_actions.go} (94%) delete mode 100644 pkg/actions/inputs.go diff --git a/cmd/root.go b/cmd/root.go index eb4c307..cf0f15b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "os" + "path/filepath" "github.com/google/go-github/github" "github.com/rajatjindal/krew-plugin-release/pkg/actions" @@ -20,6 +21,7 @@ type actionInputs struct { PluginName string Token string UpstreamKrewIndexOwner string + LocalKrewIndexOwner string localKrewIndexRepo string upstreamKrewIndexRepo string localRemoteName string @@ -31,18 +33,21 @@ var rootCmd = &cobra.Command{ Use: "krew-plugin-release", Short: "tool to make PR to krew-plugin-release", Run: func(cmd *cobra.Command, args []string) { + action := actions.FakeAction{} + inputs := actionInputs{ - PluginName: actions.GetInputForAction("plugin-name"), + PluginName: action.GetPluginName(), Token: os.Getenv("KREW_PLUGIN_RELEASE_TOKEN"), - UpstreamKrewIndexOwner: actions.GetInputForAction("upstream-krew-index-owner"), - localKrewIndexRepo: fmt.Sprintf("https://github.com/%s/krew-index.git", actions.GetRepoOwner()), - upstreamKrewIndexRepo: fmt.Sprintf("https://github.com/%s/krew-index.git", actions.GetInputForAction("upstream-krew-index-owner")), + UpstreamKrewIndexOwner: action.GetInputForAction("upstream-krew-index-owner"), + LocalKrewIndexOwner: action.GetRepoOwner(), + localKrewIndexRepo: fmt.Sprintf("https://github.com/%s/krew-index.git", action.GetRepoOwner()), + upstreamKrewIndexRepo: fmt.Sprintf("https://github.com/%s/krew-index.git", action.GetInputForAction("upstream-krew-index-owner")), localRemoteName: "local", upstreamRemoteName: "upstream", } logrus.Info("reading release payload") - releaseInfo, err := actions.GetReleaseInfo() + releaseInfo, err := actions.GetReleaseInfo(action) if err != nil { logrus.Fatal(err) } @@ -65,7 +70,10 @@ var rootCmd = &cobra.Command{ } logrus.Info("update plugin manifest with latest release info") - err = krew.UpdatePluginManifest(dir, inputs.PluginName, releaseInfo) + + templateFile := filepath.Join(action.GetWorkspace(), ".krew.yaml") + actualFile := filepath.Join(dir, "plugins", krew.PluginFileName(inputs.PluginName)) + err = krew.UpdatePluginManifest(templateFile, actualFile, releaseInfo) if err != nil { logrus.Fatal(err) } @@ -89,13 +97,13 @@ func stringp(s string) *string { } func submitPR(inputs actionInputs, branchName string) error { - ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: os.Getenv("")}) + ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: os.Getenv("KREW_PLUGIN_RELEASE_TOKEN")}) tc := oauth2.NewClient(context.TODO(), ts) client := github.NewClient(tc) prr := &github.NewPullRequest{ Title: stringp(fmt.Sprintf("release new version of %s", inputs.PluginName)), - Head: stringp(fmt.Sprintf("%s:%s", actions.GetRepoOwner(), branchName)), + Head: stringp(fmt.Sprintf("%s:%s", inputs.LocalKrewIndexOwner, branchName)), Base: stringp("master"), Body: stringp("hey krew-index team, I would like to open this PR to release new version of modify-secret"), } diff --git a/pkg/actions/actions.go b/pkg/actions/actions.go index d23357a..ec2d364 100644 --- a/pkg/actions/actions.go +++ b/pkg/actions/actions.go @@ -1,21 +1,57 @@ package actions import ( + "fmt" + "io/ioutil" "os" "strings" ) +//ActionData defines interface to get data from actions +type ActionData interface { + GetWorkspace() string + GetActor() string + GetRepoOwner() string + GetInputForAction(key string) string + GetPluginName() string + GetPayload() ([]byte, error) +} + +//RealAction is the real action +type RealAction struct{} + //GetWorkspace returns workspace -func GetWorkspace() string { +func (r RealAction) GetWorkspace() string { return os.Getenv("GITHUB_WORKSPACE") } //GetActor returns the actor -func GetActor() string { +func (r RealAction) GetActor() string { return os.Getenv("GITHUB_ACTOR") } //GetRepoOwner returns the repo owner where action is running -func GetRepoOwner() string { +func (r RealAction) GetRepoOwner() string { return strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")[0] } + +//GetPluginName gets the plugin name +func (r RealAction) GetPluginName() string { + return r.GetInputForAction("plugin-name") +} + +//GetInputForAction gets input to action +func (r RealAction) GetInputForAction(key string) string { + return os.Getenv(fmt.Sprintf("INPUT_%s", strings.ToUpper(key))) +} + +//GetPayload reads payload and returns it +func (r RealAction) GetPayload() ([]byte, error) { + eventJSONPath := os.Getenv("GITHUB_EVENT_PATH") + data, err := ioutil.ReadFile(eventJSONPath) + if err != nil { + return nil, err + } + + return data, nil +} diff --git a/pkg/actions/payload.go b/pkg/actions/fake_actions.go similarity index 94% rename from pkg/actions/payload.go rename to pkg/actions/fake_actions.go index 8195ed9..82be69d 100644 --- a/pkg/actions/payload.go +++ b/pkg/actions/fake_actions.go @@ -1,13 +1,48 @@ package actions -//GetPayload reads payload and returns it -func GetPayload() ([]byte, error) { - // eventJSONPath := os.Getenv("GITHUB_EVENT_PATH") - // data, err := ioutil.ReadFile(eventJSONPath) - // if err != nil { - // return nil, err - // } +import ( + "fmt" + "os" + "strings" +) + +//FakeAction is the real action +type FakeAction struct{} + +//GetWorkspace returns workspace +func (r FakeAction) GetWorkspace() string { + return "" +} + +//GetActor returns the actor +func (r FakeAction) GetActor() string { + return "rajatjindal" +} + +//GetRepoOwner returns the repo owner where action is running +func (r FakeAction) GetRepoOwner() string { + return "rajatjindal" +} +//GetInputForAction gets input to action +func (r FakeAction) GetInputForAction(key string) string { + switch key { + case "plugin-name": + return "modify-secret" + case "upstream-krew-index-owner": + return "rajatjin" + } + + return os.Getenv(fmt.Sprintf("INPUT_%s", strings.ToUpper(key))) +} + +//GetPluginName returns plugin name +func (r FakeAction) GetPluginName() string { + return "modify-secret" +} + +//GetPayload reads payload and returns it +func (r FakeAction) GetPayload() ([]byte, error) { return payload, nil } diff --git a/pkg/actions/inputs.go b/pkg/actions/inputs.go deleted file mode 100644 index ad47e51..0000000 --- a/pkg/actions/inputs.go +++ /dev/null @@ -1,12 +0,0 @@ -package actions - -import ( - "fmt" - "os" - "strings" -) - -//GetInputForAction gets input to action -func GetInputForAction(key string) string { - return os.Getenv(fmt.Sprintf("INPUT_%s", strings.ToUpper(key))) -} diff --git a/pkg/actions/release_info.go b/pkg/actions/release_info.go index 26697d7..6816f98 100644 --- a/pkg/actions/release_info.go +++ b/pkg/actions/release_info.go @@ -7,8 +7,8 @@ import ( ) //GetReleaseInfo gets the release info -func GetReleaseInfo() (*github.RepositoryRelease, error) { - payload, err := GetPayload() +func GetReleaseInfo(action ActionData) (*github.RepositoryRelease, error) { + payload, err := action.GetPayload() if err != nil { return nil, err } diff --git a/pkg/krew/krew.go b/pkg/krew/krew.go index 0aa9297..53acd17 100644 --- a/pkg/krew/krew.go +++ b/pkg/krew/krew.go @@ -4,54 +4,24 @@ import ( "bytes" "fmt" "io/ioutil" - "path/filepath" "text/template" "github.com/google/go-github/github" - "github.com/rajatjindal/krew-plugin-release/pkg/actions" "github.com/sirupsen/logrus" "sigs.k8s.io/krew/pkg/constants" ) -// //UpdatePluginManifest2 is 2 -// func UpdatePluginManifest2(baseDir, pluginName string, release *github.RepositoryRelease) error { -// pluginManifest, err := indexscanner.ReadPluginFile(filepath.Join(actions.GetWorkspace(), ".krew.yaml")) -// if err != nil { -// return err -// } - -// for _, platform := range pluginManifest.Spec.Platforms { -// t := template.New("uri").Parse(platform.URI) -// buf := new(bytes.Buffer) -// t.Execute(buf, release) - -// sha256, err := getSha256ForAsset(buf.String()) -// if err != nil { -// return err -// } - -// } - -// return nil -// } - //UpdatePluginManifest updates the manifest with latest release info -func UpdatePluginManifest(baseDir, pluginName string, release *github.RepositoryRelease) error { - processedPluginBytes, err := processPluginTemplate(release) +func UpdatePluginManifest(templateFile, actualFile string, release *github.RepositoryRelease) error { + processedPluginBytes, err := processPluginTemplate(templateFile, release) if err != nil { return err } - // pluginFileWithSha256, err := addSha256ToPluginFile(pluginName, processedPluginBytes) - // if err != nil { - // return err - // } - - pluginsFile := filepath.Join(baseDir, "plugins", pluginFileName(pluginName)) - return ioutil.WriteFile(pluginsFile, processedPluginBytes, 0644) + return ioutil.WriteFile(actualFile, processedPluginBytes, 0644) } -func processPluginTemplate(releaseInfo *github.RepositoryRelease) ([]byte, error) { +func processPluginTemplate(templateFile string, releaseInfo *github.RepositoryRelease) ([]byte, error) { t := template.New(".krew.yaml").Funcs(map[string]interface{}{ "addURIAndSha": func(url, tag string) string { t := struct { @@ -81,7 +51,7 @@ func processPluginTemplate(releaseInfo *github.RepositoryRelease) ([]byte, error }, }) - templateObject, err := t.ParseFiles(filepath.Join(actions.GetWorkspace(), ".krew.yaml")) + templateObject, err := t.ParseFiles(templateFile) if err != nil { return nil, err } @@ -95,6 +65,7 @@ func processPluginTemplate(releaseInfo *github.RepositoryRelease) ([]byte, error return buf.Bytes(), nil } -func pluginFileName(name string) string { +//PluginFileName returns the plugin file with extension +func PluginFileName(name string) string { return fmt.Sprintf("%s%s", name, constants.ManifestExtension) }