diff --git a/.github/workflows/AutoInviteToOrgByStar.py b/.github/workflows/AutoInviteToOrgByStar.py index 111c4db..7277c6b 100644 --- a/.github/workflows/AutoInviteToOrgByStar.py +++ b/.github/workflows/AutoInviteToOrgByStar.py @@ -1,43 +1,72 @@ -# Max Base -# 2021-06-19 +# Max Base & SunsetMkt +# 2021-06-19 & 2024-09 # https://github.com/BaseMax/AutoInviteToOrgByStar -import os -import sys import json +import os + import requests -print("Hello, World") +# print("Hello, World") -if os.getenv('CI'): - print('Looks like GitHub!') +if os.getenv("CI"): + print("Looks like GitHub Actions!") else: - print('Maybe running locally?') + print("Maybe running locally?") -print("Environ:") -print(os.environ) -print("Prefix:") -print(sys.prefix) +# Do not show your secrets in the logs +# print("Environ:") +# print(os.environ) +# print("Prefix:") +# print(sys.prefix) -MY_GITHUB_KEY = os.environ['MY_GITHUB_KEY'] -COMMUNITY_TEAM_ID = os.environ['COMMUNITY_TEAM_ID'] +ACCESS_TOKEN = os.environ["ACCESS_TOKEN"] +ORG_NAME = os.environ["ORG_NAME"] +TEAM_NAME = os.environ["TEAM_NAME"] -file = open(os.environ['GITHUB_EVENT_PATH']) +# GITHUB_EVENT_PATH: The path to the file on the runner that +# contains the full event webhook payload. +file = open(os.environ["GITHUB_EVENT_PATH"]) data = json.load(file) -print("Data:") -print(data) - -USERNAME = data['sender']['login'] +# print("Data:") +# print(data) -print('Send invite for the @'+USERNAME) +USERNAME = data["sender"]["login"] -# TODO: check user already joined or no.... -url = 'https://api.github.com/teams/'+COMMUNITY_TEAM_ID+'/memberships/' + USERNAME -payload='' headers = { - 'Accept': 'application/vnd.github.v3+json', - 'Authorization': 'token '+MY_GITHUB_KEY + "Accept": "application/vnd.github+json", + "Authorization": f"Bearer {ACCESS_TOKEN}", + "X-GitHub-Api-Version": "2022-11-28", } -response = requests.request("PUT", url, headers=headers, data=payload) -print(response.text) + +print(f"Checking if @{USERNAME} is already in the team ...") + + +response = requests.get( + f"https://api.github.com/orgs/{ORG_NAME}/teams/{TEAM_NAME}/memberships/{USERNAME}", + headers=headers, +) + +if response.status_code == 200: + print(f"User @{USERNAME} is already in the team.") + exit(0) +elif response.status_code == 404: + print(f"User @{USERNAME} is not in the team.") +else: + print(f"Error: {response.status_code} {response.text}") + exit(1) + +print(f"Inviting @{USERNAME} ...") + +response = requests.put( + f"https://api.github.com/orgs/{ORG_NAME}/teams/{TEAM_NAME}/memberships/{USERNAME}", + headers=headers, +) + +if response.status_code == 200: + print(f"Invitation sent to @{USERNAME}.") + exit(0) +else: + print(f"Error: {response.status_code} {response.text}") + exit(1) diff --git a/.github/workflows/invite-by-star.yml b/.github/workflows/invite-by-star.yml index 0459714..e7ae71a 100644 --- a/.github/workflows/invite-by-star.yml +++ b/.github/workflows/invite-by-star.yml @@ -1,26 +1,25 @@ on: watch: + # When someone stars a repository types: [started] + name: Invite a new user jobs: - build: - runs-on: ubuntu-latest + build: + runs-on: ubuntu-latest # has Python 3 installed - steps: - - name: checkout repo content - uses: actions/checkout@v2 - - name: setup python - uses: actions/setup-python@v2 - with: - python-version: 3.8 - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install requests - if [ -f ./.github/workflows/requirements.txt ]; then pip install -r requirements.txt; fi - - name: execute py script - run: | - python ./.github/workflows/AutoInviteToOrgByStar.py - env: - MY_GITHUB_KEY: ${{ secrets.MY_GITHUB_KEY }} - COMMUNITY_TEAM_ID: ${{ secrets.COMMUNITY_TEAM_ID }} + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Install requirements + run: | + python -m pip install --upgrade pip + pip install requests # Actually already installed + if [ -f ./.github/workflows/requirements.txt ]; then pip install -r requirements.txt; fi + - name: Execute script + run: | + python ./.github/workflows/AutoInviteToOrgByStar.py + env: + ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }} + ORG_NAME: ${{ secrets.ORG_NAME }} + TEAM_NAME: ${{ secrets.TEAM_NAME }} diff --git a/README.md b/README.md index 953c51f..28c750b 100644 --- a/README.md +++ b/README.md @@ -1,112 +1,23 @@ # Auto Invite To The Organization By Star -A GitHub Action script to automatically invite everyone to your organization that `stars` your repository. +A GitHub Action script to automatically invite everyone to your organization that `stars` your repository. ## What is this? -### You can FORK and STAR this repository, after that you will be invited to a surprising and fantastic organization. +You can FORK and STAR this repository, after that you will be invited to a surprising and fantastic organization. ----------- +--- ## Deploy to your organization -#### Create `MY_GITHUB_KEY` variable at Secrets +### Fork this repository -Open https://github.com/settings/tokens and create one. +### Create `ACCESS_TOKEN` variable at Secrets -After that you can create a secret at `https://github.com/ORG_NAME/REPO_NAME/settings/secrets/actions`. +Open `https://github.com/settings/tokens/new` and create a token with `admin:org` scope. -#### Create `COMMUNITY_TEAM_ID` variable at Secrets +### Create `ORG_NAME` and `TEAM_NAME` variable at Secrets -Do not have team id at your org? +Just copy from your organization and team URLs. -At first, create a team at `https://github.com/orgs/YOUR_ORG_NAME/teams` and run: - -``` -curl -H "Authorization: token *****" https://api.github.com/orgs/YOUR_ORG_NAME/teams -``` - -P.S: Put your **personal token** at the `****`, and replace your organization name at `YOUR_ORG_NAME`. - -After that, You can create a secret at `https://github.com/ORG_NAME/REPO_NAME/settings/secrets/actions` and put api number as value. - -#### Create `.github/workflows/invite-by-star.yml` file: - -```yaml -on: - watch: - types: [started] -name: Invite a new user -jobs: - build: - runs-on: ubuntu-latest - - steps: - - name: checkout repo content - uses: actions/checkout@v2 - - name: setup python - uses: actions/setup-python@v2 - with: - python-version: 3.8 - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install requests - if [ -f ./.github/workflows/requirements.txt ]; then pip install -r requirements.txt; fi - - name: execute py script - run: | - python ./.github/workflows/AutoInviteToOrgByStar.py - env: - MY_GITHUB_KEY: ${{ secrets.MY_GITHUB_KEY }} - COMMUNITY_TEAM_ID: ${{ secrets.COMMUNITY_TEAM_ID }} -``` - -### Create `.github/workflows/AutoInviteToOrgByStar.py` file - -```python -# Max Base -# 2021-06-19 -# https://github.com/BaseMax/AutoInviteToOrgByStar - -import os -import sys -import json -import requests - -print("Hello, World") - -if os.getenv('CI'): - print('Looks like GitHub!') -else: - print('Maybe running locally?') - -print("Environ:") -print(os.environ) -print("Prefix:") -print(sys.prefix) - -MY_GITHUB_KEY = os.environ['MY_GITHUB_KEY'] -COMMUNITY_TEAM_ID = os.environ['COMMUNITY_TEAM_ID'] - -file = open(os.environ['GITHUB_EVENT_PATH']) -data = json.load(file) - -print("Data:") -print(data) - -USERNAME = data['sender']['login'] - -print('Send invite for the @'+USERNAME) - -# TODO: check user already joined or no.... -url = 'https://api.github.com/teams/'+COMMUNITY_TEAM_ID+'/memberships/' + USERNAME -payload='' -headers = { - 'Accept': 'application/vnd.github.v3+json', - 'Authorization': 'token '+MY_GITHUB_KEY -} -response = requests.request("PUT", url, headers=headers, data=payload) -print(response.text) -``` - -© Copyright Max Base, 2021 +`https://github.com/orgs/{ORG_NAME}/teams/{TEAM_NAME}/members`