|
| 1 | +const { getInput, setFailed, info, setOutput } = require('@actions/core'); |
| 2 | +const { context, getOctokit } = require('@actions/github'); |
| 3 | +const { Anthropic } = require('@anthropic-ai/sdk'); |
| 4 | + |
| 5 | +function parseInputs() { |
| 6 | + const anthropicApiKey = getInput('anthropic-api-key'); |
| 7 | + const language = getInput('language'); |
| 8 | + const model = getInput('model'); |
| 9 | + const token = getInput('token'); |
| 10 | + const version = getInput('version'); |
| 11 | + return { anthropicApiKey, language, model, token, version }; |
| 12 | +} |
| 13 | + |
| 14 | +async function getPRsFromCommit(octokit, sha) { |
| 15 | + try { |
| 16 | + const pr = await octokit.rest.repos.listPullRequestsAssociatedWithCommit({ |
| 17 | + owner: context.repo.owner, |
| 18 | + repo: context.repo.repo, |
| 19 | + commit_sha: sha, |
| 20 | + }); |
| 21 | + return pr.data.map(p => ({ label: `#${p.number}`, url: p.html_url })); |
| 22 | + } catch (e) { |
| 23 | + info(`Failed to fetch PRs for commit ${sha}: ${e.message}`); |
| 24 | + return []; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +function buildPrompt(language, commitsData) { |
| 29 | + return [ |
| 30 | + 'You are a DEV OPS engineer; write a concise changelog for the new software version.', |
| 31 | + 'The changelog lists new features (use verb "Add"), changes/improvements/updates (also start with "Add"), and bug fixes (start lines with "Fix").', |
| 32 | + 'Order sections: features/changes first, then fixes.', |
| 33 | + 'Format exactly as:\n```\n## What\'s Changed\n- Add <feature/change>\n- Fix <bug>\n```', |
| 34 | + 'Do not invent items. Use ONLY the following commit data (message, author, PRs).', |
| 35 | + 'If no features or fixes are present, provide a minimal placeholder like "- Add internal refactors".', |
| 36 | + `Return output in language: ${language}. Translate commit-derived content.`, |
| 37 | + 'Commit data JSON follows:', |
| 38 | + JSON.stringify(commitsData, null, 2), |
| 39 | + ].join('\n'); |
| 40 | +} |
| 41 | + |
| 42 | +async function run() { |
| 43 | + info('Running Anthropic release notes action'); |
| 44 | + const { anthropicApiKey, language, model, token, version } = parseInputs(); |
| 45 | + const octokit = getOctokit(token); |
| 46 | + |
| 47 | + // Gather commits since last release |
| 48 | + let latestRelease = null; |
| 49 | + try { |
| 50 | + latestRelease = await octokit.rest.repos.getLatestRelease({ owner: context.repo.owner, repo: context.repo.repo }); |
| 51 | + } catch (e) { |
| 52 | + info(`No previous release found; using all recent commits (${e.message})`); |
| 53 | + } |
| 54 | + |
| 55 | + let commits = []; |
| 56 | + try { |
| 57 | + if (latestRelease) { |
| 58 | + const base = latestRelease.data.tag_name; |
| 59 | + const compare = await octokit.rest.repos.compareCommits({ |
| 60 | + owner: context.repo.owner, |
| 61 | + repo: context.repo.repo, |
| 62 | + base, |
| 63 | + head: context.sha, |
| 64 | + }); |
| 65 | + commits = compare.data.commits; |
| 66 | + } else { |
| 67 | + // fallback: list recent commits (first page) |
| 68 | + const list = await octokit.rest.repos.listCommits({ owner: context.repo.owner, repo: context.repo.repo, per_page: 100 }); |
| 69 | + commits = list.data; |
| 70 | + } |
| 71 | + } catch (error) { |
| 72 | + return setFailed(`Failed to gather commits: ${error.message || 'unknown error'}`); |
| 73 | + } |
| 74 | + |
| 75 | + if (!commits.length) { |
| 76 | + return setFailed('No commits found to generate release notes'); |
| 77 | + } |
| 78 | + |
| 79 | + // Build commit data with PR associations |
| 80 | + const commitsStructured = []; |
| 81 | + for (const c of commits) { |
| 82 | + const prs = await getPRsFromCommit(octokit, c.sha); |
| 83 | + commitsStructured.push({ |
| 84 | + sha: c.sha, |
| 85 | + message: c.commit?.message, |
| 86 | + author: c.author?.login || c.commit?.author?.name || 'unknown', |
| 87 | + authorUrl: c.author?.html_url || null, |
| 88 | + prs, |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + const prompt = buildPrompt(language || 'en', commitsStructured); |
| 93 | + |
| 94 | + try { |
| 95 | + const anthropic = new Anthropic({ apiKey: anthropicApiKey }); |
| 96 | + const completion = await anthropic.messages.create({ |
| 97 | + model: model || 'claude-3-5-sonnet-latest', |
| 98 | + max_tokens: 800, |
| 99 | + messages: [ { role: 'user', content: prompt } ], |
| 100 | + }); |
| 101 | + |
| 102 | + const contentBlock = completion.content && completion.content[0]; |
| 103 | + const responseText = contentBlock && contentBlock.text ? contentBlock.text : null; |
| 104 | + if (!responseText) { |
| 105 | + throw new Error('Anthropic did not return content'); |
| 106 | + } |
| 107 | + |
| 108 | + // Create release |
| 109 | + await octokit.rest.repos.createRelease({ |
| 110 | + owner: context.repo.owner, |
| 111 | + repo: context.repo.repo, |
| 112 | + tag_name: version, |
| 113 | + name: version, |
| 114 | + body: responseText, |
| 115 | + }); |
| 116 | + |
| 117 | + setOutput('releaseNotes', responseText); |
| 118 | + info('Release created successfully.'); |
| 119 | + } catch (error) { |
| 120 | + setFailed(error.message || 'Failed to generate release notes'); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +run(); |
0 commit comments