Repository: release-drafter/release-drafter
Pull request #1362 (2023-10-03 15:51:14)

Add config option for PR query limit
Adds a configuration option to resolve the issue described in https://github.com/release-drafter/release-drafter/issues/1354, where long-lived PRs could cause others to be omitted from the release summary. This option allows users to increase the number of PRs the action searches when looking for a merged PR containing a given commit. The option is not required and defaults to 5 (the currently used value).

An alternate approach would be to simply increase the hard-coded limit in the existing query, although this could theoretically have an impact on API load for users not affected by the original issue.


Patch:
diff --git a/bin/generate-fixtures.js b/bin/generate-fixtures.js
index 7343823..a2c4c14 100755
--- a/bin/generate-fixtures.js
+++ b/bin/generate-fixtures.js
@@ -86,6 +86,7 @@ for (const repo of repos) {
         withPullRequestURL: true,
         withBaseRefName: true,
         withHeadRefName: true,
+        pullRequestLimit: 5,
       },
     })
   )
diff --git a/dist/index.js b/dist/index.js
index bd08d2e..ade7a07 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -193753,6 +193753,7 @@ const findCommitsWithAssociatedPullRequestsQuery = /* GraphQL */ `
     $after: String
     $withBaseRefName: Boolean!
     $withHeadRefName: Boolean!
+    $pullRequestLimit: Int!
   ) {
     repository(name: $name, owner: $owner) {
       object(expression: $targetCommitish) {
@@ -193773,7 +193774,7 @@ const findCommitsWithAssociatedPullRequestsQuery = /* GraphQL */ `
                   login
                 }
               }
