File size: 4,245 Bytes
da2e594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env node

/**
 * Generate release notes from commit messages between two tags
 * Used by GitHub Actions to create automated release notes
 */

const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');

function generateReleaseNotes(previousTag, currentTag) {
  try {
    console.log(`Generating release notes from ${previousTag} to ${currentTag}`);

    // Get commits between tags
    const gitLogCommand = `git log --pretty=format:"%H|%s|%an|%ae|%ad" --date=short --no-merges ${previousTag}..${currentTag}`;
    const commitsOutput = execSync(gitLogCommand, { encoding: 'utf8' });

    if (!commitsOutput.trim()) {
      console.log('No commits found between tags');
      return 'No changes in this release.';
    }

    const commits = commitsOutput.trim().split('\n').map(line => {
      const [hash, subject, author, email, date] = line.split('|');
      return { hash, subject, author, email, date };
    });

    // Categorize commits
    const categories = {
      'feat': { title: '✨ Features', commits: [] },
      'fix': { title: 'πŸ› Bug Fixes', commits: [] },
      'docs': { title: 'πŸ“š Documentation', commits: [] },
      'refactor': { title: '♻️ Refactoring', commits: [] },
      'test': { title: 'πŸ§ͺ Testing', commits: [] },
      'perf': { title: '⚑ Performance', commits: [] },
      'style': { title: 'πŸ’… Styling', commits: [] },
      'ci': { title: 'πŸ”§ CI/CD', commits: [] },
      'build': { title: 'πŸ“¦ Build', commits: [] },
      'chore': { title: 'πŸ”§ Maintenance', commits: [] },
      'other': { title: 'πŸ“ Other Changes', commits: [] }
    };

    commits.forEach(commit => {
      const subject = commit.subject.toLowerCase();
      let categorized = false;

      // Check for conventional commit prefixes
      for (const [prefix, category] of Object.entries(categories)) {
        if (prefix !== 'other' && subject.startsWith(`${prefix}:`)) {
          category.commits.push(commit);
          categorized = true;
          break;
        }
      }

      // If not categorized, put in other
      if (!categorized) {
        categories.other.commits.push(commit);
      }
    });

    // Generate release notes
    const releaseNotes = [];

    for (const [key, category] of Object.entries(categories)) {
      if (category.commits.length > 0) {
        releaseNotes.push(`### ${category.title}`);
        releaseNotes.push('');

        category.commits.forEach(commit => {
          // Clean up the subject by removing the prefix if it exists
          let cleanSubject = commit.subject;
          const colonIndex = cleanSubject.indexOf(':');
          if (colonIndex !== -1 && cleanSubject.substring(0, colonIndex).match(/^(feat|fix|docs|refactor|test|perf|style|ci|build|chore)$/)) {
            cleanSubject = cleanSubject.substring(colonIndex + 1).trim();
            // Capitalize first letter
            cleanSubject = cleanSubject.charAt(0).toUpperCase() + cleanSubject.slice(1);
          }

          releaseNotes.push(`- ${cleanSubject} (${commit.hash.substring(0, 7)})`);
        });

        releaseNotes.push('');
      }
    }

    // Add commit statistics
    const totalCommits = commits.length;
    const contributors = [...new Set(commits.map(c => c.author))];

    releaseNotes.push('---');
    releaseNotes.push('');
    releaseNotes.push(`**Release Statistics:**`);
    releaseNotes.push(`- ${totalCommits} commit${totalCommits !== 1 ? 's' : ''}`);
    releaseNotes.push(`- ${contributors.length} contributor${contributors.length !== 1 ? 's' : ''}`);

    if (contributors.length <= 5) {
      releaseNotes.push(`- Contributors: ${contributors.join(', ')}`);
    }

    return releaseNotes.join('\n');

  } catch (error) {
    console.error(`Error generating release notes: ${error.message}`);
    return `Failed to generate release notes: ${error.message}`;
  }
}

// Parse command line arguments
const previousTag = process.argv[2];
const currentTag = process.argv[3];

if (!previousTag || !currentTag) {
  console.error('Usage: generate-release-notes.js <previous-tag> <current-tag>');
  process.exit(1);
}

const releaseNotes = generateReleaseNotes(previousTag, currentTag);
console.log(releaseNotes);