Skip to content

Commit

Permalink
CI: update changelog generator to use compare API (grafana#99688)
Browse files Browse the repository at this point in the history
update changelog generator to use compare API
  • Loading branch information
kminehart authored Jan 29, 2025
1 parent f395173 commit c060096
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions .github/workflows/actions/changelog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,17 @@ const graphql = async (ghtoken, query, variables) => {
},
body: JSON.stringify({ query, variables }),
});
const { data } = await results.json();
return data;

const res = await results.json();

LOG(
JSON.stringify({
status: results.status,
text: results.statusText,
})
);

return res.data;
};

// Using Github GraphQL API find the timestamp for the given tag/commit hash.
Expand Down Expand Up @@ -99,20 +108,20 @@ const getCommitishDate = async (name, owner, target) => {
// 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, sinceDate) => {
LOG(`Fetching ${owner}/${name} PRs since ${sinceDate} till ${target}`);
const getHistory = async (name, owner, from, to) => {
LOG(`Fetching ${owner}/${name} PRs between ${from} and ${to}`);
const query = `
query findCommitsWithAssociatedPullRequests(
$name: String!
$owner: String!
$target: String!
$sinceDate: GitTimestamp
$from: String!
$to: String!
$cursor: String
) {
repository(name: $name, owner: $owner) {
object(expression: $target) {
... on Commit {
history(first: 50, since: $sinceDate, after: $cursor) {
ref(qualifiedName: $from) {
compare(headRef: $to) {
commits(first: 25, after: $cursor) {
totalCount
pageInfo {
hasNextPage
Expand Down Expand Up @@ -155,13 +164,13 @@ const getHistory = async (name, owner, target, sinceDate) => {
const result = await graphql(ghtoken, query, {
name,
owner,
target,
sinceDate,
from,
to,
cursor,
});
LOG(`GraphQL: ${JSON.stringify(result)}`);
nodes = [...nodes, ...result.repository.object.history.nodes];
const { hasNextPage, endCursor } = result.repository.object.history.pageInfo;
nodes = [...nodes, ...result.repository.ref.compare.commits.nodes];
const { hasNextPage, endCursor } = result.repository.ref.compare.commits.pageInfo;
if (!hasNextPage) {
break;
}
Expand All @@ -175,11 +184,11 @@ const getHistory = async (name, owner, target, sinceDate) => {
// 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, sinceDate, to) => {
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, sinceDate);
const history = await getHistory(name, owner, from, to);

const items = history.flatMap((node) => {
// discard PRs without a "changelog" label
Expand Down Expand Up @@ -231,13 +240,10 @@ const previous = process.argv[3] || process.env.INPUT_PREVIOUS || (await getPrev

LOG(`Previous tag/commit: ${previous}`);

const sinceDate = await getCommitishDate('grafana', 'grafana', previous);
LOG(`Previous tag/commit timestamp: ${sinceDate}`);

// Get all changelog items from Grafana OSS
const oss = await getChangeLogItems('grafana', 'grafana', sinceDate, target);
const oss = await getChangeLogItems('grafana', 'grafana', previous, target);
// Get all changelog items from Grafana Enterprise
const entr = await getChangeLogItems('grafana-enterprise', 'grafana', sinceDate, target);
const entr = await getChangeLogItems('grafana-enterprise', 'grafana', previous, target);

LOG(`Found OSS PRs: ${oss.length}`);
LOG(`Found Enterprise PRs: ${entr.length}`);
Expand Down

0 comments on commit c060096

Please sign in to comment.