Practice merge conflict resolution #14
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: Auto-generate conflict scenario | |
| on: | |
| issues: | |
| types: [opened] | |
| jobs: | |
| generate-conflict: | |
| if: github.event.issue.title == 'Practice merge conflict resolution' | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| issues: write | |
| steps: | |
| - name: Checkout repo | |
| uses: actions/checkout@v4 | |
| - name: Set up Git | |
| run: | | |
| git config user.name "github-actions" | |
| git config user.email "[email protected]" | |
| - name: Parse challenge type and scenario file | |
| id: vars | |
| run: | | |
| uuid=$(uuidgen | cut -d- -f1) | |
| username="${{ github.event.issue.user.login }}" | |
| body="${{ github.event.issue.body }}" | |
| # Extract dropdown value and scenario file (if typed) | |
| challenge_type=$(echo "$body" | grep -A2 "What type of challenge do you want" | sed '/^$/d' | tail -n1 | tr -d '\r') | |
| scenario_file=$(echo "$body" | grep -A2 "Scenario file name" | sed '/^$/d' | tail -n1 | tr -d '\r') | |
| # Set fallback defaults | |
| fallback_code="code_scenario.rs" | |
| fallback_text="txt_scenario.md" | |
| # Determine file path | |
| if [ -z "$scenario_file" ]; then | |
| if [ "$challenge_type" = "text" ]; then | |
| scenario_file="$fallback_text" | |
| else | |
| scenario_file="$fallback_code" | |
| fi | |
| elif [ ! -f "Tasks/Conflicts/$scenario_file" ]; then | |
| # Invalid filename fallback | |
| if [ "$challenge_type" = "text" ]; then | |
| scenario_file="$fallback_text" | |
| else | |
| scenario_file="$fallback_code" | |
| fi | |
| fi | |
| target_file="Solutions/Conflict-fixes/${username}-${scenario_file}" | |
| # Compose variant file names (base, 2, 3) | |
| ext="${scenario_file##*.}" | |
| base="${scenario_file%.*}" | |
| file1="${scenario_file}" | |
| file2="${base}2.${ext}" | |
| file3="${base}3.${ext}" | |
| echo "challenge_type=$challenge_type" | |
| echo "scenario_file=$scenario_file" | |
| echo "uuid=$uuid" >> $GITHUB_OUTPUT | |
| echo "username=$username" >> $GITHUB_OUTPUT | |
| echo "scenario_file=$scenario_file" >> $GITHUB_OUTPUT | |
| echo "target_file=$target_file" >> $GITHUB_OUTPUT | |
| echo "file1=$file1" >> $GITHUB_OUTPUT | |
| echo "file2=$file2" >> $GITHUB_OUTPUT | |
| echo "file3=$file3" >> $GITHUB_OUTPUT | |
| - name: Copy stub and commit to main (version 1 file) | |
| run: | | |
| cp "Tasks/Conflicts/${{ steps.vars.outputs.file1 }}" "${{ steps.vars.outputs.target_file }}" | |
| git add "${{ steps.vars.outputs.target_file }}" | |
| git commit -m "Add scenario file for ${{ steps.vars.outputs.username }} (version 1)" | |
| git push origin main | |
| - name: Create conflict branch and overwrite with version 3 | |
| run: | | |
| git checkout -b conflict-${{ steps.vars.outputs.uuid }} | |
| cp "Tasks/Conflicts/${{ steps.vars.outputs.file3 }}" "${{ steps.vars.outputs.target_file }}" | |
| git add "${{ steps.vars.outputs.target_file }}" | |
| git commit -m "Introduce conflicting change for ${{ steps.vars.outputs.username }} (version 3)" | |
| git push origin conflict-${{ steps.vars.outputs.uuid }} | |
| - name: Generate actual conflict in main (overwrite with version 2) | |
| run: | | |
| git switch main | |
| cp "Tasks/Conflicts/${{ steps.vars.outputs.file2 }}" "${{ steps.vars.outputs.target_file }}" | |
| git add "${{ steps.vars.outputs.target_file }}" | |
| git commit -m "Introduce conflicting change for ${{ steps.vars.outputs.username }} (version 2)" | |
| git push origin main | |
| - name: Comment with next steps | |
| uses: actions/github-script@v7 | |
| env: | |
| USERNAME: ${{ steps.vars.outputs.username }} | |
| UUID: ${{ steps.vars.outputs.uuid }} | |
| SCENARIO_FILE: ${{ steps.vars.outputs.scenario_file }} | |
| with: | |
| script: | | |
| const username = process.env.USERNAME; | |
| const uuid = process.env.UUID; | |
| const scenario_file = process.env.SCENARIO_FILE; | |
| github.rest.issues.createComment({ | |
| issue_number: context.issue.number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: `🎉 Conflict scenario ready for **@${username}** | |
| 🔹 Branch \`main\`: version 2 | |
| 🔹 Branch \`conflict-${uuid}\`: version 3 | |
| File to resolve: | |
| \`Solutions/Conflict-fixes/${username}-${scenario_file}\` | |
| Fork the repo, resolve the conflict, then open a PR to complete the task! 💪` | |
| }) |