Grant Permissions #5
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
name: Grant Permissions | |
on: | |
workflow_dispatch: | |
# inputs: | |
# add-usernames: | |
# description: 'Usernames to grant permissions (comma-separated)' | |
# required: true | |
# remove-usernames: | |
# description: 'Usernames to remove permissions (comma-separated)' | |
# required: true | |
jobs: | |
add-collaborator: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Remove existing collaborators | |
if: github.actor == 'texas-egads' | |
run: | | |
REPO_OWNER="EGaDSAustin" | |
REPO_NAME="${{ secrets.REPO_NAME }}" | |
GH_TOKEN="${{ secrets.GH_TOKEN }}" | |
# Split comma-separated list of usernames to remove into an array | |
IFS=',' read -ra REMOVE_USERNAMES <<< "${{ secrets.REMOVE_NAMES }}" | |
for USERNAME in "${REMOVE_USERNAMES[@]}"; do | |
# Remove the user as a collaborator | |
curl -X DELETE -H "Authorization: token $GH_TOKEN" \ | |
"https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/collaborators/$USERNAME" | |
echo "Removed collaborator: $USERNAME" | |
done | |
env: | |
GH_TOKEN: ${{ secrets.GH_TOKEN }} | |
- name: Add collaborators | |
if: github.actor == 'texas-egads' | |
run: | | |
REPO_OWNER="EGaDSAustin" | |
REPO_NAME="${{ secrets.REPO_NAME }}" | |
GH_TOKEN="${{ secrets.GH_TOKEN }}" | |
# Split comma-separated list of usernames to add into an array | |
IFS=',' read -ra ADD_USERNAMES <<< "${{ secrets.ADD_NAMES }}" | |
for USERNAME in "${ADD_USERNAMES[@]}"; do | |
# Get the user's ID | |
USER_ID=$(curl -s -H "Authorization: token $GH_TOKEN" \ | |
"https://api.github.com/users/$USERNAME" | jq -r .id) | |
# Add the user as a collaborator | |
curl -X PUT -H "Authorization: token $GH_TOKEN" \ | |
"https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/collaborators/$USERNAME" \ | |
-d "{\"permission\":\"write\"}" | |
# Optionally, you can add some logging for each user | |
echo "Added collaborator: $USERNAME" | |
done | |
env: | |
GH_TOKEN: ${{ secrets.GH_TOKEN }} |