Repository: release-drafter/release-drafter
Pull request #424 (2020-02-13 17:39:08)

Allow creating published releases
This PR introduces an additional (optional) action input: `publish`. When set to `true`, it will cause the generated release notes not to be created in a draft state.

I'd been trying to work out a way of automating the process of publishing the draft produced by release-drafter when a new tag was created, and it finally occurred to me that the simplest approach would be to have release-drafter accept a flag for doing that itself.

I realize `draft` is in the name of this project, though, so I'll of course understand if the response to this is "nope, this is out of scope" 😄

I also couldn't find any documentation of the existing action inputs (other than `config-name` being mentioned in the sample config), so I wasn't sure where to document this if it _is_ something you'd be interested in accepting.

Regardless, thanks for maintaining this great project!


Patch:
diff --git a/index.js b/index.js
index 86f560f..866223e 100644
--- a/index.js
+++ b/index.js
@@ -3,8 +3,8 @@ const { isTriggerableBranch } = require('./lib/triggerable-branch')
 const {
   findReleases,
   generateReleaseInfo,
-  createDraftRelease,
-  updateDraftRelease
+  createRelease,
+  updateRelease
 } = require('./lib/releases')
 const { findCommitsWithAssociatedPullRequests } = require('./lib/commits')
 const { sortPullRequests } = require('./lib/sort-pull-requests')
@@ -58,20 +58,24 @@ module.exports = app => {
       name: core.getInput('name') || undefined
     })
 
+    const shouldDraft = core.getInput('publish').toLowerCase() !== 'true'
+
     let createOrUpdateReleaseResponse
     if (!draftRelease) {
-      log({ app, context, message: 'Creating new draft release' })
-      createOrUpdateReleaseResponse = await createDraftRelease({
+      log({ app, context, message: 'Creating new release' })
+      createOrUpdateReleaseResponse = await createRelease({
         context,
         releaseInfo,
+        shouldDraft,
         config
       })
     } else {
-      log({ app, context, message: 'Updating existing draft release' })
-      createOrUpdateReleaseResponse = await updateDraftRelease({
+      log({ app, context, message: 'Updating existing release' })
+      createOrUpdateReleaseResponse = await updateRelease({
         context,
         draftRelease,
         releaseInfo,
+        shouldDraft,
         config
       })
     }
diff --git a/lib/releases.js b/lib/releases.js
index 6f4e627..dc8fb48 100644
--- a/lib/releases.js
+++ b/lib/releases.js
@@ -222,22 +222,28 @@ module.exports.generateReleaseInfo = ({
   }
 }
 
-module.exports.createDraftRelease = ({ context, releaseInfo, config }) => {
+module.exports.createRelease = ({
+  context,
+  releaseInfo,
+  shouldDraft,
+  config
+}) => {
   return context.github.repos.createRelease(
     context.repo({
       name: releaseInfo.name,
       tag_name: releaseInfo.tag,
       body: releaseInfo.body,
-      draft: true,
+      draft: shouldDraft,
       prerelease: config.prerelease
     })
   )
 }
 
-module.exports.updateDraftRelease = ({
+module.exports.updateRelease = ({
   context,
   draftRelease,
   releaseInfo,
+  shouldDraft,
   config
 }) => {
   const updateReleaseParams = updateDraftReleaseParams({
@@ -249,6 +255,7 @@ module.exports.updateDraftRelease = ({
     context.repo({
       release_id: draftRelease.id,
       body: releaseInfo.body,
+      draft: shouldDraft,
       ...updateReleaseParams
     })
   )


Test patch:
diff --git a/test/index.test.js b/test/index.test.js
index 57cb02f..f4d4fcd 100644
--- a/test/index.test.js
+++ b/test/index.test.js
@@ -1458,7 +1458,7 @@ Previous tag: ''
     })
   })
 
-  describe('input version, tag and name overrides', () => {
+  describe('input publish, version, tag and name overrides', () => {
     // Method with all the test's logic, to prevent duplication
     const overridesTest = async (overrides, expectedBody) => {
       let mockEnv = {}
@@ -1483,6 +1483,10 @@ Previous tag: ''
         if (overrides.name) {
           mockEnv['INPUT_NAME'] = overrides.name
         }
+
+        if (overrides.publish) {
+          mockEnv['INPUT_PUBLISH'] = overrides.publish
+        }
       }
 
       let restoreEnv = mockedEnv(mockEnv)
@@ -1565,6 +1569,23 @@ Previous tag: ''
       })
     })
 
+    describe('with publish: true', () => {
+      it('immediately publishes the created draft', async () => {
+        return overridesTest(
+          {
+            version: '2.1.1',
+            publish: 'true'
+          },
+          {
+            body: `Placeholder with example. Automatically calculated values based on previous releases are next major=3.0.0, minor=2.1.0, patch=2.0.1. Manual input version is 2.1.1.`,
+            draft: false,
+            name: 'v2.1.1 (Code name: Placeholder)',
+            tag_name: 'v2.1.1'
+          }
+        )
+      })
+    })
+
     describe('with tag and name', () => {
       it('gets the version from the tag and forces using the tag and name', async () => {
         return overridesTest(