Skip to content

Commit

Permalink
Merge pull request #733 from buildpacks/feature/changelog-updates
Browse files Browse the repository at this point in the history
Changelog updates
  • Loading branch information
jromero authored Jul 7, 2020
2 parents 658c93a + 5c1f9f5 commit a3ff87a
Showing 1 changed file with 96 additions and 43 deletions.
139 changes: 96 additions & 43 deletions .github/workflows/build/changelog.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,77 @@
let fs = require('fs');

const libraryLabel = 'lib';

const typeLabelPrefix = 'type/';
const typeLabelsMap = {
"Features": typeLabelPrefix + "enhancement",
"Fixes": typeLabelPrefix + "bug",
};

// Map of annotations to be added to issue per label.
const annotationLabelsMap = {
"experimental": "experimental",
"breaking": "breaking-change",
};

module.exports = async ({core, github}) => {
const milestone = process.env.PACK_VERSION;
const repository = process.env.GITHUB_REPOSITORY;
console.log("looking up PRs for milestone", milestone, "in repo", repository);

const typeLabelPrefix = 'type/';
const typeLabelsMap = {
"Features": typeLabelPrefix + "enhancement",
"Fixes": typeLabelPrefix + "bug",
};

// Map of annotations to be added to issue per label.
const annotationLabelsMap = {
"experimental": "experimental",
"breaking": "breaking-change",
};

return await github.paginate("GET /search/issues", {
q: `repo:${repository} is:pr state:closed milestone:${milestone}`,
}).then((items) => {
// group issues by type label
return items.reduce((groupedMap, issue) => {
let typeLabels = issue.labels.filter(label => {
return label.name.startsWith(typeLabelPrefix);
}).map(label => label.name);

if (typeLabels.length > 1) {
console.log("issue", issue.number, "has more than one label types: ", typeLabels);
} else if (typeLabels.length === 0) {
console.log("issue", issue.number, "doesn't have a 'type/' label.");

let cliIssues = [];
let libraryIssues = [];

items.forEach(issue => {
if (issue.labels.filter(label => label.name === libraryLabel).length === 0) {
cliIssues.push(issue);
} else {
let key = typeLabels[0];
(groupedMap[key] = groupedMap[key] || []).push(issue);
libraryIssues.push(issue);
}
});

return groupedMap;
}, {});
}).then(groupedIssues => {
let output = "";
// group issues by type label
return {"cliIssues": cliIssues, "libIssues": libraryIssues};
}).then(({cliIssues, libIssues}) => {
console.log("CLI issues:", cliIssues.length);
console.log("Library issues:", libIssues.length);
console.log("Note: some issues may not be presented (eg. type/chore)");

let groupedCliIssues = groupByType(cliIssues);
let groupedLibIssues = groupByType(libIssues);
let output = "";

// issues
for (let key in typeLabelsMap) {
output += `## ${key}\n\n`;
(groupedIssues[typeLabelsMap[key]] || []).forEach(issue => {
let annotations = [];
issue.labels.forEach(label => {
for (const annotation in annotationLabelsMap) {
if (annotationLabelsMap[annotation] === label.name) {
annotations.push(annotation);
}
}
let issues = (groupedCliIssues[typeLabelsMap[key]] || []);
if (issues.length > 0) {
output += `## ${key}\n\n`;
issues.forEach(issue => {
output += createIssueEntry(issue);
});
output += "\n";
}
}

if (annotations.length !== 0) {
output += `* ${issue.title} [${annotations.join(", ")}] (#${issue.number})\n`;
} else {
output += `* ${issue.title} (#${issue.number})\n`;
// library issues
if (Object.keys(groupedLibIssues).length > 0) {
output += "## Library\n\n";
output += "<details><p>\n";
for (let key in typeLabelsMap) {
let issues = (groupedLibIssues[typeLabelsMap[key]] || []);
if (issues.length > 0) {
output += `### ${key}\n\n`;
issues.forEach(issue => {
output += createIssueEntry(issue);
});
output += "\n";
}
});
output += `\n`;
}
output += "</p></details>";
}

output = output.trim();
Expand All @@ -71,8 +84,48 @@ module.exports = async ({core, github}) => {
}
});

console.log("OUTPUT:\n", output);

core.setOutput('contents', output);
core.setOutput('file', 'changelog.md');
});
};

function createIssueEntry(issue) {
let annotations = [];

issue.labels.forEach(label => {
for (const annotation in annotationLabelsMap) {
if (annotationLabelsMap[annotation] === label.name) {
annotations.push(annotation);
}
}
});

let line = `* ${issue.title}`;
if (annotations.length !== 0) {
line += ` [${annotations.join(", ")}]`;
}
line += ` (#${issue.number} by @${issue.user.login})\n`;

return line;
}

function groupByType(issues) {
return issues.reduce((groupedMap, issue) => {
let typeLabels = issue.labels.filter(label => {
return label.name.startsWith(typeLabelPrefix);
}).map(label => label.name);

if (typeLabels.length > 1) {
console.warn("issue", issue.number, "has more than one label types: ", typeLabels);
} else if (typeLabels.length === 0) {
console.warn("issue", issue.number, "doesn't have a 'type/' label.");
} else {
let key = typeLabels[0];
(groupedMap[key] = groupedMap[key] || []).push(issue);
}

return groupedMap;
}, {})
}

0 comments on commit a3ff87a

Please sign in to comment.