-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GHA script to automatically write a user to members. (#78)
- Loading branch information
1 parent
f4f00b9
commit ce792e7
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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 [email protected] | ||
- 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 }} |
This file contains 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,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 <new_member>") | ||
sys.exit(1) | ||
run(sys.argv[1]) |