conventional-changelog / standard-version

:trophy: Automate versioning and CHANGELOG generation, with semver.org and conventionalcommits.org
ISC License
7.65k stars 787 forks source link

How to get the changelog entry for last release #786

Open MikeDabrowski opened 3 years ago

MikeDabrowski commented 3 years ago

When standard-version generates new changelog entry, I want to extract this entry and use it to update a gitlab release for that tag via gitlab API. Unfortunately, neither CLI nor programmatic usage does not return the new changelog entry in consumable way. How to do it without parsing changelog file?

MikeDabrowski commented 3 years ago

I tried reusing pieces of code in a script but the parser is not exported and hard to access. Not to mention it would be redoing the same thing (reading git and compiling the entry) again.

demoraesgui commented 3 years ago

I think I have the same problem, I want to use the generated changelog msg and add it to the release body.

MikeDabrowski commented 3 years ago

This is my current workaround. Im using the extracted parts from the original standard-version main method. Gitlab api shouldn't require any additional setup if using job token. Oh and Im using node-fetch, regular fetch won't work in node, but you may port to http-request.

const conventionalChangelog = require('conventional-changelog');
const presetLoader = require('standard-version/lib/preset-loader');
const { argv } = require('standard-version/command');
const fetch = require('node-fetch');

console.log(process.env);

function extractChangelogEntry(args) {
  return new Promise((resolve, reject) => {
    let content = '';
    const context = { version: args.releaseAs };
    const changelogStream = conventionalChangelog(
      {
        debug:
          args.verbose && console.info.bind(console, 'conventional-changelog'),
        preset: {
          ...presetLoader(args),
          skip: {
            tag: true,
            commit: true,
            bump: true,
          }
        },
        tagPrefix: args.tagPrefix,
      },
      context,
      { merges: null, path: args.path },
    ).on('error', function (err) {
      return reject(err);
    });

    changelogStream.on('data', function (buffer) {
      content += buffer.toString();
    });

    changelogStream.on('end', function () {
      return resolve(content);
    });
  });
}
async function updateRelease(description) {
  const {
    CI_API_V4_URL: apiUrl,
    CI_PROJECT_ID: projectId,
    CI_JOB_TOKEN: jobToken,
  } = process.env;
  const url = `${apiUrl}/projects/${projectId}/releases`;

  return fetch(url, {
    headers: {
      'Content-Type': 'application/json',
      'JOB-TOKEN': jobToken,
    },
    method: 'POST',
    body: JSON.stringify({
      name: argv.releaseAs,
      tag_name: argv.releaseAs,
      description,
    }),
  }).catch(console.error);
}
extractChangelogEntry(argv)
  .then(updateRelease)
  .catch(e => console.error(e))
  .then((c) => console.log(c));
viernullvier commented 3 years ago

I ran into the same issue recently. This should ideally be a dedicated feature of standard-version, but in the meantime you can just run a diff on the actual Changelog file:

diff --changed-group-format='%>' --unchanged-group-format='' <( git show HEAD~1:CHANGELOG.md ) CHANGELOG.md > RELEASE_NOTES.md || true
MikeDabrowski commented 3 years ago

interesting but to make it gitlab release you still need to make an api call