list-branches.js 3.6 KB

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