list-branches.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* eslint-disable no-console */
  2. /*
  3. * USAGE:
  4. * node list-branches [OPTION]
  5. *
  6. * OPTIONS:
  7. * --inactive : Return inactive branches (default)
  8. * --illegal : Return illegal named branches
  9. */
  10. const { execSync } = require('child_process');
  11. const url = require('url');
  12. const EXCLUDE_TERM_DAYS = 14;
  13. const EXCLUDE_PATTERNS = [
  14. /^support\/apply-tsed$/,
  15. // https://regex101.com/r/Lnx7Pz/3
  16. /^dev\/[\d.x]*$/,
  17. /^release\/.+$/,
  18. /^dependabot\/.+$/,
  19. ];
  20. const LEGAL_PATTERNS = [
  21. /^master$/,
  22. // https://regex101.com/r/p9xswM/4
  23. /^(dev|feat|imprv|support|fix|rc|release|tmp)\/.+$/,
  24. ];
  25. const GITHUB_REPOS_URI = 'https://github.com/growilabs/growi/';
  26. class BranchSummary {
  27. constructor(line) {
  28. const splitted = line.split('\t'); // split with '%09'
  29. this.authorDate = new Date(splitted[0].trim());
  30. this.authorName = splitted[1].trim();
  31. this.branchName = splitted[2].trim().replace(/^origin\//, '');
  32. this.subject = splitted[3].trim();
  33. }
  34. }
  35. function getExcludeTermDate() {
  36. const date = new Date();
  37. date.setDate(date.getDate() - EXCLUDE_TERM_DAYS);
  38. return date;
  39. }
  40. function getBranchSummaries() {
  41. // exec git for-each-ref
  42. const out = execSync(`\
  43. git for-each-ref refs/remotes \
  44. --sort=-committerdate \
  45. --format='%(authordate:iso) %09 %(authorname) %09 %(refname:short) %09 %(subject)'
  46. `).toString();
  47. // parse
  48. const summaries = out
  49. .split('\n')
  50. .filter(v => v !== '') // trim empty string
  51. .map(line => new BranchSummary(line))
  52. .filter((summary) => { // exclude branches that matches to patterns
  53. return !EXCLUDE_PATTERNS.some(pattern => pattern.test(summary.branchName));
  54. });
  55. return summaries;
  56. }
  57. function getGitHubCommitsUrl(branchName) {
  58. return url.resolve(GITHUB_REPOS_URI, `commits/${branchName}`);
  59. }
  60. function getGitHubComparingLink(branchName) {
  61. const label = `master <- ${branchName}`;
  62. const link = url.resolve(GITHUB_REPOS_URI, `compare/${branchName}`);
  63. return `<${link}|${label}>`;
  64. }
  65. /**
  66. * @see https://api.slack.com/messaging/composing/layouts#building-attachments
  67. * @see https://github.com/marketplace/actions/slack-incoming-webhook
  68. *
  69. * @param {string} mode
  70. * @param {BranchSummary} summaries
  71. */
  72. function printSlackAttachments(mode, summaries) {
  73. const color = (mode === 'illegal') ? 'warning' : '#999999';
  74. const attachments = summaries.map((summary) => {
  75. const {
  76. authorName, authorDate, branchName, subject,
  77. } = summary;
  78. return {
  79. color,
  80. title: branchName,
  81. title_link: getGitHubCommitsUrl(branchName),
  82. fields: [
  83. {
  84. title: 'Author Date',
  85. value: authorDate,
  86. short: true,
  87. },
  88. {
  89. title: 'Author',
  90. value: authorName,
  91. short: true,
  92. },
  93. {
  94. title: 'Last Commit Subject',
  95. value: subject,
  96. },
  97. {
  98. title: 'Comparing Link',
  99. value: getGitHubComparingLink(branchName),
  100. },
  101. ],
  102. };
  103. });
  104. console.log(JSON.stringify(attachments));
  105. }
  106. async function main(mode) {
  107. const summaries = getBranchSummaries();
  108. let filteredSummaries;
  109. switch (mode) {
  110. case 'illegal':
  111. filteredSummaries = summaries
  112. .filter((summary) => { // exclude branches that matches to patterns
  113. return !LEGAL_PATTERNS.some(pattern => pattern.test(summary.branchName));
  114. });
  115. break;
  116. default: {
  117. const excludeTermDate = getExcludeTermDate();
  118. filteredSummaries = summaries
  119. .filter((summary) => {
  120. return summary.authorDate < excludeTermDate;
  121. });
  122. break;
  123. }
  124. }
  125. printSlackAttachments(mode, filteredSummaries);
  126. }
  127. const args = process.argv.slice(2);
  128. let mode = 'inactive';
  129. if (args.length > 0 && args[0] === '--illegal') {
  130. mode = 'illegal';
  131. }
  132. main(mode);