From ba42ad9139712985ff57d18579e4f4f1d752cabc Mon Sep 17 00:00:00 2001
From: softprops <d.tangren@gmail.com>
Date: Sun, 29 Sep 2019 02:15:58 -0400
Subject: [PATCH] honor body_path input when provided. fixes #22

---
 __tests__/release.txt  |  1 +
 __tests__/util.test.ts | 58 +++++++++++++++++++++++++++++++++++++++++-
 src/github.ts          |  4 +--
 src/util.ts            | 10 +++++++-
 4 files changed, 69 insertions(+), 4 deletions(-)
 create mode 100644 __tests__/release.txt

diff --git a/__tests__/release.txt b/__tests__/release.txt
new file mode 100644
index 0000000..ba0e162
--- /dev/null
+++ b/__tests__/release.txt
@@ -0,0 +1 @@
+bar
\ No newline at end of file
diff --git a/__tests__/util.test.ts b/__tests__/util.test.ts
index c7cfa48..4943a1d 100644
--- a/__tests__/util.test.ts
+++ b/__tests__/util.test.ts
@@ -1,4 +1,10 @@
-import { isTag, paths, parseConfig, parseInputFiles } from "../src/util";
+import {
+  releaseBody,
+  isTag,
+  paths,
+  parseConfig,
+  parseInputFiles
+} from "../src/util";
 import * as assert from "assert";
 
 describe("util", () => {
@@ -16,6 +22,56 @@ describe("util", () => {
       );
     });
   });
+  describe("releaseBody", () => {
+    it("uses input body", () => {
+      assert.equal(
+        "foo",
+        releaseBody({
+          github_ref: "",
+          github_repository: "",
+          github_token: "",
+          input_body: "foo",
+          input_body_path: undefined,
+          input_draft: false,
+          input_prerelease: false,
+          input_files: [],
+          input_name: undefined
+        })
+      );
+    });
+    it("uses input body path", () => {
+      assert.equal(
+        "bar",
+        releaseBody({
+          github_ref: "",
+          github_repository: "",
+          github_token: "",
+          input_body: undefined,
+          input_body_path: "__tests__/release.txt",
+          input_draft: false,
+          input_prerelease: false,
+          input_files: [],
+          input_name: undefined
+        })
+      );
+    });
+    it("defaults to body when both body and body path are provided", () => {
+      assert.equal(
+        "foo",
+        releaseBody({
+          github_ref: "",
+          github_repository: "",
+          github_token: "",
+          input_body: "foo",
+          input_body_path: "__tests__/release.txt",
+          input_draft: false,
+          input_prerelease: false,
+          input_files: [],
+          input_name: undefined
+        })
+      );
+    });
+  });
   describe("parseConfig", () => {
     it("parses basic config", () => {
       assert.deepStrictEqual(parseConfig({}), {
diff --git a/src/github.ts b/src/github.ts
index f42bfd1..92f52c5 100644
--- a/src/github.ts
+++ b/src/github.ts
@@ -1,5 +1,5 @@
 import { GitHub } from "@actions/github";
-import { Config } from "./util";
+import { Config, releaseBody } from "./util";
 import { lstatSync, readFileSync } from "fs";
 import { getType } from "mime";
 import { basename } from "path";
@@ -138,7 +138,7 @@ export const release = async (
       try {
         const tag_name = tag;
         const name = config.input_name || tag;
-        const body = config.input_body;
+        const body = releaseBody(config);
         const draft = config.input_draft;
         const prerelease = config.input_prerelease;
         console.log(`👩‍🏭 Creating new GitHub release for tag ${tag_name}...`);
diff --git a/src/util.ts b/src/util.ts
index 8dd9819..befb8a4 100644
--- a/src/util.ts
+++ b/src/util.ts
@@ -1,5 +1,5 @@
 import * as glob from "glob";
-import { lstatSync } from "fs";
+import { lstatSync, readFileSync } from "fs";
 
 export interface Config {
   github_token: string;
@@ -14,6 +14,14 @@ export interface Config {
   input_prerelease?: boolean;
 }
 
+export const releaseBody = (config: Config): string | undefined => {
+  return (
+    config.input_body ||
+    (config.input_body_path &&
+      readFileSync(config.input_body_path).toString("utf8"))
+  );
+};
+
 type Env = { [key: string]: string | undefined };
 
 export const parseInputFiles = (files: string): string[] => {