Skip to content

Commit

Permalink
adapt krel/ff to run with other repositories that are not only k/k
Browse files Browse the repository at this point in the history
Signed-off-by: cpanato <ctadeu@gmail.com>
  • Loading branch information
cpanato committed Feb 3, 2024
1 parent e4a7477 commit 2fcfea6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
4 changes: 3 additions & 1 deletion cmd/krel/cmd/ff.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ Google Cloud Build job.
}

func init() {
ffCmd.PersistentFlags().StringVar(&ffOpts.RepoPath, "repo", filepath.Join(os.TempDir(), "k8s"), "the local path to the repository to be used")
ffCmd.PersistentFlags().StringVar(&ffOpts.RepoPath, "repo-path", filepath.Join(os.TempDir(), "k8s"), "the local path to the repository to be used")
ffCmd.PersistentFlags().StringVar(&ffOpts.GitHubOrg, "github-org", release.GetK8sOrg(), "the GitHub Organization to be used do the initial clone")
ffCmd.PersistentFlags().StringVar(&ffOpts.GitHubRepo, "github-repo", release.GetK8sRepo(), "the GitHub Repository to be used do the initial clone")
ffCmd.PersistentFlags().StringVar(&ffOpts.Branch, "branch", "", "branch")
ffCmd.PersistentFlags().StringVar(&ffOpts.MainRef, "ref", kgit.Remotify(kgit.DefaultBranch), "ref on the main branch")
ffCmd.PersistentFlags().StringVar(&ffOpts.GCPProjectID, "project-id", release.DefaultRelengStagingTestProject, "Google Cloud Project to use to submit the job")
Expand Down
54 changes: 30 additions & 24 deletions pkg/fastforward/fastforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ import (

// Options is the main structure for configuring a fast forward.
type Options struct {
// GitHubOrg is the GitHub Organization to be used do the initial clone.
GitHubOrg string

// GitHubRepo is the GitHub Repository to be used do the initial clone.
GitHubRepo string

// Branch is the release branch to be fast forwarded.
Branch string

Expand Down Expand Up @@ -91,7 +97,7 @@ func (f *FastForward) Run() (err error) {
return f.Submit(options)
}

repo, err := f.prepareKubernetesRepo()
repo, err := f.prepareFastForwardRepo()
if err != nil {
return fmt.Errorf("prepare repository: %w", err)
}
Expand Down Expand Up @@ -236,7 +242,7 @@ func (f *FastForward) Run() (err error) {
return fmt.Errorf("get HEAD rev: %w", err)
}

prepushMessage(f.RepoDir(repo), branch, f.options.MainRef, releaseRev, headRev)
prepushMessage(f.RepoDir(repo), f.options.GitHubOrg, f.options.GitHubRepo, branch, f.options.MainRef, releaseRev, headRev)

pushUpstream := false
if f.options.NonInteractive {
Expand All @@ -258,7 +264,7 @@ func (f *FastForward) Run() (err error) {
return nil
}

func prepushMessage(gitRoot, branch, ref, releaseRev, headRev string) {
func prepushMessage(gitRoot, org, repo, branch, ref, releaseRev, headRev string) {
fmt.Printf(`Go look around in %s to make sure things look okay before pushing…
Check for files left uncommitted using:
Expand All @@ -281,8 +287,8 @@ func prepushMessage(gitRoot, branch, ref, releaseRev, headRev string) {
gitRoot,
git.Remotify(branch),
ref,
git.DefaultGithubOrg,
git.DefaultGithubRepo,
org,
repo,
releaseRev,
headRev,
)
Expand All @@ -303,41 +309,40 @@ func (f *FastForward) branchToVersion(branch string) string {
return fmt.Sprintf("v%s.0", strings.TrimPrefix(branch, "release-"))
}

func (f *FastForward) prepareKubernetesRepo() (*git.Repo, error) {
logrus.Infof("Preparing to fast-forward from %s", f.options.MainRef)
func (f *FastForward) prepareFastForwardRepo() (*git.Repo, error) {
logrus.Infof("Preparing to %s/%s fast-forward from %s", f.options.GitHubOrg, f.options.GitHubRepo, f.options.MainRef)

token := f.EnvDefault(github.TokenEnvKey, "")

useSSH := true
stringMsg := "using SSH"
if token != "" {
logrus.Info("Found GitHub token, using it for repository interactions")
k8sOrg := release.GetK8sOrg()
k8sRepo := release.GetK8sRepo()
useSSH = false
stringMsg = "using HHTPs"
}

logrus.Info("Cloning repository by using HTTPs")
repo, err := f.CloneOrOpenGitHubRepo(f.options.RepoPath, k8sOrg, k8sRepo, false)
if err != nil {
return nil, fmt.Errorf("clone or open k/k GitHub repository: %w", err)
}
logrus.Infof("Cloning repository %s/%s %s", f.options.GitHubOrg, f.options.GitHubRepo, stringMsg)
repo, err := f.CloneOrOpenGitHubRepo(f.options.RepoPath, f.options.GitHubOrg, f.options.GitHubRepo, useSSH)
if err != nil {
return nil, fmt.Errorf("clone or open %s/%s GitHub repository: %w",
f.options.GitHubOrg, f.options.GitHubRepo, err,
)
}

if token != "" {
logrus.Info("Found GitHub token, using it for repository interactions")
if f.IsDefaultK8sUpstream() {
if err := f.RepoSetURL(repo, git.DefaultRemote, (&url.URL{
Scheme: "https",
User: url.UserPassword("git", token),
Host: "github.com",
Path: filepath.Join(git.DefaultGithubOrg, git.DefaultGithubRepo),
Path: filepath.Join(f.options.GitHubOrg, f.options.GitHubRepo),
}).String()); err != nil {
return nil, fmt.Errorf("changing git remote of repository: %w", err)
}
} else {
logrus.Info("Using non-default k8s upstream, doing no git modifications")
}

return repo, nil
}

logrus.Info("Cloning repository by using SSH")
repo, err := f.CloneOrOpenDefaultGitHubRepoSSH(f.options.RepoPath)
if err != nil {
return nil, fmt.Errorf("clone or open k/k GitHub repository: %w", err)
}

return repo, nil
Expand Down Expand Up @@ -368,5 +373,6 @@ func (f *FastForward) prepareToolRepo() error {
if err := f.Chdir(tmpPath); err != nil {
return fmt.Errorf("change directory: %w", err)
}

return nil
}
1 change: 0 additions & 1 deletion pkg/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ func GetK8sRef() string {
// GetK8sRef() point to their default values.
func IsDefaultK8sUpstream() bool {
return GetK8sOrg() == DefaultK8sOrg &&
GetK8sRepo() == DefaultK8sRepo &&
GetK8sRef() == DefaultK8sRef
}

Expand Down

0 comments on commit 2fcfea6

Please sign in to comment.