changelog.ts 2.0 KB

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