-
Notifications
You must be signed in to change notification settings - Fork 3
94 lines (76 loc) · 4.05 KB
/
pr-closure.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
name: Deployments after PR closure
on:
pull_request:
types: [closed]
jobs:
update-deployment:
runs-on: ubuntu-latest
permissions:
deployments: write
steps:
- name: Update deployment statuses to Inactive
env:
GITHUB_TOKEN: ${{ github.token }}
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
AUTHORIZATION="Authorization: token $GITHUB_TOKEN"
ACCEPT="Accept: application/vnd.github.ant-man-preview+json"
PR_URL="https://api.github.com/repos/$GITHUB_REPOSITORY/pulls/$GITHUB_PR_NUMBER"
DEPLOYMENTS_URL="https://api.github.com/repos/$GITHUB_REPOSITORY/deployments"
echo "Working with PR URL: $PR_URL"
# Get the pull request head branch
HEAD_BRANCH=$(curl -s -H "$AUTHORIZATION" "$PR_URL" | jq -r '.head.ref')
if [ "$HEAD_BRANCH" == "null" ]; then
echo "No PR available, so no founded environments. Exiting..."
exit 0
fi
# Get the pull request state
PR_STATE=$(curl -s -H "$AUTHORIZATION" "$PR_URL" | jq -r '.state')
if [ "$PR_STATE" != "closed" ]; then
echo "PR not closed. Exiting..."
exit 0
fi
# Remove "feature/" from the head branch
BUILD_NAME=$(echo "$HEAD_BRANCH" | sed 's/feature\///g')
# Function to fetch log and environment URLs from status URL
fetch_status_urls() {
STATUS_URL=$1
STATUS_RESPONSE=$(curl -s -H "$AUTHORIZATION" "$STATUS_URL")
FIRST_STATUS=$(echo "$STATUS_RESPONSE" | jq -r '.[0]')
LOG_URL=$(echo "$FIRST_STATUS" | jq -r '.log_url')
ENVIRONMENT_URL=$(echo "$FIRST_STATUS" | jq -r '.environment_url')
echo "$LOG_URL,$ENVIRONMENT_URL"
}
# Function to set inactive state to the latest deployment
set_inactive_latest_deployment() {
local ENVIRONMENT=$1
local ENVIRONMENT_ENCODED=$(echo "$ENVIRONMENT" | jq -R -r @uri)
# Get the latest active deployment
local DEPLOYMENTS_FULL_URL="$DEPLOYMENTS_URL?environment=$ENVIRONMENT_ENCODED"
local DEPLOYMENT_DATA=$(curl -s -H "$AUTHORIZATION" "$DEPLOYMENTS_FULL_URL" | jq '.[0]')
if [ "$DEPLOYMENT_DATA" != "null" ]; then
local DEPLOYMENT_ID=$(echo "$DEPLOYMENT_DATA" | jq -r '.id')
local STATUSES_URL=$(echo "$DEPLOYMENT_DATA" | jq -r '.statuses_url')
echo "Working with $ENVIRONMENT deployment ID: $DEPLOYMENT_ID"
local URLS=$(fetch_status_urls "$STATUSES_URL")
IFS=',' read -r LOG_URL ENVIRONMENT_URL <<< "$URLS"
# Mark the latest deployment as inactive
local DEPLOYMENT_URL="$DEPLOYMENTS_URL/$DEPLOYMENT_ID/statuses"
local BODY="{\"state\": \"inactive\", \"description\": \"The PR is closed.\", \
\"log_url\": \"$LOG_URL\", \"environment_url\": \"$ENVIRONMENT_URL\"}"
local RESPONSE=$(curl -s -X POST -H "$AUTHORIZATION" -H "$ACCEPT" "$DEPLOYMENT_URL" -d "$BODY")
# Extract information from the response
local RESPONSE_STATE=$(echo "$RESPONSE" | jq -r '.state')
local RESPONSE_ENVIRONMENT=$(echo "$RESPONSE" | jq -r '.environment')
# Output the information
echo "-> Update $RESPONSE_ENVIRONMENT latest deployment to $RESPONSE_STATE state."
else
echo "-> No deployment for $ENVIRONMENT environment. No update."
fi
}
# Set inactive state to the latest deployment of branch environments
set_inactive_latest_deployment "playsrg-ios-nightly+$BUILD_NAME"
set_inactive_latest_deployment "playsrg-tvos-nightly+$BUILD_NAME"
set_inactive_latest_deployment "playsrg-ios-beta+$BUILD_NAME"
set_inactive_latest_deployment "playsrg-tvos-beta+$BUILD_NAME"