release-it / keep-a-changelog

Keep a Changelog plugin for release-it
MIT License
27 stars 15 forks source link

Entire changelog used when using `--no-increment` flag #27

Closed k2snowman69 closed 2 years ago

k2snowman69 commented 2 years ago

Currently when running using the --no-increment flag, the entire changelog is provided:

# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
## [1.0.0-beta.1] - 2022-07-29
- Change1
## [1.0.0-beta.0] - 2022-07-21
- First release

This coupled with git.release=true means the github release's content contains the entire changelog.

Expected

Only the content for the latest version to be included in the release content

Actual

Entire content of the changelog is contained in the release information

k2snowman69 commented 2 years ago

Fiddling around, I think the fix is something like this:

  getChangelog(latestVersion) {
    const { isIncrement } = this.config;
    // Return the unchanged changelog content when no increment is made
    if (!isIncrement) {
      return this.getLatestVersionLog(latestVersion);
    }

    // ... all this stays the same though you might want to pull it out into it's own function
    // for symmetry with getLatestVersionLog such as getUnreleasedVersionLog
  }

  getLatestVersionLog(latestVersion) {
    const latestVersionTitle = `## [${latestVersion}]`;
    const hasLatestVersionSection = this.changelogContent.includes(latestVersionTitle);

    if (!hasLatestVersionSection) {
      throw Error(`Missing section for previous release ("${latestVersion}") in ${filename}.`);
    }

    const titleStart = this.changelogContent.indexOf(latestVersionTitle);
    const contentStart = this.changelogContent.indexOf(this.EOL, titleStart) + 1;
    const nextTitle = `## [`;
    let endIndex = this.changelogContent.indexOf(nextTitle, contentStart);
    if (endIndex === -1) {
      endIndex = this.changelogContent.length;
    }
    const changelogContent = this.changelogContent.substring(contentStart, endIndex).trim();
    if (!changelogContent) {
      throw Error(`There are no entries under "${latestVersionTitle}" section in ${filename}.`);
    }
    this.setContext({ changelog: changelogContent });
    return changelogContent;
  }
k2snowman69 commented 2 years ago

Sorry for the tag @webpro however I'm just wanting to know if this is a fix you're interested in taking? This is the last piece I needed before migrating my workflows to release-it so I'm a bit excited and wanting to close this out.

webpro commented 2 years ago

Sorry for the late reply, not sure why I missed it.

But yes! Would be a great addition/fix to have in this plugin. Happy to review and merge your PR.