|
| 1 | +name: pull requests |
| 2 | + |
| 3 | +# Credit: https://github.com/github/docs/blob/main/.github/workflows/notify-when-maintainers-cannot-edit.yaml |
| 4 | + |
| 5 | +on: |
| 6 | + workflow_call: |
| 7 | + |
| 8 | +jobs: |
| 9 | + draft: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + |
| 12 | + steps: |
| 13 | + - uses: actions/github-script@v7 |
| 14 | + with: |
| 15 | + script: | |
| 16 | + const repo = context.repo.repo; |
| 17 | + |
| 18 | + const query = ` |
| 19 | + query($number: Int!) { |
| 20 | + repository(owner: "laravel", name: "${repo}") { |
| 21 | + pullRequest(number: $number) { |
| 22 | + headRepositoryOwner { |
| 23 | + login |
| 24 | + } |
| 25 | + isDraft |
| 26 | + state |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + `; |
| 31 | +
|
| 32 | + const pullNumber = context.issue.number; |
| 33 | + const variables = { number: pullNumber }; |
| 34 | +
|
| 35 | + try { |
| 36 | + console.log(`Determine if pull request is submitted as draft ...`); |
| 37 | +
|
| 38 | + const result = await github.graphql(query, variables); |
| 39 | +
|
| 40 | + console.log(JSON.stringify(result, null, 2)); |
| 41 | +
|
| 42 | + const pullRequest = result.repository.pullRequest; |
| 43 | +
|
| 44 | + if (pullRequest.headRepositoryOwner.login === 'laravel') { |
| 45 | + console.log('PR owned by laravel'); |
| 46 | +
|
| 47 | + return; |
| 48 | + } |
| 49 | +
|
| 50 | + if (pullRequest.state !== 'OPEN') { |
| 51 | + console.log('PR has already been closed or merged.'); |
| 52 | +
|
| 53 | + return; |
| 54 | + } |
| 55 | +
|
| 56 | + if (pullRequest.isDraft) { |
| 57 | + console.log('PR not owned by Laravel and is submitted as a draft.'); |
| 58 | +
|
| 59 | + await github.rest.issues.createComment({ |
| 60 | + issue_number: pullNumber, |
| 61 | + owner: 'laravel', |
| 62 | + repo: context.repo.repo, |
| 63 | + body: "Thanks for submitting a PR!\n\nNote that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.\n\nPull requests that are abandoned in draft may be closed due to inactivity." |
| 64 | + }); |
| 65 | + } |
| 66 | + } catch(e) { |
| 67 | + console.log(e); |
| 68 | + } |
| 69 | +
|
| 70 | + uneditable: |
| 71 | + runs-on: ubuntu-latest |
| 72 | + |
| 73 | + steps: |
| 74 | + - uses: actions/github-script@v7 |
| 75 | + with: |
| 76 | + script: | |
| 77 | + const repo = context.repo.repo; |
| 78 | + |
| 79 | + const query = ` |
| 80 | + query($number: Int!) { |
| 81 | + repository(owner: "laravel", name: "${repo}") { |
| 82 | + pullRequest(number: $number) { |
| 83 | + headRepositoryOwner { |
| 84 | + login |
| 85 | + } |
| 86 | + maintainerCanModify |
| 87 | + state |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + `; |
| 92 | +
|
| 93 | + const pullNumber = context.issue.number; |
| 94 | + const variables = { number: pullNumber }; |
| 95 | +
|
| 96 | + try { |
| 97 | + console.log(`Check for maintainer edit access ...`); |
| 98 | +
|
| 99 | + const result = await github.graphql(query, variables); |
| 100 | +
|
| 101 | + console.log(JSON.stringify(result, null, 2)); |
| 102 | +
|
| 103 | + const pullRequest = result.repository.pullRequest; |
| 104 | +
|
| 105 | + if (pullRequest.headRepositoryOwner.login === 'laravel') { |
| 106 | + console.log('PR owned by laravel'); |
| 107 | +
|
| 108 | + return; |
| 109 | + } |
| 110 | +
|
| 111 | + if (pullRequest.state !== 'OPEN') { |
| 112 | + console.log('PR has already been closed or merged'); |
| 113 | +
|
| 114 | + return; |
| 115 | + } |
| 116 | +
|
| 117 | + if (! pullRequest.maintainerCanModify) { |
| 118 | + console.log('PR not owned by Laravel and does not have maintainer edits enabled'); |
| 119 | +
|
| 120 | + await github.rest.issues.createComment({ |
| 121 | + issue_number: pullNumber, |
| 122 | + owner: 'laravel', |
| 123 | + repo: context.repo.repo, |
| 124 | + body: "Thanks for submitting a PR!\n\nIn order to review and merge PRs most efficiently, we require that all PRs grant maintainer edit access before we review them. For information on how to do this, [see the relevant GitHub documentation](https://docs.github.com/en/github/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork). Additionally, GitHub doesn't allow maintainer permissions from organization accounts. Please resubmit this PR from a personal GitHub account with maintainer permissions enabled." |
| 125 | + }); |
| 126 | +
|
| 127 | + await github.rest.pulls.update({ |
| 128 | + pull_number: pullNumber, |
| 129 | + owner: 'laravel', |
| 130 | + repo: context.repo.repo, |
| 131 | + state: 'closed' |
| 132 | + }); |
| 133 | + } |
| 134 | + } catch(e) { |
| 135 | + console.log(e); |
| 136 | + } |
0 commit comments