diff --git a/.github/workflows/github-uar.yml b/.github/workflows/github-uar.yml new file mode 100644 index 0000000..87bb6e6 --- /dev/null +++ b/.github/workflows/github-uar.yml @@ -0,0 +1,54 @@ +--- +name: github-uar.yml + +on: + push: + branches: + - main + - fix/UAR-automation + pull_request: + branches: + - main + workflow_dispatch: + inputs: + role: + description: 'Role to filter members by' + required: false + default: '' + +jobs: + UAR-automation: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Get Token + id: get_workflow_token + uses: peter-murray/workflow-application-token-action@v3 + with: + application_id: ${{ secrets.GH_APP_FULL_APPLICATION_ID }} + application_private_key: ${{ secrets.APPLICATION_GITHUB_APP_FULL_PRIVATE_KEY }} + revoke_token: true + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.x' + + - name: Install dependencies + run: pip install requests + + - name: Run list_org_owners.py script + run: python scripts/list_org_owners.py + env: + GITHUB_ORG_NAME: tucowsinc + GITHUB_TOKEN: ${{ steps.get_workflow_token.outputs.token }} + ROLE: ${{ github.event.inputs.role }} + + - name: Upload CSV as artifact + uses: actions/upload-artifact@v3 + with: + name: org-members + path: org_members.csv \ No newline at end of file diff --git a/scripts/list_org_owners.py b/scripts/list_org_owners.py new file mode 100644 index 0000000..6a53bcb --- /dev/null +++ b/scripts/list_org_owners.py @@ -0,0 +1,43 @@ +import csv +import requests +import os + +def get_org_admins(org_name, token, role=None): + url = f"https://api.github.com/orgs/{org_name}/members?role={role}" + headers = { + "Authorization": f"token {token}", + "Accept": "application/vnd.github.v3+json" + } + admins = [] + + while url: + response = requests.get(url, headers=headers) + response.raise_for_status() + members = response.json() + admins.extend([{'login': member['login'], 'role': role} for member in members]) + + # Check for pagination + if 'next' in response.links: + url = response.links['next']['url'] + else: + url = None + + return admins + +def save_to_csv(data, filename): + with open(filename, mode='w', newline='') as file: + writer = csv.DictWriter(file, fieldnames=['login', 'role']) + writer.writeheader() + for row in data: + writer.writerow(row) + +if __name__ == "__main__": + org_name = os.getenv('GITHUB_ORG_NAME') + token = os.getenv('GITHUB_TOKEN') + role = os.getenv('ROLE') + if not org_name: + raise ValueError("GITHUB_ORG_NAME environment variable is not set") + if not token: + raise ValueError("GITHUB_TOKEN environment variable is not set") + admins = get_org_admins(org_name, token, role) + save_to_csv(admins, 'org_members.csv') \ No newline at end of file