Remove Inactive Users #34
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: Remove Inactive Users | |
on: | |
schedule: | |
- cron: '0 0 * * 0' # Runs every Sunday at midnight UTC | |
workflow_dispatch: # Allows manual triggering | |
jobs: | |
remove-inactive-users: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Install jq | |
run: sudo apt-get install -y jq | |
- name: Debug ORG_MEMBER | |
run: | | |
ORG_NAME="Fiserv" | |
GITHUB_API_URL="https://api.github.com" | |
TOKEN=${{ secrets.CLEANUP_TOKEN }} | |
ORG_MEMBERS=$(curl -s -H "Authorization: token $TOKEN" "$GITHUB_API_URL/orgs/$ORG_NAME/members") | |
echo "Organization Members:" | |
for MEMBER in $(echo "$ORG_MEMBERS" | jq -r '.[].login'); do | |
echo "- $MEMBER" | |
done | |
- name: Fetch team members and their contributions | |
id: fetch_contributions | |
run: | | |
GITHUB_API_URL="https://api.github.com" | |
ORG_NAME="Fiserv" | |
EXCLUDED_TEAMS=("developer-studio-admin" "developer-studio-test" "developer-studio-support" "cloud-acceleration-center") # Teams to be excluded | |
EXCLUDED_MEMBERS=("tomeck" "pavanjoshi12") # Specific users to be excluded | |
TOKEN=${{ secrets.CLEANUP_TOKEN }} | |
THIRTY_DAYS_AGO=$(date --date='30 days ago' +"%Y-%m-%dT%H:%M:%SZ") | |
REMOVED_MEMBERS_FILE="removed_members.txt" | |
# Function to URL-encode team names to handle spaces | |
url_encode() { | |
echo "$1" | jq -sRr @uri | |
} | |
# Get the list of all members of the organization | |
ORG_MEMBERS=$(curl -s -H "Authorization: token $TOKEN" "$GITHUB_API_URL/orgs/$ORG_NAME/members") | |
# Get members of the excluded teams | |
EXCLUDED_TEAM_MEMBERS=() | |
for TEAM in "${EXCLUDED_TEAMS[@]}"; do | |
TEAM_MEMBERS=$(curl -s -H "Authorization: token $TOKEN" "$GITHUB_API_URL/orgs/$ORG_NAME/teams/$TEAM/members") | |
EXCLUDED_TEAM_MEMBERS+=($(echo "$TEAM_MEMBERS" | jq -r '.[] | .login')) | |
done | |
# Get unique list of all excluded members (from teams and specific members) | |
ALL_EXCLUDED_MEMBERS=($(printf "%s\n" "${EXCLUDED_TEAM_MEMBERS[@]}" "${EXCLUDED_MEMBERS[@]}" | sort -u)) | |
# Get recent activity for each member | |
for MEMBER in $(echo "$ORG_MEMBERS" | jq -r '.[].login'); do | |
echo "- $MEMBER" | |
# Skip if the member is in the excluded list | |
if [[ " ${ALL_EXCLUDED_MEMBERS[@]} " =~ " ${MEMBER} " ]]; then | |
echo "$MEMBER is excluded, skipping..." | |
continue | |
fi | |
# Fetch the user's membership details | |
USER_MEMBERSHIP=$(curl -s -H "Authorization: token $TOKEN" "$GITHUB_API_URL/orgs/$ORG_NAME/memberships/$MEMBER") | |
JOIN_DATE=$(echo "$USER_MEMBERSHIP" | jq -r '.created_at') | |
# Check if the member has been part of the org for more than 30 days | |
if [[ "$JOIN_DATE" > "$THIRTY_DAYS_AGO" ]]; then | |
echo "$MEMBER has been a member for less than 30 days, skipping..." | |
continue | |
fi | |
# Fetch the user's events in the last 30 days | |
USER_EVENTS=$(curl -s -H "Authorization: token $TOKEN" "$GITHUB_API_URL/users/$MEMBER/events?per_page=10") | |
LAST_CONTRIBUTION=$(echo "$USER_EVENTS" | jq -r ".[] | select(.created_at > \"$THIRTY_DAYS_AGO\") | .created_at" | sort -r | head -n 1) | |
if [ -z "$LAST_CONTRIBUTION" ]; then | |
echo "$MEMBER has been inactive for over 30 days, removing..." | |
curl -X DELETE -H "Authorization: token $TOKEN" "$GITHUB_API_URL/orgs/$ORG_NAME/memberships/$MEMBER" | |
echo "$MEMBER" >> "$REMOVED_MEMBERS_FILE" | |
else | |
echo "$MEMBER has contributed recently: $LAST_CONTRIBUTION" | |
fi | |
done | |
echo "::set-output name=removed_members_file::$REMOVED_MEMBERS_FILE" | |
- name: Cleanup Summary | |
run: | | |
REMOVED_MEMBERS_FILE="${{ steps.fetch_contributions.outputs.removed_members_file }}" | |
REMOVED_COUNT=$(wc -l < "$REMOVED_MEMBERS_FILE") | |
echo "Number of members removed: $REMOVED_COUNT" | |
echo "Removed members:" | |
cat "$REMOVED_MEMBERS_FILE" |