From 8bcdca93f0adc8fca9c8219711650b9be3f9dd1c Mon Sep 17 00:00:00 2001 From: Igor Poltosi Date: Tue, 11 May 2021 11:59:38 -0300 Subject: [PATCH] New input flag called CONTENT_COMPARISON --- README.md | 9 +++++++- action.yml | 3 +++ dist/index.js | 60 +++++++++++++++++++++++++++++++++++---------------- index.js | 60 +++++++++++++++++++++++++++++++++++---------------- package.json | 2 +- 5 files changed, 94 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 431777fd..ce9463a5 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,13 @@ Set to `true` for the pull request to be opened as a draft. Default: `false` +### `CONTENT_COMPARISON` + +Set to `true` to force checking content comparison between branches. +No more empty pull requests being opened and triggering CI jobs. + +Default: `false` + ## Outputs ### `PULL_REQUEST_URL` @@ -67,7 +74,7 @@ jobs: node-version: 12 - name: Opening pull request id: pull - uses: tretuna/sync-branches@1.2.0 + uses: tretuna/sync-branches@1.3.0 with: GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} FROM_BRANCH: "master" diff --git a/action.yml b/action.yml index 984cb9fd..91fd7a39 100644 --- a/action.yml +++ b/action.yml @@ -22,6 +22,9 @@ inputs: PULL_REQUEST_IS_DRAFT: description: "Set to 'true' for the pull request to be opened as a draft. Default: 'false'" required: false + CONTENT_COMPARISON: + description: "Set to 'true' to force checking content comparison between the branches. Default: 'false'" + required: false outputs: PULL_REQUEST_URL: description: "URL for either the generated pull request or the currently open one" diff --git a/dist/index.js b/dist/index.js index 20dc66f6..c0830ceb 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1989,8 +1989,9 @@ async function run() { const pullRequestTitle = core.getInput("PULL_REQUEST_TITLE"); const pullRequestBody = core.getInput("PULL_REQUEST_BODY"); const pullRequestIsDraft = core.getInput("PULL_REQUEST_IS_DRAFT").toLowerCase() === "true"; + const contentComparison = core.getInput("CONTENT_COMPARISON").toLowerCase() === "true"; - console.log(`Making a pull request to ${toBranch} from ${fromBranch}.`); + console.log(`Should a pull request to ${toBranch} from ${fromBranch} be created?`); const { payload: { repository } @@ -2008,26 +2009,35 @@ async function run() { }); if (!currentPull) { - const { data: pullRequest } = await octokit.pulls.create({ - owner: repository.owner.login, - repo: repository.name, - head: fromBranch, - base: toBranch, - title: pullRequestTitle - ? pullRequestTitle - : `sync: ${fromBranch} to ${toBranch}`, - body: pullRequestBody - ? pullRequestBody - : `sync-branches: New code has just landed in ${fromBranch}, so let's bring ${toBranch} up to speed!`, - draft: pullRequestIsDraft - }); + let shouldCreatePullRequest = true; + if (contentComparison) { + shouldCreatePullRequest = await hasContentDifference(octokit, repository, fromBranch, toBranch); + } - console.log( - `Pull request (${pullRequest.number}) successful! You can view it here: ${pullRequest.url}.` - ); + if (shouldCreatePullRequest) { + const { data: pullRequest } = await octokit.pulls.create({ + owner: repository.owner.login, + repo: repository.name, + head: fromBranch, + base: toBranch, + title: pullRequestTitle + ? pullRequestTitle + : `sync: ${fromBranch} to ${toBranch}`, + body: pullRequestBody + ? pullRequestBody + : `sync-branches: New code has just landed in ${fromBranch}, so let's bring ${toBranch} up to speed!`, + draft: pullRequestIsDraft + }); + + console.log( + `Pull request (${pullRequest.number}) successful! You can view it here: ${pullRequest.url}` + ); - core.setOutput("PULL_REQUEST_URL", pullRequest.url.toString()); - core.setOutput("PULL_REQUEST_NUMBER", pullRequest.number.toString()); + core.setOutput("PULL_REQUEST_URL", pullRequest.url.toString()); + core.setOutput("PULL_REQUEST_NUMBER", pullRequest.number.toString()); + } else { + console.log(`There is no content difference between ${fromBranch} and ${toBranch}.`); + } } else { console.log( `There is already a pull request (${currentPull.number}) to ${toBranch} from ${fromBranch}.`, @@ -2042,6 +2052,18 @@ async function run() { } } +async function hasContentDifference(octokit, repository, fromBranch, toBranch) { + const { data: response } = await octokit.repos.compareCommits({ + owner: repository.owner.name, + repo: repository.name, + base: toBranch, + head: fromBranch, + page: 1, + per_page: 1 + }); + return response.files.length > 0; +} + run(); diff --git a/index.js b/index.js index aae229f4..de458058 100644 --- a/index.js +++ b/index.js @@ -9,8 +9,9 @@ async function run() { const pullRequestTitle = core.getInput("PULL_REQUEST_TITLE"); const pullRequestBody = core.getInput("PULL_REQUEST_BODY"); const pullRequestIsDraft = core.getInput("PULL_REQUEST_IS_DRAFT").toLowerCase() === "true"; + const contentComparison = core.getInput("CONTENT_COMPARISON").toLowerCase() === "true"; - console.log(`Making a pull request to ${toBranch} from ${fromBranch}.`); + console.log(`Should a pull request to ${toBranch} from ${fromBranch} be created?`); const { payload: { repository } @@ -28,26 +29,35 @@ async function run() { }); if (!currentPull) { - const { data: pullRequest } = await octokit.pulls.create({ - owner: repository.owner.login, - repo: repository.name, - head: fromBranch, - base: toBranch, - title: pullRequestTitle - ? pullRequestTitle - : `sync: ${fromBranch} to ${toBranch}`, - body: pullRequestBody - ? pullRequestBody - : `sync-branches: New code has just landed in ${fromBranch}, so let's bring ${toBranch} up to speed!`, - draft: pullRequestIsDraft - }); + let shouldCreatePullRequest = true; + if (contentComparison) { + shouldCreatePullRequest = await hasContentDifference(octokit, repository, fromBranch, toBranch); + } - console.log( - `Pull request (${pullRequest.number}) successful! You can view it here: ${pullRequest.url}.` - ); + if (shouldCreatePullRequest) { + const { data: pullRequest } = await octokit.pulls.create({ + owner: repository.owner.login, + repo: repository.name, + head: fromBranch, + base: toBranch, + title: pullRequestTitle + ? pullRequestTitle + : `sync: ${fromBranch} to ${toBranch}`, + body: pullRequestBody + ? pullRequestBody + : `sync-branches: New code has just landed in ${fromBranch}, so let's bring ${toBranch} up to speed!`, + draft: pullRequestIsDraft + }); + + console.log( + `Pull request (${pullRequest.number}) successful! You can view it here: ${pullRequest.url}` + ); - core.setOutput("PULL_REQUEST_URL", pullRequest.url.toString()); - core.setOutput("PULL_REQUEST_NUMBER", pullRequest.number.toString()); + core.setOutput("PULL_REQUEST_URL", pullRequest.url.toString()); + core.setOutput("PULL_REQUEST_NUMBER", pullRequest.number.toString()); + } else { + console.log(`There is no content difference between ${fromBranch} and ${toBranch}.`); + } } else { console.log( `There is already a pull request (${currentPull.number}) to ${toBranch} from ${fromBranch}.`, @@ -62,4 +72,16 @@ async function run() { } } +async function hasContentDifference(octokit, repository, fromBranch, toBranch) { + const { data: response } = await octokit.repos.compareCommits({ + owner: repository.owner.name, + repo: repository.name, + base: toBranch, + head: fromBranch, + page: 1, + per_page: 1 + }); + return response.files.length > 0; +} + run(); diff --git a/package.json b/package.json index 2cdd2970..4a8fca21 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sync-branches", - "version": "1.2.0", + "version": "1.3.0", "description": "GitHub Action to sync a branch when another one is updated", "main": "index.js", "author": "Tre Ammatuna ",