-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae031ab
commit 0bb0f87
Showing
3 changed files
with
142 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# action.yml | ||
name: 'Github Members Resolver' | ||
on: | ||
workflow_call: | ||
inputs: | ||
team: | ||
description: 'Team' | ||
type: string | ||
required: true | ||
branch: | ||
description: 'Branch' | ||
type: string | ||
required: false | ||
default: master | ||
outputs: | ||
members: | ||
value: ${{ jobs.github-members-resolver.outputs.members }} | ||
|
||
jobs: | ||
github-members-resolver: | ||
runs-on: ubuntu-20.04 | ||
outputs: | ||
members: ${{ steps.resolve-members.outputs.members }} | ||
steps: | ||
- name: Resolve Members | ||
id: resolve-members | ||
continue-on-error: true | ||
shell: bash | ||
run: | | ||
githack_host=raw.githack.com | ||
githack_core_repo_url=https://${githack_host}/${{ github.repository }} | ||
github_teams_url=${githack_core_repo_url}/${{ inputs.branch }}/.github/data/github-teams.json | ||
json=$(curl -s ${github_teams_url}) | ||
members=$(jq -r ".[] | select(.team == \"${{ inputs.team }}\") | .members[]" <<< "${json}" | tr '\n' ' ' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') | ||
echo "Found members: [${members}]" | ||
declare -A deduped_array | ||
for element in "${members[@]}"; do | ||
deduped_array["$element"]=1 | ||
done | ||
deduped_members=("${!deduped_array[@]}") | ||
members=$(echo "${deduped_members[@]}") | ||
echo "members=[${members}]" | ||
echo "members=${members}" >> $GITHUB_OUTPUT | ||
- name: Resolve Channels | ||
id: resolve-channels | ||
continue-on-error: true | ||
shell: bash | ||
run: | | ||
github_users=${{ steps.resolve-users.outputs.github_users }} | ||
echo "Found github users: [${github_users}]" | ||
githack_host=raw.githack.com | ||
githack_core_repo_url=https://${githack_host}/${{ github.repository }} | ||
slack_mappings_file=slack-mappings.json | ||
slack_mappings_path=.github/data/${slack_mappings_file} | ||
slack_mapping_url=${githack_core_repo_url}/${{ inputs.branch }}/${slack_mappings_path} | ||
channel_ids= | ||
github_users_array=(${github_users}) | ||
for github_user in "${github_users_array[@]}"; do | ||
if [[ -n "${github_user}" ]]; then | ||
json=$(curl -s ${slack_mapping_url}) | ||
channel_id=$( \ | ||
jq ".[] | select(.github_user == \"${github_user}\")" <<< "${json}" | \ | ||
jq -r '.slack_id' \ | ||
) | ||
echo "Resolved channel id [${channel_id}] from [${slack_mappings_file}]" | ||
if [[ -z "${channel_id}" ]]; then | ||
echo "Channel id could not be resolved from [${slack_mappings_file}]. Attempting to resolve from Github user email." | ||
user_email=$( \ | ||
curl \ | ||
-u ${{ secrets.CI_MACHINE_USER }}:${{ secrets.CI_MACHINE_TOKEN }} \ | ||
--request GET \ | ||
-s \ | ||
https://api.github.com/users/${github_user} | \ | ||
grep "\"email\":" | \ | ||
sed "s/\"email\"://g" | \ | ||
tr -d '",[:space:]' \ | ||
) | ||
echo "Resolved user email: [${user_email}]" | ||
if [[ -n "${user_email}" ]]; then | ||
channel_id=$( \ | ||
curl \ | ||
--request GET \ | ||
--header "Content-type: application/json" \ | ||
--header "Authorization: Bearer ${{ secrets.SLACK_BOT_TOKEN }}" \ | ||
-s \ | ||
"https://slack.com/api/users.lookupByEmail?email=${user_email}" | \ | ||
python3 -m json.tool | \ | ||
grep "\"id\":" | \ | ||
sed "s/\"id\"://g" | \ | ||
tr -d '",[:space:]' \ | ||
) | ||
echo "Resolved channel id [${channel_id}] from email [${user_email}]" | ||
fi | ||
fi | ||
fi | ||
[[ -n "${channel_id}" ]] && channel_ids="${channel_ids} ${channel_id}" | ||
done | ||
default_channel_id=${{ inputs.default_channel_id }} | ||
[[ -z "${channel_ids}" && -n "${default_channel_id}" ]] \ | ||
&& echo "Channel id could not be resolved, defaulting to channel id: [${default_channel_id}]" \ | ||
&& channel_ids=${default_channel_id} | ||
channel_ids=$(echo "${channel_ids}" | tr -d '[:space:]') | ||
if [[ -n "${channel_ids}" ]]; then | ||
channel_ids_array=(${channel_ids}) | ||
declare -A deduped_array | ||
for element in "${channel_ids_array[@]}"; do | ||
deduped_array["$element"]=1 | ||
done | ||
deduped_channel_ids=("${!deduped_array[@]}") | ||
channel_ids=$(printf '%s\n' "${deduped_channel_ids[@]}" | jq -R . | jq -s .) | ||
fi | ||
echo "channel_ids: [${channel_ids}]" | ||
echo "channel_ids=${channel_ids}" >> $GITHUB_OUTPUT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters