diff --git a/.github/workflows/repo-sync.yml b/.github/workflows/repo-sync.yml index 7c550a4285..bf90e040c8 100644 --- a/.github/workflows/repo-sync.yml +++ b/.github/workflows/repo-sync.yml @@ -57,7 +57,7 @@ jobs: console.log('Closed pull request', prNumber) } - console.log('Close any existing pull requests') + console.log('Closing any existing pull requests') const { data: existingPulls } = await github.rest.pulls.list({ owner, repo, head, base }) if (existingPulls.length) { console.log('Found existing pull requests', existingPulls.map(pull => pull.number)) @@ -67,78 +67,95 @@ jobs: console.log('Closed existing pull requests') } - console.log('Create a new pull request') + console.log('Creating a new pull request') const body = ` This is an automated pull request to sync changes between the public and private repos. Our bot will merge this pull request automatically. To preserve continuity across repos, _do not squash_ this pull request. ` - let pull + let pull, pull_number try { - pull = (await github.rest.pulls.create({ + const response = await github.rest.pulls.create({ owner, repo, head, base, title: 'Repo sync', body, - })).data + }) + pull = response.data + pull_number = pull.number + console.log('Created pull request successfully', pull.html_url) } catch (err) { + // Don't error/alert if there's no commits to sync if (err.message?.includes('No commits')) { console.log(err.message) return } throw err } - console.log('Created pull request successfully', pull.html_url) - console.log('Count files') - const { data: prFiles } = await github.rest.pulls.listFiles({ owner, repo, pull_number: pull.number }) - if (!prFiles.length) { - console.log('No files changed, closing') - await closePullRequest(pull.number) - return - } - - console.log('Check mergeable') - if (!pull.mergeable) { - console.log('Pull request is not mergeable, closing') - await closePullRequest(pull.number) - return - } - - console.log('Lock conversations') + console.log('Locking conversations to prevent spam') try { await github.rest.issues.lock({ ...context.repo, - issue_number: pull.number, + issue_number: pull_number, lock_reason: 'spam' }) - console.log('Locked the pull request to prevent spam!') + console.log('Locked the pull request to prevent spam') } catch (error) { console.error('Failed to lock the pull request.', error) // Don't fail the workflow } - console.log('Approve pull request') + console.log('Counting files changed') + const { data: prFiles } = await github.rest.pulls.listFiles({ owner, repo, pull_number }) + if (prFiles.length) { + console.log(prFiles.length, 'files have changed') + } else { + console.log('No files changed, closing') + await closePullRequest(pull_number) + return + } + + console.log('Checking for merge conflicts') + if (pull.mergeable_state === 'dirty') { + console.log('Pull request has a conflict', pull.html_url) + await closePullRequest(pull_number) + throw new Error('Pull request has a conflict, please resolve manually') + } + console.log('No detected merge conflicts') + + // Temporary, what does this prevent? + // console.log('Checking mergeable') + // if (pull.mergeable) { + // console.log('Pull request is mergeable') + // } else { + // console.log('Pull request is not mergeable, closing') + // await closePullRequest(pull_number) + // return + // } + + console.log('Approving pull request') try { await github.rest.pulls.createReview({ owner, repo, - pull_number: pull.number, + pull_number, event: 'APPROVE', }) + console.log('Approved pull request') } catch (error) { console.error('Failed to approve the pull request.', error) // Don't fail the workflow } - console.log('Admin merge the pull request') + console.log('Merging the pull request') // Admin merge pull request to avoid squash await github.rest.pulls.merge({ owner, repo, - pull_number: pull.number, + pull_number, merge_method: 'merge', }) // Error loud here, so no try/catch