From de1f607f40ee7cb84afa32ab5e2cbb64fd42db20 Mon Sep 17 00:00:00 2001 From: Rajat Jindal Date: Sat, 5 Oct 2019 16:07:03 +0530 Subject: [PATCH] rewrite --- .krew.yaml | 41 +++ cmd/helper.go | 172 ++++++++++++ cmd/root.go | 123 ++------ pkg/actions/actions.go | 17 +- pkg/actions/fake_actions.go | 341 ----------------------- pkg/actions/local_action.go | 5 +- pkg/actions/release_info.go | 11 +- pkg/actions/repo_info.go | 28 -- pkg/git/git.go | 136 --------- vendor/github.com/sirupsen/logrus/go.mod | 2 + 10 files changed, 259 insertions(+), 617 deletions(-) create mode 100644 .krew.yaml create mode 100644 cmd/helper.go delete mode 100644 pkg/actions/fake_actions.go delete mode 100644 pkg/actions/repo_info.go delete mode 100644 pkg/git/git.go diff --git a/.krew.yaml b/.krew.yaml new file mode 100644 index 0000000..5a6d6ab --- /dev/null +++ b/.krew.yaml @@ -0,0 +1,41 @@ +apiVersion: krew.googlecontainertools.github.com/v1alpha2 +kind: Plugin +metadata: + name: modify-secret +spec: + version: "{{ .TagName }}" + platforms: + - selector: + matchLabels: + os: darwin + arch: amd64 + {{addURIAndSha "https://github.com/rajatjindal/kubectl-modify-secret/releases/download/{{ .TagName }}/darwin-amd64-{{ .TagName }}.tar.gz" .TagName }} + files: + - from: "*" + to: "." + bin: kubectl-modify-secret + - selector: + matchLabels: + os: linux + arch: amd64 + {{addURIAndSha "https://github.com/rajatjindal/kubectl-modify-secret/releases/download/{{ .TagName }}/linux-amd64-{{ .TagName }}.tar.gz" .TagName }} + files: + - from: "*" + to: "." + bin: kubectl-modify-secret + shortDescription: modify secret with implicit base64 translations + caveats: | + Usage: + kubectl modify-secret secret-name -n kube-system + + Read the documentation at: + https://github.com/rajatjindal/kubectl-modify-secret + description: | + Usage: + kubectl modify-secret secret-name -n kube-system + + This plugin fetches the given secret manifest from the cluster, decodes + the payload, opens an editor to make changes, and applies the modified + manifest when done. + homepage: https://github.com/rajatjindal/kubectl-modify-secret + diff --git a/cmd/helper.go b/cmd/helper.go new file mode 100644 index 0000000..d8eb96a --- /dev/null +++ b/cmd/helper.go @@ -0,0 +1,172 @@ +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), + } + + 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..3a15628 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,140 +1,77 @@ 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{ Use: "krew-plugin-release", 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) + action := actions.LocalAction{} + 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 928a291..c45a7ec 100644 --- a/pkg/actions/actions.go +++ b/pkg/actions/actions.go @@ -22,15 +22,21 @@ type Inputs struct { UpstreamKrewIndexOwner string } +//Derived is derived data +type Derived struct { + UpstreamCloneURL string + LocalCloneURL string +} + //ActionData is action data type ActionData struct { Workspace string Actor string Repo string RepoOwner string - Payload []byte Inputs Inputs ReleaseInfo *github.RepositoryRelease + Derived Derived } //Action defines interface to get data from actions @@ -79,7 +85,6 @@ func (r RealAction) GetActionData() (ActionData, error) { Actor: os.Getenv("GITHUB_ACTOR"), Repo: strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")[1], RepoOwner: strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")[0], - Payload: payload, ReleaseInfo: releaseInfo, Inputs: Inputs{ PluginName: r.getInputForAction("plugin-name"), @@ -90,9 +95,17 @@ func (r RealAction) GetActionData() (ActionData, error) { UpstreamKrewIndexOwner: upstreamKrewIndexRepoOwner, UpstreamKrewIndexRepoName: upstreamKrewIndexRepoName, }, + Derived: Derived{ + UpstreamCloneURL: getRepoURL(upstreamKrewIndexRepoOwner, upstreamKrewIndexRepoName), + LocalCloneURL: getRepoURL(strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")[10], strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")[1]), + }, }, nil } +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 { return os.Getenv(fmt.Sprintf("INPUT_%s", strings.ToUpper(key))) 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 index 741577f..bf6e3d5 100644 --- a/pkg/actions/local_action.go +++ b/pkg/actions/local_action.go @@ -17,7 +17,6 @@ func (l LocalAction) GetActionData() (ActionData, error) { Actor: "rajatjindal", Repo: "krew-plugin-release", RepoOwner: "rajatjindal", - Payload: l.getPayload(), ReleaseInfo: releaseInfo, Inputs: Inputs{ PluginName: "modify-secret", @@ -28,6 +27,10 @@ func (l LocalAction) GetActionData() (ActionData, error) { UpstreamKrewIndexOwner: "rajatjin", UpstreamKrewIndexRepoName: "krew-index", }, + Derived: Derived{ + UpstreamCloneURL: getRepoURL("rajatjin", "krew-index"), + LocalCloneURL: getRepoURL("rajatjindal", "krew-index"), + }, }, nil } 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/github.com/sirupsen/logrus/go.mod b/vendor/github.com/sirupsen/logrus/go.mod index 12fdf98..ea26622 100644 --- a/vendor/github.com/sirupsen/logrus/go.mod +++ b/vendor/github.com/sirupsen/logrus/go.mod @@ -8,3 +8,5 @@ require ( github.com/stretchr/testify v1.2.2 golang.org/x/sys v0.0.0-20190422165155-953cdadca894 ) + +go 1.13