-
-
Notifications
You must be signed in to change notification settings - Fork 845
Reopen Issues Without Linked PR #8438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: gh-pages
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| name: Check Closed Issue for Linked PR | ||
|
|
||
| on: | ||
| issues: | ||
| types: [closed] | ||
|
|
||
| jobs: | ||
| check-for-linked-issue: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Check Out Repository | ||
| uses: actions/checkout@v6 | ||
|
|
||
| - name: Check Issue Labels And Linked PRs | ||
| uses: actions/github-script@v8 | ||
| id: check-issue-labels-and-linked-prs | ||
| with: | ||
| script: | | ||
| const script = require( | ||
| './github-actions' | ||
| + '/check-closed-issue-for-linked-pr' | ||
| + '/check-for-linked-issue' | ||
| + '/check-issue-labels-and-linked-prs.js' | ||
| ); | ||
| const isValidClose = await script({github, context}); | ||
| core.setOutput('isValidClose', isValidClose); | ||
|
|
||
| # Sleep to allow other GitHub Actions to change project status. | ||
| - name: Sleep | ||
| id: sleep | ||
| shell: bash | ||
| run: sleep 30s | ||
|
|
||
| - name: Reopen Issue | ||
| if: steps.check-issue-labels-and-linked-prs.outputs.isValidClose == 'false' | ||
| uses: actions/github-script@v8 | ||
| id: reopen-issue | ||
| with: | ||
| github-token: ${{ secrets.HACKFORLA_GRAPHQL_TOKEN }} | ||
| script: | | ||
| const script = require( | ||
| './github-actions' | ||
| + '/check-closed-issue-for-linked-pr' | ||
| + '/check-for-linked-issue' | ||
| + '/reopen-issue.js' | ||
| ); | ||
| await script({github, context}); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| const retrieveLabelDirectory = require('../../utils/retrieve-label-directory'); | ||
|
|
||
| // Use labelKeys to retrieve current labelNames from directory | ||
| const [ | ||
| nonPrContribution | ||
| ] = [ | ||
| 'NEW-nonPrContribution' | ||
| ].map(retrieveLabelDirectory); | ||
|
|
||
| // ================================================== | ||
|
|
||
| /** | ||
| * Checks whether a closed issue has a linked PR or one of the labels to excuse | ||
| * this GitHub Actions workflow. | ||
| * | ||
| * @param {{github: object, context: object}} actionsGithubScriptArgs - GitHub | ||
| * objects from actions/github-script | ||
| * @returns {boolean} False if the issue does not have a linked PR, a "non-PR | ||
| * contribution" label, or an "Ignore..." label. | ||
| */ | ||
| async function hasLinkedPrOrExcusableLabel({ github, context }) { | ||
| const repoOwner = context.repo.owner; | ||
| const repoName = context.repo.repo; | ||
| const issueNumber = context.payload.issue.number; | ||
|
|
||
| const labels = context.payload.issue.labels.map((label) => label.name); | ||
|
|
||
| const consoleMessageAllowClose = | ||
| `Issue #${issueNumber} is allowed to be closed.`; | ||
|
|
||
| // -------------------------------------------------- | ||
|
|
||
| // Check if the issue has the labels that will avoid re-opening it. | ||
| if ( | ||
| labels.some( | ||
| (label) => | ||
| label === nonPrContribution || label.toLowerCase().includes('ignore') | ||
| ) | ||
| ) { | ||
| console.info(consoleMessageAllowClose); | ||
| return true; | ||
| } | ||
|
|
||
| console.info( | ||
| `Issue #${issueNumber} does not have ` + | ||
| `the necessary labels to excuse reopening it.` | ||
| ); | ||
|
|
||
| // Use GitHub's GraphQL's closedByPullRequestsReferences to more reliably | ||
| // determine if there is a linked PR. | ||
| const query = `query($owner: String!, $repo: String!, $issue: Int!) { | ||
| repository(owner: $owner, name: $repo) { | ||
| issue(number: $issue) { | ||
| closedByPullRequestsReferences(includeClosedPrs: true, first: 1) { | ||
| totalCount | ||
| } | ||
| } | ||
| } | ||
| }`; | ||
|
|
||
| const variables = { | ||
| owner: repoOwner, | ||
| repo: repoName, | ||
| issue: issueNumber, | ||
| }; | ||
|
|
||
| // Determine if there is a linked PR. | ||
| try { | ||
| const response = await github.graphql(query, variables); | ||
|
|
||
| const numLinkedPrs = | ||
| response.repository.issue.closedByPullRequestsReferences.totalCount; | ||
|
|
||
| console.debug(`Number of linked PRs found: ${numLinkedPrs}.`); | ||
|
|
||
| if (numLinkedPrs > 0) { | ||
| console.info(consoleMessageAllowClose); | ||
| return true; | ||
| } | ||
| } catch (err) { | ||
| throw new Error( | ||
| `Can not find issue #${issueNumber} or its PR count; error = ${err}` | ||
| ); | ||
| } | ||
| console.info(`Issue #${issueNumber} does not have a linked PR.`); | ||
|
|
||
| // If the issue does not have a linked PR or any of the excusable labels. | ||
| console.info(`Issue #${issueNumber} is not allowed to be closed.`); | ||
| return false; | ||
| } | ||
|
|
||
| // ================================================== | ||
|
|
||
| module.exports = hasLinkedPrOrExcusableLabel; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| 'use strict'; | ||
|
|
||
| const hasLinkedPrOrExcusableLabel = require('./check-issue-labels-and-linked-prs'); | ||
|
|
||
| // ================================================== | ||
|
|
||
| // Create the github and context mocks. Freezing Objects to prevent accidental | ||
| // changes. | ||
| const github = Object.freeze({ graphql: jest.fn() }); | ||
| const context = deepFreeze({ | ||
| repo: { | ||
| owner: 'owner1', | ||
| repo: 'repo1', | ||
| }, | ||
| payload: { | ||
| issue: { | ||
| number: 1, | ||
| labels: [], | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| describe('hasLinkedPrOrExcusableLabel', () => { | ||
| let contextCopy; | ||
|
|
||
| beforeEach(() => { | ||
| contextCopy = structuredClone(context); | ||
| jest.resetAllMocks(); | ||
| }); | ||
|
|
||
| test.each([ | ||
| [[{ name: 'non-PR contribution' }]], | ||
| [ | ||
| [ | ||
| { name: 'non-PR contribution' }, | ||
| { name: 'good first issue' }, | ||
| { name: 'size: 1pt' }, | ||
|
Comment on lines
+36
to
+37
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Testing for when issues have other labels as well. |
||
| ], | ||
| ], | ||
| ])( | ||
| 'If the issue has the "non-PR contribution" label, then return true. ' + | ||
| 'Labels: %j.', | ||
| async (labelsList) => { | ||
| // Arrange | ||
| contextCopy.payload.issue.labels = labelsList; | ||
|
|
||
| // Act | ||
| const result = await hasLinkedPrOrExcusableLabel({ | ||
| github, | ||
| context: contextCopy, | ||
| }); | ||
|
|
||
| // Assert | ||
| expect(result).toBe(true); | ||
| expect(github.graphql).not.toHaveBeenCalled(); | ||
| } | ||
| ); | ||
|
|
||
| test.each([ | ||
| [[{ name: 'Ignore: Test' }]], | ||
| [ | ||
| [ | ||
| { name: 'Ignore: Test' }, | ||
| { name: 'good first issue' }, | ||
| { name: 'size: 1pt' }, | ||
|
Comment on lines
+64
to
+65
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Testing for when issues have other labels as well. |
||
| ], | ||
| ], | ||
| ])( | ||
| 'If the issue has a label that includes "Ignore", then return true. ' + | ||
| 'Labels: %j', | ||
| async (labelsList) => { | ||
| // Arrange | ||
| contextCopy.payload.issue.labels = labelsList; | ||
|
|
||
| // Act | ||
| const result = await hasLinkedPrOrExcusableLabel({ | ||
| github, | ||
| context: contextCopy, | ||
| }); | ||
|
|
||
| // Assert | ||
| expect(result).toBe(true); | ||
| expect(github.graphql).not.toHaveBeenCalled(); | ||
| } | ||
| ); | ||
|
|
||
| test('If the issue has a linked PR, then return true.', async () => { | ||
| // Arrange | ||
| github.graphql.mockResolvedValue({ | ||
| repository: { | ||
| issue: { | ||
| closedByPullRequestsReferences: { | ||
| totalCount: 1, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| // Act | ||
| const result = await hasLinkedPrOrExcusableLabel({ | ||
| github, | ||
| context: contextCopy, | ||
| }); | ||
|
|
||
| // Assert | ||
| expect(result).toBe(true); | ||
| expect(github.graphql).toHaveBeenCalledWith( | ||
| expect.stringContaining('query'), | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't really care how the query is constructed, as long as it returns the number of linked PRs. |
||
| { | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue: context.payload.issue.number, | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| test( | ||
| 'If there is no linked PR nor any of the excusable labels, ' + | ||
| 'then return false.', | ||
| async () => { | ||
| // Arrange | ||
| github.graphql.mockResolvedValue({ | ||
| repository: { | ||
| issue: { | ||
| closedByPullRequestsReferences: { | ||
| totalCount: 0, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| // Act | ||
| const result = await hasLinkedPrOrExcusableLabel({ | ||
| github, | ||
| context: contextCopy, | ||
| }); | ||
|
|
||
| // Assert | ||
| expect(result).toBe(false); | ||
| expect(github.graphql).toHaveBeenCalledWith( | ||
| expect.stringContaining('query'), | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't really care how the query is constructed, as long as it returns the number of linked PRs. |
||
| { | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue: context.payload.issue.number, | ||
| } | ||
| ); | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| // ================================================== | ||
|
|
||
| /** | ||
| * Helper function taken from MDN. Freezes nested Objects. | ||
| * | ||
| * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze#deep_freezing | ||
| * | ||
| * @param {*} object - Any JavaScript Object. | ||
| * @returns Passed-in Object. | ||
| */ | ||
| function deepFreeze(object) { | ||
| // Retrieve the property names defined on object | ||
| const propNames = Reflect.ownKeys(object); | ||
|
|
||
| // Freeze properties before freezing self | ||
| for (const name of propNames) { | ||
| const value = object[name]; | ||
|
|
||
| if ((value && typeof value === 'object') || typeof value === 'function') { | ||
| deepFreeze(value); | ||
| } | ||
| } | ||
|
|
||
| return Object.freeze(object); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not 100% sure that this is the correct token to use. This workflow does need project permission though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are correct- this is the token to use