Skip to content

chore(NODE-5347): add standard release automation #3717

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

Merged
merged 22 commits into from
Jun 21, 2023
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
8 changes: 8 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ If you haven't already, it would greatly help the team review this work in a tim
You can do that here: https://jira.mongodb.org/projects/NODE
-->

#### Release Highlight

<!-- RELEASE_HIGHLIGHT_START -->

## Fill in title or leave empty for no highlight

<!-- RELEASE_HIGHLIGHT_END -->

### Double check the following

- [ ] Ran `npm run check:lint` script
Expand Down
76 changes: 76 additions & 0 deletions .github/scripts/highlights.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// @ts-check
import * as process from 'node:process';
import { Octokit } from '@octokit/core';
import { output } from './util.mjs';

const {
GITHUB_TOKEN = '',
PR_LIST = '',
owner = 'mongodb',
repo = 'node-mongodb-native'
} = process.env;
if (GITHUB_TOKEN === '') throw new Error('GITHUB_TOKEN cannot be empty');

const octokit = new Octokit({
auth: GITHUB_TOKEN,
log: {
debug: msg => console.error('Octokit.debug', msg),
info: msg => console.error('Octokit.info', msg),
warn: msg => console.error('Octokit.warn', msg),
error: msg => console.error('Octokit.error', msg)
}
});

const prs = PR_LIST.split(',').map(pr => {
const prNum = Number(pr);
if (Number.isNaN(prNum))
throw Error(`expected PR number list: ${PR_LIST}, offending entry: ${pr}`);
return prNum;
});

/** @param {number} pull_number */
async function getPullRequestContent(pull_number) {
const startIndicator = 'RELEASE_HIGHLIGHT_START -->';
const endIndicator = '<!-- RELEASE_HIGHLIGHT_END';

let body;
try {
const res = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}', {
owner,
repo,
pull_number,
headers: { 'X-GitHub-Api-Version': '2022-11-28' }
});
body = res.data.body;
} catch (error) {
console.log(`Could not get PR ${pull_number}, skipping. ${error.status}`);
return '';
}

if (body == null || !(body.includes(startIndicator) && body.includes(endIndicator))) {
console.log(`PR #${pull_number} has no highlight`);
return '';
}

const start = body.indexOf('## ', body.indexOf(startIndicator));
const end = body.indexOf(endIndicator);
const highlightSection = body.slice(start, end).trim();

console.log(`PR #${pull_number} has a highlight ${highlightSection.length} characters long`);
return highlightSection;
}

/** @param {number[]} prs */
async function pullRequestHighlights(prs) {
const highlights = [];
for (const pr of prs) {
const content = await getPullRequestContent(pr);
highlights.push(content);
}
return highlights.join('');
}

console.log('List of PRs to collect highlights from:', prs);
const highlights = await pullRequestHighlights(prs);

await output('highlights', JSON.stringify({ highlights }));
15 changes: 4 additions & 11 deletions .github/scripts/nightly.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,18 @@ import * as path from 'node:path';
import * as process from 'node:process';
import * as child_process from 'node:child_process';
import * as util from 'node:util';
import { output } from './util.mjs';
const exec = util.promisify(child_process.exec);

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
const pkgFilePath = path.join(__dirname, '..', '..', 'package.json');

process.env.TZ = 'Etc/UTC';

/** @param {boolean} publish */
async function shouldPublish(publish) {
const githubOutput = process.env.GITHUB_OUTPUT ?? '';
if (githubOutput.length === 0) {
console.log('output file does not exist');
process.exit(1);
}

const outputFile = await fs.open(githubOutput, 'a');
const output = publish ? 'publish=yes' : 'publish=no';
console.log('outputting:', output, 'to', githubOutput);
await outputFile.appendFile(output, { encoding: 'utf8' });
await outputFile.close();
const answer = publish ? 'yes' : 'no';
await output('publish', answer);
}

