-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.bit.pull-request
More file actions
77 lines (64 loc) · 3.18 KB
/
Copy pathgithub.bit.pull-request
File metadata and controls
77 lines (64 loc) · 3.18 KB
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
#!/bin/bash
# Check if $BIT_CONFIG_ACCESS_TOKEN is set
if [[ -z "$BIT_CONFIG_USER_TOKEN" && -z "$BIT_CONFIG_ACCESS_TOKEN" ]]; then
echo "Error: BIT_CONFIG_ACCESS_TOKEN environment variable is not set. It is required!"
exit 1
fi
# Assign BIT_CONFIG_ACCESS_TOKEN to BIT_CONFIG_USER_TOKEN
if [[ -z "$BIT_CONFIG_USER_TOKEN" ]]; then
BIT_CONFIG_USER_TOKEN="$BIT_CONFIG_ACCESS_TOKEN"
export BIT_CONFIG_USER_TOKEN
fi
# Ensure required environment variables are set
if [[ -z "$GITHUB_TOKEN" ]]; then
echo "Error: GITHUB_TOKEN environment variable is not set"
exit 1
fi
# Ensure the event is a pull request
if [[ -z "$GITHUB_EVENT_NAME" || "$GITHUB_EVENT_NAME" != "pull_request" ]]; then
echo "Error: Ensure this action is triggered by a GitHub pull request"
exit 1
fi
# Get the pull request number from the GitHub context
PR_NUMBER=$(jq --raw-output .number "$GITHUB_EVENT_PATH")
# Determine if there are any new or modified components
statusRaw=$(bit status --strict --json)
newComponentsCount=$(echo "$statusRaw" | jq '.newComponents | length')
modifiedComponentsCount=$(echo "$statusRaw" | jq '.modifiedComponents | length')
laneName="pr-$PR_NUMBER"
if [ "$newComponentsCount" -gt 0 ] || [ "$modifiedComponentsCount" -gt 0 ]; then
laneLink="https://bit.cloud/${ORG}/${SCOPE}/~lane/${laneName}"
bit lane create "${laneName}" || exit 1
bit snap -m "CI" --build || exit 1
# Operations on bit lane
bit lane remove "${ORG}.${SCOPE}/${laneName}" --remote --silent --force &> /dev/null || echo "Warning: Failed to remove the lane."
bit export || exit 1
# Fetch existing comments from the pull request using GitHub API
existingComments=$(curl --header "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments")
# Search for a comment containing the link
commentID=$(echo "$existingComments" | jq '.[] | select(.body | contains("Please review the changes in the Bit lane: https://bit.cloud")) | .id')
getHumanReadableTimestamp() {
date -u +"%B %d, %Y, %I:%M:%S %p UTC"
}
currentTime=$(getHumanReadableTimestamp)
if [[ ! -z "$commentID" ]]; then
# If such a comment is found, update it
commentBody="⚠️ Please review the changes in the Bit lane: ${laneLink}\n\n_Lane updated: ${currentTime}_"
curl --request PATCH \
--header "Authorization: token $GITHUB_TOKEN" \
--header "Content-Type: application/json" \
--data '{ "body": "'"$commentBody"'" }' \
"https://api.github.com/repos/$GITHUB_REPOSITORY/issues/comments/$commentID"
echo "Updated existing comment."
else
# If no such comment is found, post a new comment
commentBody="⚠️ Please review the changes in the Bit lane: ${laneLink}\n\n_Lane created: ${currentTime}_"
curl --request POST \
--header "Authorization: token $GITHUB_TOKEN" \
--header "Content-Type: application/json" \
--data '{ "body": "'"$commentBody"'" }' \
"https://api.github.com/repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments"
echo "Posted a new comment."
fi
fi