Skip to content

chore: [FX-4058] update jira deployments GH Action #9

chore: [FX-4058] update jira deployments GH Action

chore: [FX-4058] update jira deployments GH Action #9

on:
# push:
# branches:
# - main
pull_request:
branches:
- master # triggers the flow for every PR to master
- 'feature/**' # triggers the flow for a PR to a branch like feature/v9
types:
- synchronize # PR was updated
- opened # PR was open
- reopened # PR was closed and is now open again
- ready_for_review # PR was converted from draft to open
jobs:
create-jira-deployment:
runs-on: ubuntu-latest
name: Create JIRA deployment
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OWNER: toptal
REPO: picasso
ENV_NAME: temploy
steps:
- name: Checkout code
uses: actions/checkout@v2
- id: branch
uses: xt0rted/[email protected]
- name: Track deployment
shell: bash
run: |
BASE_REF="${{ steps.branch.outputs.base_ref }}"
HEAD_REF="${{ steps.branch.outputs.head_ref }}"
requiredVars=("GITHUB_TOKEN" "OWNER" "REPO" "BASE_REF" "HEAD_REF")
for req in ${requiredVars[@]}; do
if [ -z "${!req}" ]; then
echo "Reqired variable '${req}' is blank or not defined"
allSet=false
fi
done
[[ "${allSet}" == false ]] && exit 1
NUMCOMMITS=$(curl --silent -H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
https://api.github.com/repos/${OWNER}/${REPO}/compare/${BASE_REF}...${HEAD_REF} |
jq '.commits | length')
echo "Number of commits: ${NUMCOMMITS}"
if [ ${NUMCOMMITS} -eq 0 ]; then
echo "[]"
exit 0
fi
JSON_PAYLOAD=$(jq -cn '
{
query: $query,
variables: {
"repoOwner": $repoOwnerVar,
"repoName": $repoNameVar,
"numCommits": $numCommitsVar,
"headRef": $headRefVar
}
}' \
--arg query '
query ($repoOwner: String!, $repoName: String!, $headRef: String!, $numCommits: Int!) {
repository(name: $repoName, owner: $repoOwner) {
commit: object(expression: $headRef) {
... on Commit {
history(first: $numCommits) {
pageInfo {
hasNextPage
startCursor
endCursor
}
nodes {
associatedPullRequests(first: 100) {
nodes {
title
body
}
}
}
}
}
}
}
}' \
--arg repoOwnerVar "$OWNER" \
--arg repoNameVar "$REPO" \
--argjson numCommitsVar ${NUMCOMMITS} \
--arg headRefVar "$HEAD_REF")
RESPONSE=$(curl --silent -H "Authorization: Bearer ${GITHUB_TOKEN}" \
-H "Content-Type: application/json" \
https://api.github.com/graphql \
-d "$JSON_PAYLOAD")
echo "$RESPONSE"
JIRA_KEYS=$(echo "$RESPONSE" | jq -c '[ .data.repository.commit.history.nodes[].associatedPullRequests.nodes[] | [.title, .body] ]?
| flatten
| map(capture("^\\[(?<jirakey>[A-Z]{2,}-[0-9]+)\\]"))
| map(.jirakey)
| unique')
# Send deployment info to JIRA
if [ -n "$JIRA_KEYS" ]; then
# Replace this with the appropriate command to send deployment info to JIRA
echo "Sending deployment info to JIRA: $JIRA_KEYS"
else
echo "No issue keys found, skipping deployment reporting"
fi