From 10f2c452a549e27cdfdbade301b0f3746836aa0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Mayr?= Date: Wed, 10 Mar 2021 15:29:25 -0300 Subject: [PATCH] feat: add flag to skip unstaged files --- README.md | 2 +- action.yml | 3 +++ dist/index.js | 23 ++++++++++++++++------- src/create-or-update-branch.ts | 5 +++-- src/create-pull-request.ts | 4 +++- src/main.ts | 3 ++- 6 files changed, 28 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 5fdd8ea..1474160 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ All inputs are **optional**. If not set, sensible defaults will be used. | `team-reviewers` | A comma or newline-separated list of GitHub teams to request a review from. Note that a `repo` scoped [PAT](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) may be required. See [this issue](https://github.com/peter-evans/create-pull-request/issues/155). | | | `milestone` | The number of the milestone to associate this pull request with. | | | `draft` | Create a [draft pull request](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests#draft-pull-requests). | `false` | - +| `skip-unstaged-files` | Skips adding any unstaged files when creating the pull request | `false` | ### Action outputs The following outputs can be used by subsequent workflow steps. diff --git a/action.yml b/action.yml index c169c32..4f9750e 100644 --- a/action.yml +++ b/action.yml @@ -64,6 +64,9 @@ inputs: draft: description: 'Create a draft pull request' default: false + skinUnstagedFiles: + description: 'Skips any unstaged files when creating a pull request.' + default: false outputs: pull-request-number: description: 'The pull request number' diff --git a/dist/index.js b/dist/index.js index 51c5f31..95458f9 100644 --- a/dist/index.js +++ b/dist/index.js @@ -99,7 +99,7 @@ function splitLines(multilineString) { .map(s => s.trim()) .filter(x => x !== ''); } -function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName, signoff) { +function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName, signoff, skipUnstagedFiles) { return __awaiter(this, void 0, void 0, function* () { // Get the working base. // When a ref, it may or may not be the actual base. @@ -122,7 +122,7 @@ function createOrUpdateBranch(git, commitMessage, base, branch, branchRemoteName const tempBranch = uuid_1.v4(); yield git.checkout(tempBranch, 'HEAD'); // Commit any uncommitted changes - if (yield git.isDirty(true)) { + if (skipUnstagedFiles === false && (yield git.isDirty(true))) { core.info('Uncommitted changes found. Adding a commit.'); yield git.exec(['add', '-A']); const params = ['-m', commitMessage]; @@ -375,7 +375,7 @@ function createPullRequest(inputs) { core.endGroup(); // Create or update the pull request branch core.startGroup('Create or update the pull request branch'); - const result = yield create_or_update_branch_1.createOrUpdateBranch(git, inputs.commitMessage, inputs.base, inputs.branch, branchRemoteName, inputs.signoff); + const result = yield create_or_update_branch_1.createOrUpdateBranch(git, inputs.commitMessage, inputs.base, inputs.branch, branchRemoteName, inputs.signoff, inputs.skipUnstagedFiles); core.endGroup(); if (['created', 'updated'].includes(result.action)) { // The branch was created or updated @@ -1082,7 +1082,8 @@ function run() { reviewers: utils.getInputAsArray('reviewers'), teamReviewers: utils.getInputAsArray('team-reviewers'), milestone: Number(core.getInput('milestone')), - draft: core.getInput('draft') === 'true' + draft: core.getInput('draft') === 'true', + skipUnstagedFiles: core.getInput('skip-unstaged-files') === 'true' }; core.debug(`Inputs: ${util_1.inspect(inputs)}`); yield create_pull_request_1.createPullRequest(inputs); @@ -3497,7 +3498,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); var request = __nccwpck_require__(234); var universalUserAgent = __nccwpck_require__(30); -const VERSION = "4.6.0"; +const VERSION = "4.6.1"; class GraphqlError extends Error { constructor(request, response) { @@ -3520,10 +3521,18 @@ class GraphqlError extends Error { } const NON_VARIABLE_OPTIONS = ["method", "baseUrl", "url", "headers", "request", "query", "mediaType"]; +const FORBIDDEN_VARIABLE_OPTIONS = ["query", "method", "url"]; const GHES_V3_SUFFIX_REGEX = /\/api\/v3\/?$/; function graphql(request, query, options) { - if (typeof query === "string" && options && "query" in options) { - return Promise.reject(new Error(`[@octokit/graphql] "query" cannot be used as variable name`)); + if (options) { + if (typeof query === "string" && "query" in options) { + return Promise.reject(new Error(`[@octokit/graphql] "query" cannot be used as variable name`)); + } + + for (const key in options) { + if (!FORBIDDEN_VARIABLE_OPTIONS.includes(key)) continue; + return Promise.reject(new Error(`[@octokit/graphql] "${key}" cannot be used as variable name`)); + } } const parsedOptions = typeof query === "string" ? Object.assign({ diff --git a/src/create-or-update-branch.ts b/src/create-or-update-branch.ts index 2767014..e3e4683 100644 --- a/src/create-or-update-branch.ts +++ b/src/create-or-update-branch.ts @@ -91,7 +91,8 @@ export async function createOrUpdateBranch( base: string, branch: string, branchRemoteName: string, - signoff: boolean + signoff: boolean, + skipUnstagedFiles: boolean ): Promise { // Get the working base. // When a ref, it may or may not be the actual base. @@ -117,7 +118,7 @@ export async function createOrUpdateBranch( const tempBranch = uuidv4() await git.checkout(tempBranch, 'HEAD') // Commit any uncommitted changes - if (await git.isDirty(true)) { + if (skipUnstagedFiles === false && (await git.isDirty(true))) { core.info('Uncommitted changes found. Adding a commit.') await git.exec(['add', '-A']) const params = ['-m', commitMessage] diff --git a/src/create-pull-request.ts b/src/create-pull-request.ts index dc52b19..cbd6caf 100644 --- a/src/create-pull-request.ts +++ b/src/create-pull-request.ts @@ -29,6 +29,7 @@ export interface Inputs { teamReviewers: string[] milestone: number draft: boolean + skipUnstagedFiles: boolean } export async function createPullRequest(inputs: Inputs): Promise { @@ -173,7 +174,8 @@ export async function createPullRequest(inputs: Inputs): Promise { inputs.base, inputs.branch, branchRemoteName, - inputs.signoff + inputs.signoff, + inputs.skipUnstagedFiles ) core.endGroup() diff --git a/src/main.ts b/src/main.ts index 157659b..05a14eb 100644 --- a/src/main.ts +++ b/src/main.ts @@ -24,7 +24,8 @@ async function run(): Promise { reviewers: utils.getInputAsArray('reviewers'), teamReviewers: utils.getInputAsArray('team-reviewers'), milestone: Number(core.getInput('milestone')), - draft: core.getInput('draft') === 'true' + draft: core.getInput('draft') === 'true', + skipUnstagedFiles: core.getInput('skip-unstaged-files') === 'true' } core.debug(`Inputs: ${inspect(inputs)}`)