|
| 1 | +name: subrepo-pull-into-main |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: [release] |
| 5 | + |
| 6 | +permissions: |
| 7 | + contents: write |
| 8 | + pull-requests: write |
| 9 | + |
| 10 | +jobs: |
| 11 | + pull-to-main: |
| 12 | + if: ${{ github.run_number > 1 }} |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Set up Git user |
| 16 | + run: | |
| 17 | + git config --global user.email "action@github.com" |
| 18 | + git config --global user.name "GitHub Action" |
| 19 | +
|
| 20 | + - name: Install git-subrepo |
| 21 | + run: | |
| 22 | + git clone https://github.com/ingydotnet/git-subrepo ~/.git-subrepo |
| 23 | + source ~/.git-subrepo/.rc |
| 24 | + git subrepo --version |
| 25 | +
|
| 26 | + - uses: actions/checkout@v4 |
| 27 | + with: |
| 28 | + fetch-depth: 0 |
| 29 | + ref: release |
| 30 | + |
| 31 | + - name: Revert (non-merge) commits from this push |
| 32 | + id: revert-release |
| 33 | + run: | |
| 34 | + set -e |
| 35 | +
|
| 36 | + # Commit at the tip of release when this workflow runs (i.e., AFTER) |
| 37 | + TRIGGER_COMMIT=$(git rev-parse HEAD) |
| 38 | + echo "Trigger commit: $TRIGGER_COMMIT" |
| 39 | + echo "trigger-commit=$TRIGGER_COMMIT" >> "$GITHUB_OUTPUT" |
| 40 | +
|
| 41 | + # Get BEFORE and AFTER from the push event |
| 42 | + BEFORE="${{ github.event.before }}" |
| 43 | + AFTER="${{ github.sha }}" |
| 44 | + echo "Before: $BEFORE" |
| 45 | + echo "After: $AFTER" |
| 46 | +
|
| 47 | + # List non-merge commits introduced by this push (newest-first) |
| 48 | + COMMITS=$(git rev-list --no-merges "$BEFORE..$AFTER" || true) |
| 49 | +
|
| 50 | + if [ -z "$COMMITS" ]; then |
| 51 | + echo "No non-merge commits to revert between $BEFORE and $AFTER." |
| 52 | + fi |
| 53 | +
|
| 54 | + echo "Non-merge commits found:" |
| 55 | + echo "$COMMITS" |
| 56 | +
|
| 57 | + # ---- Collect all commit messages (newest → oldest) ---- |
| 58 | + ALL_MSGS="" |
| 59 | + for c in $COMMITS; do |
| 60 | + MSG=$(git log -1 --format=%B "$c") |
| 61 | + ALL_MSGS="$ALL_MSGS |
| 62 | + |
| 63 | + --- |
| 64 | + Commit: $c |
| 65 | + $MSG |
| 66 | + " |
| 67 | + done |
| 68 | +
|
| 69 | + echo "all-commit-messages<<EOF" >> "$GITHUB_OUTPUT" |
| 70 | + echo "$ALL_MSGS" >> "$GITHUB_OUTPUT" |
| 71 | + echo "EOF" >> "$GITHUB_OUTPUT" |
| 72 | +
|
| 73 | + # ---- Revert the commits (newest-first) ---- |
| 74 | + for c in $COMMITS; do |
| 75 | + echo "Reverting $c" |
| 76 | + git revert --no-edit "$c" |
| 77 | + done |
| 78 | +
|
| 79 | + # Capture the commit that represents the final revert state |
| 80 | + REVERT_COMMIT=$(git rev-parse HEAD) |
| 81 | + echo "revert-commit=$REVERT_COMMIT" >> "$GITHUB_OUTPUT" |
| 82 | +
|
| 83 | + # Push the revert(s) back to release |
| 84 | + git push origin release |
| 85 | +
|
| 86 | + - uses: actions/checkout@v4 |
| 87 | + with: |
| 88 | + fetch-depth: 0 |
| 89 | + ref: main |
| 90 | + |
| 91 | + - name: Check for release/ folder |
| 92 | + run: | |
| 93 | + if [ ! -d "release" ]; then |
| 94 | + echo "release/ folder not found on main branch. Exiting early." |
| 95 | + exit 0 |
| 96 | + fi |
| 97 | +
|
| 98 | + - name: Update ./release to latest |
| 99 | + run: | |
| 100 | + set -e |
| 101 | + source ~/.git-subrepo/.rc |
| 102 | + git subrepo pull ./release |
| 103 | +
|
| 104 | + - name: Capture .gitrepo content |
| 105 | + id: capture-gitrepo |
| 106 | + run: | |
| 107 | + set -e |
| 108 | + # Save the current .gitrepo content |
| 109 | + GITREPO_CONTENT=$(cat ./release/.gitrepo) |
| 110 | + echo "gitrepo-content<<EOF" >> $GITHUB_OUTPUT |
| 111 | + echo "$GITREPO_CONTENT" >> $GITHUB_OUTPUT |
| 112 | + echo "EOF" >> $GITHUB_OUTPUT |
| 113 | +
|
| 114 | + - name: Determine repository owner and name |
| 115 | + id: repo-info |
| 116 | + run: | |
| 117 | + set -e |
| 118 | + OWNER="${{ github.repository_owner }}" |
| 119 | + NAME="${{ github.event.repository.name }}" |
| 120 | + echo "owner=$OWNER" >> $GITHUB_OUTPUT |
| 121 | + echo "name=$NAME" >> $GITHUB_OUTPUT |
| 122 | + echo "Repository: $OWNER/$NAME" |
| 123 | +
|
| 124 | + - name: Delete release folder |
| 125 | + run: | |
| 126 | + rm -rf ./release |
| 127 | + echo "Deleted release/ folder" |
| 128 | +
|
| 129 | + - name: Install trigger commit using degit.sh |
| 130 | + run: | |
| 131 | + set -e |
| 132 | + # Download and execute degit.sh to install the trigger commit |
| 133 | + bash <(curl -fsSL https://raw.githubusercontent.com/pmalacho-mit/suede/refs/heads/main/scripts/utils/degit.sh) \ |
| 134 | + --repo "${{ steps.repo-info.outputs.owner }}/${{ steps.repo-info.outputs.name }}" \ |
| 135 | + --commit "${{ steps.revert-release.outputs.trigger-commit }}" \ |
| 136 | + --destination release |
| 137 | + echo "Installed trigger commit into release/ folder" |
| 138 | +
|
| 139 | + - name: Recreate .gitrepo file |
| 140 | + run: | |
| 141 | + set -e |
| 142 | + # Write the original .gitrepo content |
| 143 | + cat > ./release/.gitrepo << 'EOF' |
| 144 | + ${{ steps.capture-gitrepo.outputs.gitrepo-content }} |
| 145 | + EOF |
| 146 | +
|
| 147 | + echo "Updated .gitrepo file:" |
| 148 | + cat ./release/.gitrepo |
| 149 | +
|
| 150 | + - name: Generate branch name |
| 151 | + id: generate-branch-name |
| 152 | + run: | |
| 153 | + TIMESTAMP=$(date +%Y_%m_%d-%H_%M_%S_%Z) |
| 154 | + BRANCH_NAME="chore/update-release-${TIMESTAMP}" |
| 155 | + echo "branch-name=$BRANCH_NAME" >> $GITHUB_OUTPUT |
| 156 | + echo "Using branch name: $BRANCH_NAME" |
| 157 | +
|
| 158 | + - name: Commit and push changes |
| 159 | + run: | |
| 160 | + set -e |
| 161 | + git switch -c "${{ steps.generate-branch-name.outputs.branch-name }}" |
| 162 | + git add release/ |
| 163 | + git commit -m "chore(suede): update release to trigger commits:\n ${{ steps.revert-release.outputs.trigger-commit }} |
| 164 | +
|
| 165 | + Original commit messages: |
| 166 | + ${{ steps.revert-release.outputs.all-commit-messages }}" |
| 167 | + git push -u origin HEAD |
| 168 | +
|
| 169 | + - name: Create PR |
| 170 | + env: |
| 171 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 172 | + run: | |
| 173 | + gh pr create \ |
| 174 | + --base main \ |
| 175 | + --head "${{ steps.generate-branch-name.outputs.branch-name }}" \ |
| 176 | + --title "chore(suede): pull latest upstream release into main" \ |
| 177 | + --body "Automated subrepo pull of origin:release into ./release |
| 178 | +
|
| 179 | + **Trigger commit:** ${{ steps.revert-release.outputs.trigger-commit }} |
| 180 | +
|
| 181 | + **Original commit messages:** |
| 182 | + \`\`\` |
| 183 | + ${{ steps.revert-release.outputs.all-commit-messages }} |
| 184 | + \`\`\`" |
0 commit comments