/**
Expand Down
28 changes: 28 additions & 0 deletions .github/scripts/pr_list.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// @ts-check
import * as url from 'node:url';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import { getCurrentHistorySection, output } from './util.mjs';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
const historyFilePath = path.join(__dirname, '..', '..', 'HISTORY.md');

/**
* @param {string} history
* @returns {string[]}
*/
function parsePRList(history) {
const prRegexp = /node-mongodb-native\/issues\/(?<prNum>\d+)\)/giu;
return history
.split('\n')
.map(line => prRegexp.exec(line)?.groups?.prNum ?? '')
.filter(prNum => prNum !== '');
}

const historyContents = await fs.readFile(historyFilePath, { encoding: 'utf8' });

const currentHistorySection = getCurrentHistorySection(historyContents);

const prs = parsePRList(currentHistorySection);

await output('pr_list', prs.join(','));
56 changes: 56 additions & 0 deletions .github/scripts/release_notes.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//@ts-check
import * as url from 'node:url';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import * as process from 'node:process';
import * as semver from 'semver';
import { getCurrentHistorySection, output } from './util.mjs';

const { HIGHLIGHTS = '' } = process.env;
if (HIGHLIGHTS === '') throw new Error('HIGHLIGHTS cannot be empty');

const { highlights } = JSON.parse(HIGHLIGHTS);

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
const historyFilePath = path.join(__dirname, '..', '..', 'HISTORY.md');
const packageFilePath = path.join(__dirname, '..', '..', 'package.json');

const historyContents = await fs.readFile(historyFilePath, { encoding: 'utf8' });

const currentHistorySection = getCurrentHistorySection(historyContents);

const version = semver.parse(
JSON.parse(await fs.readFile(packageFilePath, { encoding: 'utf8' })).version
);
if (version == null) throw new Error(`could not create semver from package.json`);

console.log('\n\n--- history entry ---\n\n', currentHistorySection);

const currentHistorySectionLines = currentHistorySection.split('\n');
const header = currentHistorySectionLines[0];
const history = currentHistorySectionLines.slice(1).join('\n').trim();

const releaseNotes = `${header}

The MongoDB Node.js team is pleased to announce version ${version.version} of the \`mongodb\` package!

${highlights}
${history}
## Documentation

* [Reference](https://docs.mongodb.com/drivers/node/current/)
* [API](https://mongodb.github.io/node-mongodb-native/${version.major}.${version.minor}/)
* [Changelog](https://github.com/mongodb/node-mongodb-native/blob/v${version.version}/HISTORY.md)

We invite you to try the \`mongodb\` library immediately, and report any issues to the [NODE project](https://jira.mongodb.org/projects/NODE).
`;

const releaseNotesPath = path.join(process.cwd(), 'release_notes.md');

await fs.writeFile(
releaseNotesPath,
`:seedling: A new release!\n---\n${releaseNotes}\n---\n`,
{ encoding:'utf8' }
);

await output('release_notes_path', releaseNotesPath)
47 changes: 47 additions & 0 deletions .github/scripts/util.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// @ts-check
import * as process from 'node:process';
import * as fs from 'node:fs/promises';

export async function output(key, value) {
const { GITHUB_OUTPUT = '' } = process.env;
const output = `${key}=${value}\n`;
console.log('outputting:', output);

if (GITHUB_OUTPUT.length === 0) {
// This is always defined in Github actions, and if it is not for some reason, tasks that follow will fail.
// For local testing it's convenient to see what scripts would output without requiring the variable to be defined.
console.log('GITHUB_OUTPUT not defined, printing only');
return;
}

const outputFile = await fs.open(GITHUB_OUTPUT, 'a');
await outputFile.appendFile(output, { encoding: 'utf8' });
await outputFile.close();
}

