-
Notifications
You must be signed in to change notification settings - Fork 15
Add Team Member Auto Sync GitHub Action #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
krapie
wants to merge
1
commit into
main
Choose a base branch
from
auto-sync-team-member
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,115 @@ | ||
| #!/usr/bin/env python3 | ||
| import os | ||
| import sys | ||
| import yaml | ||
| import requests | ||
|
|
||
| def load_yaml(file_path): | ||
| """Load YAML file and return data.""" | ||
| with open(file_path, 'r') as f: | ||
| return yaml.safe_load(f) | ||
|
|
||
| def github_api_request(method, url, data=None): | ||
| """Make GitHub API request.""" | ||
| headers = { | ||
| 'Authorization': f'token {os.environ["GITHUB_TOKEN"]}', | ||
| 'Accept': 'application/vnd.github.v3+json', | ||
| } | ||
|
|
||
| if method == 'GET': | ||
| response = requests.get(url, headers=headers) | ||
| elif method == 'PUT': | ||
| response = requests.put(url, headers=headers, json=data) | ||
| elif method == 'DELETE': | ||
| response = requests.delete(url, headers=headers) | ||
| elif method == 'POST': | ||
| response = requests.post(url, headers=headers, json=data) | ||
|
|
||
| return response | ||
|
|
||
| def get_org(): | ||
| """Get organization name from repository.""" | ||
| repo = os.environ.get('GITHUB_REPOSITORY', '') | ||
| return repo.split('/')[0] if '/' in repo else 'yorkie-team' | ||
|
|
||
| def sync_team(team_name, members): | ||
| """Sync GitHub team with members list.""" | ||
| org = get_org() | ||
| base_url = f'https://api.github.com/orgs/{org}' | ||
|
|
||
| print(f"Syncing team: {team_name} with {len(members)} members") | ||
|
|
||
| # Create team if it doesn't exist | ||
| team_data = {'name': team_name, 'privacy': 'closed'} | ||
| create_response = github_api_request('POST', f'{base_url}/teams', team_data) | ||
|
|
||
| if create_response.status_code == 422: | ||
| # Team already exists, get team info | ||
| teams_response = github_api_request('GET', f'{base_url}/teams') | ||
| if teams_response.status_code == 200: | ||
| teams = teams_response.json() | ||
| team_slug = None | ||
| for team in teams: | ||
| if team['name'] == team_name: | ||
| team_slug = team['slug'] | ||
| break | ||
| else: | ||
| print(f"Failed to get teams: {teams_response.status_code}") | ||
| return | ||
| elif create_response.status_code == 201: | ||
| team_slug = create_response.json()['slug'] | ||
| else: | ||
| print(f"Failed to create team {team_name}: {create_response.status_code}") | ||
| return | ||
|
|
||
| if not team_slug: | ||
| team_slug = team_name.lower().replace(' ', '-') | ||
|
|
||
| # Get current team members | ||
| current_members_response = github_api_request('GET', f'{base_url}/teams/{team_slug}/members') | ||
| current_members = [] | ||
| if current_members_response.status_code == 200: | ||
| current_members = [member['login'] for member in current_members_response.json()] | ||
|
|
||
| # Add new members | ||
| for member in members: | ||
| if member not in current_members: | ||
| add_response = github_api_request('PUT', f'{base_url}/teams/{team_slug}/memberships/{member}') | ||
| if add_response.status_code in [200, 201]: | ||
| print(f" Added {member}") | ||
| else: | ||
| print(f" Failed to add {member}: {add_response.status_code}") | ||
|
|
||
| # Remove old members | ||
| for member in current_members: | ||
| if member not in members: | ||
| remove_response = github_api_request('DELETE', f'{base_url}/teams/{team_slug}/memberships/{member}') | ||
| if remove_response.status_code in [204, 404]: | ||
| print(f" Removed {member}") | ||
| else: | ||
| print(f" Failed to remove {member}: {remove_response.status_code}") | ||
|
|
||
| def main(): | ||
| """Main sync function.""" | ||
| print("Starting team synchronization...") | ||
|
|
||
| # Sync members | ||
| members_data = load_yaml('teams/members.yaml') | ||
| sync_team('members', members_data['members']) | ||
|
|
||
| # Sync mentors (latest year only) | ||
| mentors_data = load_yaml('teams/mentors.yaml') | ||
| latest_mentor_year = max(mentors_data['mentors'].keys()) | ||
| mentor_members = mentors_data['mentors'][latest_mentor_year] | ||
| sync_team('mentors', mentor_members) | ||
|
|
||
| # Sync OSSCA (latest year only) | ||
| ossca_data = load_yaml('teams/ossca.yaml') | ||
| latest_ossca_year = max(ossca_data['ossca'].keys()) | ||
| ossca_members = ossca_data['ossca'][latest_ossca_year] | ||
| sync_team('ossca', ossca_members) | ||
|
|
||
| print("Team synchronization completed!") | ||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
This file contains hidden or 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,32 @@ | ||
| name: Sync GitHub Teams | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main] | ||
| paths: | ||
| - 'teams/**' | ||
| workflow_dispatch: | ||
|
|
||
| jobs: | ||
| sync-teams: | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: read | ||
|
|
||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Setup Python | ||
| uses: actions/setup-python@v4 | ||
| with: | ||
| python-version: '3.x' | ||
|
|
||
| - name: Install dependencies | ||
| run: pip install PyYAML requests | ||
|
|
||
| - name: Sync teams | ||
| env: | ||
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| GITHUB_REPOSITORY: ${{ github.repository }} | ||
| run: python .github/scripts/sync.py |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.