Skip to content

Commit

Permalink
Merge pull request #460 from justaugustus/pr-cleanups
Browse files Browse the repository at this point in the history
kpromo(pr): Minor cleanups
  • Loading branch information
k8s-ci-robot authored Nov 18, 2021
2 parents 5599f36 + 9e3b952 commit 5bb1798
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 93 deletions.
98 changes: 8 additions & 90 deletions cmd/kpromo/cmd/pr/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,14 @@ func runPromote(opts *promoteOptions) error {
gh := github.New()

// Verify the repository is a fork of k8s.io
if err = verifyFork(
if err = github.VerifyFork(
branchname, userForkOrg, userForkRepo, git.DefaultGithubOrg, k8sioRepo,
); err != nil {
return errors.Wrapf(err, "while checking fork of %s/%s ", git.DefaultGithubOrg, k8sioRepo)
}

// Clone k8s.io
repo, err := prepareFork(branchname, git.DefaultGithubOrg, k8sioRepo, userForkOrg, userForkRepo)
repo, err := github.PrepareFork(branchname, git.DefaultGithubOrg, k8sioRepo, userForkOrg, userForkRepo)
if err != nil {
return errors.Wrap(err, "while preparing k/k8s.io fork")
}
Expand Down Expand Up @@ -268,7 +268,10 @@ func runPromote(opts *promoteOptions) error {
return errors.Wrap(err, "adding image manifest to staging area")
}

commitMessage := "releng: Image promotion for " + opts.project + " " + strings.Join(opts.tags, " / ")
commitMessage := "Image promotion for " + opts.project + " " + strings.Join(opts.tags, " / ")
if opts.project == image.StagingRepoSuffix {
commitMessage = "releng: " + commitMessage
}

// Commit files
logrus.Debug("Creating commit")
Expand All @@ -279,8 +282,8 @@ func runPromote(opts *promoteOptions) error {
// Push to fork
if mustRun(opts, fmt.Sprintf("Push changes to user's fork at %s/%s?", userForkOrg, userForkRepo)) {
logrus.Infof("Pushing manifest changes to %s/%s", userForkOrg, userForkRepo)
if err := repo.PushToRemote(userForkName, branchname); err != nil {
return errors.Wrapf(err, "pushing %s to %s/%s", userForkName, userForkOrg, userForkRepo)
if err := repo.PushToRemote(github.UserForkName, branchname); err != nil {
return errors.Wrapf(err, "pushing %s to %s/%s", github.UserForkName, userForkOrg, userForkRepo)
}
} else {
// Exit if no push was made
Expand Down Expand Up @@ -354,88 +357,3 @@ func generatePRBody(opts *promoteOptions) string {

return prBody
}

// TODO: Consider moving this section to sigs.k8s.io/release-sdk

// Copied from https://github.com/kubernetes/release/blob/df4a45eead2cfb79deb1337a9817e137c9739d41/cmd/krel/cmd/release_notes.go

const (
// userForkName The name we will give to the user's remote when adding it to repos
userForkName = "userfork"
)

// prepareFork Prepare a branch a repo
func prepareFork(branchName, upstreamOrg, upstreamRepo, myOrg, myRepo string) (repo *git.Repo, err error) {
// checkout the upstream repository
logrus.Infof("Cloning/updating repository %s/%s", upstreamOrg, upstreamRepo)

repo, err = git.CleanCloneGitHubRepo(
upstreamOrg, upstreamRepo, false,
)
if err != nil {
return nil, errors.Wrapf(err, "cloning %s/%s", upstreamOrg, upstreamRepo)
}

// test if the fork remote is already existing
url := git.GetRepoURL(myOrg, myRepo, false)
if repo.HasRemote(userForkName, url) {
logrus.Infof(
"Using already existing remote %s (%s) in repository",
userForkName, url,
)
} else {
// add the user's fork as a remote
err = repo.AddRemote(userForkName, myOrg, myRepo)
if err != nil {
return nil, errors.Wrap(err, "adding user's fork as remote repository")
}
}

// checkout the new branch
err = repo.Checkout("-B", branchName)
if err != nil {
return nil, errors.Wrapf(err, "creating new branch %s", branchName)
}

return repo, nil
}

// verifyFork does a pre-check of a fork to see if we can create a PR from it
func verifyFork(branchName, forkOwner, forkRepo, parentOwner, parentRepo string) error {
logrus.Infof("Checking if a PR can be created from %s/%s", forkOwner, forkRepo)
gh := github.New()

// Check th PR
isrepo, err := gh.RepoIsForkOf(
forkOwner, forkRepo, parentOwner, parentRepo,
)
if err != nil {
return errors.Wrapf(
err, "while checking if repository is a fork of %s/%s",
parentOwner, parentRepo,
)
}

if !isrepo {
return errors.Errorf(
"cannot create PR, %s/%s is not a fork of %s/%s",
forkOwner, forkRepo, parentOwner, parentRepo,
)
}

// verify the branch does not previously exist
branchExists, err := gh.BranchExists(
forkOwner, forkRepo, branchName,
)
if err != nil {
return errors.Wrap(err, "while checking if branch can be created")
}

if branchExists {
return errors.Errorf(
"a branch named %s already exists in %s/%s",
branchName, forkOwner, forkRepo,
)
}
return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ require (
gopkg.in/src-d/go-git.v4 v4.13.1
gopkg.in/yaml.v2 v2.4.0
k8s.io/utils v0.0.0-20210305010621-2afb4311ab10
sigs.k8s.io/release-sdk v0.4.0
sigs.k8s.io/release-sdk v0.5.0
sigs.k8s.io/release-utils v0.3.0
sigs.k8s.io/yaml v1.3.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1401,8 +1401,8 @@ rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.14/go.mod h1:LEScyzhFmoF5pso/YSeBstl57mOzx9xlU9n85RGrDQg=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.15/go.mod h1:LEScyzhFmoF5pso/YSeBstl57mOzx9xlU9n85RGrDQg=
sigs.k8s.io/release-sdk v0.4.0 h1:QxU07rac9r5wOe/QNBHRuMv05RjqwPrrm/bVwCIt+f0=
sigs.k8s.io/release-sdk v0.4.0/go.mod h1:m7EwAKZb9Hua+b8pXnJLMJ/5WY/Fs1CeqxBZYXx0u0A=
sigs.k8s.io/release-sdk v0.5.0 h1:+1QwF6/7WvFXU5GWUcx7/bK9Ju4o/xybVZbPuIDD+rk=
sigs.k8s.io/release-sdk v0.5.0/go.mod h1:m7EwAKZb9Hua+b8pXnJLMJ/5WY/Fs1CeqxBZYXx0u0A=
sigs.k8s.io/release-utils v0.3.0 h1:cyNeXvm+2lPn67f4MWmq9xapZDAI5hekpT7iQPRxta4=
sigs.k8s.io/release-utils v0.3.0/go.mod h1:J9xpziRNRI4mAeMZxPRryDodQMoMudMu6yC1aViFHU4=
sigs.k8s.io/structured-merge-diff/v4 v4.0.2/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
Expand Down

0 comments on commit 5bb1798

Please sign in to comment.