/**
* @param {string} historyContents
* @returns {string}
*/
export function getCurrentHistorySection(historyContents) {
/** Markdown version header */
const VERSION_HEADER = /^#.+\(\d{4}-\d{2}-\d{2}\)$/g;

const historyLines = historyContents.split('\n');

// Search for the line with the first version header, this will be the one we're releasing
const headerLineIndex = historyLines.findIndex(line => VERSION_HEADER.test(line));
if (headerLineIndex < 0) throw new Error('Could not find any version header');

console.log('Found markdown header current release', headerLineIndex, ':', historyLines[headerLineIndex]);

// Search lines starting after the first header, and add back the offset we sliced at
const nextHeaderLineIndex = historyLines
.slice(headerLineIndex + 1)
.findIndex(line => VERSION_HEADER.test(line)) + headerLineIndex + 1;
if (nextHeaderLineIndex < 0) throw new Error(`Could not find previous version header, searched ${headerLineIndex + 1}`);

console.log('Found markdown header previous release', nextHeaderLineIndex, ':', historyLines[nextHeaderLineIndex]);

return historyLines.slice(headerLineIndex, nextHeaderLineIndex).join('\n');
}
2 changes: 1 addition & 1 deletion .github/workflows/build_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ jobs:
- name: Open Pull Request
uses: peter-evans/create-pull-request@v4
with:
title: 'docs: generate docs from latest main'
title: 'docs: generate docs from latest main [skip-ci]'
delete-branch: true
27 changes: 21 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
on:
# push:
# branches: [ main, 4.x ]
push:
branches: [main]
workflow_dispatch: {}

permissions:
Expand All @@ -13,10 +13,25 @@ jobs:
release-please:
runs-on: ubuntu-latest
steps:
- uses: google-github-actions/release-please-action@v3
- id: release
uses: google-github-actions/release-please-action@v3
with:
release-type: node
package-name: mongodb
pull-request-title-pattern: "chore${scope}: release${component} ${version} [skip-ci]"
# Publication steps would be dependent on the output of the above step:
# steps.release.outputs.release_created
# Example: chore(main): release 5.7.0 [skip-ci]
# ${scope} - parenthesis included, base branch name
pull-request-title-pattern: 'chore${scope}: release ${version} [skip-ci]'
pull-request-header: 'Please run history action before releasing to generate release highlights'
changelog-path: HISTORY.md
default-branch: main

# If release-please created a release, publish to npm
- if: ${{ steps.release.outputs.release_created }}
uses: actions/checkout@v3
- if: ${{ steps.release.outputs.release_created }}
name: actions/setup
uses: ./.github/actions/setup
- if: ${{ steps.release.outputs.release_created }}
run: npm publish --provenance
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you have a link to your fork or somewhere that tests all this as a dry run?

Copy link
Contributor Author

@nbbeeken nbbeeken Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/nbbeeken/node-mongodb-native/releases

Copy link
Contributor Author

@nbbeeken nbbeeken Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the steps as this PR stands:

  • every releasable unit (fix/feat) commit to main will cause release-please to create/update the release PR
    • It will overwrite the contents of the PR body indiscriminately
  • We currently have to manually run the release_notes action, it gathers the PRs involved in the release and posts the highlights defined in each one to the release PR
    • There's no strong reason to run this any earlier than when we're ready for releasing
    • We can run it early to preview how the highlights look when all gathered together
    • Essentially we use the PRs going into the release as storage for the text that is overwritten by release-please
  • Merging the release PR causes the release, the body of the PR between the horizontal lines: (markdown ---) is used for the body of our release.
    • I think we should have authors go back and edit their PRs & rerun the release_notes action.
    • However, we can make last minute edits to the PR directly and it will be transferred to the GH release.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently have to manually run the history action, it gathers the PRs involved in the release and posts the highlights defined in each one to the release PR

Why can't we just regenerate the history as a part of generating/updating the release PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging the release PR causes the release, the body of the PR between the horizontal lines: (markdown ---) is used for the body of our release.

  • I think we should have authors go back and edit their PRs & rerun the history action.
  • However, we can make last minute edits to the PR directly and it will be transferred to the GH release.

