Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ $ docker run -v /path/to/your/template-file.yaml:/tmp/template-file.yaml ghcr.io

# Inputs for the action

| Key | Default Value | Description |
| ------------------ | ---------------------- | ------------------------------------------------------------------------------------ |
| workdir | `env.GITHUB_WORKSPACE` | Overrides the GitHub workspace directory path |
| krew_template_file | `.krew.yaml` | The path to template file relative to $workdir. e.g. templates/misc/plugin-name.yaml |
| Key | Default Value | Description |
| ------------------------------ | ---------------------- | ------------------------------------------------------------------------------------ |
| workdir | `env.GITHUB_WORKSPACE` | Overrides the GitHub workspace directory path |
| krew_template_file | `.krew.yaml` | The path to template file relative to $workdir. e.g. templates/misc/plugin-name.yaml |
| upstream_krew_index_repo_owner | `kubernetes-sigs` | Overrides the GitHub owner of the Krew index, defaults to kubernetes-sigs |
| upstream_krew_index_repo_name | `krew-index` | Overrides the GitHub repository of the Krew index, defaults to krew-index |

# Limitations of krew-release-bot

Expand Down
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ inputs:
description: "Working directory, defaults to env.GITHUB_WORKSPACE"
krew_template_file:
description: "the path to template file relative to $workdir. e.g. templates/misc/plugin-name.yaml. defaults to .krew.yaml"
upstream_krew_index_repo_owner:
description: 'The owner of the Krew index repository'
required: false
default: kubernetes-sigs
upstream_krew_index_repo_name:
description: 'The name of the Krew index repository'
required: false
default: krew-index
20 changes: 20 additions & 0 deletions pkg/cicd/circleci/circleci.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,23 @@ func (p *Provider) GetTemplateFile() string {

return filepath.Join(p.GetWorkDirectory(), ".krew.yaml")
}

// GetKrewIndexRepoName gets upstream_krew_index_repo_name
func (p *Provider) GetKrewIndexRepoName() string {
nameInput := getInputForAction("UPSTREAM_KREW_INDEX_REPO_NAME")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be consistent with other places, we should keep it same as the input as defined in action.yml, so kindly change it to upstream_krew_index_repo_name

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no problem. I will update the fields to be lowercase.

if nameInput != "" {
return nameInput
}

return os.Getenv("UPSTREAM_KREW_INDEX_REPO_NAME")
}

// GetKrewIndexRepoName gets upstream_krew_index_repo_owner
func (p *Provider) GetKrewIndexRepoOwner() string {
ownerInput := getInputForAction("UPSTREAM_KREW_INDEX_REPO_OWNER")
if ownerInput != "" {
return ownerInput
}

return os.Getenv("UPSTREAM_KREW_INDEX_REPO_OWNER")
}
20 changes: 20 additions & 0 deletions pkg/cicd/github/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,23 @@ func getReleaseForTag(client *github.Client, owner, repo, tag string) (*github.R

return release, nil
}

// GetKrewIndexRepoName gets upstream_krew_index_repo_name
func (p *Actions) GetKrewIndexRepoName() string {
nameInput := getInputForAction("UPSTREAM_KREW_INDEX_REPO_NAME")
if nameInput != "" {
return nameInput
}

return os.Getenv("UPSTREAM_KREW_INDEX_REPO_NAME")
}

// GetKrewIndexRepoName gets upstream_krew_index_repo_owner
func (p *Actions) GetKrewIndexRepoOwner() string {
ownerInput := getInputForAction("UPSTREAM_KREW_INDEX_REPO_OWNER")
if ownerInput != "" {
return ownerInput
}

return os.Getenv("UPSTREAM_KREW_INDEX_REPO_OWNER")
}
2 changes: 2 additions & 0 deletions pkg/cicd/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Provider interface {
GetWorkDirectory() string
GetTemplateFile() string
IsPreRelease(owner, repo, tag string) (bool, error)
GetKrewIndexRepoName() string
GetKrewIndexRepoOwner() string
}

// GetProvider returns the CI/CD provider
Expand Down
20 changes: 20 additions & 0 deletions pkg/cicd/travisci/travisci.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,23 @@ func (p *Provider) GetTemplateFile() string {

return filepath.Join(p.GetWorkDirectory(), ".krew.yaml")
}

