Repository: release-drafter/release-drafter
Pull request #350 (2019-12-24 12:04:22)

Allow specifying the config name through an input option
This is useful if you want to create different drafts, depending on the triggered workflow.
You may want to create a pre-release draft in the development branch (targeting this branch) and have a `-alpha` suffix in the tag-template and, separately, create a release draft in the master branch (targeting it), without the suffix in the tag-template and maybe other different options.
So, in the workflow we can pass a different config name as an env variable when using release-drafter.

pdcmoreira: Does it make more sense to load the name from `with` arguments instead of an `env` variable?
TimonVS: Hey, thanks a lot for contributing! It would indeed make more sense to pass this as a `with` argument, you can use [`core.getInput`](https://github.com/actions/toolkit/tree/master/packages/core#inputsoutputs) for this :)

Could you add a test that demonstrates that it does indeed load from another location if configured? You can simulate Action inputs with environment variables (see https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67). And could you add documentation to the README?
TimonVS: Everything looks fine. I'll merge this once I have a release window :)
casz: I'd prefer to merge #193 before this one 😅 

Patch:
diff --git a/README.md b/README.md
index 9064e9c..4e4cd6d 100644
--- a/README.md
+++ b/README.md
@@ -25,6 +25,9 @@ jobs:
     steps:
       # Drafts your next Release notes as Pull Requests are merged into "master"
       - uses: release-drafter/release-drafter@v5
+        with:
+          # (Optional) specify config name to use, relative to .github/. Default: release-drafter.yml
+          # config-name: my-config.yml
         env:
           GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
 ```
diff --git a/index.js b/index.js
index d8713e7..19dc78e 100644
--- a/index.js
+++ b/index.js
@@ -11,7 +11,8 @@ module.exports = app => {
     const config = await getConfig({
       app,
       context,
-      getConfig: require('probot-config')
+      getConfig: require('probot-config'),
+      configName: core.getInput('config-name')
     })
 
     if (config === null) return
diff --git a/lib/config.js b/lib/config.js
index e1bc539..c94b34c 100644
--- a/lib/config.js
+++ b/lib/config.js
@@ -2,15 +2,20 @@ const { validateSchema } = require('./schema')
 const { DEFAULT_CONFIG } = require('./default-config')
 const log = require('./log')
 
-const CONFIG_NAME = 'release-drafter.yml'
+const DEFAULT_CONFIG_NAME = 'release-drafter.yml'
 
 module.exports.getConfig = async function getConfig({
   app,
   context,
+  configName,
   getConfig: fetchConfig
 }) {
   try {
-    const repoConfig = await fetchConfig(context, CONFIG_NAME, DEFAULT_CONFIG)
+    const repoConfig = await fetchConfig(
+      context,
+      configName || DEFAULT_CONFIG_NAME,
+      DEFAULT_CONFIG
+    )
 
     const config = validateSchema(app, context, repoConfig)
 


Test patch:
diff --git a/test/fixtures/config/config-name-input.yml b/test/fixtures/config/config-name-input.yml
new file mode 100644
index 0000000..763ba07
--- /dev/null
+++ b/test/fixtures/config/config-name-input.yml
@@ -0,0 +1,2 @@
+template: |
+  # There's new stuff!
diff --git a/test/helpers/config-mock.js b/test/helpers/config-mock.js
index 1c60585..5d32d79 100644
--- a/test/helpers/config-mock.js
+++ b/test/helpers/config-mock.js
@@ -2,40 +2,39 @@ const fs = require('fs')
 const nock = require('nock')
 const { encodeContent } = require('../../lib/base64')
 
-function configFixture(fileName = 'config.yml') {
+function configFixture(
+  fileName = 'config.yml',
+  repoFileName = 'release-drafter.yml'
+) {
   return {
     type: 'file',
     encoding: 'base64',
     size: 5362,
-    name: 'release-drafter.yml',
-    path: '.github/release-drafter.yml',
+    name: repoFileName,
+    path: `.github/${repoFileName}`,
     content: encodeContent(
       fs.readFileSync(`${__dirname}/../fixtures/config/${fileName}`)
     ),
     sha: '3d21ec53a331a6f037a91c368710b99387d012c1',
-    url:
-      'https://api.github.com/repos/octokit/octokit.rb/contents/.github/release-drafter.yml',
-    git_url:
-      'https://api.github.com/repos/octokit/octokit.rb/git/blobs/3d21ec53a331a6f037a91c368710b99387d012c1',
-    html_url:
-      'https://github.com/octokit/octokit.rb/blob/master/.github/release-drafter.yml',
-    download_url:
-      'https://raw.githubusercontent.com/octokit/octokit.rb/master/.github/release-drafter.yml',
+    url: `https://api.github.com/repos/octokit/octokit.rb/contents/.github/${repoFileName}`,
+    git_url: `https://api.github.com/repos/octokit/octokit.rb/git/blobs/3d21ec53a331a6f037a91c368710b99387d012c1`,
+    html_url: `https://github.com/octokit/octokit.rb/blob/master/.github/${repoFileName}`,
+    download_url: `https://raw.githubusercontent.com/octokit/octokit.rb/master/.github/${repoFileName}`,
     _links: {
-      git:
-        'https://api.github.com/repos/octokit/octokit.rb/git/blobs/3d21ec53a331a6f037a91c368710b99387d012c1',
-      self:
-        'https://api.github.com/repos/octokit/octokit.rb/contents/.github/release-drafter.yml',
-      html:
-        'https://github.com/octokit/octokit.rb/blob/master/.github/release-drafter.yml'
+      git: `https://api.github.com/repos/octokit/octokit.rb/git/blobs/3d21ec53a331a6f037a91c368710b99387d012c1`,
+      self: `https://api.github.com/repos/octokit/octokit.rb/contents/.github/${repoFileName}`,
+      html: `https://github.com/octokit/octokit.rb/blob/master/.github/${repoFileName}`
     }
   }
 }
 