When do PR authors edit their PRs? Is that after we run the release action?

I just want this process to be very clear before we try it on a release with @nbbeeken OOO.


It sounds like there are more steps than just

  • PR author includes highlights in PR description, if necessary
  • PR merges into main, triggering the action that regenerates history, pulls in the new highlights if necessary, and then updates the release notes
  • Release PR is merged for release

What technical limitations are there that prevent the above workflow?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also - how do docs factor into the release workflow? Our current practice is to regenerate them and include them in the release commit. Will they be auto-generated as a part of the release and how do we specify that we need to regenerate for a minor or major?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When do PR authors edit their PRs? Is that after we run the release action?

The release_notes workflow collects the highlights from the PRs listed in the changelog, so we can run the workflow on demand after making edits to the relevant PR descriptions.

We don't manually run release that runs on every merge to main and keeps the release PR updated with the latest changelog. When merging the auto generated PR it triggers the actual release.

Your bullets are nearly correct, the generating of our human friendly release notes is manual b/c triggering an action from an action was not easily accomplished. I suspect this to be low hanging fruit we can address in the future.

As I said there's not much gain (beyond previewing) to have this done on each commit, we can think more about this but possibly having it trigger via comments on the release pr like "/regen-notes" to save us the navigation and workflow data entry could be a nice to have.

how do docs factor into the release workflow? Our current practice is to regenerate them and include them in the release commit. Will they be auto-generated as a part of the release and how do we specify that we need to regenerate for a minor or major?

There's no auto docs handling in this work, we will generate them and put them on main after publishing the release. I've captured this in the release instructions.

Copy link
Contributor Author

@nbbeeken nbbeeken Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we just regenerate the history release_notes as a part of generating/updating the release PR?

I don't have direct access to the PR number & git ref that the auto release is pending at, finding it isn't exactly clear, we'd have to fetch all the PRs and filter, and I'm not sure on what.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some updates from slack yesterday

  • release please handles the creating the release PR for whenever non-release PR is merged into the target branch (if the release PR exists, it is updated)
  • release please will run an npm release for us whenever a release PR merges into the target branch
  • release please only gives the PR name / number as output when the release PR is explicitly created. this makes it difficult to update the changelog / release notes in the release PR after a commit lands in main, because we'd have to programmatically detect which PR is the release PR

I think we should include "generating docs into the release PR before release" as the process, instead of generating them after but that doesn't affect the changes in this PR.

env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
48 changes: 48 additions & 0 deletions .github/workflows/release_notes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: release_notes

on:
workflow_dispatch:
inputs:
releasePr:
description: 'Enter release PR number'
required: true
type: number

jobs:
release_notes:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: actions/setup
uses: ./.github/actions/setup

# See: https://github.com/googleapis/release-please/issues/1274

# Get the PRs that are in this release
# Outputs a list of comma seperated PR numbers, parsed from HISTORY.md
- id: pr_list
run: node .github/scripts/pr_list.mjs
env:
GITHUB_TOKEN: ${{ github.token }}

# From the list of PRs, gather the highlight sections of the PR body
# output JSON with "highlights" key (to preserve newlines)
- id: highlights
run: node .github/scripts/highlights.mjs
env:
GITHUB_TOKEN: ${{ github.token }}
PR_LIST: ${{ steps.pr_list.outputs.pr_list }}

# The combined output is available
- id: release_notes
run: node .github/scripts/release_notes.mjs
env:
GITHUB_TOKEN: ${{ github.token }}
HIGHLIGHTS: ${{ steps.highlights.outputs.highlights }}

# Update the release PR body
- run: gh pr edit ${{ inputs.releasePr }} --body-file ${{ steps.release_notes.outputs.release_notes_path }}
shell: bash
env:
GITHUB_TOKEN: ${{ github.token }}
6 changes: 0 additions & 6 deletions etc/check-remote.sh

This file was deleted.

Loading