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

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rajatjindal committed Oct 5, 2019
1 parent f7e2c2b commit 1cd5fe3
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 52 deletions.
6 changes: 3 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ inputs:
plugin-name:
description: 'plugin name, defaults to git repo name where this action is triggered'
required: true
who-to-greet: # id of input
description: 'Who to greet'
upstream-krew-index-owner:
description: 'the owner of upstream krew-index repo'
required: true
default: 'World'
default: 'rajatjin'
outputs:
time: # id of output
description: 'The time we greeted you'
Expand Down
69 changes: 36 additions & 33 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,31 @@ import (
ugit "gopkg.in/src-d/go-git.v4"
)

type actionInputs struct {
PluginName string
Token string
UpstreamKrewIndexOwner string
localKrewIndexRepo string
upstreamKrewIndexRepo string
localRemoteName string
upstreamRemoteName string
}

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "krew-plugin-release",
Short: "tool to make PR to krew-plugin-release",
Run: func(cmd *cobra.Command, args []string) {
inputs := actionInputs{
PluginName: actions.GetInputForAction("plugin-name"),
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")),
localRemoteName: "local",
upstreamRemoteName: "upstream",
}

logrus.Info("reading release payload")
releaseInfo, err := actions.GetReleaseInfo()
if err != nil {
Expand All @@ -33,7 +53,7 @@ var rootCmd = &cobra.Command{
}

logrus.Infof("will operate in tempdir %s", dir)
repo, err := updateOriginFromUpstream(dir)
repo, err := cloneRepos(inputs, dir)
if err != nil {
logrus.Fatal(err)
}
Expand All @@ -44,26 +64,20 @@ var rootCmd = &cobra.Command{
logrus.Fatal(err)
}

logrus.Infof("checking out branch %s", releaseInfo.GetTagName())
err = git.CheckoutBranch(repo, releaseInfo.GetTagName())
if err != nil {
logrus.Fatal(err)
}

logrus.Info("update plugin manifest with latest release info")
err = krew.UpdatePluginManifest(dir, "modify-secret", releaseInfo)
err = krew.UpdatePluginManifest(dir, inputs.PluginName, releaseInfo)
if err != nil {
logrus.Fatal(err)
}

logrus.Infof("pushing changes to branch %s", releaseInfo.GetTagName())
err = git.AddCommitAndPush(repo, "new version of modify-secret", releaseInfo.GetTagName())
err = git.AddCommitAndPush(repo, fmt.Sprintf("new version of %s", inputs.PluginName), inputs.localRemoteName, releaseInfo.GetTagName())
if err != nil {
logrus.Fatal(err)
}

logrus.Info("submitting the pr")
err = submitPR(releaseInfo.GetTagName())
err = submitPR(inputs, releaseInfo.GetTagName())
if err != nil {
logrus.Fatal(err)
}
Expand All @@ -74,23 +88,24 @@ func stringp(s string) *string {
return &s
}

func submitPR(branchName string) error {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: os.Getenv("KREW_PLUGIN_RELEASE_TOKEN")})
func submitPR(inputs actionInputs, branchName string) error {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: os.Getenv("")})
tc := oauth2.NewClient(context.TODO(), ts)
client := github.NewClient(tc)

pr := &github.NewPullRequest{
Title: stringp("release new version of modify-secret"),
Head: stringp(fmt.Sprintf("rajatjindal:%s", branchName)),
prr := &github.NewPullRequest{
Title: stringp(fmt.Sprintf("release new version of %s", inputs.PluginName)),
Head: stringp(fmt.Sprintf("%s:%s", actions.GetRepoOwner(), branchName)),
Base: stringp("master"),
Body: stringp("hey krew-index team, I would like to open this PR to release new version of modify-secret"),
}

_, _, err := client.PullRequests.Create(context.TODO(), "rajatjin", "krew-index", pr)
pr, _, err := client.PullRequests.Create(context.TODO(), inputs.UpstreamKrewIndexOwner, "krew-index", prr)
if err != nil {
return err
}

logrus.Infof("pr %q opened for releasing new version", pr.GetHTMLURL())
return nil
}

Expand All @@ -103,30 +118,18 @@ func Execute() {
}
}

