Skip to content

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

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

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

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 }}
CLIENT_ID: ${{ secrets.CLIENT_ID }}
CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}
OWNER: toptal
REPO: picasso
ENV_NAME: temploy
steps:
- name: Checkout code
uses: actions/checkout@v2
# - name: Install Dependencies
# uses: toptal/davinci-github-actions/[email protected]
- id: branch
uses: xt0rted/[email protected]
- name: Check ENV Variables
run: |
echo "BASE_REF=${{ steps.branch.outputs.base_ref }}" >> $GITHUB_ENV
echo "HEAD_REF=${{ steps.branch.outputs.head_ref }}" >> $GITHUB_ENV
# - name: Check ENV Variables
# run: |
# requiredVars=("GITHUB_TOKEN" "OWNER" "REPO" "ENV_NAME" "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
- name: Get JIRA tickets
id: get-jira-tickets
run: |
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
# | join(",")' | tr -d '"'
# )
# JIRA_KEYS=$(echo "$RESPONSE" | jq -r '[ .data.repository.commit.history.nodes[].associatedPullRequests.nodes[] | [.title, .body] | join(" ") | gsub("\n"; " ") | split(" ") | .[] | select(test("^[A-Z]{2,}-[0-9]+$")) ] | unique | join(",")')
JIRA_KEYS=$(echo "$RESPONSE" | jq -r '[ .data.repository.commit.history.nodes[].associatedPullRequests.nodes[] | [.title, .body] | join(" ") | gsub("\n"; " ") | split(" ") | .[] | capture("\\[(?<jirakey>[A-Z]{2,}-[0-9]+)\\]") | .jirakey ] | unique | join(",")')
# | tr -d '"'
echo "JIRA_KEYS=$JIRA_KEYS" >> $GITHUB_ENV
if [ -n "$JIRA_KEYS" ]; then
echo "Found JIRA tickets: $JIRA_KEYS"
else
echo "No JIRA tickets found, skipping deployment reporting"
fi
- name: Convert your JIRA base URL into a Cloud ID
if: ${{ env.JIRA_KEYS != '' }}
env:
JIRA_BASE_URL: ${{ secrets.CLOUD_INSTANCE_BASE_URL }}
run: |
CLOUD_ID_TOKEN=$(\
curl "${JIRA_BASE_URL}/_edge/tenant_info" | \
jq --raw-output '.cloudId'
)
echo "CLOUD_ID_TOKEN=$CLOUD_ID_TOKEN" >> $GITHUB_ENV
- name: Get Access Token
if: ${{ env.JIRA_KEYS != '' }}
run: |
ACCESS_TOKEN=$(\
curl --silent \
--request POST \
--url "https://api.atlassian.com/oauth/token" \
--header "Content-Type: application/json" \
--data '{
"grant_type": "client_credentials",
"client_id": "'${CLIENT_ID}'",
"client_secret": "'${CLIENT_SECRET}'",
"audience": "api.atlassian.com"
}' | \
jq --raw-output '.access_token'
)
echo "ACCESS_TOKEN=$ACCESS_TOKEN" >> $GITHUB_ENV
- name: Send request to Create new JIRA deployment
if: ${{ env.JIRA_KEYS != '' }}
run: |
# RESPONSE=$(curl --silent \
# --request POST \
# --url "https://api.atlassian.com/jira/deployments/0.1/cloud/${CLOUD_ID_TOKEN}/bulk" \
# --header "Authorization: Bearer ${ACCESS_TOKEN}" \
# --header "Accept: application/json" \
# --header "Content-Type: application/json" \
# --data '{
# "deployments": [
# {
# "deploymentSequenceNumber": '${{ github.run_number }}',
# "updateSequenceNumber": '${{ github.run_number }}',
# "displayName": "Deployment #${{ github.run_number }} from branch: ${{ github.head_ref }}",
# "url": "http://picasso.toptal.net/${{ github.head_ref }}",
# "description": "Deployment ${{ github.workflow }} (Run: ${{ github.run_number }}) for env: '${{ env.ENV_NAME }}' from branch: ${{ github.head_ref }}",
# "lastUpdated": '${{ github.event.pull_request.updated_at || github.event.head_commit.timestamp }}',
# "label": "Deployment",
# "state": "successful",
# "pipeline": {
# "id": "'${{ github.repository }}-${{ github.workflow }}'",
# "displayName": "'${{ github.repository }}-${{ github.workflow }}'",
# "url": "${{ github.event.repository.url }}/actions/runs/${{ github.run_id }}"
# },
# "environment": {
# "id": "'${{ github.repository }}-${{ env.ENV_NAME }}'",
# "displayName": "'${{ github.repository }}-${{ env.ENV_NAME }}'",
# "type": "${{ env.ENV_NAME }}"
# },
# "association": {
# "type": "issueIdOrKeys",
# "values": [
# '${{ env.JIRA_KEYS }}'
# ]
# }
# }
# ]
# }' | \
# jq --raw-output '.'
# )
JIRA_KEYS_QUOTED=$(echo "$JIRA_KEYS" | sed "s/,/\",\"/g")
JIRA_KEYS_QUOTED="\"$JIRA_KEYS_QUOTED\""
echo "${JIRA_KEYS_QUOTED}"
# echo "$RESPONSE"
echo "$JIRA_KEYS"
echo "${{ env.JIRA_KEYS }}"
# - name: Send Deployment Info
# if: ${{ env.JIRA_KEYS != '' }}
# uses: ./.github/actions/send-deployment-info
# # run: node ./.github/actions/send-deployment-info.js
# with:
# client-id: ${{ secrets.CLIENT_ID }}
# client-secret: ${{ secrets.CLIENT_SECRET }}
# cloud-instance-base-url: ${{ secrets.CLOUD_INSTANCE_BASE_URL }}
# display-name: "Deployment #${{ github.run_number }} from branch: ${{ github.head_ref }}"
# description: "Deployment ${{ github.workflow }} (Run: ${{ github.run_number }}) for env: '${{ env.ENV_NAME }}' from branch: ${{ github.head_ref }}"
# environment: ${{ env.ENV_NAME }}
# jira-keys: ${{ env.JIRA_KEYS }}
# label: "deployment-${{ env.ENV_NAME }}"
# state: success