Skip to content
This repository has been archived by the owner on Aug 12, 2020. It is now read-only.

Commit

Permalink
getting there
Browse files Browse the repository at this point in the history
  • Loading branch information
rajatjindal committed Oct 5, 2019
1 parent 1cd5fe3 commit ebea9eb
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 68 deletions.
24 changes: 16 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -20,6 +21,7 @@ type actionInputs struct {
PluginName string
Token string
UpstreamKrewIndexOwner string
LocalKrewIndexOwner string
localKrewIndexRepo string
upstreamKrewIndexRepo string
localRemoteName string
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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"),
}
Expand Down
42 changes: 39 additions & 3 deletions pkg/actions/actions.go
Original file line number Diff line number Diff line change
@@ -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
}
49 changes: 42 additions & 7 deletions pkg/actions/payload.go → pkg/actions/fake_actions.go
Original file line number Diff line number Diff line change
@@ -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
}

Expand Down
12 changes: 0 additions & 12 deletions pkg/actions/inputs.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/actions/release_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
43 changes: 7 additions & 36 deletions pkg/krew/krew.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}

0 comments on commit ebea9eb

Please sign in to comment.