func updateOriginFromUpstream(dir string) (*ugit.Repository, error) {
logrus.Infof("Cloning %s", "https://github.com/rajatjindal/krew-index.git")
repo, err := git.Clone("https://github.com/rajatjindal/krew-index.git", git.GetMasterBranchRefs(), dir)
func cloneRepos(inputs actionInputs, dir string) (*ugit.Repository, error) {
logrus.Infof("Cloning %s", inputs.upstreamKrewIndexRepo)
repo, err := git.Clone(inputs.upstreamKrewIndexRepo, inputs.upstreamRemoteName, git.GetMasterBranchRefs(), dir)
if err != nil {
return nil, err
}

logrus.Infof("Adding remote %s", "https://github.com/rajatjin/krew-index.git")
remote, err := git.AddUpstream(repo, "https://github.com/rajatjin/krew-index.git")
logrus.Infof("Adding remote %s at %s", inputs.localRemoteName, inputs.localKrewIndexRepo)
_, err = git.AddUpstream(repo, inputs.localRemoteName, inputs.localKrewIndexRepo)
if err != nil {
return nil, err
}

logrus.Info("fetching upstream")
err = git.FetchUpstream(remote)
if err != nil {
return nil, err
}

logrus.Infof("pushing to origin/master of %s", "https://github.com/rajatjindal/krew-index.git")
err = git.PushOriginMaster(repo)
if err != nil && err.Error() != "already up-to-date" {
return nil, err
}

return repo, nil
}
18 changes: 17 additions & 1 deletion pkg/actions/actions.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
package actions

import (
"os"
"strings"
)

//GetWorkspace returns workspace
func GetWorkspace() string {
return ""
return os.Getenv("GITHUB_WORKSPACE")
}

//GetActor returns the actor
func GetActor() string {
return os.Getenv("GITHUB_ACTOR")
}

//GetRepoOwner returns the repo owner where action is running
func GetRepoOwner() string {
return strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")[0]
}
12 changes: 12 additions & 0 deletions pkg/actions/inputs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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)))
}
30 changes: 15 additions & 15 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ func getAuth() transport.AuthMethod {
type Repository ugit.Repository

//Clone clones the repo
func Clone(origin, branch, dir string) (*ugit.Repository, error) {

func Clone(remoteURL, remoteName, branch, dir string) (*ugit.Repository, error) {
return ugit.PlainClone(dir, false, &ugit.CloneOptions{
URL: origin,
URL: remoteURL,
Progress: os.Stdout,
ReferenceName: plumbing.ReferenceName(branch),
SingleBranch: true,
Auth: getAuth(),
RemoteName: remoteName,
})
}

Expand All @@ -42,24 +42,24 @@ func GetMasterBranchRefs() string {
}

//AddUpstream adds the upstream
func AddUpstream(repo *ugit.Repository, upstream string) (*ugit.Remote, error) {
func AddUpstream(repo *ugit.Repository, remoteName, remoteURL string) (*ugit.Remote, error) {
return repo.CreateRemote(&config.RemoteConfig{
Name: "upstream",
URLs: []string{upstream},
Name: remoteName,
URLs: []string{remoteURL},
})
}

//FetchUpstream fetches the upstream
func FetchUpstream(remote *ugit.Remote) error {
func FetchUpstream(remote *ugit.Remote, remoteName string) error {
return remote.Fetch(&ugit.FetchOptions{
RemoteName: "upstream",
RemoteName: remoteName,
})
}

//PushOriginMaster push code to master
func PushOriginMaster(repo *ugit.Repository) error {
//Push push code
func Push(repo *ugit.Repository, remoteName string) error {
return repo.Push(&ugit.PushOptions{
RemoteName: "origin",
RemoteName: remoteName,
RefSpecs: []config.RefSpec{config.DefaultPushRefSpec},
Auth: getAuth(),
})
Expand Down Expand Up @@ -104,7 +104,7 @@ func CheckoutBranch(repo *ugit.Repository, branchName string) error {
}

//AddCommitAndPush commits and push
func AddCommitAndPush(repo *ugit.Repository, commitMsg, branchName string) error {
func AddCommitAndPush(repo *ugit.Repository, commitMsg, remoteName, branchName string) error {
w, err := repo.Worktree()
if err != nil {
return err
Expand All @@ -120,21 +120,21 @@ func AddCommitAndPush(repo *ugit.Repository, commitMsg, branchName string) error
})

return repo.Push(&ugit.PushOptions{
RemoteName: "origin",
RemoteName: remoteName,
RefSpecs: []config.RefSpec{config.DefaultPushRefSpec},
Auth: getAuth(),
})
}

//PullRebase rebases from pull
func PullRebase(repo *ugit.Repository, branchName string) error {
func PullRebase(repo *ugit.Repository, remoteName, branchName string) error {
w, err := repo.Worktree()
if err != nil {
return err
}

return w.Pull(&git.PullOptions{
RemoteName: "origin",
RemoteName: remoteName,
Auth: getAuth(),
})
}
2 changes: 2 additions & 0 deletions vendor/github.com/sirupsen/logrus/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vendor/gopkg.in/src-d/go-git.v4/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1cd5fe3

Please sign in to comment.