Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

separate out tagging function #57

Merged
merged 1 commit into from
Aug 24, 2022
Merged
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
85 changes: 7 additions & 78 deletions rcpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ func newRcpr(ctx context.Context, c *commander) (*rcpr, error) {
}

func isRcpr(pr *github.PullRequest) bool {
if pr == nil {
return false
}
for _, label := range pr.Labels {
if label.GetName() == autoLableName {
return true
Expand Down Expand Up @@ -177,87 +180,13 @@ func Run(ctx context.Context, argv []string, outStream, errStream io.Writer) err
rp.c.Git("config", "--local", "user.name", gitUser)
}

{
// tag and exit if the HEAD is the merged rcpr
commitish, _, err := rp.c.GitE("rev-parse", "HEAD")
if err != nil {
return err
}
pulls, _, err := rp.gh.PullRequests.ListPullRequestsWithCommit(
ctx, rp.owner, rp.repo, commitish, nil)
// If the latest commit is a merge commit of the pull request by rcpr,
// tag the semver to the commit and create a release and exit.
if pr, err := rp.latestPullRequest(ctx); err != nil || isRcpr(pr) {
if err != nil {
return err
}
if len(pulls) > 0 && isRcpr(pulls[0]) {
var vfile string
if rp.cfg.versionFile == nil {
rp.c.Git("checkout", "HEAD~")
vfile, err = detectVersionFile(".", currVer)
if err != nil {
return err
}
rp.c.Git("checkout", releaseBranch)
} else {
vfile = rp.cfg.versionFile.String()
}

var nextTag string
if vfile != "" {
nextVer, err := retrieveVersionFromFile(vfile, currVer.vPrefix)
if err != nil {
return err
}
nextTag = nextVer.Tag()
} else {
nextTag = guessNextSemver(currVer, pulls[0]).Tag()
}
previousTag := &latestSemverTag
if *previousTag == "" {
previousTag = nil
}

// To avoid putting pull requests created by rcpr itself in the release notes,
// we generate release notes in advance.
// Get the previous commitish to avoid picking up the merge of the pull
// request made by rcpr.
targetCommitish, _, err := rp.c.GitE("rev-parse", "HEAD~")
if err != nil {
return nil
}
releases, _, err := rp.gh.Repositories.GenerateReleaseNotes(
ctx, rp.owner, rp.repo, &github.GenerateNotesOptions{
TagName: nextTag,
PreviousTagName: previousTag,
TargetCommitish: &targetCommitish,
})
if err != nil {
return err
}

rp.c.Git("tag", nextTag)
if rp.c.err != nil {
return rp.c.err
}
_, _, err = rp.c.GitE("push", "--tags")
if err != nil {
return err
}

// Don't use GenerateReleaseNote flag and use pre generated one
_, _, err = rp.gh.Repositories.CreateRelease(
ctx, rp.owner, rp.repo, &github.RepositoryRelease{
TagName: &nextTag,
TargetCommitish: &releaseBranch,
Name: &releases.Name,
Body: &releases.Body,
// I want to make it as a draft release by default, but it is difficult to get a draft release
// from another tool via API, and there is no tool supports it, so I will make it as a normal
// release. In the future, there may be an option to create it as a Draft, or conversely,
// an option not to create a release.
// Draft: github.Bool(true),
})
return err
}
return rp.tagRelease(ctx, pr, currVer, latestSemverTag)
}

rcBranch := fmt.Sprintf("rcpr-from-%s", currVer.Tag())
Expand Down
99 changes: 99 additions & 0 deletions tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package rcpr

import (
"context"

"github.com/google/go-github/v45/github"
)

func (rp *rcpr) latestPullRequest(ctx context.Context) (*github.PullRequest, error) {
// tag and exit if the HEAD is the merged rcpr
commitish, _, err := rp.c.GitE("rev-parse", "HEAD")
if err != nil {
return nil, err
}
pulls, _, err := rp.gh.PullRequests.ListPullRequestsWithCommit(
ctx, rp.owner, rp.repo, commitish, nil)
if err != nil {
return nil, err
}
if len(pulls) == 0 {
return nil, nil
}
return pulls[0], nil
}

func (rp *rcpr) tagRelease(ctx context.Context, pr *github.PullRequest, currVer *semv, latestSemverTag string) error {
var (
vfile string
err error
)
releaseBranch := rp.cfg.releaseBranch.String()
if rp.cfg.versionFile == nil {
rp.c.Git("checkout", "HEAD~")
vfile, err = detectVersionFile(".", currVer)
if err != nil {
return err
}
rp.c.Git("checkout", releaseBranch)
} else {
vfile = rp.cfg.versionFile.String()
}

var nextTag string
if vfile != "" {
nextVer, err := retrieveVersionFromFile(vfile, currVer.vPrefix)
if err != nil {
return err
}
nextTag = nextVer.Tag()
} else {
nextTag = guessNextSemver(currVer, pr).Tag()
}
previousTag := &latestSemverTag
if *previousTag == "" {
previousTag = nil
}

// To avoid putting pull requests created by rcpr itself in the release notes,
// we generate release notes in advance.
// Get the previous commitish to avoid picking up the merge of the pull
// request made by rcpr.
targetCommitish, _, err := rp.c.GitE("rev-parse", "HEAD~")
if err != nil {
return nil
}
releases, _, err := rp.gh.Repositories.GenerateReleaseNotes(
ctx, rp.owner, rp.repo, &github.GenerateNotesOptions{
TagName: nextTag,
PreviousTagName: previousTag,
TargetCommitish: &targetCommitish,
})
if err != nil {
return err
}

rp.c.Git("tag", nextTag)
if rp.c.err != nil {
return rp.c.err
}
_, _, err = rp.c.GitE("push", "--tags")
if err != nil {
return err
}

// Don't use GenerateReleaseNote flag and use pre generated one
_, _, err = rp.gh.Repositories.CreateRelease(
ctx, rp.owner, rp.repo, &github.RepositoryRelease{
TagName: &nextTag,
TargetCommitish: &releaseBranch,
Name: &releases.Name,
Body: &releases.Body,
// I want to make it as a draft release by default, but it is difficult to get a draft release
// from another tool via API, and there is no tool supports it, so I will make it as a normal
// release. In the future, there may be an option to create it as a Draft, or conversely,
// an option not to create a release.
// Draft: github.Bool(true),
})
return err
}