diff --git a/.github/workflows/sync.yml b/.github/workflows/sync.yml index cd534821..22e3a6ae 100644 --- a/.github/workflows/sync.yml +++ b/.github/workflows/sync.yml @@ -9,11 +9,10 @@ jobs: sync-upstream: runs-on: ubuntu-latest env: - UPSTREAM: "https://github.com/bitcoin-core/secp256k1.git" - UPSTREAM_BRANCH: "master" - ORIGIN_BRANCH: "master" - MIN_UPSTREAM_MERGES: 1 - + UPSTREAM: "https://github.com/bitcoin-core/secp256k1.git" + BASE_BRANCH: "master" + UPSTREAM_REF: "upstream/master" + steps: - name: Checkout repository uses: actions/checkout@v6 @@ -26,66 +25,37 @@ jobs: git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" - - name: Fetch upstream & generate branch name & check for new commits - id: check_commits - env: - GH_TOKEN: ${{ secrets.SYNC_PAT }} + - name: Fetch upstream run: | - gh repo set-default ${{ github.repository }} # Set the default repo to the origin repository git remote add upstream ${{ env.UPSTREAM }} git fetch upstream - - MERGES=$(git rev-list --count --merges HEAD..upstream/${{ env.UPSTREAM_BRANCH }}) - echo "Found $MERGES new merge commits in upstream." - - if [ "$MERGES" -lt ${{ env.MIN_UPSTREAM_MERGES }} ]; then - echo "Exiting." - echo "skip=true" >> "$GITHUB_OUTPUT" - else - echo "skip=false" >> "$GITHUB_OUTPUT" - fi - - # Generate a sync branch name "sync-UPSTREAM_HEAD", where UPSTREAM_HEAD is a commit ID. - UPSTREAM_HEAD=$(git rev-parse --short upstream/${{ env.UPSTREAM_BRANCH }}) - SYNC_BRANCH="sync-$UPSTREAM_HEAD" - echo "Sync branch name: $SYNC_BRANCH" - echo "SYNC_BRANCH=$SYNC_BRANCH" >> "$GITHUB_ENV" - - # Check if the sync branch already exists in the origin repository - if git ls-remote --heads origin "$SYNC_BRANCH" | grep -q "$SYNC_BRANCH"; then - echo "Branch $SYNC_BRANCH already exists. Skipping the sync." - echo "skip=true" >> "$GITHUB_OUTPUT" - else - echo "skip=false" >> "$GITHUB_OUTPUT" - fi - - - name: Generate PR metadata - id: branch_pr_metadata - if: steps.check_commits.outputs.skip == 'false' - run: | - # Call the sync script to generate PR title and body - ./contrib/sync-upstream.sh -b ${{ env.ORIGIN_BRANCH }} "$SYNC_BRANCH" - - - name: Push a sync branch - id: push_branch - if: steps.check_commits.outputs.skip == 'false' - run: | - echo "Creating sync branch: $SYNC_BRANCH" - git checkout upstream/${{ env.UPSTREAM_BRANCH }} - git checkout -b "$SYNC_BRANCH" - git push -u origin "$SYNC_BRANCH" - - - name: Create pull request - id: create_pr - if: steps.check_commits.outputs.skip == 'false' + gh repo set-default ${{ github.repository }} env: GH_TOKEN: ${{ secrets.SYNC_PAT }} - run: | - # Execute the generated PR creation script - ./contrib/gh-pr-create.sh - - name: Cleanup sync branch on failure - if: steps.push_branch.outcome == 'success' && steps.create_pr.outcome == 'failure' + - name: Run sync-upstream.sh + id: sync run: | - echo "PR creation failed but branch was pushed. Deleting: $SYNC_BRANCH" - git push origin --delete "$SYNC_BRANCH" \ No newline at end of file + OUTPUT=$(./contrib/sync-upstream.sh --switch "${{ env.BASE_BRANCH }}" "${{ env.UPSTREAM_REF }}" 2>&1) || { echo "$OUTPUT"; exit 1; } + echo "$OUTPUT" + if echo "$OUTPUT" | grep -qv "^No merge commits"; then + echo "newcommits=true" >> "$GITHUB_OUTPUT" + else + echo "Skipping further workflow steps." + fi + + - name: Push sync branch + id: push + if: steps.sync.outputs.newcommits == 'true' + run: | + if git push -u origin HEAD; then + echo "pushed=true" >> "$GITHUB_OUTPUT" + else + echo "Skipping further workflow steps." + fi + + - name: Create pull request + if: steps.sync.outputs.newcommits == 'true' && steps.push.outputs.pushed == 'true' + env: + GH_TOKEN: ${{ secrets.SYNC_PAT }} + run: ./gh-pr-create.sh