changelog.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import axios from 'axios';
  2. import _ from 'lodash';
  3. import { Task, TaskRunner } from './task';
  4. const githubGrafanaUrl = 'https://github.com/grafana/grafana';
  5. interface ChangelogOptions {
  6. milestone: string;
  7. }
  8. const changelogTaskRunner: TaskRunner<ChangelogOptions> = async ({ milestone }) => {
  9. const client = axios.create({
  10. baseURL: 'https://api.github.com/repos/grafana/grafana',
  11. timeout: 10000,
  12. });
  13. const res = await client.get('/issues', {
  14. params: {
  15. state: 'closed',
  16. per_page: 100,
  17. labels: 'add to changelog',
  18. },
  19. });
  20. const issues = res.data.filter(item => {
  21. if (!item.milestone) {
  22. console.log('Item missing milestone', item.number);
  23. return false;
  24. }
  25. return item.milestone.title === milestone;
  26. });
  27. const bugs = _.sortBy(
  28. issues.filter(item => {
  29. if (item.title.match(/fix|fixes/i)) {
  30. return true;
  31. }
  32. if (item.labels.find(label => label.name === 'type/bug')) {
  33. return true;
  34. }
  35. return false;
  36. }),
  37. 'title'
  38. );
  39. const notBugs = _.sortBy(issues.filter(item => !bugs.find(bug => bug === item)), 'title');
  40. let markdown = '### Features / Enhancements\n';
  41. for (const item of notBugs) {
  42. markdown += getMarkdownLineForIssue(item);
  43. }
  44. markdown += '\n### Bug Fixes\n';
  45. for (const item of bugs) {
  46. markdown += getMarkdownLineForIssue(item);
  47. }
  48. console.log(markdown);
  49. };
  50. function getMarkdownLineForIssue(item: any) {
  51. let markdown = '';
  52. const title = item.title.replace(/^([^:]*)/, (match, g1) => {
  53. return `**${g1}**`;
  54. });
  55. markdown += '* ' + title + '.';
  56. markdown += ` [#${item.number}](${githubGrafanaUrl}/pull/${item.number})`;
  57. markdown += `, [@${item.user.login}](${item.user.html_url})`;
  58. markdown += '\n';
  59. return markdown;
  60. }
  61. export const changelogTask = new Task<ChangelogOptions>();
  62. changelogTask.setName('Changelog generator task');
  63. changelogTask.setRunner(changelogTaskRunner);