|
| 1 | +--- |
| 2 | +name: Upstream Sync Workflow |
| 3 | +'on': |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + branch: |
| 7 | + description: "OpenStack release name you wish to sync against, for example; xena or wallaby" |
| 8 | + required: true |
| 9 | + type: string |
| 10 | +jobs: |
| 11 | + upstream-sync: |
| 12 | + runs-on: ubuntu-20.04 |
| 13 | + env: |
| 14 | + downstream-prefix: stackhpc |
| 15 | + upstream-prefix: openstack:stable |
| 16 | + steps: |
| 17 | + - name: Check if ${{ format('{0}/{1}', env.upstream-prefix, inputs.branch) }} is ahead of ${{ format('{0}/{1}', env.downstream-prefix, inputs.branch) }} |
| 18 | + uses: actions/github-script@9ac08808f993958e9de277fe43a64532a609130e |
| 19 | + id: check-if-ahead |
| 20 | + with: |
| 21 | + script: | |
| 22 | + const { repo, owner } = context.repo; |
| 23 | + const result = await github.rest.repos.compareCommits({ |
| 24 | + owner, |
| 25 | + repo, |
| 26 | + head: '${{ format('{0}/{1}', env.upstream-prefix, inputs.branch) }}', |
| 27 | + base: '${{ format('{0}/{1}', env.downstream-prefix, inputs.branch) }}', |
| 28 | + }); |
| 29 | + return result.data['ahead_by'] > 0 ? 'true' : 'false'; |
| 30 | + result-encoding: string |
| 31 | + - name: Get upstream SHA |
| 32 | + uses: actions/github-script@9ac08808f993958e9de277fe43a64532a609130e |
| 33 | + id: get-upstream-sha |
| 34 | + with: |
| 35 | + script: | |
| 36 | + if(${{ steps.check-if-ahead.outputs.result }} == true) { |
| 37 | + const { repo, owner } = context.repo; |
| 38 | + const result = await github.rest.repos.getCommit({ |
| 39 | + owner: '${{ format('openstack') }}', |
| 40 | + repo: repo, |
| 41 | + ref: '${{ format('{0}/{1}', 'stable', inputs.branch) }}', |
| 42 | + }); |
| 43 | + return result.data['sha']; |
| 44 | + } |
| 45 | + result-encoding: string |
| 46 | + - name: Create copy of upstream branch |
| 47 | + uses: actions/github-script@9ac08808f993958e9de277fe43a64532a609130e |
| 48 | + id: branch-name |
| 49 | + with: |
| 50 | + script: | |
| 51 | + if(${{ steps.check-if-ahead.outputs.result }} == true) { |
| 52 | + const { repo, owner } = context.repo; |
| 53 | + const date = new Date(); |
| 54 | + const prDate = `${date.getFullYear()}-${date.getMonth()}-${date.getDay()}`; |
| 55 | + const branchName = '${{ format('upstream-{0}-', inputs.branch) }}' + prDate; |
| 56 | + const result = await github.rest.git.createRef({ |
| 57 | + owner, |
| 58 | + repo, |
| 59 | + ref: 'refs/heads/' + branchName, |
| 60 | + sha: '${{ steps.get-upstream-sha.outputs.result }}', |
| 61 | + }); |
| 62 | + return branchName; |
| 63 | + } |
| 64 | + result-encoding: string |
| 65 | + - name: Create PR ${{ format('{0}/{1}', env.downstream-prefix, inputs.branch) }} with ${{ steps.branch-name.outputs.result }} |
| 66 | + uses: actions/github-script@9ac08808f993958e9de277fe43a64532a609130e |
| 67 | + with: |
| 68 | + script: | |
| 69 | + if(${{ steps.check-if-ahead.outputs.result }} == true) { |
| 70 | + const { repo, owner } = context.repo; |
| 71 | + const result = await github.rest.pulls.create({ |
| 72 | + title: 'Synchronise ${{ inputs.branch }} with ${{ steps.branch-name.outputs.result }}', |
| 73 | + owner, |
| 74 | + repo, |
| 75 | + head: '${{ steps.branch-name.outputs.result }}', |
| 76 | + base: '${{ format('{0}/{1}', env.downstream-prefix, inputs.branch) }}', |
| 77 | + maintainer_can_modify: false, |
| 78 | + body: [ |
| 79 | + 'The changes from ${{ env.upstream-prefix }}/${{ inputs.branch }} have been included in this automated PR' |
| 80 | + ].join('\n') |
| 81 | + }); |
| 82 | + } |
0 commit comments