// GetKrewIndexRepoName gets upstream_krew_index_repo_name
func (p *Provider) GetKrewIndexRepoName() string {
nameInput := getInputForAction("UPSTREAM_KREW_INDEX_REPO_NAME")
if nameInput != "" {
return nameInput
}

return os.Getenv("UPSTREAM_KREW_INDEX_REPO_NAME")
}

// GetKrewIndexRepoName gets upstream_krew_index_repo_owner
func (p *Provider) GetKrewIndexRepoOwner() string {
ownerInput := getInputForAction("UPSTREAM_KREW_INDEX_REPO_OWNER")
if ownerInput != "" {
return ownerInput
}

return os.Getenv("UPSTREAM_KREW_INDEX_REPO_OWNER")
}
28 changes: 22 additions & 6 deletions pkg/krew/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,42 @@ package krew
import "os"

const (
krewIndexRepoName = "krew-index"
krewIndexRepoOwner = "kubernetes-sigs"
krewIndexRepoName = "krew-index"
krewIndexRepoOwner = "kubernetes-sigs"
upstreamKrewIndexRepoName = "UPSTREAM_KREW_INDEX_REPO_NAME"
upstreamKrewIndexRepoOwner = "UPSTREAM_KREW_INDEX_REPO_OWNER"
)

//GetKrewIndexRepoName returns the krew-index repo name
// GetKrewIndexRepoName returns the krew-index repo name
func GetKrewIndexRepoName() string {
override := os.Getenv("UPSTREAM_KREW_INDEX_REPO_NAME")
override := os.Getenv(upstreamKrewIndexRepoName)
if override != "" {
return override
}

return krewIndexRepoName
}

//GetKrewIndexRepoOwner returns the krew-index repo owner
// GetKrewIndexRepoOwner returns the krew-index repo owner
func GetKrewIndexRepoOwner() string {
override := os.Getenv("UPSTREAM_KREW_INDEX_REPO_OWNER")
override := os.Getenv(upstreamKrewIndexRepoOwner)
if override != "" {
return override
}

return krewIndexRepoOwner
}

func SetKrewIndexRepoName(name string) string {
if name != "" && os.Getenv(upstreamKrewIndexRepoName) == "" {
return name
}
return GetKrewIndexRepoName()
}

func SetKrewIndexRepoOwner(owner string) string {
if owner != "" && os.Getenv(upstreamKrewIndexRepoOwner) == "" {
return owner
}
return GetKrewIndexRepoName()
}
6 changes: 3 additions & 3 deletions pkg/krew/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"sigs.k8s.io/krew/pkg/index/validation"
)

