Skip to content

Commit

Permalink
Refactor retrieval of pr-branch
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-bc authored Oct 5, 2023
2 parents 6188632 + 139217f commit af9f919
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 31 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/build-tutorial-hello-world.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ on:
- '.github/ISSUE_TEMPLATE/**'

jobs:
get-branch:
name: "Get target branch"
uses: ./.github/workflows/reusable-get-pr-branch.yml
with:
target_repository: fprime-community/fprime-tutorial-hello-world

run:
needs: get-branch
name: ""
uses: ./.github/workflows/reusable-builder.yml
uses: ./.github/workflows/reusable-project-builder.yml
with:
target_repository: fprime-community/fprime-tutorial-hello-world
build_location: HelloWorldDeployment
run_unit_tests: true
target_ref: ${{ needs.get-branch.outputs.target-branch }}
10 changes: 9 additions & 1 deletion .github/workflows/build-tutorial-led-blinker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ on:
- '.github/ISSUE_TEMPLATE/**'

jobs:
get-branch:
name: "Get target branch"
uses: ./.github/workflows/reusable-get-pr-branch.yml
with:
target_repository: fprime-community/fprime-workshop-led-blinker

run:
needs: get-branch
name: ""
uses: ./.github/workflows/reusable-builder.yml
uses: ./.github/workflows/reusable-project-builder.yml
with:
target_repository: fprime-community/fprime-workshop-led-blinker
build_location: LedBlinker
run_unit_tests: true
target_ref: ${{ needs.get-branch.outputs.target-branch }}
10 changes: 9 additions & 1 deletion .github/workflows/build-tutorial-math-comp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ on:
- '.github/ISSUE_TEMPLATE/**'

jobs:
get-branch:
name: "Get target branch"
uses: ./.github/workflows/reusable-get-pr-branch.yml
with:
target_repository: fprime-community/fprime-tutorial-math-component

run:
needs: get-branch
name: ""
uses: ./.github/workflows/reusable-builder.yml
uses: ./.github/workflows/reusable-project-builder.yml
with:
target_repository: fprime-community/fprime-tutorial-math-component
build_location: MathDeployment
run_unit_tests: true
target_ref: ${{ needs.get-branch.outputs.target-branch }}
42 changes: 42 additions & 0 deletions .github/workflows/reusable-get-pr-branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# If the event that triggered this action is a PR and has a matching `pr-<number>` branch on
# target_repository, then return the name of that branch. Otherwise, return default_target_ref.
# See the CONTRIBUTING.md for info on why this is used.

name: 'Get PR Branch'

on:
workflow_call:
inputs:
target_repository:
description: 'The repository to check for the PR branch'
type: string
required: true
default_target_ref:
description: 'Ref to use if the PR branch is not found'
type: string
required: false
default: devel
outputs:
target-branch:
value: ${{ jobs.runs.outputs.target-branch }}

jobs:
runs:
runs-on: "ubuntu-latest"
outputs:
target-branch: ${{ steps.get_target_branch.outputs.TARGET_BRANCH }}
steps:
- name: "Get target branch"
id: get_target_branch
run: |
response_code=`curl -w '%{response_code}' https://api.github.com/repos/${{ inputs.target_repository }}/branches/pr-${{ github.event.number }} -o /dev/null`
if [[ "${{ github.event_name }}" == "pull_request" && "$response_code" == "200" ]]; then
echo "TARGET_BRANCH=pr-${{ github.event.number }}" >> $GITHUB_OUTPUT
echo "PR branch found, using pr-${{ github.event.number }}"
else
echo "TARGET_BRANCH=${{ inputs.default_target_ref }}" >> $GITHUB_OUTPUT
echo "PR branch not found, using ${{ inputs.default_target_ref }}"
fi
shell: bash


Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,17 @@ on:
default: "devel"

jobs:

build:
runs-on: ${{ inputs.runs_on }}
name: "Build"
steps:
# If the PR that triggered this job has a matching `pr-<number>` branch on inputs.target_repository,
# then return the name of that branch. Otherwise, return inputs.target_ref.
- name: "Get PR branch"
id: get_pr_branch
run: |
response_code=`curl -w '%{response_code}' https://api.github.com/repos/${{ inputs.target_repository }}/branches/pr-${{ github.event.number }} -o /dev/null`
if [[ "${{ github.event_name }}" == "pull_request" && "$response_code" == "200" ]]; then
echo "TARGET_BRANCH=pr-${{ github.event.number }}" >> $GITHUB_OUTPUT
echo "PR branch found, using pr-${{ github.event.number }}"
else
echo "TARGET_BRANCH=${{ inputs.target_ref }}" >> $GITHUB_OUTPUT
echo "PR branch not found, using ${{ inputs.target_ref }}"
fi
shell: bash
- name: "Checkout target repository"
uses: actions/checkout@v3
with:
submodules: recursive
repository: ${{ inputs.target_repository }}
ref: ${{ steps.get_pr_branch.outputs.TARGET_BRANCH }}
ref: ${{ inputs.target_ref }}
- name: "Overlay current F´ revision"
uses: actions/checkout@v3
with:
Expand All @@ -90,24 +77,12 @@ jobs:
runs-on: ${{ inputs.runs_on }}
name: "Unit Tests"
steps:
- name: "Get PR branch"
id: get_pr_branch
run: |
response_code=`curl -w '%{response_code}' https://api.github.com/repos/${{ inputs.target_repository }}/branches/pr-${{ github.event.number }} -o /dev/null`
if [[ "${{ github.event_name }}" == "pull_request" && "$response_code" == "200" ]]; then
echo "TARGET_BRANCH=pr-${{ github.event.number }}" >> $GITHUB_OUTPUT
echo "PR branch found, using pr-${{ github.event.number }}"
else
echo "TARGET_BRANCH=${{ inputs.target_ref }}" >> $GITHUB_OUTPUT
echo "PR branch not found, using ${{ inputs.target_ref }}"
fi
shell: bash
- name: "Checkout target repository"
uses: actions/checkout@v3
with:
submodules: recursive
repository: ${{ inputs.target_repository }}
ref: ${{ steps.get_pr_branch.outputs.TARGET_BRANCH }}
ref: ${{ inputs.target_ref }}
- name: "Overlay current F´ revision"
uses: actions/checkout@v3
with:
Expand Down

0 comments on commit af9f919

Please sign in to comment.