-module.exports = function getConfigMock(fileName) {
+module.exports = function getConfigMock(
+  fileName,
+  repoFileName = 'release-drafter.yml'
+) {
   return nock('https://api.github.com')
     .get(
-      '/repos/toolmantim/release-drafter-test-project/contents/.github/release-drafter.yml'
+      `/repos/toolmantim/release-drafter-test-project/contents/.github/${repoFileName}`
     )
-    .reply(200, configFixture(fileName))
+    .reply(200, configFixture(fileName, repoFileName))
 }
diff --git a/test/index.test.js b/test/index.test.js
index fc633d5..a4761e4 100644
--- a/test/index.test.js
+++ b/test/index.test.js
@@ -3,6 +3,8 @@ const route = require('nock-knock/lib').default
 const { Probot, Octokit } = require('probot')
 const getConfigMock = require('./helpers/config-mock')
 const releaseDrafter = require('../index')
+const fs = require('fs')
+const { encodeContent } = require('../lib/base64')
 
 nock.disableNetConnect()
 
@@ -1273,4 +1275,56 @@ Previous tag: ''
       )
     })
   })
+
+  describe('with config-name input', () => {
+    it('loads from another config path', async () => {
+      /*
+        Mock
+        with:
+          config-name: 'config-name-input.yml'
+      */
+      process.env['INPUT_CONFIG-NAME'] = 'config-name-input.yml'
+
+      // Mock config request for file 'config-name-input.yml'
+      const getConfigScope = getConfigMock(
+        'config-name-input.yml',
+        'config-name-input.yml'
+      )
+
+      nock('https://api.github.com')
+        .post('/graphql', body =>
+          body.query.includes('query findCommitsWithAssociatedPullRequests')
+        )
+        .reply(200, require('./fixtures/graphql-commits-no-prs.json'))
+
+      nock('https://api.github.com')
+        .get('/repos/toolmantim/release-drafter-test-project/releases')
+        .query(true)
+        .reply(200, [require('./fixtures/release')])
+        .post(
+          '/repos/toolmantim/release-drafter-test-project/releases',
+          body => {
+            // Assert that the correct body was used
+            expect(body).toMatchObject({
+              name: '',
+              tag_name: '',
+              body: `# There's new stuff!\n`,
+              draft: true
+            })
+            return true
+          }
+        )
+        .reply(200)
+
+      await probot.receive({
+        name: 'push',
+        payload: require('./fixtures/push')
+      })
+
+      // Assert that the GET request was called for the correct config file
+      expect(getConfigScope.isDone()).toBe(true)
+
+      expect.assertions(2)
+    })
+  })
 })