Create team: TEAM-Hello #30
Workflow file for this run
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
| name: Create Team | |
| on: | |
| pull_request: | |
| types: [closed] | |
| paths: | |
| - 'team-name.txt' | |
| jobs: | |
| create_team: | |
| if: | | |
| github.event.pull_request.merged == true && | |
| contains(github.event.pull_request.labels.*.name, 'team-creation') | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| steps: | |
| - name: Checkout Repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Extract Team Details | |
| id: extract | |
| run: | | |
| if [ ! -f team-name.txt ]; then | |
| echo "::error::team-name.txt not found!" | |
| exit 1 | |
| fi | |
| TEAM_NAME=$(cat team-name.txt | sed 's/[`]//g' | xargs) | |
| if [[ ! "$TEAM_NAME" =~ ^TEAM-[a-zA-Z0-9_-]+$ ]]; then | |
| echo "::error::Invalid team name format: '$TEAM_NAME'" | |
| exit 1 | |
| fi | |
| PR_BODY=$(echo '${{ toJSON(github.event.pull_request.body) }}' | jq -r .) | |
| PRIVACY=$(echo "$PR_BODY" | grep -oP '(?<=Visibility:\*\* ).*' | head -1 | xargs | tr '[:upper:]' '[:lower:]') | |
| DESCRIPTION=$(echo "$PR_BODY" | grep -oP '(?<=Purpose:\*\* ).*' | head -1 | xargs) | |
| MAINTAINERS=$(echo "$PR_BODY" | grep -oP '(?<=Maintainers:\*\* ).*' | head -1 | sed 's/none//' | xargs | sed 's/^,*//;s/,*$//') | |
| MEMBERS=$(echo "$PR_BODY" | grep -oP '(?<=Team Members:\*\* ).*' | head -1 | sed 's/none//' | xargs | sed 's/^,*//;s/,*$//') | |
| PARENT_TEAM=$(echo "$PR_BODY" | grep -oP '(?<=Parent Team:\*\* ).*' | head -1 | xargs) | |
| echo "TEAM_NAME=$TEAM_NAME" >> $GITHUB_ENV | |
| echo "PRIVACY=${PRIVACY:-secret}" >> $GITHUB_ENV | |
| echo "DESCRIPTION=${DESCRIPTION:-Team created via automation}" >> $GITHUB_ENV | |
| echo "MAINTAINERS=${MAINTAINERS:-none}" >> $GITHUB_ENV | |
| echo "MEMBERS=${MEMBERS:-none}" >> $GITHUB_ENV | |
| echo "PARENT_TEAM=${PARENT_TEAM}" >> $GITHUB_ENV | |
| echo "Extracted values:" | |
| echo "Team: $TEAM_NAME" | |
| echo "Privacy: $PRIVACY" | |
| echo "Description: $DESCRIPTION" | |
| echo "Maintainers: $MAINTAINERS" | |
| echo "Team Members: $MEMBERS" | |
| echo "Parent Team: $PARENT_TEAM" | |
| - name: Create GitHub Team | |
| uses: actions/github-script@v6 | |
| env: | |
| GH_TOKEN: ${{ secrets.GH_PAT }} | |
| with: | |
| github-token: ${{ secrets.GH_PAT }} | |
| script: | | |
| try { | |
| const teamSlug = process.env.TEAM_NAME.toLowerCase().replace(/[^a-z0-9-]/g, '-'); | |
| let parentTeamId = undefined; | |
| if (process.env.PARENT_TEAM) { | |
| try { | |
| const { data: parent } = await github.rest.teams.getByName({ | |
| org: context.repo.owner, | |
| team_slug: process.env.PARENT_TEAM.toLowerCase().replace(/[^a-z0-9-]/g, '-') | |
| }); | |
| parentTeamId = parent.id; | |
| console.log(`Found parent team ${process.env.PARENT_TEAM} with ID ${parentTeamId}`); | |
| } catch (err) { | |
| console.warn(`Could not find parent team '${process.env.PARENT_TEAM}': ${err.message}`); | |
| } | |
| } | |
| const { data: team } = await github.rest.teams.create({ | |
| org: context.repo.owner, | |
| name: process.env.TEAM_NAME, | |
| slug: teamSlug, | |
| description: process.env.DESCRIPTION, | |
| privacy: process.env.PRIVACY, | |
| ...(parentTeamId && { parent_team_id: parentTeamId }) | |
| }); | |
| console.log(`Team ${team.name} created successfully`); | |
| const addMembers = async (userList, role) => { | |
| if (!userList || userList === 'none') return; | |
| const users = userList.split(',').map(u => u.trim()).filter(Boolean); | |
| for (const username of users) { | |
| try { | |
| await github.rest.teams.addOrUpdateMembershipForUserInOrg({ | |
| org: context.repo.owner, | |
| team_slug: team.slug, | |
| username, | |
| role | |
| }); | |
| console.log(`Added ${role}: ${username}`); | |
| } catch (error) { | |
| console.warn(`Failed to add ${username} as ${role}: ${error.message}`); | |
| } | |
| } | |
| }; | |
| await addMembers(process.env.MAINTAINERS, 'maintainer'); | |
| await addMembers(process.env.MEMBERS, 'member'); | |
| const commentLines = [ | |
| `✅ Team **${team.name}** created!`, | |
| `• Visibility: ${process.env.PRIVACY}`, | |
| `• Description: ${process.env.DESCRIPTION}`, | |
| ]; | |
| if (process.env.MAINTAINERS !== 'none') { | |
| commentLines.push(`• Maintainers: ${process.env.MAINTAINERS.replace(/,/g, ', ')}`); | |
| } | |
| if (process.env.MEMBERS !== 'none') { | |
| commentLines.push(`• Members: ${process.env.MEMBERS.replace(/,/g, ', ')}`); | |
| } | |
| await github.rest.issues.createComment({ | |
| issue_number: context.payload.pull_request.number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: commentLines.join('\n') | |
| }); | |
| } catch (error) { | |
| core.setFailed(`Team creation failed: ${error.message}`); | |
| await github.rest.issues.createComment({ | |
| issue_number: context.payload.pull_request.number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: `❌ Failed to create team: ${error.message}` | |
| }); | |
| } | |