-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction.yml
160 lines (159 loc) · 9.54 KB
/
action.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
name: 'Pull Request notification for tasks'
description: 'This action is used to send notification to eXo Tasks during the Pull Request workflow (creation, approval, closing, merge).'
inputs:
SERVER_URL:
description: Target Server URL
required: true
SERVER_DEFAULT_SITENAME:
description: Target Server Default Sitename
required: false
default: "dw"
SERVER_USERNAME:
description: Target Server username
required: true
SERVER_PASSWORD:
description: Target Server user password
required: true
TASKS_REGEX_FILTER:
description: Pull request tasks regex filter
required: false
default: "(task|maint|exo)((-|_)[0-9]{4,})+"
runs:
using: "composite"
steps:
- name: PLF Tasks Webhook
shell: bash
run: |
doGetCurl() {
curl -A "PR Webhook Tasks/1.0" -s -f -L -u ${SERVER_USERNAME}:${SERVER_PASSWORD} -XGET $*
}
getAssociatedTribeAccount() {
[ -f /tmp/ghtribe.json ] || return
jq -r ".[] | select(.githubid == \"$1\") | .tribeid" /tmp/ghtribe.json
}
if [[ ! ${base_branch_name} =~ ^(master|develop(-exo|-meed)?|feature\/[A-Za-z-]+[0-9]?|stable\/[0-9]+(\.[0-9]+)*\.x(-exo)?)$ ]]; then
echo "❌ Branch ${base_branch_name} is not supported for Task notification. Abort"
exit 0
fi
if [[ ${creator} =~ ^(dependabot\[bot\]|snyk-bot)$ ]]; then
echo "❌ PR created with Bot users is not supported for Task notification. Abort"
exit 0
fi
if ! echo ${message:-} | grep -qioP "${TASKS_REGEX_FILTER}"; then
echo "No tasks found! Abort."
exit 0
fi
TASKS_IDS="$(echo ${message:-} | grep -ioP "${TASKS_REGEX_FILTER}" | grep -oP '[0-9]+' | xargs)"
echo "OK Task(s) found! Starting notifications..."
reducedBaseBranchName=$(echo ${base_branch_name} | sed -e 's|feature/||gi' -e 's|stable/||gi')
link="PR <a href=\"https://github.com/${full_repo_name}/pull/${pull_number}\" title="${full_repo_name}#${pull_number}:${base_branch_name}" target="_blank">${repo_name}#${pull_number}:${reducedBaseBranchName}</a>"
curl -A "PR Webhook Tasks/1.0" -s -f -L "${SERVER_GAMGH_MAPPING_FILE_URL}" > /tmp/ghtribe.json
if [ "${{ github.event_name }}" = "pull_request" ]; then
if [ "${{ github.event.action }}" = "review_requested" ]; then
serverUser=$(getAssociatedTribeAccount ${requested_reviewer})
if [ ! -z "${serverUser}" ]; then
echo "Requested reviewer is: ${serverUser}."
msg="💭 $link requested a review from @${serverUser} "
else
echo "❌ Could not get Server user identifier! Abort"
exit 0
fi
elif [ "${{ github.event.pull_request.merged }}" = "true" ]; then
shortCommitId=$(echo ${{ github.event.pull_request.merge_commit_sha }} | cut -c1-7)
mergeMethod="merged"
if [ "${{ github.event.pull_request.auto_merge }}" != "null" ] && [ "${{ github.event.pull_request.auto_merge }}" != "" ]; then
mergeMethod="auto ${{ github.event.pull_request.auto_merge.merge_method }}-merged"
fi
mergedCommitlink="<a href=\"https://github.com/${full_repo_name}/commit/${{ github.event.pull_request.merge_commit_sha }}\" target="_blank">${shortCommitId}</a>"
base_branch_link="<a href=\"https://github.com/${full_repo_name}/tree/${base_branch_name}\" target="_blank">${base_branch_name}</a>"
mergerGithubUser="${{ github.event.pull_request.merged_by.login }}"
mergerServerUser=$(getAssociatedTribeAccount ${mergerGithubUser})
mergerServerProfile=$(doGetCurl "${SERVER_URL}/rest/private/v1/social/users/${mergerServerUser}" 2>/dev/null || echo "")
mergerServerFullName="$(echo ${mergerServerProfile} | jq -r .fullname)"
mergerServerLink="<a target=\"_self\" rel=\"noopener\" href=\"/portal/${SERVER_DEFAULT_SITENAME}/profile/${mergerServerUser}\" class=\"user-suggester\">${mergerServerFullName}</a>"
if [ -z "${mergerServerFullName:-}" ] || [ "${mergerServerFullName:-}" = "null" ]; then
msg="🌟 $link has <tt>${mergeMethod}</tt> ${mergedCommitlink} into ${base_branch_link}."
else
msg="🌟 $link has <tt>${mergeMethod}</tt> ${mergedCommitlink} into ${base_branch_link} by ${mergerServerLink}."
fi
elif [ "${{ github.event.action }}" = "closed" ]; then
msg="🍂 $link has been <tt>${{ github.event.action }}</tt>."
elif [ "${{ github.event.action }}" = "opened" ]; then
msg="🌱 $link has been <tt>created</tt>."
elif [ "${{ github.event.action }}" = "reopened" ]; then
msg="🍃 $link has been <tt>${{ github.event.action }}</tt>."
else
msg="ℹ️ $link has been <tt>${{ github.event.action }}</tt>."
fi
elif [ "${{ github.event_name }}" = "pull_request_review" ] && [ "${{ github.event.action }}" = "submitted" ]; then
mentionCreator=""
commentMentionFilterRegex='( |^)@[a-zA-Z0-9]+-?[a-zA-Z0-9]+( |$)'
response=$(getAssociatedTribeAccount ${creator})
[ -z "${response}" ] || mentionCreator=" FYI @${response} "
reviewerGithubUser="${{ github.event.review.user.login }}"
reviewerServerUser=$(getAssociatedTribeAccount ${reviewerGithubUser})
reviewerServerProfile=$(doGetCurl "${SERVER_URL}/rest/private/v1/social/users/${reviewerServerUser}" 2>/dev/null || echo "")
reviewerServerFullName="$(echo ${reviewerServerProfile} | jq -r .fullname)"
reviewerServerLink="<a target=\"_self\" rel=\"noopener\" href=\"/portal/${SERVER_DEFAULT_SITENAME}/profile/${reviewerServerUser}\" class=\"user-suggester\">${reviewerServerFullName}</a>"
reviewEventLink="${{ github.event.review.html_url }}"
if [ "${{ github.event.review.state }}" = "changes_requested" ]; then
requestChangesEventLink="<a href=\"$reviewEventLink\">changes</a>"
if [ -z "${reviewerServerFullName:-}" ]; then
msg="🛠️ $link requires ${requestChangesEventLink}.${mentionCreator}"
else
msg="🛠️ $link requires ${requestChangesEventLink} from ${reviewerServerLink}.${mentionCreator}"
fi
elif [ "${{ github.event.review.state }}" = "approved" ]; then
approvedEventLink="<a href=\"$reviewEventLink\">approved</a>"
if [ -z "${reviewerServerFullName:-}" ]; then
msg="✅ $link has been ${approvedEventLink}.${mentionCreator}"
else
msg="✅ $link has been ${approvedEventLink} by ${reviewerServerLink}.${mentionCreator}"
fi
elif [ "${{ github.event.review.state }}" = "commented" ]; then
if [[ "${{ github.event.review.body }}" =~ $commentMentionFilterRegex ]]; then
mentionEventLink="<a href=\"$reviewEventLink\" target="_blank">mentioned</a>"
mentionedGithubUsers=$(echo "${{ github.event.review.body }}" | grep -oP "$commentMentionFilterRegex" | tr -d '@' | xargs)
mentionedServerUsers=""
for mentionedGithubUser in ${mentionedGithubUsers}; do
response=$(getAssociatedTribeAccount ${mentionedGithubUser})
[ -z "${response}" ] && continue
[ -z "${mentionedServerUsers}" ] && mentionedServerUsers="@${response}" || mentionedServerUsers="${mentionedServerUsers} and @${response}"
done
[ -z "${mentionedServerUsers}" ] && mentionedServerUsers="$(echo $mentionedGithubUsers | wc -w ) user(s)"
msg="🙋 $link has ${mentionEventLink} ${mentionedServerUsers} in a comment by ${reviewerServerLink}."
else
commentEventLink="<a href=\"$reviewEventLink\" target="_blank">commented</a>"
msg="💬 $link has been ${commentEventLink} by ${reviewerServerLink}.${mentionCreator}"
fi
else
stateEventLink="<a href=\"$reviewEventLink\">${{ github.event.review.state }}</a>"
msg="ℹ️ $link has been ${stateEventLink}."
fi
fi
echo "*** Message is:"
echo ${msg}
echo "***"
for TASK_ID in ${TASKS_IDS}; do
echo "Commenting to Task #${TASK_ID}..."
curl -so /dev/null -w '%{http_code}' --retry 3 --retry-max-time 60 -L -u ${SERVER_USERNAME}:${SERVER_PASSWORD} -XPOST --data-raw "<p>${msg}</p>" "${SERVER_TASK_REST_PREFIXE_URL}/comments/${TASK_ID}"
done
env:
SERVER_URL: ${{ inputs.SERVER_URL }}
SERVER_USERNAME: ${{ inputs.SERVER_USERNAME }}
SERVER_PASSWORD: ${{ inputs.SERVER_PASSWORD }}
SERVER_TASK_REST_PREFIXE_URL: ${{ inputs.SERVER_URL }}/rest/private/tasks
SERVER_GAMGH_MAPPING_FILE_URL: ${{ inputs.SERVER_URL }}/rest/jcr/repository/collaboration/Users/g___/gi___/git___/githubagent/Public/ghtribe.json
TASKS_REGEX_FILTER: ${{ inputs.TASKS_REGEX_FILTER }}
SERVER_DEFAULT_SITENAME: ${{ inputs.SERVER_DEFAULT_SITENAME }}
message: ${{ github.event.pull_request.title }}
state: ${{ github.event.pull_request.state }}
pull_number: ${{ github.event.pull_request.number }}
requested_reviewer: ${{ github.event.requested_reviewer.login }}
creator: ${{ github.event.pull_request.user.login }}
full_repo_name: ${{ github.event.repository.full_name }}
repo_name: ${{ github.event.repository.name }}
base_branch_name: ${{ github.event.pull_request.base.ref }}
branding:
icon: "bell"
color: "gray-dark"