Update Amazon API models #239
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Update Amazon API models | |
| on: | |
| schedule: | |
| # Run daily at 2:00 UTC | |
| - cron: "0 2 * * *" | |
| workflow_dispatch: | |
| # Allow manual trigger from GitHub UI | |
| permissions: | |
| contents: read | |
| jobs: | |
| update-api: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v5 | |
| with: | |
| fetch-depth: 0 # Full history for proper commit messages | |
| - name: Set up Ruby | |
| uses: ruby/setup-ruby@v1 | |
| with: | |
| ruby-version: "4.0" | |
| bundler-cache: true | |
| - name: Install dependencies | |
| run: bundle install | |
| - name: Generate code from API models | |
| run: bundle exec rake generate | |
| - name: Create Pull Request | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const { execSync } = require('child_process'); | |
| // Check for changes | |
| const status = execSync('git status --porcelain', { encoding: 'utf8' }); | |
| if (!status.trim()) { | |
| console.log('No changes detected, skipping PR creation'); | |
| return; | |
| } | |
| // Get changed files (excluding untracked) | |
| const changedFiles = status | |
| .split('\n') | |
| .filter(line => line && !line.startsWith('??')) | |
| .map(line => line.substring(3)); | |
| // Extract API names from changed files | |
| const apiUpdates = changedFiles | |
| .filter(file => file.match(/lib\/peddler\/apis\/[^\/]*\.rb/)) | |
| .map(file => file.replace(/lib\/peddler\/apis\/(.*)\.rb/, '$1')) | |
| .filter((name, index, arr) => arr.indexOf(name) === index) | |
| .sort(); | |
| // Configure git | |
| execSync('git config user.name "github-actions[bot]"'); | |
| execSync('git config user.email "github-actions[bot]@users.noreply.github.com"'); | |
| // Use fixed branch name and handle existing branch | |
| const branchName = 'update-api-models'; | |
| // Delete remote branch if it exists | |
| try { | |
| execSync(`git push origin --delete ${branchName}`, { stdio: 'ignore' }); | |
| } catch (e) { | |
| // Branch doesn't exist remotely, that's fine | |
| } | |
| // Delete local branch if it exists | |
| try { | |
| execSync(`git branch -D ${branchName}`, { stdio: 'ignore' }); | |
| } catch (e) { | |
| // Branch doesn't exist locally, that's fine | |
| } | |
| execSync(`git checkout -b ${branchName}`); | |
| // Create commit message with conventional format | |
| let commitMessage = 'feat: update amazon models'; | |
| if (apiUpdates.length > 0) { | |
| commitMessage += '\n\nUpdated APIs:\n' + apiUpdates.map(api => `- ${api}`).join('\n'); | |
| } | |
| // Commit changes | |
| execSync('git add -A'); | |
| execSync(`git commit -m "${commitMessage}"`); | |
| execSync(`git push --set-upstream origin ${branchName}`); | |
| // Create PR title and body | |
| const prTitle = `feat: update amazon models`; | |
| // Build PR body with character limit handling | |
| const apiList = apiUpdates.join(', ') || 'various APIs'; | |
| const baseBody = `This PR was automatically generated to update the Amazon SP-API models. | |
| **APIs affected:** ${apiList} | |
| **Changes include:** | |
| `; | |
| const claudePrompt = ` | |
| --- | |
| @claude Summarize the changes - what's new, what's removed, and any breaking changes.`; | |
| const maxLength = 65536; | |
| const truncationMessage = '(truncated due to length)'; | |
| let filesList = changedFiles.map(file => `- ${file}`).join('\n'); | |
| // Calculate max allowed length for the complete body (including Claude prompt) | |
| const maxBodyLength = maxLength; | |
| const currentLength = baseBody.length + filesList.length + claudePrompt.length; | |
| // Truncate if necessary | |
| if (currentLength > maxBodyLength) { | |
| const maxFilesListLength = maxBodyLength - baseBody.length - claudePrompt.length - truncationMessage.length; | |
| filesList = filesList.substring(0, maxFilesListLength); | |
| // Find the last complete line | |
| const lastNewline = filesList.lastIndexOf('\n'); | |
| if (lastNewline > 0) { | |
| filesList = filesList.substring(0, lastNewline); | |
| } | |
| filesList += truncationMessage; | |
| } | |
| const prBody = baseBody + filesList + claudePrompt; | |
| // Create PR using GitHub API | |
| const { data: pr } = await github.rest.pulls.create({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| title: prTitle, | |
| body: prBody, | |
| head: branchName, | |
| base: 'main' | |
| }); | |
| console.log(`Created PR #${pr.number}: ${pr.html_url}`); |