|
| 1 | +name: Initialize repository |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + |
| 6 | +jobs: |
| 7 | + initialize_repo: |
| 8 | + runs-on: ubuntu-latest |
| 9 | + permissions: |
| 10 | + contents: write |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | + steps: |
| 14 | + - name: Checkout repository |
| 15 | + uses: actions/checkout@v4 |
| 16 | + with: |
| 17 | + ref: main |
| 18 | + fetch-depth: 0 |
| 19 | + |
| 20 | + - name: Setup git |
| 21 | + run: | |
| 22 | + git config --global user.name "github-actions" |
| 23 | + git config --global user.email "[email protected]" |
| 24 | +
|
| 25 | + - name: Install GitHub CLI |
| 26 | + run: | |
| 27 | + sudo apt-get update |
| 28 | + sudo apt-get install gh -y |
| 29 | +
|
| 30 | + - name: Extract repository and username |
| 31 | + id: extract |
| 32 | + run: | |
| 33 | + REPO_NAME="${{ github.repository }}" |
| 34 | + USERNAME=$(echo $REPO_NAME | cut -d'/' -f1) |
| 35 | + REPO_NAME_ONLY=$(echo $REPO_NAME | cut -d'/' -f2) |
| 36 | + echo "username=$USERNAME" >> $GITHUB_OUTPUT |
| 37 | + echo "reponame=$REPO_NAME_ONLY" >> $GITHUB_OUTPUT |
| 38 | +
|
| 39 | + - name: Set branch name |
| 40 | + id: vars |
| 41 | + run: echo "branch=initialize-repo-$(date +%Y%m%d%H%M%S)" >> $GITHUB_OUTPUT |
| 42 | + |
| 43 | + - name: Create develop branch if it doesn't exist |
| 44 | + run: | |
| 45 | + # Check if develop branch exists |
| 46 | + if ! git ls-remote --heads origin develop | grep develop; then |
| 47 | + echo "Creating develop branch from main..." |
| 48 | + git checkout -b develop |
| 49 | + git push -u origin develop |
| 50 | + git checkout main # Return to main branch for the rest of the workflow |
| 51 | + else |
| 52 | + echo "Develop branch already exists." |
| 53 | + fi |
| 54 | +
|
| 55 | + - name: Replace {reponame} and {username} with actual values in non-workflow files |
| 56 | + run: | |
| 57 | + REPO_NAME_ONLY="${{ steps.extract.outputs.reponame }}" |
| 58 | + USERNAME="${{ steps.extract.outputs.username }}" |
| 59 | + REPO_NAME_ESCAPED=$(echo $REPO_NAME_ONLY | sed 's/\//\\\//g') |
| 60 | + USERNAME_ESCAPED=$(echo $USERNAME | sed 's/\//\\\//g') |
| 61 | + |
| 62 | + # Modify all files except workflow files |
| 63 | + find . -type f -not -path "./.git/*" -not -path "./.github/workflows/*" -exec sed -i "s/{reponame}/$REPO_NAME_ESCAPED/g" {} \; |
| 64 | + find . -type f -not -path "./.git/*" -not -path "./.github/workflows/*" -exec sed -i "s/{username}/$USERNAME_ESCAPED/g" {} \; |
| 65 | +
|
| 66 | + - name: Remove initializer workflow |
| 67 | + run: | |
| 68 | + rm -f .github/workflows/initializer.yml |
| 69 | +
|
| 70 | + - name: Commit changes |
| 71 | + run: | |
| 72 | + BRANCH_NAME="${{ steps.vars.outputs.branch }}" |
| 73 | + git checkout -b $BRANCH_NAME |
| 74 | + |
| 75 | + # Exclude workflow files to avoid permission errors |
| 76 | + git add --all -- ':!.github/workflows' |
| 77 | + |
| 78 | + # Explicitly add the deletion of the initializer workflow |
| 79 | + git add .github/workflows/initializer.yml |
| 80 | + |
| 81 | + git commit -m "Initialize repository with repo name ${{ steps.extract.outputs.reponame }} and username ${{ steps.extract.outputs.username }}" |
| 82 | + git push origin HEAD:$BRANCH_NAME |
| 83 | +
|
| 84 | + - name: Create pull request |
| 85 | + env: |
| 86 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 87 | + run: | |
| 88 | + BRANCH_NAME="${{ steps.vars.outputs.branch }}" |
| 89 | + PR_URL=$(gh pr create --base main --head $BRANCH_NAME --title "Initialize repository" --body "This PR initializes the repository with the actual repository name, with the actual username and removing the initializer workflow.") |
| 90 | + |
| 91 | + # Extract PR number from URL |
| 92 | + PR_NUMBER=$(basename $PR_URL) |
| 93 | + |
| 94 | + # Add any desired labels to the pull request |
| 95 | + # gh pr edit $PR_NUMBER --add-label "initialization" |
0 commit comments