Skip to content

Commit 355b4aa

Browse files
authored
Merge pull request #3445 from cpanato/GH-3380
adapt krel/ff to run with other repositories that are not only k/k
2 parents f3dc9be + 2fe45f7 commit 355b4aa

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

cmd/krel/cmd/ff.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ Google Cloud Build job.
7575
}
7676

7777
func init() {
78-
ffCmd.PersistentFlags().StringVar(&ffOpts.RepoPath, "repo", filepath.Join(os.TempDir(), "k8s"), "the local path to the repository to be used")
78+
ffCmd.PersistentFlags().StringVar(&ffOpts.RepoPath, "repo-path", filepath.Join(os.TempDir(), "k8s"), "the local path to the repository to be used")
79+
ffCmd.PersistentFlags().StringVar(&ffOpts.GitHubOrg, "github-org", release.GetK8sOrg(), "the GitHub Organization to be used do the initial clone")
80+
ffCmd.PersistentFlags().StringVar(&ffOpts.GitHubRepo, "github-repo", release.GetK8sRepo(), "the GitHub Repository to be used do the initial clone")
7981
ffCmd.PersistentFlags().StringVar(&ffOpts.Branch, "branch", "", "branch")
8082
ffCmd.PersistentFlags().StringVar(&ffOpts.MainRef, "ref", kgit.Remotify(kgit.DefaultBranch), "ref on the main branch")
8183
ffCmd.PersistentFlags().StringVar(&ffOpts.GCPProjectID, "project-id", release.DefaultRelengStagingTestProject, "Google Cloud Project to use to submit the job")

pkg/fastforward/fastforward.go

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ import (
3232

3333
// Options is the main structure for configuring a fast forward.
3434
type Options struct {
35+
// GitHubOrg is the GitHub Organization to be used do the initial clone.
36+
GitHubOrg string
37+
38+
// GitHubRepo is the GitHub Repository to be used do the initial clone.
39+
GitHubRepo string
40+
3541
// Branch is the release branch to be fast forwarded.
3642
Branch string
3743

@@ -91,7 +97,7 @@ func (f *FastForward) Run() (err error) {
9197
return f.Submit(options)
9298
}
9399

94-
repo, err := f.prepareKubernetesRepo()
100+
repo, err := f.prepareFastForwardRepo()
95101
if err != nil {
96102
return fmt.Errorf("prepare repository: %w", err)
97103
}
@@ -236,7 +242,7 @@ func (f *FastForward) Run() (err error) {
236242
return fmt.Errorf("get HEAD rev: %w", err)
237243
}
238244

239-
prepushMessage(f.RepoDir(repo), branch, f.options.MainRef, releaseRev, headRev)
245+
prepushMessage(f.RepoDir(repo), f.options.GitHubOrg, f.options.GitHubRepo, branch, f.options.MainRef, releaseRev, headRev)
240246

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

261-
func prepushMessage(gitRoot, branch, ref, releaseRev, headRev string) {
267+
func prepushMessage(gitRoot, org, repo, branch, ref, releaseRev, headRev string) {
262268
fmt.Printf(`Go look around in %s to make sure things look okay before pushing…
263269
264270
Check for files left uncommitted using:
@@ -281,8 +287,8 @@ func prepushMessage(gitRoot, branch, ref, releaseRev, headRev string) {
281287
gitRoot,
282288
git.Remotify(branch),
283289
ref,
284-
git.DefaultGithubOrg,
285-
git.DefaultGithubRepo,
290+
org,
291+
repo,
286292
releaseRev,
287293
headRev,
288294
)
@@ -303,41 +309,40 @@ func (f *FastForward) branchToVersion(branch string) string {
303309
return fmt.Sprintf("v%s.0", strings.TrimPrefix(branch, "release-"))
304310
}
305311

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

309315
token := f.EnvDefault(github.TokenEnvKey, "")
316+
317+
useSSH := true
318+
stringMsg := "using SSH"
310319
if token != "" {
311-
logrus.Info("Found GitHub token, using it for repository interactions")
312-
k8sOrg := release.GetK8sOrg()
313-
k8sRepo := release.GetK8sRepo()
320+
useSSH = false
321+
stringMsg = "using HTTPs"
322+
}
314323

315-
logrus.Info("Cloning repository by using HTTPs")
316-
repo, err := f.CloneOrOpenGitHubRepo(f.options.RepoPath, k8sOrg, k8sRepo, false)
317-
if err != nil {
318-
return nil, fmt.Errorf("clone or open k/k GitHub repository: %w", err)
319-
}
324+
logrus.Infof("Cloning repository %s/%s %s", f.options.GitHubOrg, f.options.GitHubRepo, stringMsg)
325+
repo, err := f.CloneOrOpenGitHubRepo(f.options.RepoPath, f.options.GitHubOrg, f.options.GitHubRepo, useSSH)
326+
if err != nil {
327+
return nil, fmt.Errorf("clone or open %s/%s GitHub repository: %w",
328+
f.options.GitHubOrg, f.options.GitHubRepo, err,
329+
)
330+
}
320331

332+
if token != "" {
333+
logrus.Info("Found GitHub token, using it for repository interactions")
321334
if f.IsDefaultK8sUpstream() {
322335
if err := f.RepoSetURL(repo, git.DefaultRemote, (&url.URL{
323336
Scheme: "https",
324337
User: url.UserPassword("git", token),
325338
Host: "github.com",
326-
Path: filepath.Join(git.DefaultGithubOrg, git.DefaultGithubRepo),
339+
Path: filepath.Join(f.options.GitHubOrg, f.options.GitHubRepo),
327340
}).String()); err != nil {
328341
return nil, fmt.Errorf("changing git remote of repository: %w", err)
329342
}
330343
} else {
331344
logrus.Info("Using non-default k8s upstream, doing no git modifications")
332345
}
333-
334-
return repo, nil
335-
}
336-
337-
logrus.Info("Cloning repository by using SSH")
338-
repo, err := f.CloneOrOpenDefaultGitHubRepoSSH(f.options.RepoPath)
339-
if err != nil {
340-
return nil, fmt.Errorf("clone or open k/k GitHub repository: %w", err)
341346
}
342347

343348
return repo, nil
@@ -368,5 +373,6 @@ func (f *FastForward) prepareToolRepo() error {
368373
if err := f.Chdir(tmpPath); err != nil {
369374
return fmt.Errorf("change directory: %w", err)
370375
}
376+
371377
return nil
372378
}

pkg/release/release.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ func GetK8sRef() string {
188188
// GetK8sRef() point to their default values.
189189
func IsDefaultK8sUpstream() bool {
190190
return GetK8sOrg() == DefaultK8sOrg &&
191-
GetK8sRepo() == DefaultK8sRepo &&
192191
GetK8sRef() == DefaultK8sRef
193192
}
194193

0 commit comments

Comments
 (0)