|
| 1 | +name: 'WPackagist Changelog Action' |
| 2 | +description: 'Automatically comment WordPress plugin changelogs on pull requests when WPackagist dependencies change' |
| 3 | +author: 'Roots Software LLC' |
| 4 | +branding: |
| 5 | + icon: 'package' |
| 6 | + color: 'blue' |
| 7 | + |
| 8 | +inputs: |
| 9 | + token: |
| 10 | + description: 'GitHub token with permissions to comment on PRs' |
| 11 | + required: false |
| 12 | + default: ${{ github.token }} |
| 13 | + max-changelog-length: |
| 14 | + description: 'Maximum length of each plugin changelog (characters)' |
| 15 | + required: false |
| 16 | + default: '4000' |
| 17 | + max-comment-length: |
| 18 | + description: 'Maximum total comment length (characters)' |
| 19 | + required: false |
| 20 | + default: '60000' |
| 21 | + |
| 22 | +runs: |
| 23 | + using: 'composite' |
| 24 | + steps: |
| 25 | + - name: Comment changelogs on PR |
| 26 | + uses: actions/github-script@v7 |
| 27 | + env: |
| 28 | + MAX_CHANGELOG_LENGTH: ${{ inputs.max-changelog-length }} |
| 29 | + MAX_COMMENT_LENGTH: ${{ inputs.max-comment-length }} |
| 30 | + with: |
| 31 | + github-token: ${{ inputs.token }} |
| 32 | + script: | |
| 33 | + const MARKER = '<!-- wpackagist-changelog-bot -->'; |
| 34 | + let plugins = new Set(); |
| 35 | +
|
| 36 | + // Paginate through changed files (API approach - no git needed) |
| 37 | + let page = 1; |
| 38 | + const per_page = 100; |
| 39 | + while (true) { |
| 40 | + const { data: files } = await github.rest.pulls.listFiles({ |
| 41 | + owner: context.repo.owner, |
| 42 | + repo: context.repo.repo, |
| 43 | + pull_number: context.issue.number, |
| 44 | + per_page, |
| 45 | + page, |
| 46 | + }); |
| 47 | + if (!files.length) break; |
| 48 | +
|
| 49 | + for (const f of files) { |
| 50 | + if ((f.filename === 'composer.lock' || f.filename === 'composer.json') && f.patch) { |
| 51 | + const addedLines = f.patch.split('\n').filter(line => line.startsWith('+')); |
| 52 | + const addedContent = addedLines.join('\n'); |
| 53 | + const matches = addedContent.match(/wpackagist-plugin\/([a-z0-9\-]+)/gi) || []; |
| 54 | + matches.forEach(m => plugins.add(m.toLowerCase().replace('wpackagist-plugin/', ''))); |
| 55 | + } |
| 56 | + } |
| 57 | + if (files.length < per_page) break; |
| 58 | + page += 1; |
| 59 | + } |
| 60 | +
|
| 61 | + plugins = [...plugins].sort(); |
| 62 | +
|
| 63 | + if (plugins.length === 0) { |
| 64 | + console.log('No WPackagist plugins changed'); |
| 65 | + return; |
| 66 | + } |
| 67 | +
|
| 68 | + // Find existing bot comment via hidden marker |
| 69 | + const comments = await github.rest.issues.listComments({ |
| 70 | + issue_number: context.issue.number, |
| 71 | + owner: context.repo.owner, |
| 72 | + repo: context.repo.repo, |
| 73 | + per_page: 100, |
| 74 | + }); |
| 75 | + const botComment = comments.data.find(c => c.body?.includes(MARKER)); |
| 76 | +
|
| 77 | + let comment = `${MARKER}\n# 🔌 WordPress Plugin Changelogs\n\n`; |
| 78 | + let totalLength = 0; |
| 79 | + const maxCommentLength = parseInt(process.env.MAX_COMMENT_LENGTH); |
| 80 | + const maxChangelogLength = parseInt(process.env.MAX_CHANGELOG_LENGTH); |
| 81 | +
|
| 82 | + for (const plugin of plugins) { |
| 83 | + try { |
| 84 | + const controller = new AbortController(); |
| 85 | + const timeout = setTimeout(() => controller.abort(), 8000); |
| 86 | + const response = await fetch( |
| 87 | + `https://api.wordpress.org/plugins/info/1.0/${plugin}.json`, |
| 88 | + { signal: controller.signal } |
| 89 | + ); |
| 90 | + clearTimeout(timeout); |
| 91 | +
|
| 92 | + if (!response.ok) throw new Error('Plugin not found'); |
| 93 | +
|
| 94 | + const data = await response.json(); |
| 95 | + const changelog = data.sections?.changelog || 'No changelog available'; |
| 96 | +
|
| 97 | + const pluginSection = |
| 98 | + `## ${plugin}\n\n` + |
| 99 | + `<details>\n<summary>View changelog</summary>\n\n` + |
| 100 | + changelog.substring(0, maxChangelogLength) + |
| 101 | + (changelog.length > maxChangelogLength ? '\n\n[...truncated]\n\n' : '\n\n') + |
| 102 | + `[View full changelog on WordPress.org](https://wordpress.org/plugins/${plugin}/#developers)\n\n` + |
| 103 | + `</details>\n\n`; |
| 104 | +
|
| 105 | + if (totalLength + pluginSection.length < maxCommentLength) { |
| 106 | + comment += pluginSection; |
| 107 | + totalLength += pluginSection.length; |
| 108 | + continue; |
| 109 | + } |
| 110 | +
|
| 111 | + comment += `\n\n⚠️ Additional plugins changed but comment size limit reached. Check composer files for details.\n`; |
| 112 | + break; |
| 113 | + } catch (error) { |
| 114 | + comment += `## ${plugin}\n\n⚠️ Could not fetch changelog (${error.message})\n\n`; |
| 115 | + } |
| 116 | + } |
| 117 | +
|
| 118 | + // Upsert comment |
| 119 | + if (botComment) { |
| 120 | + await github.rest.issues.updateComment({ |
| 121 | + comment_id: botComment.id, |
| 122 | + owner: context.repo.owner, |
| 123 | + repo: context.repo.repo, |
| 124 | + body: comment |
| 125 | + }); |
| 126 | + console.log('Updated existing comment'); |
| 127 | + return; |
| 128 | + } |
| 129 | +
|
| 130 | + await github.rest.issues.createComment({ |
| 131 | + issue_number: context.issue.number, |
| 132 | + owner: context.repo.owner, |
| 133 | + repo: context.repo.repo, |
| 134 | + body: comment |
| 135 | + }); |
| 136 | + console.log('Created new comment'); |
0 commit comments