mirror of
https://github.com/grafana/grafana.git
synced 2025-01-10 08:03:58 -06:00
Chore: Add changelog github action (#90183)
* add changelog github action * tabs vs spaces * better disable release, even in draft * add codeowners
This commit is contained in:
parent
bba26f8e13
commit
c5f903f6dd
2
.github/CODEOWNERS
vendored
2
.github/CODEOWNERS
vendored
@ -720,6 +720,8 @@ embed.go @grafana/grafana-as-code
|
||||
/.github/workflows/run-scenes-e2e.yml @grafana/dashboards-squad
|
||||
/.github/workflows/go_lint.yml @grafana/grafana-backend-services-squad
|
||||
/.github/workflows/trivy-scan.yml @grafana/grafana-backend-services-squad
|
||||
/.github/workflows/changelog.yml @zserge
|
||||
/.github/workflows/actions/changelog @zserge
|
||||
|
||||
# Generated files not requiring owner approval
|
||||
/packages/grafana-data/src/types/featureToggles.gen.ts @grafanabot
|
||||
|
22
.github/workflows/actions/changelog/action.yml
vendored
Normal file
22
.github/workflows/actions/changelog/action.yml
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
name: Changelog generator
|
||||
description: Generates and publishes a changelog for the given release version
|
||||
inputs:
|
||||
version:
|
||||
description: Version number
|
||||
required: true
|
||||
prev_version:
|
||||
description: Previous version number
|
||||
required: false
|
||||
token:
|
||||
description: GitHub token with read/write access to all necessary repositories
|
||||
required: true
|
||||
output_file:
|
||||
description: A file to store resulting changelog markdown
|
||||
required: false
|
||||
outputs:
|
||||
changelog:
|
||||
description: Changelog contents between the two given versions in Markdown format
|
||||
runs:
|
||||
using: 'node20'
|
||||
main: 'index.js'
|
||||
|
317
.github/workflows/actions/changelog/index.js
vendored
Normal file
317
.github/workflows/actions/changelog/index.js
vendored
Normal file
@ -0,0 +1,317 @@
|
||||
import { appendFileSync, writeFileSync } from 'fs';
|
||||
import { exec as execCallback } from 'node:child_process';
|
||||
import { promisify } from 'node:util';
|
||||
|
||||
//
|
||||
// Github Action core utils: logging (notice + debug log levels), must escape
|
||||
// newlines and percent signs
|
||||
//
|
||||
const escapeData = (s) => s.replace(/%/g, '%25').replace(/\r/g, '%0D').replace(/\n/g, '%0A');
|
||||
const LOG = (msg) => console.log(`::notice::${escapeData(msg)}`);
|
||||
|
||||
//
|
||||
// Semver utils: parse, compare, sort etc (using official regexp)
|
||||
// https://regex101.com/r/Ly7O1x/3/
|
||||
//
|
||||
const semverRegExp =
|
||||
/^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
|
||||
|
||||
const semverParse = (tag) => {
|
||||
const m = tag.match(semverRegExp);
|
||||
if (!m) {
|
||||
return;
|
||||
}
|
||||
const [_, major, minor, patch, prerelease] = m;
|
||||
return [+major, +minor, +patch, prerelease, tag];
|
||||
};
|
||||
|
||||
// semverCompare takes two parsed semver tags and comparest them more or less
|
||||
// according to the semver specs
|
||||
const semverCompare = (a, b) => {
|
||||
for (let i = 0; i < 3; i++) {
|
||||
if (a[i] !== b[i]) {
|
||||
return a[i] < b[i] ? 1 : -1;
|
||||
}
|
||||
}
|
||||
if (a[3] !== b[3]) {
|
||||
return a[3] < b[3] ? 1 : -1;
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
// Using `git tag -l` output find the tag (version) that goes semantically
|
||||
// right before the given version. This might not work correctly with some
|
||||
// pre-release versions, which is why it's possible to pass previous version
|
||||
// into this action explicitly to avoid this step.
|
||||
const getPreviousVersion = async (version) => {
|
||||
const exec = promisify(execCallback);
|
||||
const { stdout } = await exec('git tag -l');
|
||||
const prev = stdout
|
||||
.split('\n')
|
||||
.map(semverParse)
|
||||
.filter((tag) => tag)
|
||||
.sort(semverCompare)
|
||||
.find((tag) => semverCompare(tag, semverParse(version)) > 0);
|
||||
return prev;
|
||||
};
|
||||
|
||||
// A helper for Github GraphQL API endpoint
|
||||
const graphql = async (token, query, variables) => {
|
||||
const { env } = process;
|
||||
const results = await fetch('https://api.github.com/graphql', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: JSON.stringify({ query, variables }),
|
||||
});
|
||||
const { data } = await results.json();
|
||||
return data;
|
||||
};
|
||||
|
||||
// Using Github GraphQL API find the timestamp for the given tag/commit hash.
|
||||
// This is required for PR listing, because Github API only takes date/time as
|
||||
// a "since" parameter while listing. Currently there is no way to provide two
|
||||
// "commitish" items and get a list of PRs in between them.
|
||||
const getCommitishDate = async (name, owner, target) => {
|
||||
const result = await graphql(
|
||||
token,
|
||||
`
|
||||
query getCommitDate($owner: String!, $name: String!, $target: String!) {
|
||||
repository(owner: $owner, name: $name) {
|
||||
object(expression: $target) {
|
||||
... on Commit {
|
||||
committedDate
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
{ name, owner, target }
|
||||
);
|
||||
return result.repository.object.committedDate;
|
||||
};
|
||||
|
||||
// Using Github GraphQL API get a list of PRs between the two "commitish" items.
|
||||
// This resoves the "since" item's timestamp first and iterates over all PRs
|
||||
// till "target" using naïve pagination.
|
||||
const getHistory = async (name, owner, target, since) => {
|
||||
const sinceDate = await getCommitishDate(name, owner, since);
|
||||
LOG(`Fetching ${owner}/${name} PRs since ${since} (${sinceDate}) till ${target}`);
|
||||
const query = `
|
||||
query findCommitsWithAssociatedPullRequests(
|
||||
$name: String!
|
||||
$owner: String!
|
||||
$target: String!
|
||||
$sinceDate: GitTimestamp
|
||||
$cursor: String
|
||||
) {
|
||||
repository(name: $name, owner: $owner) {
|
||||
object(expression: $target) {
|
||||
... on Commit {
|
||||
history(first: 50, since: $sinceDate, after: $cursor) {
|
||||
totalCount
|
||||
pageInfo {
|
||||
hasNextPage
|
||||
endCursor
|
||||
}
|
||||
nodes {
|
||||
id
|
||||
associatedPullRequests(first: 1) {
|
||||
nodes {
|
||||
title
|
||||
number
|
||||
labels(first: 10) {
|
||||
nodes {
|
||||
name
|
||||
}
|
||||
}
|
||||
commits(first: 1) {
|
||||
nodes {
|
||||
commit {
|
||||
author {
|
||||
user {
|
||||
login
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`;
|
||||
|
||||
let cursor;
|
||||
let nodes = [];
|
||||
for (;;) {
|
||||
const result = await graphql(token, query, {
|
||||
name,
|
||||
owner,
|
||||
target,
|
||||
sinceDate,
|
||||
cursor,
|
||||
});
|
||||
LOG(`GraphQL: ${JSON.stringify(result)}`);
|
||||
nodes = [...nodes, ...result.repository.object.history.nodes];
|
||||
const { hasNextPage, endCursor } = result.repository.object.history.pageInfo;
|
||||
if (!hasNextPage) {
|
||||
break;
|
||||
}
|
||||
cursor = endCursor;
|
||||
}
|
||||
return nodes;
|
||||
};
|
||||
|
||||
// The main function for this action: given two "commitish" items it gets a
|
||||
// list of PRs between them and filters/groups the PRs by category (bugfix,
|
||||
// feature, deprecation, breaking change and plugin fixes/enhancements).
|
||||
//
|
||||
// PR grouping relies on Github labels only, not on the PR contents.
|
||||
const getChangeLogItems = async (name, owner, from, to) => {
|
||||
// check if a node contains a certain label
|
||||
const hasLabel = ({ labels }, label) => labels.nodes.some(({ name }) => name === label);
|
||||
// get all the PRs between the two "commitish" items
|
||||
const history = await getHistory(name, owner, to, from);
|
||||
|
||||
const items = history.flatMap((node) => {
|
||||
// discard PRs without a "changelog" label
|
||||
const changes = node.associatedPullRequests.nodes.filter((PR) => hasLabel(PR, 'add to changelog'));
|
||||
if (changes.length === 0) {
|
||||
return [];
|
||||
}
|
||||
const item = changes[0];
|
||||
const { number, url, labels } = item;
|
||||
const title = item.title.replace(/^\[[^\]]+\]:?\s*/, '');
|
||||
// for changelog PRs try to find a suitable category.
|
||||
// Note that we can not detect "deprecation notices" like that
|
||||
// as there is no suitable label yet.
|
||||
const isBug = /fix/i.test(title) || hasLabel({ labels }, 'type/bug');
|
||||
const isBreaking = hasLabel({ labels }, 'breaking change');
|
||||
const isPlugin =
|
||||
hasLabel({ labels }, 'area/grafana/ui') ||
|
||||
hasLabel({ labels }, 'area/grafana/toolkit') ||
|
||||
hasLabel({ labels }, 'area/grafana/runtime');
|
||||
const author = item.commits.nodes[0].commit.author.user.login;
|
||||
return {
|
||||
repo: name,
|
||||
number,
|
||||
title,
|
||||
author,
|
||||
isBug,
|
||||
isPlugin,
|
||||
isBreaking,
|
||||
};
|
||||
});
|
||||
return items;
|
||||
};
|
||||
|
||||
// ======================================================
|
||||
// GENERATE CHANGELOG
|
||||
// ======================================================
|
||||
|
||||
LOG(`Changelog action started`);
|
||||
const version = process.argv[2] || process.env.INPUT_VERSION;
|
||||
LOG(`Target version: ${version}`);
|
||||
const prevVersion = process.argv[3] || process.env.INPUT_PREV_VERSION || (await getPreviousVersion(version));
|
||||
LOG(`Previous version: ${prevVersion}`);
|
||||
const token = process.env.GITHUB_TOKEN || process.env.INPUT_TOKEN;
|
||||
|
||||
if (!token) {
|
||||
throw 'GITHUB_TOKEN is not set and "token" input is empty';
|
||||
}
|
||||
|
||||
// Get all changelog items from Grafana OSS
|
||||
const oss = await getChangeLogItems('grafana', 'grafana', prevVersion, version);
|
||||
// Get all changelog items from Grafana Enterprise
|
||||
const entr = await getChangeLogItems('grafana-enterprise', 'grafana', prevVersion, version);
|
||||
|
||||
LOG(`Found OSS PRs: ${oss.length}`);
|
||||
LOG(`Found Enterprise PRs: ${entr.length}`);
|
||||
|
||||
// Sort PRs and categorise them into sections
|
||||
const changelog = [...oss, ...entr]
|
||||
.sort((a, b) => (a.title < b.title ? -1 : 1))
|
||||
.reduce(
|
||||
(changelog, item) => {
|
||||
if (item.isPlugin) {
|
||||
changelog.plugins.push(item);
|
||||
} else if (item.isBug) {
|
||||
changelog.bugfixes.push(item);
|
||||
} else if (item.isBreaking) {
|
||||
changelog.breaking.push(item);
|
||||
} else {
|
||||
changelog.features.push(item);
|
||||
}
|
||||
return changelog;
|
||||
},
|
||||
{
|
||||
version,
|
||||
breaking: [],
|
||||
plugins: [],
|
||||
bugfixes: [],
|
||||
features: [],
|
||||
// looks like JS doesn't have a nicer way to format date as YYYY-MM-DD
|
||||
releaseDate: new Date().toISOString().split('T')[0],
|
||||
}
|
||||
);
|
||||
|
||||
// Convert PR numbers to Github links
|
||||
const pullRequestLink = (n) => `[#${n}](https://github.com/grafana/grafana/pull/${n})`;
|
||||
// Convert Github user IDs to Github links
|
||||
const userLink = (u) => `[@${u}](https://github.com/${u})`;
|
||||
|
||||
// Now that we have a changelog - we can render some markdown as an output
|
||||
const markdown = (changelog) => {
|
||||
// This convers a list of changelog items into a markdown section with a list of titles/links
|
||||
const section = (title, items) =>
|
||||
items.length === 0
|
||||
? ''
|
||||
: `### ${title}
|
||||
|
||||
${items
|
||||
.map(
|
||||
(item) =>
|
||||
`- ${item.title.replace(/^([^:]*:)/gm, '**$1**')} ${
|
||||
item.repo === 'grafana-enterprise'
|
||||
? '(Enterprise)'
|
||||
: `${pullRequestLink(item.number)}, ${userLink(item.author)}`
|
||||
}`
|
||||
)
|
||||
.join('\n')}
|
||||
`;
|
||||
|
||||
// Render all present sections for the given changelog
|
||||
return `# ${changelog.version} (${changelog.releaseDate})
|
||||
|
||||
${section('Breaking changes', changelog.breaking)}
|
||||
${section('Bug fixes', changelog.bugfixes)}
|
||||
${section('Plugin development fixes & changes', changelog.plugins)}
|
||||
${section('Features and enhancements', changelog.features)}
|
||||
`;
|
||||
};
|
||||
|
||||
const md = markdown(changelog);
|
||||
|
||||
// Print changelog, mostly for debugging
|
||||
LOG(`Resulting markdown: ${md}`);
|
||||
|
||||
// Save changelog as an output for this action
|
||||
if (process.env.GITHUB_OUTPUT) {
|
||||
LOG(`Output to ${process.env.GITHUB_OUTPUT}`);
|
||||
appendFileSync(process.env.GITHUB_OUTPUT, `changelog<<EOF\n${escapeData(md)}\nEOF`);
|
||||
} else {
|
||||
LOG('GITHUB_OUTPUT is not set');
|
||||
}
|
||||
|
||||
// Save changelog as an output file (if requested)
|
||||
if (process.env.INPUT_OUTPUT_FILE) {
|
||||
LOG(`Output to ${process.env.INPUT_OUTPUT_FILE}`);
|
||||
writeFileSync(process.env.INPUT_OUTPUT_FILE, md);
|
||||
}
|
||||
|
43
.github/workflows/changelog.yml
vendored
Normal file
43
.github/workflows/changelog.yml
vendored
Normal file
@ -0,0 +1,43 @@
|
||||
name: Generate changelog
|
||||
on:
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
version:
|
||||
required: true
|
||||
description: 'Target release version (git tag)'
|
||||
prev_version:
|
||||
required: true
|
||||
description: 'Previous release version (git tag)'
|
||||
jobs:
|
||||
main:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: write
|
||||
steps:
|
||||
- name: "Generate token"
|
||||
id: generate_token
|
||||
uses: tibdex/github-app-token@b62528385c34dbc9f38e5f4225ac829252d1ea92
|
||||
with:
|
||||
app_id: ${{ secrets.GRAFANA_DELIVERY_BOT_APP_ID }}
|
||||
private_key: ${{ secrets.GRAFANA_DELIVERY_BOT_APP_PEM }}
|
||||
- name: "Checkout Grafana repo"
|
||||
uses: "actions/checkout@v4"
|
||||
with:
|
||||
sparse-checkout: .github/workflows
|
||||
fetch-depth: 0
|
||||
fetch-tags: true
|
||||
- name: "Generate changelog"
|
||||
id: changelog
|
||||
uses: ./.github/workflows/actions/changelog
|
||||
with:
|
||||
token: ${{ steps.generate_token.outputs.token }}
|
||||
version: ${{ inputs.version }}
|
||||
prev_version: ${{ inputs.prev_version }}
|
||||
output_file: _changelog.md
|
||||
- name: "Draft Github Release"
|
||||
env:
|
||||
GH_TOKEN: ${{ steps.generate_token.outputs.token }}
|
||||
run: |
|
||||
cat _changelog.md
|
||||
# skip for now
|
||||
# gh release create --draft ${{ inputs.version }} --notes-file _changelog.md
|
Loading…
Reference in New Issue
Block a user