Skip to content

Commit

Permalink
New input flag called CONTENT_COMPARISON
Browse files Browse the repository at this point in the history
  • Loading branch information
igorpoltosi committed May 11, 2021
1 parent 7e3d719 commit 8bcdca9
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 40 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
60 changes: 41 additions & 19 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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}.`,
Expand All @@ -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();


Expand Down
60 changes: 41 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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}.`,
Expand All @@ -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();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down

0 comments on commit 8bcdca9

Please sign in to comment.