func releaseNotesJSON()

in cmd/krel/cmd/release_notes.go [760:885]


func releaseNotesJSON(repoPath, tag string) (jsonString string, err error) {
	logrus.Infof("Generating release notes for tag %s", tag)

	tagVersion, err := util.TagStringToSemver(tag)
	if err != nil {
		return "", errors.Wrap(err, "parsing semver from tag string")
	}

	logrus.Info("Cloning kubernetes/sig-release to read mapping files")
	sigReleaseRepo, err := git.CleanCloneGitHubRepo(
		git.DefaultGithubOrg, git.DefaultGithubReleaseRepo, false,
	)
	if err != nil {
		return "", errors.Wrap(err, "performing clone of k/sig-release")
	}
	defer func() {
		if e := sigReleaseRepo.Cleanup(); e != nil {
			err = sigReleaseRepo.Cleanup()
		}
	}()

	branchName := git.DefaultBranch
	releaseBranch := fmt.Sprintf("release-%d.%d", tagVersion.Major, tagVersion.Minor)

	// Ensure we have a valid branch
	if !git.IsReleaseBranch(branchName) {
		return "", errors.New("Could not determine a release branch for tag")
	}

	// Preclone the repo to be able to read branches and tags
	logrus.Infof("Cloning %s/%s", git.DefaultGithubOrg, git.DefaultGithubRepo)
	repo, err := git.CloneOrOpenDefaultGitHubRepoSSH(repoPath)
	if err != nil {
		return "", errors.Wrap(err, "cloning default github repo")
	}

	// Chech if release branch already exists
	_, err = repo.RevParseTag(releaseBranch)
	if err == nil {
		logrus.Infof("Working on branch %s instead of %s", releaseBranch, git.DefaultBranch)
		branchName = releaseBranch
	} else {
		logrus.Infof("Release branch %s does not exist, working on %s", releaseBranch, git.DefaultBranch)
	}

	// Notes for patch releases are generated starting from the previous patch release:
	var startTag, tagChoice string
	if tagVersion.Patch > 0 {
		startTag = fmt.Sprintf("v%d.%d.%d", tagVersion.Major, tagVersion.Minor, tagVersion.Patch-1)
		tagChoice = "previous patch release"
	} else {
		// From 1.20 the notes for the first alpha start from the previous minor
		if len(tagVersion.Pre) == 2 &&
			tagVersion.Pre[0].String() == "alpha" &&
			tagVersion.Pre[1].VersionNum == 1 {
			startTag = util.SemverToTagString(semver.Version{
				Major: tagVersion.Major, Minor: tagVersion.Minor - 1, Patch: 0,
			})
			tagChoice = "previous minor version"
		} else if len(tagVersion.Pre) == 0 && tagVersion.Patch == 0 {
			// If we are writing the notes for the first minor version (eg 1.20.0)
			// we choose as the start tag also the previous minor
			startTag = util.SemverToTagString(semver.Version{
				Major: tagVersion.Major, Minor: tagVersion.Minor - 1, Patch: 0,
			})
			tagChoice = "previous minor version because we are in a new minor version"
		} else {
			// All others from the previous existing tag
			startTag, err = repo.PreviousTag(tag, branchName)
			if err != nil {
				return "", errors.Wrap(err, "getting previous tag from branch")
			}
			tagChoice = "previous tag"
		}
	}
	logrus.Infof("Using start tag %v from %s", startTag, tagChoice)
	logrus.Infof("Using end tag %v", tag)

	notesOptions := options.New()
	notesOptions.Branch = branchName
	notesOptions.RepoPath = repoPath
	notesOptions.StartRev = startTag
	notesOptions.EndRev = tag
	notesOptions.Debug = logrus.StandardLogger().Level >= logrus.DebugLevel
	notesOptions.MapProviderStrings = releaseNotesOpts.mapProviders
	notesOptions.AddMarkdownLinks = true

	// If the the release for the tag we are using has a mapping directory,
	// add it to the mapProviders array to read the edits from the release team:
	mapsDir := filepath.Join(
		sigReleaseRepo.Dir(), "releases",
		fmt.Sprintf("release-%d.%d", tagVersion.Major, tagVersion.Minor),
		releaseNotesWorkDir, mapsMainDirectory,
	)
	if util.Exists(mapsDir) {
		logrus.Infof("Notes gatherer will read maps from %s", mapsDir)
		notesOptions.MapProviderStrings = append(notesOptions.MapProviderStrings, mapsDir)
	}

	if err := notesOptions.ValidateAndFinish(); err != nil {
		return "", err
	}

	// Fetch the notes
	releaseNotes, err := notes.GatherReleaseNotes(notesOptions)
	if err != nil {
		return "", errors.Wrapf(err, "gathering release notes")
	}

	doc, err := document.New(
		releaseNotes, notesOptions.StartRev, notesOptions.EndRev,
	)
	if err != nil {
		return "", errors.Wrapf(err, "creating release note document")
	}
	doc.PreviousRevision = startTag
	doc.CurrentRevision = tag

	// Create the JSON
	j, err := json.Marshal(releaseNotes.ByPR())
	if err != nil {
		return "", errors.Wrapf(err, "generating release notes JSON")
	}

	return string(j), err
}