-              associatedPullRequests(first: 5) {
+              associatedPullRequests(first: $pullRequestLimit) {
                 nodes {
                   title
                   number
@@ -193822,6 +193823,7 @@ const findCommitsWithAssociatedPullRequests = async ({
     withPullRequestURL: config['change-template'].includes('$URL'),
     withBaseRefName: config['change-template'].includes('$BASE_REF_NAME'),
     withHeadRefName: config['change-template'].includes('$HEAD_REF_NAME'),
+    pullRequestLimit: config['pull-request-limit'],
   }
   const includePaths = config['include-paths']
   const dataPath = ['repository', 'object', 'history']
@@ -194016,6 +194018,7 @@ const DEFAULT_CONFIG = Object.freeze({
   latest: 'true',
   'filter-by-commitish': false,
   commitish: '',
+  'pull-request-limit': 5,
   'category-template': `## $TITLE`,
   header: '',
   footer: '',
@@ -194738,6 +194741,11 @@ const schema = (context) => {
 
       commitish: Joi.string().allow('').default(DEFAULT_CONFIG['commitish']),
 
+      'pull-request-limit': Joi.number()
+        .positive()
+        .integer()
+        .default(DEFAULT_CONFIG['pull-request-limit']),
+
       replacers: Joi.array()
         .items(
           Joi.object().keys({
diff --git a/lib/commits.js b/lib/commits.js
index d752590..4df5fe0 100644
--- a/lib/commits.js
+++ b/lib/commits.js
@@ -40,6 +40,7 @@ const findCommitsWithAssociatedPullRequestsQuery = /* GraphQL */ `
     $after: String
     $withBaseRefName: Boolean!
     $withHeadRefName: Boolean!
+    $pullRequestLimit: Int!
   ) {
     repository(name: $name, owner: $owner) {
       object(expression: $targetCommitish) {
@@ -60,7 +61,7 @@ const findCommitsWithAssociatedPullRequestsQuery = /* GraphQL */ `
                   login
                 }
               }
-              associatedPullRequests(first: 5) {
+              associatedPullRequests(first: $pullRequestLimit) {
                 nodes {
                   title
                   number
@@ -109,6 +110,7 @@ const findCommitsWithAssociatedPullRequests = async ({
     withPullRequestURL: config['change-template'].includes('$URL'),
     withBaseRefName: config['change-template'].includes('$BASE_REF_NAME'),
     withHeadRefName: config['change-template'].includes('$HEAD_REF_NAME'),
+    pullRequestLimit: config['pull-request-limit'],
   }
   const includePaths = config['include-paths']
   const dataPath = ['repository', 'object', 'history']
diff --git a/lib/default-config.js b/lib/default-config.js
index 22af5fc..ff0e751 100644
--- a/lib/default-config.js
+++ b/lib/default-config.js
@@ -30,6 +30,7 @@ const DEFAULT_CONFIG = Object.freeze({
   latest: 'true',
   'filter-by-commitish': false,
   commitish: '',
+  'pull-request-limit': 5,
   'category-template': `## $TITLE`,
   header: '',
   footer: '',
diff --git a/lib/schema.js b/lib/schema.js
index 7f0d42c..7773abf 100644
--- a/lib/schema.js
+++ b/lib/schema.js
@@ -95,6 +95,11 @@ const schema = (context) => {
 
       commitish: Joi.string().allow('').default(DEFAULT_CONFIG['commitish']),
 
+      'pull-request-limit': Joi.number()
+        .positive()
+        .integer()
+        .default(DEFAULT_CONFIG['pull-request-limit']),
+
       replacers: Joi.array()
         .items(
           Joi.object().keys({


Test patch:
diff --git a/test/fixtures/config/config-with-pull-request-limit.yml b/test/fixtures/config/config-with-pull-request-limit.yml
new file mode 100644
index 0000000..5d6bebd
--- /dev/null
+++ b/test/fixtures/config/config-with-pull-request-limit.yml
@@ -0,0 +1,2 @@
+template: '$CHANGES'
+pull-request-limit: 34
diff --git a/test/index.test.js b/test/index.test.js
index 443b952..c4cd55e 100644
--- a/test/index.test.js
+++ b/test/index.test.js
@@ -2424,6 +2424,78 @@ describe('release-drafter', () => {
     })
   })
 
+  describe('with pull-request-limit config', () => {
+    it('uses the correct default when not specified', async () => {
+      getConfigMock()
+
+      nock('https://api.github.com')
+        .post('/graphql', (body) => {
+          if (
+            body.query.includes('query findCommitsWithAssociatedPullRequests')
+          ) {
+            expect(body.variables.pullRequestLimit).toBe(5)
+            return true
+          }
+          return false
+        })
+        .reply(200, graphqlCommitsNoPRsPayload)
+
+      nock('https://api.github.com')
+        .get(
+          '/repos/toolmantim/release-drafter-test-project/releases?per_page=100'
+        )
+        .reply(200, [])
+
+      nock('https://api.github.com')
+        .post('/repos/toolmantim/release-drafter-test-project/releases')
+        .reply(200, releasePayload)
+
+      const payload = pushPayload
+
+      await probot.receive({
+        name: 'push',
+        payload,
+      })
+
+      expect.assertions(1)
+    })
+
+    it('requests the specified number of associated PRs', async () => {
+      getConfigMock('config-with-pull-request-limit.yml')
+
+      nock('https://api.github.com')
+        .post('/graphql', (body) => {
+          if (
+            body.query.includes('query findCommitsWithAssociatedPullRequests')
+          ) {
+            expect(body.variables.pullRequestLimit).toBe(34)
+            return true
+          }
+          return false
+        })
+        .reply(200, graphqlCommitsNoPRsPayload)
+
+      nock('https://api.github.com')
+        .get(
+          '/repos/toolmantim/release-drafter-test-project/releases?per_page=100'
+        )
+        .reply(200, [])
+
+      nock('https://api.github.com')
+        .post('/repos/toolmantim/release-drafter-test-project/releases')
+        .reply(200, releasePayload)
+
+      const payload = pushPayload
+
+      await probot.receive({
+        name: 'push',
+        payload,
+      })
+
+      expect.assertions(1)
+    })
+  })
+
   describe('config error handling', () => {
     it('schema error', async () => {
       getConfigMock('config-with-schema-error.yml')
diff --git a/test/schema.test.js b/test/schema.test.js
index 9599a2b..25cfec9 100644
--- a/test/schema.test.js
+++ b/test/schema.test.js
@@ -20,6 +20,7 @@ const validConfigs = [
   [{ template, header: 'I am on top' }],
   [{ template, footer: 'I am on bottm' }],
   [{ template, header: 'I am on top', footer: 'I am on bottm' }],
+  [{ template, 'pull-request-limit': 49 }],
 ]
 
 const invalidConfigs = [
@@ -53,6 +54,7 @@ const invalidConfigs = [
   ],
   [{ replacers: [{ search: '123', replace: 123 }] }, 'must be a string'],
   [{ commitish: false }, 'must be a string'],
+  [{ 'pull-request-limit': 'forty nine' }, 'must be a number'],
 ]
 
 describe('schema', () => {