//ValidatePlugin validates the plugin spec
// ValidatePlugin validates the plugin spec
func ValidatePlugin(name, file string) error {
plugin, err := indexscanner.ReadPluginFile(file)
if err != nil {
Expand All @@ -18,7 +18,7 @@ func ValidatePlugin(name, file string) error {
return validation.ValidatePlugin(name, plugin)
}

//GetPluginName gets the plugin name from template .krew.yaml file
// GetPluginName gets the plugin name from template .krew.yaml file
func GetPluginName(spec []byte) (string, error) {
plugin, err := indexscanner.DecodePluginFile(bytes.NewReader(spec))
if err != nil {
Expand All @@ -28,7 +28,7 @@ func GetPluginName(spec []byte) (string, error) {
return plugin.GetName(), nil
}

//PluginFileName returns the plugin file with extension
// PluginFileName returns the plugin file with extension
func PluginFileName(name string) string {
return fmt.Sprintf("%s%s", name, ".yaml")
}
42 changes: 27 additions & 15 deletions pkg/releaser/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
"github.com/google/go-github/v66/github"
"github.com/rajatjindal/krew-release-bot/pkg/source"
"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"
Expand All @@ -28,12 +26,17 @@ const (
)

// CloneRepos clones the repo
func (r *Releaser) cloneRepos(dir string, request *source.ReleaseRequest) (*ugit.Repository, error) {
func (r *Releaser) cloneRepos(dir string, request *source.ReleaseRequest) (*git.Repository, error) {
logrus.Infof("Cloning %s", r.UpstreamKrewIndexRepoCloneURL)
repo, err := ugit.PlainClone(dir, false, &ugit.CloneOptions{
remoteRepo, err := r.getRepo(r.Client, r.UpstreamKrewIndexRepoOwner, r.UpstreamKrewIndexRepoName)
if err != nil {
return nil, err
}

repo, err := git.PlainClone(dir, false, &git.CloneOptions{
URL: r.UpstreamKrewIndexRepoCloneURL,
Progress: os.Stdout,
ReferenceName: plumbing.Master,
ReferenceName: plumbing.ReferenceName(*remoteRepo.DefaultBranch),
SingleBranch: true,
Auth: r.getAuth(),
RemoteName: OriginNameUpstream,
Expand Down Expand Up @@ -62,7 +65,7 @@ func (r *Releaser) cloneRepos(dir string, request *source.ReleaseRequest) (*ugit
}

// CreateBranch creates branch
func (r *Releaser) createBranch(repo *ugit.Repository, branchName string) error {
func (r *Releaser) createBranch(repo *git.Repository, branchName string) error {
w, err := repo.Worktree()
if err != nil {
return err
Expand Down Expand Up @@ -94,7 +97,7 @@ type commitConfig struct {
}

// AddCommitAndPush commits and push
func (r *Releaser) addCommitAndPush(repo *ugit.Repository, commit commitConfig, request *source.ReleaseRequest) error {
func (r *Releaser) addCommitAndPush(repo *git.Repository, commit commitConfig, request *source.ReleaseRequest) error {
w, err := repo.Worktree()
if err != nil {
return err
Expand All @@ -119,7 +122,7 @@ func (r *Releaser) addCommitAndPush(repo *ugit.Repository, commit commitConfig,
branchName := r.getBranchName(request)
pushRef := getPushRefSpec(*branchName)

return repo.Push(&ugit.PushOptions{
return repo.Push(&git.PushOptions{
RemoteName: commit.RemoteName,
RefSpecs: []config.RefSpec{config.RefSpec(pushRef)},
Auth: r.getAuth(),
Expand All @@ -130,30 +133,39 @@ func getPushRefSpec(branchName string) string {
return fmt.Sprintf("refs/heads/%s:refs/heads/%s", branchName, branchName)
}

func (r *Releaser) getRepo(client *github.Client, owner string, repoName string) (*github.Repository, error) {
repo, _, err := client.Repositories.Get(context.TODO(), owner, repoName)
if err != nil {
return nil, err
}
return repo, nil
}

// SubmitPR submits the PR
func (r *Releaser) submitPR(request *source.ReleaseRequest) (string, error) {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: r.Token})
tc := oauth2.NewClient(context.TODO(), ts)
client := github.NewClient(tc)
repo, err := r.getRepo(r.Client, r.UpstreamKrewIndexRepoOwner, r.UpstreamKrewIndexRepoName)
if err != nil {
return "", err
}

prr := &github.NewPullRequest{
Title: r.getTitle(request),
Head: r.getHead(request),
Base: github.String("master"),
Base: github.String(*repo.DefaultBranch),
Body: r.getPRBody(request),
}

logrus.Infof("creating pr with title %q, \nhead %q, \nbase %q, \nbody %q",
github.Stringify(r.getTitle(request)),
github.Stringify(r.getHead(request)),
"master",
github.Stringify(*repo.DefaultBranch),
github.Stringify(r.getPRBody(request)),
)

pr, _, err := client.PullRequests.Create(
pr, _, err := r.Client.PullRequests.Create(
context.TODO(),
r.UpstreamKrewIndexRepoOwner,
r.UpstreamKrewIndexRepo,
r.UpstreamKrewIndexRepoName,
prr,
)
if err != nil {
Expand Down
14 changes: 12 additions & 2 deletions pkg/releaser/releaser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ import (
"net/http"

"github.com/aws/aws-lambda-go/events"
"github.com/google/go-github/v66/github"
"github.com/pkg/errors"
"github.com/rajatjindal/krew-release-bot/pkg/krew"
"github.com/rajatjindal/krew-release-bot/pkg/source/actions"
"golang.org/x/oauth2"
)

// Releaser is what opens PR
type Releaser struct {
Client *github.Client
Token string
TokenEmail string
TokenUserHandle string
TokenUsername string
UpstreamKrewIndexRepo string
UpstreamKrewIndexRepoName string
UpstreamKrewIndexRepoOwner string
UpstreamKrewIndexRepoCloneURL string
LocalKrewIndexRepo string
Expand All @@ -29,6 +32,12 @@ func getCloneURL(owner, repo string) string {
return fmt.Sprintf("https://github.com/%s/%s.git", owner, repo)
}

func getGitHubClient(token string) *github.Client {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
tc := oauth2.NewClient(context.TODO(), ts)
return github.NewClient(tc)
}

// TODO: get email, userhandle, name from token
func getUserDetails(_ string) (string, string, string) {
return "krew-release-bot", "Krew Release Bot", "krewpluginreleasebot@gmail.com"
Expand All @@ -39,11 +48,12 @@ func New(ghToken string) *Releaser {
tokenUserHandle, tokenUsername, tokenEmail := getUserDetails(ghToken)

return &Releaser{
Client: getGitHubClient(ghToken),
Token: ghToken,
TokenEmail: tokenEmail,
TokenUserHandle: tokenUserHandle,
TokenUsername: tokenUsername,
UpstreamKrewIndexRepo: krew.GetKrewIndexRepoName(),
UpstreamKrewIndexRepoName: krew.GetKrewIndexRepoName(),
UpstreamKrewIndexRepoOwner: krew.GetKrewIndexRepoOwner(),
UpstreamKrewIndexRepoCloneURL: getCloneURL(krew.GetKrewIndexRepoOwner(), krew.GetKrewIndexRepoName()),
LocalKrewIndexRepo: krew.GetKrewIndexRepoName(),
Expand Down
4 changes: 4 additions & 0 deletions pkg/releaser/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func (releaser *Releaser) Release(request *source.ReleaseRequest) (string, error
return "", err
}
defer os.RemoveAll(tempdir)
// Check krew-index inputs
releaser.UpstreamKrewIndexRepoName = krew.SetKrewIndexRepoName(request.KrewIndexName)
releaser.UpstreamKrewIndexRepoOwner = krew.SetKrewIndexRepoOwner(request.KrewIndexOwner)
releaser.UpstreamKrewIndexRepoCloneURL = getCloneURL(releaser.UpstreamKrewIndexRepoOwner, releaser.UpstreamKrewIndexRepoName)

logrus.Infof("will operate in tempdir %s", tempdir)
repo, err := releaser.cloneRepos(tempdir, request)
Expand Down
8 changes: 8 additions & 0 deletions pkg/source/actions/action_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,20 @@ func RunAction() error {
templateFile := provider.GetTemplateFile()
logrus.Infof("using template file %q", templateFile)

krewIndexOwner := provider.GetKrewIndexRepoOwner()
logrus.Infof("using krew-index owner %q", krewIndexOwner)

krewIndexName := provider.GetKrewIndexRepoName()
logrus.Infof("using krew-index name %q", krewIndexName)

releaseRequest := &source.ReleaseRequest{
TagName: tag,
PluginOwner: owner,
PluginRepo: repo,
PluginReleaseActor: actor,
TemplateFile: templateFile,
KrewIndexName: krewIndexName,
KrewIndexOwner: krewIndexOwner,
}

pluginName, pluginManifest, err := source.ProcessTemplate(templateFile, releaseRequest)
Expand Down
6 changes: 4 additions & 2 deletions pkg/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"net/http"
)

//Source is a release source interface
// Source is a release source interface
type Source interface {
Parse(r *http.Request) (*ReleaseRequest, error)
}

//ReleaseRequest is the release request for new plugin
// ReleaseRequest is the release request for new plugin
type ReleaseRequest struct {
TagName string `json:"tagName"`
PluginName string `json:"pluginName"`
Expand All @@ -18,4 +18,6 @@ type ReleaseRequest struct {
PluginReleaseActor string `json:"pluginReleaseActor"`
TemplateFile string `json:"templateFile"`
ProcessedTemplate []byte `json:"processedTemplate"`
KrewIndexName string `json:"krewIndexName"`
KrewIndexOwner string `json:"krewIndexOwner"`
}
Loading