|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * PR Title Labeler Action |
| 5 | + * Labels PRs based on the title prefix |
| 6 | + */ |
| 7 | + |
| 8 | +const { Octokit } = require('@octokit/rest'); |
| 9 | +const yaml = require('js-yaml'); |
| 10 | +const fs = require('fs'); |
| 11 | +const path = require('path'); |
| 12 | + |
| 13 | +async function run() { |
| 14 | + try { |
| 15 | + // Get environment variables |
| 16 | + const githubToken = process.env.GITHUB_TOKEN; |
| 17 | + const configFilePath = |
| 18 | + process.env.CONFIG_FILE || 'pr-title-labeler-config.yml'; |
| 19 | + const eventPath = process.env.GITHUB_EVENT_PATH; |
| 20 | + const repository = process.env.GITHUB_REPOSITORY; |
| 21 | + |
| 22 | + if (!githubToken) { |
| 23 | + console.error('❌ GITHUB_TOKEN is required'); |
| 24 | + process.exit(1); |
| 25 | + } |
| 26 | + |
| 27 | + if (!eventPath) { |
| 28 | + console.error('❌ GITHUB_EVENT_PATH is required'); |
| 29 | + process.exit(1); |
| 30 | + } |
| 31 | + |
| 32 | + // Read GitHub event data |
| 33 | + const eventData = JSON.parse(fs.readFileSync(eventPath, 'utf8')); |
| 34 | + const pr = eventData.pull_request; |
| 35 | + |
| 36 | + if (!pr) { |
| 37 | + console.error('❌ No pull request found in event data'); |
| 38 | + process.exit(1); |
| 39 | + } |
| 40 | + |
| 41 | + // Get config file path from environment variable |
| 42 | + const configPath = path.resolve(__dirname, configFilePath); |
| 43 | + |
| 44 | + let labelMappings; |
| 45 | + try { |
| 46 | + const configContent = fs.readFileSync(configPath, 'utf8'); |
| 47 | + labelMappings = yaml.load(configContent); |
| 48 | + } catch (error) { |
| 49 | + console.error( |
| 50 | + `❌ Error reading config file "${configFilePath}": ${error.message}` |
| 51 | + ); |
| 52 | + process.exit(1); |
| 53 | + } |
| 54 | + |
| 55 | + const title = pr.title.toLowerCase(); |
| 56 | + |
| 57 | + // Find matching prefix and label |
| 58 | + let label = null; |
| 59 | + for (const mapping of labelMappings) { |
| 60 | + for (const prefix of mapping.prefixes) { |
| 61 | + // Try both with and without colon for flexibility |
| 62 | + const prefixWithColon = `${prefix}:`; |
| 63 | + if (title.startsWith(prefixWithColon)) { |
| 64 | + label = mapping.label; |
| 65 | + console.log( |
| 66 | + `Found prefix "${prefixWithColon}" -> applying label "${label}"` |
| 67 | + ); |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + if (label) break; |
| 72 | + } |
| 73 | + |
| 74 | + // Create GitHub client |
| 75 | + const octokit = new Octokit({ |
| 76 | + auth: githubToken |
| 77 | + }); |
| 78 | + |
| 79 | + // Parse repository owner and name |
| 80 | + const [owner, repo] = repository.split('/'); |
| 81 | + |
| 82 | + // Apply label if found |
| 83 | + if (label) { |
| 84 | + await octokit.rest.issues.addLabels({ |
| 85 | + owner, |
| 86 | + repo, |
| 87 | + issue_number: pr.number, |
| 88 | + labels: [label] |
| 89 | + }); |
| 90 | + |
| 91 | + console.log(`✅ Successfully applied label: ${label}`); |
| 92 | + } else { |
| 93 | + console.log('ℹ️ No matching prefix found in PR title'); |
| 94 | + console.log(`PR title: "${title}"`); |
| 95 | + const allPrefixes = labelMappings.flatMap(mapping => mapping.prefixes); |
| 96 | + console.log('Available prefixes:', allPrefixes.join(', ')); |
| 97 | + } |
| 98 | + } catch (error) { |
| 99 | + console.error(`❌ Error labeling PR: ${error.message}`); |
| 100 | + process.exit(1); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +run(); |
0 commit comments