Skip to content

Commit 98a1fa7

Browse files
committed
convert to draft on branch updates when there is a diff with base
1 parent 26f6f18 commit 98a1fa7

File tree

6 files changed

+44
-41
lines changed

6 files changed

+44
-41
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ All inputs are **optional**. If not set, sensible defaults will be used.
7272
| `reviewers` | A comma or newline-separated list of reviewers (GitHub usernames) to request a review from. | |
7373
| `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), or equivalent [GitHub App permissions](docs/concepts-guidelines.md#authenticating-with-github-app-generated-tokens), are required. | |
7474
| `milestone` | The number of the milestone to associate this pull request with. | |
75-
| `draft` | Create a [draft pull request](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests#draft-pull-requests). It is not possible to change draft status after creation except through the web interface. | `false` |
75+
| `draft` | Create a [draft pull request](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests#draft-pull-requests). Valid values are `true` (only on create), `always-true` (on create and update), and `false`. | `false` |
7676
| `maintainer-can-modify` | Indicates whether [maintainers can modify](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork) the pull request. | `true` |
7777

7878
#### token

action.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ inputs:
7575
milestone:
7676
description: 'The number of the milestone to associate the pull request with.'
7777
draft:
78-
description: 'Create a draft pull request. It is not possible to change draft status after creation except through the web interface'
78+
description: >
79+
Create a draft pull request.
80+
Valid values are `true` (only on create), `always-true` (on create and update), and `false`.
7981
default: false
8082
maintainer-can-modify:
8183
description: 'Indicates whether maintainers can modify the pull request.'

dist/index.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,11 @@ function createPullRequest(inputs) {
495495
}
496496
else if (result.action == 'updated') {
497497
outputs.set('pull-request-operation', 'updated');
498+
// The pull request was updated AND the branch was updated.
499+
// Convert back to draft if 'draft: always-true' is set.
500+
if (inputs.draft.always && pull.draft !== undefined && !pull.draft) {
501+
yield ghPull.convertToDraft(pull.node_id);
502+
}
498503
}
499504
core.endGroup();
500505
}
@@ -1306,28 +1311,9 @@ class GitHubHelper {
13061311
throw e;
13071312
}
13081313
}
1309-
// Convert back to draft if 'draft: always-true' is set
1310-
if (inputs.draft.always && pull.draft !== undefined && !pull.draft) {
1311-
yield this.convertToDraft(pull.node_id);
1312-
}
13131314
return pull;
13141315
});
13151316
}
1316-
convertToDraft(id) {
1317-
return __awaiter(this, void 0, void 0, function* () {
1318-
core.info(`Converting pull request to draft`);
1319-
yield this.octokit.graphql({
1320-
query: `mutation($pullRequestId: ID!) {
1321-
convertPullRequestToDraft(input: {pullRequestId: $pullRequestId}) {
1322-
pullRequest {
1323-
isDraft
1324-
}
1325-
}
1326-
}`,
1327-
pullRequestId: id
1328-
});
1329-
});
1330-
}
13311317
pushSignedCommits(branchCommits, baseSha, repoPath, branchRepository, branch) {
13321318
return __awaiter(this, void 0, void 0, function* () {
13331319
let headCommit = {
@@ -1401,6 +1387,21 @@ class GitHubHelper {
14011387
}
14021388
});
14031389
}
1390+
convertToDraft(id) {
1391+
return __awaiter(this, void 0, void 0, function* () {
1392+
core.info(`Converting pull request to draft`);
1393+
yield this.octokit.graphql({
1394+
query: `mutation($pullRequestId: ID!) {
1395+
convertPullRequestToDraft(input: {pullRequestId: $pullRequestId}) {
1396+
pullRequest {
1397+
isDraft
1398+
}
1399+
}
1400+
}`,
1401+
pullRequestId: id
1402+
});
1403+
});
1404+
}
14041405
}
14051406
exports.GitHubHelper = GitHubHelper;
14061407

docs/concepts-guidelines.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ There are a number of workarounds with different pros and cons.
150150

151151
- Use the default `GITHUB_TOKEN` and allow the action to create pull requests that have no checks enabled. Manually close pull requests and immediately reopen them. This will enable `on: pull_request` workflows to run and be added as checks. To prevent merging of pull requests without checks erroneously, use [branch protection rules](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/defining-the-mergeability-of-pull-requests).
152152

153-
- Create draft pull requests by setting the `draft: true` input, and configure your workflow to trigger `on: ready_for_review`. The workflow will run when users manually click the "Ready for review" button on the draft pull requests.
153+
- Create draft pull requests by setting the `draft: always-true` input, and configure your workflow to trigger `on: ready_for_review`. The workflow will run when users manually click the "Ready for review" button on the draft pull requests. If the pull request is updated by the action, the `always-true` mode ensures that the pull request will be converted back to a draft.
154154

155155
- Use a [Personal Access Token (PAT)](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) created on an account that has write access to the repository that pull requests are being created in. This is the standard workaround and [recommended by GitHub](https://docs.github.com/en/actions/using-workflows/triggering-a-workflow#triggering-a-workflow-from-a-workflow). It's advisable to use a dedicated [machine account](https://docs.github.com/en/github/site-policy/github-terms-of-service#3-account-requirements) that has collaborator access to the repository, rather than creating a PAT on a personal user account. Also note that because the account that owns the PAT will be the creator of pull requests, that user account will be unable to perform actions such as request changes or approve the pull request.
156156

src/create-pull-request.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
249249
outputs.set('pull-request-operation', 'created')
250250
} else if (result.action == 'updated') {
251251
outputs.set('pull-request-operation', 'updated')
252+
// The pull request was updated AND the branch was updated.
253+
// Convert back to draft if 'draft: always-true' is set.
254+
if (inputs.draft.always && pull.draft !== undefined && !pull.draft) {
255+
await ghPull.convertToDraft(pull.node_id)
256+
}
252257
}
253258
core.endGroup()
254259
} else {

src/github-helper.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -215,28 +215,9 @@ export class GitHubHelper {
215215
}
216216
}
217217

218-
// Convert back to draft if 'draft: always-true' is set
219-
if (inputs.draft.always && pull.draft !== undefined && !pull.draft) {
220-
await this.convertToDraft(pull.node_id)
221-
}
222-
223218
return pull
224219
}
225220

226-
private async convertToDraft(id: string): Promise<void> {
227-
core.info(`Converting pull request to draft`)
228-
await this.octokit.graphql({
229-
query: `mutation($pullRequestId: ID!) {
230-
convertPullRequestToDraft(input: {pullRequestId: $pullRequestId}) {
231-
pullRequest {
232-
isDraft
233-
}
234-
}
235-
}`,
236-
pullRequestId: id
237-
})
238-
}
239-
240221
async pushSignedCommits(
241222
branchCommits: Commit[],
242223
baseSha: string,
@@ -368,4 +349,18 @@ export class GitHubHelper {
368349
})
369350
}
370351
}
352+
353+
async convertToDraft(id: string): Promise<void> {
354+
core.info(`Converting pull request to draft`)
355+
await this.octokit.graphql({
356+
query: `mutation($pullRequestId: ID!) {
357+
convertPullRequestToDraft(input: {pullRequestId: $pullRequestId}) {
358+
pullRequest {
359+
isDraft
360+
}
361+
}
362+
}`,
363+
pullRequestId: id
364+
})
365+
}
371366
}

0 commit comments

Comments
 (0)