Skip to content

Commit

Permalink
feat: auto-merge option (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Oct 31, 2021
1 parent f35c2ec commit e442c32
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 29 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,29 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

autoMerge:
name: "[TEST] Auto merge"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
persist-credentials: false
- run: "date > test.txt"
- run: "npm ci"
- run: "npm run build"
- uses: ./
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ACTIONS_STEP_DEBUG: true
with:
title: Test pull request for `auto-merge` option
commit-message: "Testing auto-merge"
auto-merge: squash
branch: test-auto-merge
- run: "git push https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git :test-auto-merge"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# expect to detect changes in a hidden file:
# https://github.com/gr2m/create-or-update-pull-request-action/pull/262
hiddenFile:
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ with:
author: "Lorem J. Ipsum <lorem@example.com>"
labels: label1, label2
assignees: user1, user2
auto-merge: squash
```
**Note:** `auto-merge` is optional. It can be set to `merge`, `squash`, or `rebase`. If [auto-merging](https://docs.github.com/en/github/collaborating-with-pull-requests/incorporating-changes-from-a-pull-request/automatically-merging-a-pull-request) is disabled in the repository, a warning will be logged, but the action will not fail.

To create multiple commits for different paths, use the action multiple times

```yml
Expand Down
65 changes: 48 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ const { inspect } = require("util");

const { command } = require("execa");
const core = require("@actions/core");
const {
request: { defaults },
} = require("@octokit/request");
const { Octokit } = require("@octokit/core");

const TEMPORARY_BRANCH_NAME = `tmp-create-or-update-pull-request-action-${Math.random()
.toString(36)
Expand Down Expand Up @@ -38,10 +36,8 @@ async function main() {
return;
}

const request = defaults({
headers: {
authorization: `token ${process.env.GITHUB_TOKEN}`,
},
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
});

const [owner, repo] = process.env.GITHUB_REPOSITORY.split("/");
Expand All @@ -55,14 +51,25 @@ async function main() {
commitMessage: core.getInput("commit-message"),
author: core.getInput("author"),
labels: core.getInput("labels"),
assignees: core.getInput("assignees")
assignees: core.getInput("assignees"),
autoMerge: core.getInput("auto-merge"),
};

core.debug(`Inputs: ${inspect(inputs)}`);

if (
inputs.autoMerge &&
!["merge", "squash", "rebase"].includes(inputs.autoMerge)
) {
core.setFailed(
`auto-merge is set to "${inputs.autoMerge}", but must be one of "merge", "squash", "rebase"`
);
process.exit(1);
}

const {
data: { default_branch },
} = await request(`GET /repos/{owner}/{repo}`, {
} = await octokit.request(`GET /repos/{owner}/{repo}`, {
owner,
repo,
});
Expand Down Expand Up @@ -138,7 +145,7 @@ async function main() {

if (remoteBranchExists) {
const q = `head:${inputs.branch} type:pr is:open repo:${process.env.GITHUB_REPOSITORY}`;
const { data } = await request("GET /search/issues", {
const { data } = await octokit.request("GET /search/issues", {
q,
});

Expand All @@ -152,8 +159,8 @@ async function main() {

core.debug(`Creating pull request`);
const {
data: { html_url, number },
} = await request(`POST /repos/{owner}/{repo}/pulls`, {
data: { html_url, number, node_id },
} = await octokit.request(`POST /repos/{owner}/{repo}/pulls`, {
owner,
repo,
title: inputs.title,
Expand All @@ -167,7 +174,7 @@ async function main() {
if (inputs.labels) {
core.debug(`Adding labels: ${inputs.labels}`);
const labels = inputs.labels.trim().split(/\s*,\s*/);
const { data } = await request(
const { data } = await octokit.request(
`POST /repos/{owner}/{repo}/issues/{issue_number}/labels`,
{
owner,
Expand All @@ -183,7 +190,7 @@ async function main() {
if (inputs.assignees) {
core.debug(`Adding assignees: ${inputs.assignees}`);
const assignees = inputs.assignees.trim().split(/\s*,\s*/);
const { data } = await request(
const { data } = await octokit.request(
`POST /repos/{owner}/{repo}/issues/{issue_number}/assignees`,
{
owner,
Expand All @@ -196,6 +203,31 @@ async function main() {
core.debug(inspect(data));
}

if (inputs.autoMerge) {
const query = `
mutation($pullRequestId: ID!, $mergeMethod: PullRequestMergeMethod!, $commitHeadline: String!) {
enablePullRequestAutoMerge(input: {pullRequestId: $pullRequestId, mergeMethod: $mergeMethod, commitHeadline: $commitHeadline}) {
actor {
login
}
}
}
`;
try {
const result = await octokit.graphql(query, {
pullRequestId: node_id,
mergeMethod: inputs.autoMerge.toUpperCase(),
commitHeadline: inputs.title,
});
core.info(`Auto merge enabled`);
core.debug(inspect(result));
} catch (error) {
core.warning(
`Auto merge could not be enabled for the pull request. Make sure the feature is enabled in the repository settings`
);
}
}

await runShellCommand(`git stash pop || true`);
} catch (error) {
core.info(inspect(error));
Expand All @@ -210,9 +242,8 @@ async function getLocalChanges(path) {
return {};
}

const hasUncommitedChanges = /(Changes to be committed|Changes not staged|Untracked files)/.test(
output
);
const hasUncommitedChanges =
/(Changes to be committed|Changes not staged|Untracked files)/.test(output);

return {
hasUncommitedChanges,
Expand Down
14 changes: 3 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"license": "ISC",
"dependencies": {
"@actions/core": "^1.2.6",
"@octokit/request": "^5.1.0",
"@octokit/core": "^3.5.1",
"execa": "^2.0.4"
},
"devDependencies": {
Expand Down

0 comments on commit e442c32

Please sign in to comment.