list-branches.js 3.7 KB

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