yorkie-team / community

Yorkie community materials
Apache License 2.0
5 stars 7 forks source link

Add GitHub Actions to Sync with Member Team #5

Open krapie opened 3 weeks ago

krapie commented 3 weeks ago

What would you like to be added:

I propose adding a GitHub Action that automates the process of syncing team members with a members.yaml file. This feature would enhance our workflow by ensuring that team membership is regularly updated without manual intervention.

Currently, there is an available action called github-team-sync. However, developing our own tailored action would be beneficial to better meet our specific needs and requirements.

Why is this needed: Automating the membership syncing process will save time, reduce the potential for errors, and ensure that our team settings remain current. This enhancement can lead to a more efficient workflow and improved member management.

krapie commented 3 weeks ago

GitHub Action workflow will look like below:

name: Sync Team Members

on:
  push:
    paths:
      - 'members.yaml'
    branches:
      - main
  workflow_dispatch:  # Allow manual triggering

permissions:
  contents: read
  organization-projects: write
  administration: write

jobs:
  sync-members:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.x'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install PyYAML PyGithub

      - name: Sync Team Members
        env:
          GITHUB_TOKEN: ${{ secrets.ADMIN_TOKEN }}
          ORGANIZATION: "yorkie-team"
          TEAM_SLUG: "members"
        run: |
          cat > sync_members.py << 'EOF'
          import os
          import yaml
          from github import Github

          def get_yaml_members():
              with open('members.yaml', 'r') as file:
                  data = yaml.safe_load(file)
                  return [member['github'].replace('@', '') for member in data]

          def sync_members():
              # Initialize GitHub client
              g = Github(os.environ['GITHUB_TOKEN'])
              org = g.get_organization(os.environ['ORGANIZATION'])
              team = org.get_team_by_slug(os.environ['TEAM_SLUG'])

              # Get current team members
              current_members = set(member.login for member in team.get_members())

              # Get members from YAML
              yaml_members = set(get_yaml_members())

              # Members to add
              for member in yaml_members - current_members:
                  try:
                      user = g.get_user(member)
                      team.add_membership(user, role='member')
                      print(f"✅ Added {member} to the team")
                  except Exception as e:
                      print(f"❌ Failed to add {member}: {str(e)}")

              # Members to remove
              for member in current_members - yaml_members:
                  try:
                      user = g.get_user(member)
                      team.remove_membership(user)
                      print(f"✅ Removed {member} from the team")
                  except Exception as e:
                      print(f"❌ Failed to remove {member}: {str(e)}")

          if __name__ == '__main__':
              sync_members()
          EOF

          python sync_members.py

      - name: Create Issue on Failure
        if: failure()
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        uses: actions/github-script@v6
        with:
          script: |
            github.rest.issues.create({
              owner: context.repo.owner,
              repo: context.repo.repo,
              title: 'Team Sync Failed',
              body: 'The team synchronization workflow failed. Please check the workflow logs for more details.'
            });