From ce792e7935474357ca51608d52593607f1de1637 Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Fri, 1 Nov 2024 08:02:15 -0500 Subject: [PATCH] Add GHA script to automatically write a user to members. (#78) --- .github/workflows/add_member.yml | 58 ++++++++++++++++++++++++++++++++ scripts/add_member.py | 42 +++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 .github/workflows/add_member.yml create mode 100755 scripts/add_member.py diff --git a/.github/workflows/add_member.yml b/.github/workflows/add_member.yml new file mode 100644 index 0000000..cc919a1 --- /dev/null +++ b/.github/workflows/add_member.yml @@ -0,0 +1,58 @@ +name: "Add new member to django-commons" + +on: + workflow_dispatch: + inputs: + username: + description: "Username of the new member" + required: true + default: "new_member" + issue_number: + description: "Issue number to reference in the PR body" + required: false + default: "0" + +jobs: + add-member: + name: "Add new member" + runs-on: ubuntu-latest + permissions: + id-token: write + contents: write + + steps: + - name: Setup python + uses: actions/setup-python@v5 + with: + python-version: '3.12' + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Initialize mandatory git config + run: | + git config user.name "GitHub Actions" + git config user.email noreply@github.com + + - name: Create branch + run: git checkout -b add-user/${{ inputs.username }} + + - name: Add user to the list + run: | + python scripts/add_member.py ${{ inputs.username }} + + - name: Commit changes + run: | + git add terraform/production/org.tfvars + git commit -m "Add ${{ inputs.username }} to django-commons" + git push origin add-user/${{ inputs.username }} + + - name: Create pull request + run: | + gh pr create \ + --title "Add ${{ inputs.username }} to django-commons" \ + --body "Fix #${{ inputs.issue_number }}" \ + --base main \ + --head add-user/${{ inputs.username }} + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/scripts/add_member.py b/scripts/add_member.py new file mode 100755 index 0000000..f814979 --- /dev/null +++ b/scripts/add_member.py @@ -0,0 +1,42 @@ +import re +import sys + +TERRAFORM_ORG_FILE = "./terraform/production/org.tfvars" + + +def add_member(content, new_member_name): + # Regular expression to find the members list + members_pattern = re.compile(r"(members\s*=\s*\[)([^\]]*)(\])", re.DOTALL) + match = members_pattern.search(content) + if match: + # Extract the members part + members_list = match.group(2).rstrip(",\n").replace('"', "").replace("\n ", "").split(",") + if new_member_name in members_list: + print(f"Member {new_member_name} already exists") + exit(1) + # Add the new value and sort alphabetically + members_list.append(new_member_name) + members_list = sorted(members_list, key=str.lower) + + # Create the formatted members list as a string + formatted_members = '\n '.join([f'"{m}",' for m in members_list]) + + # Rebuild the file content with the new members list + new_content = content[:match.start(2)] + "\n " + formatted_members + "\n" + content[match.end(2):] + return new_content + return content + + +def run(new_member_name): + with open(TERRAFORM_ORG_FILE, "r") as f: + file_content = f.read() + updated_content = add_member(file_content, new_member_name) + with open(TERRAFORM_ORG_FILE, "w") as f: + f.write(updated_content) + + +if __name__ == '__main__': + if len(sys.argv) != 2: + print("Usage: python add_member.py ") + sys.exit(1) + run(sys.argv[1])