draft always-true

This commit is contained in:
Peter Evans 2024-08-18 21:54:43 +00:00
parent 10454726b6
commit c64379e4f4
5 changed files with 73 additions and 6 deletions

35
dist/index.js vendored
View file

@ -1217,11 +1217,13 @@ class GitHubHelper {
// Try to create the pull request
try {
core.info(`Attempting creation of pull request`);
const { data: pull } = yield this.octokit.rest.pulls.create(Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { title: inputs.title, head: headBranch, head_repo: headRepository, base: inputs.base, body: inputs.body, draft: inputs.draft, maintainer_can_modify: inputs.maintainerCanModify }));
const { data: pull } = yield this.octokit.rest.pulls.create(Object.assign(Object.assign({}, this.parseRepository(baseRepository)), { title: inputs.title, head: headBranch, head_repo: headRepository, base: inputs.base, body: inputs.body, draft: inputs.draft.value, maintainer_can_modify: inputs.maintainerCanModify }));
core.info(`Created pull request #${pull.number} (${headBranch} => ${inputs.base})`);
return {
number: pull.number,
html_url: pull.html_url,
node_id: pull.node_id,
draft: pull.draft,
created: true
};
}
@ -1248,6 +1250,8 @@ class GitHubHelper {
return {
number: pull.number,
html_url: pull.html_url,
node_id: pull.node_id,
draft: pull.draft,
created: false
};
});
@ -1302,9 +1306,28 @@ class GitHubHelper {
throw e;
}
}
// Convert back to draft if 'draft: always-true' is set
if (inputs.draft.always && pull.draft !== undefined && !pull.draft) {
yield this.convertToDraft(pull.node_id);
}
return pull;
});
}
convertToDraft(id) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Converting pull request to draft`);
yield this.octokit.graphql({
query: `mutation($pullRequestId: ID!) {
convertPullRequestToDraft(input: {pullRequestId: $pullRequestId}) {
pullRequest {
isDraft
}
}
}`,
pullRequestId: id
});
});
}
pushSignedCommits(branchCommits, baseSha, repoPath, branchRepository, branch) {
return __awaiter(this, void 0, void 0, function* () {
let headCommit = {
@ -1426,6 +1449,14 @@ const core = __importStar(__nccwpck_require__(2186));
const create_pull_request_1 = __nccwpck_require__(3780);
const util_1 = __nccwpck_require__(3837);
const utils = __importStar(__nccwpck_require__(918));
function getDraftInput() {
if (core.getInput('draft') === 'always-true') {
return { value: true, always: true };
}
else {
return { value: core.getBooleanInput('draft'), always: false };
}
}
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
@ -1452,7 +1483,7 @@ function run() {
reviewers: utils.getInputAsArray('reviewers'),
teamReviewers: utils.getInputAsArray('team-reviewers'),
milestone: Number(core.getInput('milestone')),
draft: core.getBooleanInput('draft'),
draft: getDraftInput(),
maintainerCanModify: core.getBooleanInput('maintainer-can-modify')
};
core.debug(`Inputs: ${(0, util_1.inspect)(inputs)}`);

View file

@ -269,7 +269,7 @@ The `token` input will then default to the repository's `GITHUB_TOKEN`, which wi
> - Since `GITHUB_TOKEN` will be used to create the pull request, the workflow *must* be executing in the parent repository where the pull request should be created.
> - `maintainer-can-modify` *must* be set to `false`, because the `GITHUB_TOKEN` will not have `write` access to the head branch in the fork.
The following is an example of pushing to a fork
The following is an example of pushing to a fork using GitHub App tokens.
```yaml
- uses: actions/create-github-app-token@v1
id: generate-token

View file

@ -32,7 +32,10 @@ export interface Inputs {
reviewers: string[]
teamReviewers: string[]
milestone: number
draft: boolean
draft: {
value: boolean
always: boolean
}
maintainerCanModify: boolean
}

View file

@ -20,6 +20,8 @@ interface Repository {
interface Pull {
number: number
html_url: string
node_id: string
draft?: boolean
created: boolean
}
@ -78,7 +80,7 @@ export class GitHubHelper {
head_repo: headRepository,
base: inputs.base,
body: inputs.body,
draft: inputs.draft,
draft: inputs.draft.value,
maintainer_can_modify: inputs.maintainerCanModify
})
core.info(
@ -87,6 +89,8 @@ export class GitHubHelper {
return {
number: pull.number,
html_url: pull.html_url,
node_id: pull.node_id,
draft: pull.draft,
created: true
}
} catch (e) {
@ -127,6 +131,8 @@ export class GitHubHelper {
return {
number: pull.number,
html_url: pull.html_url,
node_id: pull.node_id,
draft: pull.draft,
created: false
}
}
@ -209,9 +215,28 @@ export class GitHubHelper {
}
}
// Convert back to draft if 'draft: always-true' is set
if (inputs.draft.always && pull.draft !== undefined && !pull.draft) {
await this.convertToDraft(pull.node_id)
}
return pull
}
private async convertToDraft(id: string): Promise<void> {
core.info(`Converting pull request to draft`)
await this.octokit.graphql({
query: `mutation($pullRequestId: ID!) {
convertPullRequestToDraft(input: {pullRequestId: $pullRequestId}) {
pullRequest {
isDraft
}
}
}`,
pullRequestId: id
})
}
async pushSignedCommits(
branchCommits: Commit[],
baseSha: string,

View file

@ -3,6 +3,14 @@ import {Inputs, createPullRequest} from './create-pull-request'
import {inspect} from 'util'
import * as utils from './utils'
function getDraftInput(): {value: boolean; always: boolean} {
if (core.getInput('draft') === 'always-true') {
return {value: true, always: true}
} else {
return {value: core.getBooleanInput('draft'), always: false}
}
}
async function run(): Promise<void> {
try {
const inputs: Inputs = {
@ -28,7 +36,7 @@ async function run(): Promise<void> {
reviewers: utils.getInputAsArray('reviewers'),
teamReviewers: utils.getInputAsArray('team-reviewers'),
milestone: Number(core.getInput('milestone')),
draft: core.getBooleanInput('draft'),
draft: getDraftInput(),
maintainerCanModify: core.getBooleanInput('maintainer-can-modify')
}
core.debug(`Inputs: ${inspect(inputs)}`)