|
| 1 | +name: Update b2c-cli |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Run daily at 9am UTC |
| 6 | + - cron: '0 9 * * *' |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +jobs: |
| 10 | + check-and-update: |
| 11 | + runs-on: ubuntu-22.04 |
| 12 | + permissions: |
| 13 | + contents: write |
| 14 | + pull-requests: write |
| 15 | + steps: |
| 16 | + - name: Checkout |
| 17 | + uses: actions/checkout@v4 |
| 18 | + |
| 19 | + - name: Check for updates |
| 20 | + id: check |
| 21 | + run: | |
| 22 | + # Get latest version from npm |
| 23 | + LATEST=$(curl -s https://registry.npmjs.org/@salesforce/b2c-cli/latest | jq -r '.version') |
| 24 | + echo "Latest npm version: $LATEST" |
| 25 | +
|
| 26 | + # Get current version from formula |
| 27 | + CURRENT=$(grep -oP '(?<=b2c-cli-)[0-9.]+(?=\.tgz)' Formula/b2c-cli.rb) |
| 28 | + echo "Current formula version: $CURRENT" |
| 29 | +
|
| 30 | + echo "latest=$LATEST" >> $GITHUB_OUTPUT |
| 31 | + echo "current=$CURRENT" >> $GITHUB_OUTPUT |
| 32 | +
|
| 33 | + if [ "$LATEST" != "$CURRENT" ]; then |
| 34 | + echo "Update available: $CURRENT -> $LATEST" |
| 35 | + echo "needs_update=true" >> $GITHUB_OUTPUT |
| 36 | + else |
| 37 | + echo "Already up to date" |
| 38 | + echo "needs_update=false" >> $GITHUB_OUTPUT |
| 39 | + fi |
| 40 | +
|
| 41 | + - name: Update formula |
| 42 | + if: steps.check.outputs.needs_update == 'true' |
| 43 | + env: |
| 44 | + LATEST: ${{ steps.check.outputs.latest }} |
| 45 | + CURRENT: ${{ steps.check.outputs.current }} |
| 46 | + run: | |
| 47 | + # Calculate new sha256 |
| 48 | + NEW_SHA=$(curl -sL "https://registry.npmjs.org/@salesforce/b2c-cli/-/b2c-cli-${LATEST}.tgz" | sha256sum | cut -d' ' -f1) |
| 49 | + echo "New sha256: $NEW_SHA" |
| 50 | +
|
| 51 | + # Update URL |
| 52 | + sed -i "s|b2c-cli-${CURRENT}\.tgz|b2c-cli-${LATEST}.tgz|g" Formula/b2c-cli.rb |
| 53 | +
|
| 54 | + # Update sha256 |
| 55 | + sed -i "s|sha256 \"[a-f0-9]*\"|sha256 \"${NEW_SHA}\"|g" Formula/b2c-cli.rb |
| 56 | +
|
| 57 | + echo "Updated formula:" |
| 58 | + cat Formula/b2c-cli.rb |
| 59 | +
|
| 60 | + - name: Create Pull Request |
| 61 | + if: steps.check.outputs.needs_update == 'true' |
| 62 | + uses: actions/github-script@v7 |
| 63 | + with: |
| 64 | + script: | |
| 65 | + const branch = `update-b2c-cli-${{ steps.check.outputs.latest }}`; |
| 66 | + const base = 'main'; |
| 67 | +
|
| 68 | + // Get the default branch SHA |
| 69 | + const { data: ref } = await github.rest.git.getRef({ |
| 70 | + owner: context.repo.owner, |
| 71 | + repo: context.repo.repo, |
| 72 | + ref: `heads/${base}` |
| 73 | + }); |
| 74 | +
|
| 75 | + // Create new branch |
| 76 | + try { |
| 77 | + await github.rest.git.createRef({ |
| 78 | + owner: context.repo.owner, |
| 79 | + repo: context.repo.repo, |
| 80 | + ref: `refs/heads/${branch}`, |
| 81 | + sha: ref.object.sha |
| 82 | + }); |
| 83 | + } catch (e) { |
| 84 | + if (e.status === 422) { |
| 85 | + console.log('Branch already exists, updating...'); |
| 86 | + } else { |
| 87 | + throw e; |
| 88 | + } |
| 89 | + } |
| 90 | +
|
| 91 | + // Get current file content |
| 92 | + const { data: file } = await github.rest.repos.getContent({ |
| 93 | + owner: context.repo.owner, |
| 94 | + repo: context.repo.repo, |
| 95 | + path: 'Formula/b2c-cli.rb', |
| 96 | + ref: base |
| 97 | + }); |
| 98 | +
|
| 99 | + // Read updated file from workspace |
| 100 | + const fs = require('fs'); |
| 101 | + const newContent = fs.readFileSync('Formula/b2c-cli.rb', 'utf8'); |
| 102 | +
|
| 103 | + // Update file on branch |
| 104 | + await github.rest.repos.createOrUpdateFileContents({ |
| 105 | + owner: context.repo.owner, |
| 106 | + repo: context.repo.repo, |
| 107 | + path: 'Formula/b2c-cli.rb', |
| 108 | + message: `b2c-cli: update to ${{ steps.check.outputs.latest }}`, |
| 109 | + content: Buffer.from(newContent).toString('base64'), |
| 110 | + sha: file.sha, |
| 111 | + branch: branch |
| 112 | + }); |
| 113 | +
|
| 114 | + // Create PR |
| 115 | + const { data: pr } = await github.rest.pulls.create({ |
| 116 | + owner: context.repo.owner, |
| 117 | + repo: context.repo.repo, |
| 118 | + title: `b2c-cli: update to ${{ steps.check.outputs.latest }}`, |
| 119 | + body: `Updates \`@salesforce/b2c-cli\` from ${{ steps.check.outputs.current }} to ${{ steps.check.outputs.latest }}.\n\n[npm package](https://www.npmjs.com/package/@salesforce/b2c-cli)\n[Changelog](https://github.com/SalesforceCommerceCloud/b2c-developer-tooling/releases)`, |
| 120 | + head: branch, |
| 121 | + base: base |
| 122 | + }); |
| 123 | +
|
| 124 | + console.log(`Created PR #${pr.number}: ${pr.html_url}`); |
0 commit comments