Skip to content

Commit

Permalink
Testing Approval
Browse files Browse the repository at this point in the history
  • Loading branch information
mikereiddigital committed Jan 24, 2025
1 parent bd7e00a commit ba79a21
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
40 changes: 40 additions & 0 deletions .github/workflows/reusable_terraform_plan_apply.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ jobs:
- name: Get Destroy Count
if: github.event_name == 'pull_request'
id: get_destroy_count
env:
destroy_threshold: ${{ env.TF_DESTROY_THRESHOLD }}
plan_summary: ${{ steps.show.outputs.summary }}
Expand Down Expand Up @@ -164,6 +165,45 @@ jobs:
});
}
- name: Check for Approval
id: check_approval
if: github.event_name == 'pull_request' && steps.get_destroy_count.outputs.destroy_notify == 'true'
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
github-token: ${{ secrets.pipeline_github_token }}
script: |
const orgName = 'ministryofjustice';
const teamSlug = 'modernisation-platform';
const {data: reviews} = await github.rest.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number
});
const {data: team_members} = await github.rest.teams.listMembersInOrg({
org: orgName,
team_slug: teamSlug
});
const teamMemberLogins = team_members.map(member => member.login);
const approved = reviews.some(review => review.state === 'APPROVED' && teamMemberLogins.includes(review.user.login));
if (approved) {
core.notice('Pull request approved by a member of @ministryofjustice/modernisation-platform');
}
else {
core.setFailed('Terraform plan evaluation detected that resources will be destroyed & so requires approval from a member of @ministryofjustice/modernisation-platform - please contact #ask-modernisation-platform for assistance');
await github.rest.pulls.createReview({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
event: 'REQUEST_CHANGES',
body: 'Terraform plan evaluation detected that resources will be destroyed & so requires approval from @ministryofjustice/modernsation-platform - please contact #ask-modernisation-platform for assistance'
});
}
- name: Post Comment
if: github.event.ref != 'refs/heads/main'
uses: actions/github-script@v7
Expand Down
6 changes: 5 additions & 1 deletion scripts/get-terraform-destroy-count.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ fi

destroy_count=$(echo "$plan_summary" | grep -oE 'Plan: [0-9]+ to add, [0-9]+ to change, [0-9]+ to destroy.' | awk '{print $8}')

destroy_notify=false

echo "destroy_threshold=$destroy_threshold"
echo "destroy_count=$destroy_count"

Expand All @@ -36,10 +38,12 @@ fi
# These checks will print a warning if the destroy count is above the threshold. Useful for trouble-shooting.
if [ "$destroy_count" -gt "$destroy_threshold" ]; then
echo "Warning: There are $destroy_count resources to be destroyed in this plan."
destroy_notify=true
elif [ "$destroy_count" -gt 0 ]; then
echo "There are $destroy_count resources to be destroyed, which is below the set threshold of $DESTROY_THRESHOLD."
else
echo "No resources to be destroyed"
fi

echo "destroy_count=$destroy_count" >> $GITHUB_ENV
echo "destroy_count=$destroy_count" >> $GITHUB_ENV
echo "destroy_notify=$destroy_notify" >> $GITHUB_OUTPUT

0 comments on commit ba79a21

Please sign in to comment.