From eb04c3103016f05e99469671ed61af1c29a2f7d8 Mon Sep 17 00:00:00 2001 From: Rajat Jindal Date: Sat, 5 Oct 2019 19:45:49 +0530 Subject: [PATCH] Rewrite (#1) rewrite --- README.md | 2 +- action.yml | 11 +- cmd/helper.go | 179 +++++++++++++ cmd/root.go | 121 ++------- pkg/actions/actions.go | 129 ++++++++-- pkg/actions/fake_actions.go | 341 ------------------------- pkg/actions/local_action.go | 331 ++++++++++++++++++++++++ pkg/actions/release_info.go | 11 +- pkg/actions/repo_info.go | 28 -- pkg/git/git.go | 136 ---------- vendor/gopkg.in/src-d/go-git.v4/go.mod | 2 - 11 files changed, 647 insertions(+), 644 deletions(-) create mode 100644 cmd/helper.go delete mode 100644 pkg/actions/fake_actions.go create mode 100644 pkg/actions/local_action.go delete mode 100644 pkg/actions/repo_info.go delete mode 100644 pkg/git/git.go diff --git a/README.md b/README.md index 58ba20f..2989a33 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ jobs: - name: Checkout uses: actions/checkout@v1 - name: Open PR for new release of Krew Plugin - uses: rajatjindal/krew-plugin-release@v0.0.26 + uses: rajatjindal/krew-plugin-release@v0.0.27 with: plugin-name: 'your-plugin-name' upstream-krew-index-owner: 'kubernetes-sigs' diff --git a/action.yml b/action.yml index 3519e71..87718bf 100644 --- a/action.yml +++ b/action.yml @@ -6,11 +6,16 @@ inputs: description: 'plugin-name' required: true upstream-krew-index-owner: - description: 'the owner of upstream krew-index repo' - required: true + description: 'the owner of upstream krew-index repo. defaults to kubernetes-sigs' + required: false + upstream-krew-index-repo-name: + description: 'the owner of upstream krew-index repo. defaults to krew-index' + required: false runs: using: 'docker' - image: 'docker://rajatjindal/krew-plugin-release:v0.0.26' + image: 'docker://rajatjindal/krew-plugin-release:v0.0.27' args: - ${{ inputs.upstream-krew-index-owner }} + - ${{ inputs.upstream-krew-index-repo-name }} - ${{ inputs.plugin-name }} + diff --git a/cmd/helper.go b/cmd/helper.go new file mode 100644 index 0000000..f92257d --- /dev/null +++ b/cmd/helper.go @@ -0,0 +1,179 @@ +package cmd + +import ( + "context" + "fmt" + "os" + "time" + + "github.com/google/go-github/github" + "github.com/rajatjindal/krew-plugin-release/pkg/actions" + "github.com/sirupsen/logrus" + "golang.org/x/oauth2" + "gopkg.in/src-d/go-git.v4" + ugit "gopkg.in/src-d/go-git.v4" + "gopkg.in/src-d/go-git.v4/config" + "gopkg.in/src-d/go-git.v4/plumbing" + "gopkg.in/src-d/go-git.v4/plumbing/object" + "gopkg.in/src-d/go-git.v4/plumbing/transport" + githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http" +) + +func cloneRepos(actionData actions.ActionData, dir string) (*ugit.Repository, error) { + logrus.Infof("Cloning %s", actionData.Derived.UpstreamCloneURL) + repo, err := ugit.PlainClone(dir, false, &ugit.CloneOptions{ + URL: actionData.Derived.UpstreamCloneURL, + Progress: os.Stdout, + ReferenceName: plumbing.Master, + SingleBranch: true, + Auth: getAuth(actionData), + RemoteName: originNameUpstream, + }) + if err != nil { + return nil, err + } + + logrus.Infof("Adding remote %s at %s", originNameLocal, actionData.Derived.LocalCloneURL) + _, err = repo.CreateRemote(&config.RemoteConfig{ + Name: originNameLocal, + URLs: []string{actionData.Derived.LocalCloneURL}, + }) + if err != nil { + return nil, err + } + + logrus.Infof("creating branch %s", actionData.ReleaseInfo.GetTagName()) + err = createBranch(repo, actionData.ReleaseInfo.GetTagName()) + if err != nil { + return nil, err + } + + return repo, nil +} + +//createBranch creates branch +func createBranch(repo *ugit.Repository, branchName string) error { + w, err := repo.Worktree() + if err != nil { + return err + } + + // First try to create branch + err = w.Checkout(&git.CheckoutOptions{ + Create: true, + Force: false, + Branch: plumbing.NewBranchReferenceName(branchName), + }) + + if err == nil { + return nil + } + + //may be it already exists + return w.Checkout(&git.CheckoutOptions{ + Create: false, + Force: false, + Branch: plumbing.NewBranchReferenceName(branchName), + }) +} + +type commit struct { + msg string + remoteName string +} + +//addCommitAndPush commits and push +func addCommitAndPush(repo *ugit.Repository, commit commit, actionData actions.ActionData) error { + w, err := repo.Worktree() + if err != nil { + return err + } + + w.Add(".") + _, err = w.Commit(commit.msg, &git.CommitOptions{ + Author: &object.Signature{ + Name: actionData.Inputs.TokenUserName, + Email: actionData.Inputs.TokenUserEmail, + When: time.Now(), + }, + }) + + return repo.Push(&ugit.PushOptions{ + RemoteName: commit.remoteName, + RefSpecs: []config.RefSpec{config.DefaultPushRefSpec}, + Auth: getAuth(actionData), + }) +} + +func submitPR(actionData actions.ActionData) error { + ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: actionData.Inputs.Token}) + tc := oauth2.NewClient(context.TODO(), ts) + client := github.NewClient(tc) + + prr := &github.NewPullRequest{ + Title: getTitle(actionData), + Head: getHead(actionData), + Base: github.String("master"), + Body: getPRBody(actionData), + } + + logrus.Infof("creating pr with title %q, \nhead %q, \nbase %q, \nbody %q", + github.Stringify(getTitle(actionData)), + github.Stringify(getHead(actionData)), + "master", + github.Stringify(getPRBody(actionData)), + ) + + pr, _, err := client.PullRequests.Create( + context.TODO(), + actionData.Inputs.UpstreamKrewIndexOwner, + actionData.Inputs.UpstreamKrewIndexRepoName, + prr, + ) + if err != nil { + return err + } + + logrus.Infof("pr %q opened for releasing new version", pr.GetHTMLURL()) + return nil +} + +func getTitle(actionData actions.ActionData) *string { + s := fmt.Sprintf( + "release new version %s of %s", + actionData.ReleaseInfo.GetTagName(), + actionData.Inputs.PluginName, + ) + + return github.String(s) +} + +func getHead(actionData actions.ActionData) *string { + s := fmt.Sprintf("%s:%s", actionData.RepoOwner, actionData.ReleaseInfo.GetTagName()) + return github.String(s) +} + +func getPRBody(actionData actions.ActionData) *string { + prBody := `hey krew-index team, + +I would like to open this PR to publish version %s of %s on behalf of [%s](https://github.com/%s). + +Thanks, +[krew-plugin-release](https://github.com/rajatjindal/krew-plugin-release)` + + s := fmt.Sprintf(prBody, + fmt.Sprintf("`%s`", actionData.ReleaseInfo.GetTagName()), + fmt.Sprintf("`%s`", actionData.Inputs.PluginName), + actionData.Actor, + actionData.Actor, + ) + + return github.String(s) +} + +func getAuth(actionData actions.ActionData) transport.AuthMethod { + return &githttp.BasicAuth{ + Username: actionData.Inputs.TokenUserHandle, + Password: actionData.Inputs.Token, + } +} diff --git a/cmd/root.go b/cmd/root.go index ee0e325..0712285 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,42 +1,21 @@ package cmd import ( - "context" "fmt" "io/ioutil" "os" "path/filepath" - "github.com/google/go-github/github" "github.com/rajatjindal/krew-plugin-release/pkg/actions" - "github.com/rajatjindal/krew-plugin-release/pkg/git" "github.com/rajatjindal/krew-plugin-release/pkg/krew" "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "golang.org/x/oauth2" - ugit "gopkg.in/src-d/go-git.v4" ) -const prBody = `hey krew-index team, - -I would like to open this PR to release new version %s of %s on behalf of %s. - -Thanks, -[krew-plugin-release](https://github.com/rajatjindal/krew-plugin-release)` - -type actionData struct { - pluginName string - tagName string - token string - localKrewIndexOwner string - localKrewIndexRepo string - localRemoteName string - upstreamKrewIndexRepo string - upstreamKrewIndexOwner string - upstreamRemoteName string - actor string - branchName string -} +const ( + originNameUpstream = "upstream" + originNameLocal = "local" +) // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ @@ -44,97 +23,55 @@ var rootCmd = &cobra.Command{ Short: "github action to open PR for krew-index on release of new version of krew-plugin", Run: func(cmd *cobra.Command, args []string) { action := actions.RealAction{} - - data := actionData{ - pluginName: action.GetPluginName(), - token: os.Getenv("KREW_PLUGIN_RELEASE_TOKEN"), - 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", - actor: action.GetActor(), - } - - logrus.Info("reading release payload") - releaseInfo, err := actions.GetReleaseInfo(action) + actionData, err := action.GetActionData() if err != nil { logrus.Fatal(err) } - if releaseInfo.GetPrerelease() { - logrus.Infof("%s is a pre-release. not opening the PR", releaseInfo.GetTagName()) + logrus.Info(actionData.Inputs, actionData.Derived) + if actionData.ReleaseInfo.GetPrerelease() { + logrus.Infof("%s is a pre-release. not opening the PR", actionData.ReleaseInfo.GetTagName()) logrus.Exit(0) } - data.tagName = releaseInfo.GetTagName() - data.branchName = releaseInfo.GetTagName() - dir, err := ioutil.TempDir("", "krew-index-") + tempdir, err := ioutil.TempDir("", "krew-index-") if err != nil { logrus.Fatal(err) } - logrus.Infof("will operate in tempdir %s", dir) - repo, err := cloneRepos(data, dir) - if err != nil { - logrus.Fatal(err) - } - - logrus.Infof("creating branch %s", releaseInfo.GetTagName()) - err = git.CreateBranch(repo, releaseInfo.GetTagName()) + logrus.Infof("will operate in tempdir %s", tempdir) + repo, err := cloneRepos(actionData, tempdir) if err != nil { logrus.Fatal(err) } logrus.Info("update plugin manifest with latest release info") - templateFile := filepath.Join(action.GetWorkspace(), ".krew.yaml") - actualFile := filepath.Join(dir, "plugins", krew.PluginFileName(data.pluginName)) - err = krew.UpdatePluginManifest(templateFile, actualFile, releaseInfo) + templateFile := filepath.Join(actionData.Workspace, ".krew.yaml") + actualFile := filepath.Join(tempdir, "plugins", krew.PluginFileName(actionData.Inputs.PluginName)) + err = krew.UpdatePluginManifest(templateFile, actualFile, actionData.ReleaseInfo) if err != nil { logrus.Fatal(err) } - logrus.Infof("pushing changes to branch %s", data.tagName) - err = git.AddCommitAndPush(repo, fmt.Sprintf("new version of %s", data.pluginName), data.localRemoteName, data.tagName) + logrus.Infof("pushing changes to branch %s", actionData.ReleaseInfo.GetTagName()) + commit := commit{ + msg: fmt.Sprintf("new version %s of %s", actionData.ReleaseInfo.GetTagName(), actionData.Inputs.PluginName), + remoteName: originNameLocal, + } + err = addCommitAndPush(repo, commit, actionData) if err != nil { logrus.Fatal(err) } logrus.Info("submitting the pr") - err = submitPR(data) + err = submitPR(actionData) if err != nil { logrus.Fatal(err) } }, } -func stringp(s string) *string { - return &s -} - -func submitPR(data actionData) error { - 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 %s of %s", data.tagName, data.pluginName)), - Head: stringp(fmt.Sprintf("%s:%s", data.localKrewIndexOwner, data.branchName)), - Base: stringp("master"), - Body: stringp(fmt.Sprintf(prBody, data.tagName, data.pluginName, data.actor)), - } - - pr, _, err := client.PullRequests.Create(context.TODO(), data.upstreamKrewIndexOwner, "krew-index", prr) - if err != nil { - return err - } - - logrus.Infof("pr %q opened for releasing new version", pr.GetHTMLURL()) - return nil -} - // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { @@ -143,19 +80,3 @@ func Execute() { os.Exit(1) } } - -func cloneRepos(data actionData, dir string) (*ugit.Repository, error) { - logrus.Infof("Cloning %s", data.upstreamKrewIndexRepo) - repo, err := git.Clone(data.upstreamKrewIndexRepo, data.upstreamRemoteName, git.GetMasterBranchRefs(), dir) - if err != nil { - return nil, err - } - - logrus.Infof("Adding remote %s at %s", data.localRemoteName, data.localKrewIndexRepo) - _, err = git.AddUpstream(repo, data.localRemoteName, data.localKrewIndexRepo) - if err != nil { - return nil, err - } - - return repo, nil -} diff --git a/pkg/actions/actions.go b/pkg/actions/actions.go index ec2d364..97883d9 100644 --- a/pkg/actions/actions.go +++ b/pkg/actions/actions.go @@ -1,52 +1,118 @@ package actions import ( + "context" "fmt" "io/ioutil" "os" "strings" + + "github.com/google/go-github/github" + "golang.org/x/oauth2" ) -//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) +//Inputs is action inputs +type Inputs struct { + PluginName string + Token string + TokenUserHandle string + TokenUserEmail string + TokenUserName string + UpstreamKrewIndexRepoName string + UpstreamKrewIndexOwner string } -//RealAction is the real action -type RealAction struct{} +//Derived is derived data +type Derived struct { + UpstreamCloneURL string + LocalCloneURL string +} -//GetWorkspace returns workspace -func (r RealAction) GetWorkspace() string { - return os.Getenv("GITHUB_WORKSPACE") +//ActionData is action data +type ActionData struct { + Workspace string + Actor string + Repo string + RepoOwner string + Inputs Inputs + ReleaseInfo *github.RepositoryRelease + Derived Derived } -//GetActor returns the actor -func (r RealAction) GetActor() string { - return os.Getenv("GITHUB_ACTOR") +//Action defines interface to get data from actions +type Action interface { + GetActionData() ActionData } -//GetRepoOwner returns the repo owner where action is running -func (r RealAction) GetRepoOwner() string { - return strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")[0] +//RealAction is the real action +type RealAction struct{} + +//GetActionData returns action data +func (r RealAction) GetActionData() (ActionData, error) { + payload, err := r.getPayload() + if err != nil { + return ActionData{}, err + } + + releaseInfo, err := getReleaseInfo(payload) + if err != nil { + return ActionData{}, err + } + + tokenUserHandle := os.Getenv("KREW_PLUGIN_RELEASE_USER") + if tokenUserHandle == "" { + tokenUserHandle = strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")[0] + } + + token := os.Getenv("KREW_PLUGIN_RELEASE_TOKEN") + tokenUserEmail, tokenUserName, err := r.getUserInfo(tokenUserHandle, token) + if err != nil { + return ActionData{}, err + } + + upstreamKrewIndexRepoName := r.getInputForAction("upstream-krew-index-repo-name") + if upstreamKrewIndexRepoName == "" { + upstreamKrewIndexRepoName = "krew-index" + } + + upstreamKrewIndexRepoOwner := r.getInputForAction("upstream-krew-index-owner") + if upstreamKrewIndexRepoOwner == "" { + upstreamKrewIndexRepoOwner = "kubernetes-sigs" + } + + return ActionData{ + Workspace: os.Getenv("GITHUB_WORKSPACE"), + Actor: os.Getenv("GITHUB_ACTOR"), + Repo: strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")[1], + RepoOwner: strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")[0], + ReleaseInfo: releaseInfo, + Inputs: Inputs{ + PluginName: r.getInputForAction("plugin-name"), + TokenUserHandle: tokenUserHandle, + Token: os.Getenv("KREW_PLUGIN_RELEASE_TOKEN"), + TokenUserEmail: tokenUserEmail, + TokenUserName: tokenUserName, + UpstreamKrewIndexOwner: upstreamKrewIndexRepoOwner, + UpstreamKrewIndexRepoName: upstreamKrewIndexRepoName, + }, + Derived: Derived{ + UpstreamCloneURL: getRepoURL(upstreamKrewIndexRepoOwner, upstreamKrewIndexRepoName), + LocalCloneURL: getRepoURL(strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")[0], upstreamKrewIndexRepoName), + }, + }, nil } -//GetPluginName gets the plugin name -func (r RealAction) GetPluginName() string { - return r.GetInputForAction("plugin-name") +func getRepoURL(owner, repo string) string { + return fmt.Sprintf("https://github.com/%s/%s.git", owner, repo) } -//GetInputForAction gets input to action -func (r RealAction) GetInputForAction(key string) string { +//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) { +func (r RealAction) getPayload() ([]byte, error) { eventJSONPath := os.Getenv("GITHUB_EVENT_PATH") data, err := ioutil.ReadFile(eventJSONPath) if err != nil { @@ -55,3 +121,16 @@ func (r RealAction) GetPayload() ([]byte, error) { return data, nil } + +func (r RealAction) getUserInfo(username, token string) (string, string, error) { + ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}) + tc := oauth2.NewClient(context.TODO(), ts) + client := github.NewClient(tc) + + user, _, err := client.Users.Get(context.TODO(), username) + if err != nil { + return "", "", err + } + + return user.GetName(), user.GetEmail(), nil +} diff --git a/pkg/actions/fake_actions.go b/pkg/actions/fake_actions.go deleted file mode 100644 index 82be69d..0000000 --- a/pkg/actions/fake_actions.go +++ /dev/null @@ -1,341 +0,0 @@ -package actions - -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 -} - -var payload = []byte(`{ - "action": "published", - "release": { - "assets": [ - { - "browser_download_url": "https://github.com/rajatjindal/krew-plugin-release/releases/download/v0.0.6/darwin-amd64-v0.0.6.tar.gz", - "content_type": "application/x-gzip", - "created_at": "2019-09-30T10:51:23Z", - "download_count": 0, - "id": 15204574, - "label": null, - "name": "darwin-amd64-v0.0.6.tar.gz", - "node_id": "MDEyOlJlbGVhc2VBc3NldDE1MjA0NTc0", - "size": 9150835, - "state": "uploaded", - "updated_at": "2019-09-30T10:51:43Z", - "uploader": { - "avatar_url": "https://avatars2.githubusercontent.com/u/612092?v=4", - "events_url": "https://api.github.com/users/rajatjindal/events{/privacy}", - "followers_url": "https://api.github.com/users/rajatjindal/followers", - "following_url": "https://api.github.com/users/rajatjindal/following{/other_user}", - "gists_url": "https://api.github.com/users/rajatjindal/gists{/gist_id}", - "gravatar_id": "", - "html_url": "https://github.com/rajatjindal", - "id": 612092, - "login": "rajatjindal", - "node_id": "MDQ6VXNlcjYxMjA5Mg==", - "organizations_url": "https://api.github.com/users/rajatjindal/orgs", - "received_events_url": "https://api.github.com/users/rajatjindal/received_events", - "repos_url": "https://api.github.com/users/rajatjindal/repos", - "site_admin": false, - "starred_url": "https://api.github.com/users/rajatjindal/starred{/owner}{/repo}", - "subscriptions_url": "https://api.github.com/users/rajatjindal/subscriptions", - "type": "User", - "url": "https://api.github.com/users/rajatjindal" - }, - "url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/releases/assets/15204574" - }, - { - "browser_download_url": "https://github.com/rajatjindal/krew-plugin-release/releases/download/v0.0.6/linux-amd64-v0.0.6.tar.gz", - "content_type": "application/x-gzip", - "created_at": "2019-09-30T10:50:34Z", - "download_count": 0, - "id": 15204570, - "label": null, - "name": "linux-amd64-v0.0.6.tar.gz", - "node_id": "MDEyOlJlbGVhc2VBc3NldDE1MjA0NTcw", - "size": 8657197, - "state": "uploaded", - "updated_at": "2019-09-30T10:51:02Z", - "uploader": { - "avatar_url": "https://avatars2.githubusercontent.com/u/612092?v=4", - "events_url": "https://api.github.com/users/rajatjindal/events{/privacy}", - "followers_url": "https://api.github.com/users/rajatjindal/followers", - "following_url": "https://api.github.com/users/rajatjindal/following{/other_user}", - "gists_url": "https://api.github.com/users/rajatjindal/gists{/gist_id}", - "gravatar_id": "", - "html_url": "https://github.com/rajatjindal", - "id": 612092, - "login": "rajatjindal", - "node_id": "MDQ6VXNlcjYxMjA5Mg==", - "organizations_url": "https://api.github.com/users/rajatjindal/orgs", - "received_events_url": "https://api.github.com/users/rajatjindal/received_events", - "repos_url": "https://api.github.com/users/rajatjindal/repos", - "site_admin": false, - "starred_url": "https://api.github.com/users/rajatjindal/starred{/owner}{/repo}", - "subscriptions_url": "https://api.github.com/users/rajatjindal/subscriptions", - "type": "User", - "url": "https://api.github.com/users/rajatjindal" - }, - "url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/releases/assets/15204570" - }, - { - "browser_download_url": "https://github.com/rajatjindal/krew-plugin-release/releases/download/v0.0.6/sha256-darwin-amd64-v0.0.6", - "content_type": "application/octet-stream", - "created_at": "2019-09-30T10:51:23Z", - "download_count": 0, - "id": 15204575, - "label": null, - "name": "sha256-darwin-amd64-v0.0.6", - "node_id": "MDEyOlJlbGVhc2VBc3NldDE1MjA0NTc1", - "size": 65, - "state": "uploaded", - "updated_at": "2019-09-30T10:51:44Z", - "uploader": { - "avatar_url": "https://avatars2.githubusercontent.com/u/612092?v=4", - "events_url": "https://api.github.com/users/rajatjindal/events{/privacy}", - "followers_url": "https://api.github.com/users/rajatjindal/followers", - "following_url": "https://api.github.com/users/rajatjindal/following{/other_user}", - "gists_url": "https://api.github.com/users/rajatjindal/gists{/gist_id}", - "gravatar_id": "", - "html_url": "https://github.com/rajatjindal", - "id": 612092, - "login": "rajatjindal", - "node_id": "MDQ6VXNlcjYxMjA5Mg==", - "organizations_url": "https://api.github.com/users/rajatjindal/orgs", - "received_events_url": "https://api.github.com/users/rajatjindal/received_events", - "repos_url": "https://api.github.com/users/rajatjindal/repos", - "site_admin": false, - "starred_url": "https://api.github.com/users/rajatjindal/starred{/owner}{/repo}", - "subscriptions_url": "https://api.github.com/users/rajatjindal/subscriptions", - "type": "User", - "url": "https://api.github.com/users/rajatjindal" - }, - "url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/releases/assets/15204575" - }, - { - "browser_download_url": "https://github.com/rajatjindal/krew-plugin-release/releases/download/v0.0.6/sha256-linux-amd64-v0.0.6", - "content_type": "application/octet-stream", - "created_at": "2019-09-30T10:50:35Z", - "download_count": 0, - "id": 15204571, - "label": null, - "name": "sha256-linux-amd64-v0.0.6", - "node_id": "MDEyOlJlbGVhc2VBc3NldDE1MjA0NTcx", - "size": 65, - "state": "uploaded", - "updated_at": "2019-09-30T10:51:02Z", - "uploader": { - "avatar_url": "https://avatars2.githubusercontent.com/u/612092?v=4", - "events_url": "https://api.github.com/users/rajatjindal/events{/privacy}", - "followers_url": "https://api.github.com/users/rajatjindal/followers", - "following_url": "https://api.github.com/users/rajatjindal/following{/other_user}", - "gists_url": "https://api.github.com/users/rajatjindal/gists{/gist_id}", - "gravatar_id": "", - "html_url": "https://github.com/rajatjindal", - "id": 612092, - "login": "rajatjindal", - "node_id": "MDQ6VXNlcjYxMjA5Mg==", - "organizations_url": "https://api.github.com/users/rajatjindal/orgs", - "received_events_url": "https://api.github.com/users/rajatjindal/received_events", - "repos_url": "https://api.github.com/users/rajatjindal/repos", - "site_admin": false, - "starred_url": "https://api.github.com/users/rajatjindal/starred{/owner}{/repo}", - "subscriptions_url": "https://api.github.com/users/rajatjindal/subscriptions", - "type": "User", - "url": "https://api.github.com/users/rajatjindal" - }, - "url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/releases/assets/15204571" - } - ], - "assets_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/releases/20348112/assets", - "author": { - "avatar_url": "https://avatars2.githubusercontent.com/u/612092?v=4", - "events_url": "https://api.github.com/users/rajatjindal/events{/privacy}", - "followers_url": "https://api.github.com/users/rajatjindal/followers", - "following_url": "https://api.github.com/users/rajatjindal/following{/other_user}", - "gists_url": "https://api.github.com/users/rajatjindal/gists{/gist_id}", - "gravatar_id": "", - "html_url": "https://github.com/rajatjindal", - "id": 612092, - "login": "rajatjindal", - "node_id": "MDQ6VXNlcjYxMjA5Mg==", - "organizations_url": "https://api.github.com/users/rajatjindal/orgs", - "received_events_url": "https://api.github.com/users/rajatjindal/received_events", - "repos_url": "https://api.github.com/users/rajatjindal/repos", - "site_admin": false, - "starred_url": "https://api.github.com/users/rajatjindal/starred{/owner}{/repo}", - "subscriptions_url": "https://api.github.com/users/rajatjindal/subscriptions", - "type": "User", - "url": "https://api.github.com/users/rajatjindal" - }, - "body": "v0.0.6", - "created_at": "2019-09-29T15:14:53Z", - "draft": false, - "html_url": "https://github.com/rajatjindal/krew-plugin-release/releases/tag/v0.0.6", - "id": 20348112, - "name": "release v0.0.6", - "node_id": "MDc6UmVsZWFzZTIwMzQ4MTEy", - "prerelease": false, - "published_at": "2019-09-30T10:51:48Z", - "tag_name": "v0.0.6", - "tarball_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/tarball/v0.0.6", - "target_commitish": "master", - "upload_url": "https://uploads.github.com/repos/rajatjindal/krew-plugin-release/releases/20348112/assets{?name,label}", - "url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/releases/20348112", - "zipball_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/zipball/v0.0.6" - }, - "repository": { - "archive_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/{archive_format}{/ref}", - "archived": false, - "assignees_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/assignees{/user}", - "blobs_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/git/blobs{/sha}", - "branches_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/branches{/branch}", - "clone_url": "https://github.com/rajatjindal/krew-plugin-release.git", - "collaborators_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/collaborators{/collaborator}", - "comments_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/comments{/number}", - "commits_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/commits{/sha}", - "compare_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/compare/{base}...{head}", - "contents_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/contents/{+path}", - "contributors_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/contributors", - "created_at": "2019-09-29T10:03:17Z", - "default_branch": "master", - "deployments_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/deployments", - "description": null, - "disabled": false, - "downloads_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/downloads", - "events_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/events", - "fork": false, - "forks": 0, - "forks_count": 0, - "forks_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/forks", - "full_name": "rajatjindal/krew-plugin-release", - "git_commits_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/git/commits{/sha}", - "git_refs_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/git/refs{/sha}", - "git_tags_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/git/tags{/sha}", - "git_url": "git://github.com/rajatjindal/krew-plugin-release.git", - "has_downloads": true, - "has_issues": true, - "has_pages": false, - "has_projects": true, - "has_wiki": true, - "homepage": null, - "hooks_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/hooks", - "html_url": "https://github.com/rajatjindal/krew-plugin-release", - "id": 211642796, - "issue_comment_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/issues/comments{/number}", - "issue_events_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/issues/events{/number}", - "issues_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/issues{/number}", - "keys_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/keys{/key_id}", - "labels_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/labels{/name}", - "language": "Go", - "languages_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/languages", - "license": null, - "merges_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/merges", - "milestones_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/milestones{/number}", - "mirror_url": null, - "name": "krew-plugin-release", - "node_id": "MDEwOlJlcG9zaXRvcnkyMTE2NDI3OTY=", - "notifications_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/notifications{?since,all,participating}", - "open_issues": 0, - "open_issues_count": 0, - "owner": { - "avatar_url": "https://avatars2.githubusercontent.com/u/612092?v=4", - "events_url": "https://api.github.com/users/rajatjindal/events{/privacy}", - "followers_url": "https://api.github.com/users/rajatjindal/followers", - "following_url": "https://api.github.com/users/rajatjindal/following{/other_user}", - "gists_url": "https://api.github.com/users/rajatjindal/gists{/gist_id}", - "gravatar_id": "", - "html_url": "https://github.com/rajatjindal", - "id": 612092, - "login": "rajatjindal", - "node_id": "MDQ6VXNlcjYxMjA5Mg==", - "organizations_url": "https://api.github.com/users/rajatjindal/orgs", - "received_events_url": "https://api.github.com/users/rajatjindal/received_events", - "repos_url": "https://api.github.com/users/rajatjindal/repos", - "site_admin": false, - "starred_url": "https://api.github.com/users/rajatjindal/starred{/owner}{/repo}", - "subscriptions_url": "https://api.github.com/users/rajatjindal/subscriptions", - "type": "User", - "url": "https://api.github.com/users/rajatjindal" - }, - "private": true, - "pulls_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/pulls{/number}", - "pushed_at": "2019-09-30T10:51:48Z", - "releases_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/releases{/id}", - "size": 1238, - "ssh_url": "git@github.com:rajatjindal/krew-plugin-release.git", - "stargazers_count": 0, - "stargazers_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/stargazers", - "statuses_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/statuses/{sha}", - "subscribers_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/subscribers", - "subscription_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/subscription", - "svn_url": "https://github.com/rajatjindal/krew-plugin-release", - "tags_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/tags", - "teams_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/teams", - "trees_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/git/trees{/sha}", - "updated_at": "2019-09-29T15:15:04Z", - "url": "https://api.github.com/repos/rajatjindal/krew-plugin-release", - "watchers": 0, - "watchers_count": 0 - }, - "sender": { - "avatar_url": "https://avatars2.githubusercontent.com/u/612092?v=4", - "events_url": "https://api.github.com/users/rajatjindal/events{/privacy}", - "followers_url": "https://api.github.com/users/rajatjindal/followers", - "following_url": "https://api.github.com/users/rajatjindal/following{/other_user}", - "gists_url": "https://api.github.com/users/rajatjindal/gists{/gist_id}", - "gravatar_id": "", - "html_url": "https://github.com/rajatjindal", - "id": 612092, - "login": "rajatjindal", - "node_id": "MDQ6VXNlcjYxMjA5Mg==", - "organizations_url": "https://api.github.com/users/rajatjindal/orgs", - "received_events_url": "https://api.github.com/users/rajatjindal/received_events", - "repos_url": "https://api.github.com/users/rajatjindal/repos", - "site_admin": false, - "starred_url": "https://api.github.com/users/rajatjindal/starred{/owner}{/repo}", - "subscriptions_url": "https://api.github.com/users/rajatjindal/subscriptions", - "type": "User", - "url": "https://api.github.com/users/rajatjindal" - } - }`) diff --git a/pkg/actions/local_action.go b/pkg/actions/local_action.go new file mode 100644 index 0000000..bf6e3d5 --- /dev/null +++ b/pkg/actions/local_action.go @@ -0,0 +1,331 @@ +package actions + +import "os" + +//LocalAction for local action +type LocalAction struct{} + +//GetActionData returns data to run on local laptop +func (l LocalAction) GetActionData() (ActionData, error) { + releaseInfo, err := getReleaseInfo(l.getPayload()) + if err != nil { + return ActionData{}, err + } + + return ActionData{ + Workspace: "", + Actor: "rajatjindal", + Repo: "krew-plugin-release", + RepoOwner: "rajatjindal", + ReleaseInfo: releaseInfo, + Inputs: Inputs{ + PluginName: "modify-secret", + TokenUserHandle: "rajatjindal", + Token: os.Getenv("KREW_PLUGIN_RELEASE_TOKEN"), + TokenUserEmail: "rajatjindal83@gmail.com", + TokenUserName: "Rajat Jindal", + UpstreamKrewIndexOwner: "rajatjin", + UpstreamKrewIndexRepoName: "krew-index", + }, + Derived: Derived{ + UpstreamCloneURL: getRepoURL("rajatjin", "krew-index"), + LocalCloneURL: getRepoURL("rajatjindal", "krew-index"), + }, + }, nil +} + +func (l LocalAction) getPayload() []byte { + return []byte(`{ + "action": "published", + "release": { + "assets": [ + { + "browser_download_url": "https://github.com/rajatjindal/krew-plugin-release/releases/download/v0.0.6/darwin-amd64-v0.0.6.tar.gz", + "content_type": "application/x-gzip", + "created_at": "2019-09-30T10:51:23Z", + "download_count": 0, + "id": 15204574, + "label": null, + "name": "darwin-amd64-v0.0.6.tar.gz", + "node_id": "MDEyOlJlbGVhc2VBc3NldDE1MjA0NTc0", + "size": 9150835, + "state": "uploaded", + "updated_at": "2019-09-30T10:51:43Z", + "uploader": { + "avatar_url": "https://avatars2.githubusercontent.com/u/612092?v=4", + "events_url": "https://api.github.com/users/rajatjindal/events{/privacy}", + "followers_url": "https://api.github.com/users/rajatjindal/followers", + "following_url": "https://api.github.com/users/rajatjindal/following{/other_user}", + "gists_url": "https://api.github.com/users/rajatjindal/gists{/gist_id}", + "gravatar_id": "", + "html_url": "https://github.com/rajatjindal", + "id": 612092, + "login": "rajatjindal", + "node_id": "MDQ6VXNlcjYxMjA5Mg==", + "organizations_url": "https://api.github.com/users/rajatjindal/orgs", + "received_events_url": "https://api.github.com/users/rajatjindal/received_events", + "repos_url": "https://api.github.com/users/rajatjindal/repos", + "site_admin": false, + "starred_url": "https://api.github.com/users/rajatjindal/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/rajatjindal/subscriptions", + "type": "User", + "url": "https://api.github.com/users/rajatjindal" + }, + "url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/releases/assets/15204574" + }, + { + "browser_download_url": "https://github.com/rajatjindal/krew-plugin-release/releases/download/v0.0.6/linux-amd64-v0.0.6.tar.gz", + "content_type": "application/x-gzip", + "created_at": "2019-09-30T10:50:34Z", + "download_count": 0, + "id": 15204570, + "label": null, + "name": "linux-amd64-v0.0.6.tar.gz", + "node_id": "MDEyOlJlbGVhc2VBc3NldDE1MjA0NTcw", + "size": 8657197, + "state": "uploaded", + "updated_at": "2019-09-30T10:51:02Z", + "uploader": { + "avatar_url": "https://avatars2.githubusercontent.com/u/612092?v=4", + "events_url": "https://api.github.com/users/rajatjindal/events{/privacy}", + "followers_url": "https://api.github.com/users/rajatjindal/followers", + "following_url": "https://api.github.com/users/rajatjindal/following{/other_user}", + "gists_url": "https://api.github.com/users/rajatjindal/gists{/gist_id}", + "gravatar_id": "", + "html_url": "https://github.com/rajatjindal", + "id": 612092, + "login": "rajatjindal", + "node_id": "MDQ6VXNlcjYxMjA5Mg==", + "organizations_url": "https://api.github.com/users/rajatjindal/orgs", + "received_events_url": "https://api.github.com/users/rajatjindal/received_events", + "repos_url": "https://api.github.com/users/rajatjindal/repos", + "site_admin": false, + "starred_url": "https://api.github.com/users/rajatjindal/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/rajatjindal/subscriptions", + "type": "User", + "url": "https://api.github.com/users/rajatjindal" + }, + "url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/releases/assets/15204570" + }, + { + "browser_download_url": "https://github.com/rajatjindal/krew-plugin-release/releases/download/v0.0.6/sha256-darwin-amd64-v0.0.6", + "content_type": "application/octet-stream", + "created_at": "2019-09-30T10:51:23Z", + "download_count": 0, + "id": 15204575, + "label": null, + "name": "sha256-darwin-amd64-v0.0.6", + "node_id": "MDEyOlJlbGVhc2VBc3NldDE1MjA0NTc1", + "size": 65, + "state": "uploaded", + "updated_at": "2019-09-30T10:51:44Z", + "uploader": { + "avatar_url": "https://avatars2.githubusercontent.com/u/612092?v=4", + "events_url": "https://api.github.com/users/rajatjindal/events{/privacy}", + "followers_url": "https://api.github.com/users/rajatjindal/followers", + "following_url": "https://api.github.com/users/rajatjindal/following{/other_user}", + "gists_url": "https://api.github.com/users/rajatjindal/gists{/gist_id}", + "gravatar_id": "", + "html_url": "https://github.com/rajatjindal", + "id": 612092, + "login": "rajatjindal", + "node_id": "MDQ6VXNlcjYxMjA5Mg==", + "organizations_url": "https://api.github.com/users/rajatjindal/orgs", + "received_events_url": "https://api.github.com/users/rajatjindal/received_events", + "repos_url": "https://api.github.com/users/rajatjindal/repos", + "site_admin": false, + "starred_url": "https://api.github.com/users/rajatjindal/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/rajatjindal/subscriptions", + "type": "User", + "url": "https://api.github.com/users/rajatjindal" + }, + "url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/releases/assets/15204575" + }, + { + "browser_download_url": "https://github.com/rajatjindal/krew-plugin-release/releases/download/v0.0.6/sha256-linux-amd64-v0.0.6", + "content_type": "application/octet-stream", + "created_at": "2019-09-30T10:50:35Z", + "download_count": 0, + "id": 15204571, + "label": null, + "name": "sha256-linux-amd64-v0.0.6", + "node_id": "MDEyOlJlbGVhc2VBc3NldDE1MjA0NTcx", + "size": 65, + "state": "uploaded", + "updated_at": "2019-09-30T10:51:02Z", + "uploader": { + "avatar_url": "https://avatars2.githubusercontent.com/u/612092?v=4", + "events_url": "https://api.github.com/users/rajatjindal/events{/privacy}", + "followers_url": "https://api.github.com/users/rajatjindal/followers", + "following_url": "https://api.github.com/users/rajatjindal/following{/other_user}", + "gists_url": "https://api.github.com/users/rajatjindal/gists{/gist_id}", + "gravatar_id": "", + "html_url": "https://github.com/rajatjindal", + "id": 612092, + "login": "rajatjindal", + "node_id": "MDQ6VXNlcjYxMjA5Mg==", + "organizations_url": "https://api.github.com/users/rajatjindal/orgs", + "received_events_url": "https://api.github.com/users/rajatjindal/received_events", + "repos_url": "https://api.github.com/users/rajatjindal/repos", + "site_admin": false, + "starred_url": "https://api.github.com/users/rajatjindal/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/rajatjindal/subscriptions", + "type": "User", + "url": "https://api.github.com/users/rajatjindal" + }, + "url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/releases/assets/15204571" + } + ], + "assets_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/releases/20348112/assets", + "author": { + "avatar_url": "https://avatars2.githubusercontent.com/u/612092?v=4", + "events_url": "https://api.github.com/users/rajatjindal/events{/privacy}", + "followers_url": "https://api.github.com/users/rajatjindal/followers", + "following_url": "https://api.github.com/users/rajatjindal/following{/other_user}", + "gists_url": "https://api.github.com/users/rajatjindal/gists{/gist_id}", + "gravatar_id": "", + "html_url": "https://github.com/rajatjindal", + "id": 612092, + "login": "rajatjindal", + "node_id": "MDQ6VXNlcjYxMjA5Mg==", + "organizations_url": "https://api.github.com/users/rajatjindal/orgs", + "received_events_url": "https://api.github.com/users/rajatjindal/received_events", + "repos_url": "https://api.github.com/users/rajatjindal/repos", + "site_admin": false, + "starred_url": "https://api.github.com/users/rajatjindal/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/rajatjindal/subscriptions", + "type": "User", + "url": "https://api.github.com/users/rajatjindal" + }, + "body": "v0.0.6", + "created_at": "2019-09-29T15:14:53Z", + "draft": false, + "html_url": "https://github.com/rajatjindal/krew-plugin-release/releases/tag/v0.0.6", + "id": 20348112, + "name": "release v0.0.6", + "node_id": "MDc6UmVsZWFzZTIwMzQ4MTEy", + "prerelease": false, + "published_at": "2019-09-30T10:51:48Z", + "tag_name": "v0.0.6", + "tarball_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/tarball/v0.0.6", + "target_commitish": "master", + "upload_url": "https://uploads.github.com/repos/rajatjindal/krew-plugin-release/releases/20348112/assets{?name,label}", + "url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/releases/20348112", + "zipball_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/zipball/v0.0.6" + }, + "repository": { + "archive_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/{archive_format}{/ref}", + "archived": false, + "assignees_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/assignees{/user}", + "blobs_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/git/blobs{/sha}", + "branches_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/branches{/branch}", + "clone_url": "https://github.com/rajatjindal/krew-plugin-release.git", + "collaborators_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/collaborators{/collaborator}", + "comments_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/comments{/number}", + "commits_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/commits{/sha}", + "compare_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/compare/{base}...{head}", + "contents_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/contents/{+path}", + "contributors_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/contributors", + "created_at": "2019-09-29T10:03:17Z", + "default_branch": "master", + "deployments_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/deployments", + "description": null, + "disabled": false, + "downloads_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/downloads", + "events_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/events", + "fork": false, + "forks": 0, + "forks_count": 0, + "forks_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/forks", + "full_name": "rajatjindal/krew-plugin-release", + "git_commits_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/git/commits{/sha}", + "git_refs_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/git/refs{/sha}", + "git_tags_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/git/tags{/sha}", + "git_url": "git://github.com/rajatjindal/krew-plugin-release.git", + "has_downloads": true, + "has_issues": true, + "has_pages": false, + "has_projects": true, + "has_wiki": true, + "homepage": null, + "hooks_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/hooks", + "html_url": "https://github.com/rajatjindal/krew-plugin-release", + "id": 211642796, + "issue_comment_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/issues/comments{/number}", + "issue_events_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/issues/events{/number}", + "issues_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/issues{/number}", + "keys_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/keys{/key_id}", + "labels_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/labels{/name}", + "language": "Go", + "languages_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/languages", + "license": null, + "merges_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/merges", + "milestones_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/milestones{/number}", + "mirror_url": null, + "name": "krew-plugin-release", + "node_id": "MDEwOlJlcG9zaXRvcnkyMTE2NDI3OTY=", + "notifications_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/notifications{?since,all,participating}", + "open_issues": 0, + "open_issues_count": 0, + "owner": { + "avatar_url": "https://avatars2.githubusercontent.com/u/612092?v=4", + "events_url": "https://api.github.com/users/rajatjindal/events{/privacy}", + "followers_url": "https://api.github.com/users/rajatjindal/followers", + "following_url": "https://api.github.com/users/rajatjindal/following{/other_user}", + "gists_url": "https://api.github.com/users/rajatjindal/gists{/gist_id}", + "gravatar_id": "", + "html_url": "https://github.com/rajatjindal", + "id": 612092, + "login": "rajatjindal", + "node_id": "MDQ6VXNlcjYxMjA5Mg==", + "organizations_url": "https://api.github.com/users/rajatjindal/orgs", + "received_events_url": "https://api.github.com/users/rajatjindal/received_events", + "repos_url": "https://api.github.com/users/rajatjindal/repos", + "site_admin": false, + "starred_url": "https://api.github.com/users/rajatjindal/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/rajatjindal/subscriptions", + "type": "User", + "url": "https://api.github.com/users/rajatjindal" + }, + "private": true, + "pulls_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/pulls{/number}", + "pushed_at": "2019-09-30T10:51:48Z", + "releases_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/releases{/id}", + "size": 1238, + "ssh_url": "git@github.com:rajatjindal/krew-plugin-release.git", + "stargazers_count": 0, + "stargazers_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/stargazers", + "statuses_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/statuses/{sha}", + "subscribers_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/subscribers", + "subscription_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/subscription", + "svn_url": "https://github.com/rajatjindal/krew-plugin-release", + "tags_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/tags", + "teams_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/teams", + "trees_url": "https://api.github.com/repos/rajatjindal/krew-plugin-release/git/trees{/sha}", + "updated_at": "2019-09-29T15:15:04Z", + "url": "https://api.github.com/repos/rajatjindal/krew-plugin-release", + "watchers": 0, + "watchers_count": 0 + }, + "sender": { + "avatar_url": "https://avatars2.githubusercontent.com/u/612092?v=4", + "events_url": "https://api.github.com/users/rajatjindal/events{/privacy}", + "followers_url": "https://api.github.com/users/rajatjindal/followers", + "following_url": "https://api.github.com/users/rajatjindal/following{/other_user}", + "gists_url": "https://api.github.com/users/rajatjindal/gists{/gist_id}", + "gravatar_id": "", + "html_url": "https://github.com/rajatjindal", + "id": 612092, + "login": "rajatjindal", + "node_id": "MDQ6VXNlcjYxMjA5Mg==", + "organizations_url": "https://api.github.com/users/rajatjindal/orgs", + "received_events_url": "https://api.github.com/users/rajatjindal/received_events", + "repos_url": "https://api.github.com/users/rajatjindal/repos", + "site_admin": false, + "starred_url": "https://api.github.com/users/rajatjindal/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/rajatjindal/subscriptions", + "type": "User", + "url": "https://api.github.com/users/rajatjindal" + } + }`) +} diff --git a/pkg/actions/release_info.go b/pkg/actions/release_info.go index 2919f96..d59da42 100644 --- a/pkg/actions/release_info.go +++ b/pkg/actions/release_info.go @@ -6,13 +6,8 @@ import ( "github.com/google/go-github/github" ) -//GetReleaseInfo gets the release info -func GetReleaseInfo(action ActionData) (*github.RepositoryRelease, error) { - payload, err := action.GetPayload() - if err != nil { - return nil, err - } - +//getReleaseInfo gets the release info +func getReleaseInfo(payload []byte) (*github.RepositoryRelease, error) { e, err := github.ParseWebHook("release", payload) if err != nil { return nil, err @@ -28,7 +23,7 @@ func GetReleaseInfo(action ActionData) (*github.RepositoryRelease, error) { } if len(event.Release.Assets) == 0 { - return nil, fmt.Errorf("no assets found") + return nil, fmt.Errorf("no release assets found") } return event.Release, nil diff --git a/pkg/actions/repo_info.go b/pkg/actions/repo_info.go deleted file mode 100644 index a15c980..0000000 --- a/pkg/actions/repo_info.go +++ /dev/null @@ -1,28 +0,0 @@ -package actions - -import ( - "fmt" - "net/url" - - "github.com/go-resty/resty" -) - -//RepoExists checks if repo exists -func RepoExists(owner, repo string) (bool, error) { - u := url.URL{ - Host: "github.com", - Scheme: "https", - Path: fmt.Sprintf("%s/%s", owner, repo), - } - - resp, err := resty.New().R().Get(u.String()) - if err != nil { - return false, err - } - - if resp.IsError() { - return false, fmt.Errorf("received status %d from %s", resp.StatusCode(), u.String()) - } - - return true, nil -} diff --git a/pkg/git/git.go b/pkg/git/git.go deleted file mode 100644 index 9f81183..0000000 --- a/pkg/git/git.go +++ /dev/null @@ -1,136 +0,0 @@ -package git - -import ( - "fmt" - "os" - "time" - - "gopkg.in/src-d/go-git.v4/plumbing/transport" - - "gopkg.in/src-d/go-git.v4" - ugit "gopkg.in/src-d/go-git.v4" - "gopkg.in/src-d/go-git.v4/config" - "gopkg.in/src-d/go-git.v4/plumbing" - "gopkg.in/src-d/go-git.v4/plumbing/object" - githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http" -) - -func getAuth() transport.AuthMethod { - return &githttp.BasicAuth{ - Username: "rjindal", - Password: os.Getenv("KREW_PLUGIN_RELEASE_TOKEN"), - } -} - -//Repository represents upstream repo -type Repository ugit.Repository - -//Clone clones the repo -func Clone(remoteURL, remoteName, branch, dir string) (*ugit.Repository, error) { - return ugit.PlainClone(dir, false, &ugit.CloneOptions{ - URL: remoteURL, - Progress: os.Stdout, - ReferenceName: plumbing.ReferenceName(branch), - SingleBranch: true, - Auth: getAuth(), - RemoteName: remoteName, - }) -} - -//GetMasterBranchRefs gets the branch name -func GetMasterBranchRefs() string { - return string(plumbing.Master) -} - -//AddUpstream adds the upstream -func AddUpstream(repo *ugit.Repository, remoteName, remoteURL string) (*ugit.Remote, error) { - return repo.CreateRemote(&config.RemoteConfig{ - Name: remoteName, - URLs: []string{remoteURL}, - }) -} - -//FetchUpstream fetches the upstream -func FetchUpstream(remote *ugit.Remote, remoteName string) error { - return remote.Fetch(&ugit.FetchOptions{ - RemoteName: remoteName, - }) -} - -//CreateBranch creates branch -func CreateBranch(repo *ugit.Repository, branchName string) error { - w, err := repo.Worktree() - if err != nil { - return err - } - - // First try to create branch - err = w.Checkout(&git.CheckoutOptions{ - Create: true, - Force: false, - Branch: plumbing.NewBranchReferenceName(branchName), - }) - - if err == nil { - return nil - } - - //may be it already exists - return w.Checkout(&git.CheckoutOptions{ - Create: false, - Force: false, - Branch: plumbing.NewBranchReferenceName(branchName), - }) -} - -//CheckoutBranch checksout branch -func CheckoutBranch(repo *ugit.Repository, branchName string) error { - w, err := repo.Worktree() - if err != nil { - return err - } - - return w.Checkout(&ugit.CheckoutOptions{ - Branch: plumbing.NewBranchReferenceName(branchName), - }) -} - -//AddCommitAndPush commits and push -func AddCommitAndPush(repo *ugit.Repository, commitMsg, remoteName, branchName string) error { - w, err := repo.Worktree() - if err != nil { - return err - } - - w.Add(".") - _, err = w.Commit(commitMsg, &git.CommitOptions{ - Author: &object.Signature{ - Name: "Rajat Jindal", - Email: "rajatjindal83@gmail.com", - When: time.Now(), - }, - }) - - return repo.Push(&ugit.PushOptions{ - RemoteName: remoteName, - RefSpecs: []config.RefSpec{getRefSpec(remoteName, branchName)}, - Auth: getAuth(), - }) -} - -//PullRebase rebases from pull -func PullRebase(repo *ugit.Repository, remoteName, branchName string) error { - w, err := repo.Worktree() - if err != nil { - return err - } - - return w.Pull(&git.PullOptions{ - RemoteName: remoteName, - Auth: getAuth(), - }) -} - -func getRefSpec(remoteName, branchName string) config.RefSpec { - return config.RefSpec(fmt.Sprintf("refs/heads/%s:refs/heads/%s/%s", branchName, remoteName, branchName)) -} diff --git a/vendor/gopkg.in/src-d/go-git.v4/go.mod b/vendor/gopkg.in/src-d/go-git.v4/go.mod index 31907a6..6f8b3d2 100644 --- a/vendor/gopkg.in/src-d/go-git.v4/go.mod +++ b/vendor/gopkg.in/src-d/go-git.v4/go.mod @@ -27,5 +27,3 @@ require ( gopkg.in/src-d/go-git-fixtures.v3 v3.5.0 gopkg.in/warnings.v0 v0.1.2 // indirect ) - -go 1.13