Skip to content

Instantly share code, notes, and snippets.

@theolampert
Created March 5, 2019 14:19
Show Gist options
  • Save theolampert/3e69ef41c8984a1e0788fe525df59bd8 to your computer and use it in GitHub Desktop.
Save theolampert/3e69ef41c8984a1e0788fe525df59bd8 to your computer and use it in GitHub Desktop.
Get all semantic commits from a PR
const fs = require('fs');
const request = require('request-promise');
const query = `query {
repository(name:"<project>", owner:"<owner>") {
pullRequest(number: 993) {
id
commits(last:250) {
edges {
node {
commit {
message
}
}
}
}
}
}
}`;
function writeMarkdownFile(body) {
let markdown = '';
Object.keys(body).forEach((semanticType) => {
if (!body[semanticType].length) return;
markdown += `## ${semanticType} \n`;
body[semanticType].forEach((change) => {
markdown += `- ${change} \n`;
});
});
fs.writeFile('./changelog.md', markdown, () => {
console.log('done');
});
}
function parseCommits(commits) {
const regex = /(chore|docs|feat|fix|refactor|style|test):/gm;
const semanticTypes = {
chore: [],
docs: [],
feat: [],
fix: [],
refactor: [],
style: [],
test: [],
};
commits.edges.map((commit) => {
const result = regex.exec(commit.node.commit.message);
if (result) {
semanticTypes[result[1]].push(result.input.replace(`${result[0]} `, ''));
}
});
writeMarkdownFile(semanticTypes);
}
const options = {
uri: 'https://api.github.com/graphql',
method: 'POST',
headers: {
Authorization: 'bearer <token>',
'User-Agent': 'foo',
},
body: JSON.stringify({ query }),
};
request(options)
.then((response) => {
parseCommits(JSON.parse(response).data.repository.pullRequest.commits);
})
.catch((error) => {
console.error(error.message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment