diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index cf4bf71a..f6249b67 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -10,12 +10,20 @@ jobs: - name: Checkout uses: actions/checkout@v3 - - name: Comment PR + - name: Comment PR with message uses: ./ with: message: | Current branch is `${{ github.head_ref }}`. _(execution **${{ github.run_id }}** / attempt **${{ github.run_attempt }}**)_ - comment_tag: nrt + comment_tag: nrt_message + reactions: eyes, rocket + mode: recreate + + - name: Comment PR with file + uses: ./ + with: + filePath: README.md + comment_tag: nrt_file reactions: eyes, rocket mode: recreate \ No newline at end of file diff --git a/README.md b/README.md index e2d42d10..d4009462 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,19 @@ jobs: Hello world ! :wave: ``` +### Comment a file content + +Thanks to the `filePath` input, a file content can be commented. +You can either pass an absolute filePath or a relative one that will be by default retrieved from `GITHUB_WORKSPACE`. +(Note that if both a `message` and `filePath` are provided, `message` will take precedence.) + +```yml +- name: PR comment with file + uses: thollander/actions-comment-pull-request@v2 + with: + filePath: /path/to/file.txt +``` + ### Setting reactions @@ -86,7 +99,8 @@ Note: the input `mode` can be used to either `upsert` (by default) or `recreate` | Name | Description | Required | Default | | --- | --- | --- | --- | | `GITHUB_TOKEN` | Token that is used to create comments. Defaults to ${{ github.token }} | ✅ | | -| `message` | The comment body | ✅ | | +| `message` | Comment body | | | +| `filePath` | Path of the file that should be commented | | | | `reactions` | List of reactions for the comment (comma separated). See https://docs.github.com/en/rest/reactions#reaction-types | | | | `pr_number` | The number of the pull request where to create the comment | | current pull-request/issue number (deduced from context) | | `comment_tag` | A tag on your comment that will be used to identify a comment in case of replacement | | | diff --git a/action.yml b/action.yml index 76315a45..9b0eb2b5 100644 --- a/action.yml +++ b/action.yml @@ -6,7 +6,8 @@ description: 'Comments a pull request with the provided message' inputs: message: description: 'Message that should be printed in the pull request' - required: true + filePath: + description: 'Path of the file that should be commented' GITHUB_TOKEN: description: 'Github token of the repository (automatically created by Github)' default: ${{ github.token }} diff --git a/lib/index.js b/lib/index.js index 724a8bea..70f6919b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9535,7 +9535,12 @@ var __importStar = (this && this.__importStar) || function (mod) { __setModuleDefault(result, mod); return result; }; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; Object.defineProperty(exports, "__esModule", ({ value: true })); +const path_1 = __importDefault(__nccwpck_require__(1017)); +const fs_1 = __importDefault(__nccwpck_require__(7147)); const github = __importStar(__nccwpck_require__(5438)); const core = __importStar(__nccwpck_require__(2186)); // See https://docs.github.com/en/rest/reactions#reaction-types @@ -9543,11 +9548,29 @@ const REACTIONS = ['+1', '-1', 'laugh', 'confused', 'heart', 'hooray', 'rocket', async function run() { try { const message = core.getInput('message'); + const filePath = core.getInput('filePath'); const github_token = core.getInput('GITHUB_TOKEN'); const pr_number = core.getInput('pr_number'); const comment_tag = core.getInput('comment_tag'); const reactions = core.getInput('reactions'); const mode = core.getInput('mode'); + if (!message && !filePath) { + core.setFailed('Either "filePath" or "message" input should be provided'); + return; + } + let content = message; + let _filePath = filePath; + if (!message && filePath) { + if (!path_1.default.isAbsolute(filePath)) { + const { GITHUB_WORKSPACE } = process.env; + if (!GITHUB_WORKSPACE) { + core.setFailed('GITHUB_WORKSPACE env variable should be defined because the "filePath" provided is relative.'); + return; + } + _filePath = path_1.default.join(GITHUB_WORKSPACE, filePath); + } + content = fs_1.default.readFileSync(_filePath, 'utf8'); + } const context = github.context; const issue_number = parseInt(pr_number) || context.payload.pull_request?.number || context.payload.issue?.number; const octokit = github.getOctokit(github_token); @@ -9571,7 +9594,7 @@ async function run() { const comment_tag_pattern = comment_tag ? `` : null; - const body = comment_tag_pattern ? `${message}\n${comment_tag_pattern}` : message; + const body = comment_tag_pattern ? `${content}\n${comment_tag_pattern}` : content; if (comment_tag_pattern) { let comment; for await (const { data: comments } of octokit.paginate.iterator(octokit.rest.issues.listComments, { diff --git a/src/main.ts b/src/main.ts index 328e86e9..9115936f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,3 +1,5 @@ +import path from 'path'; +import fs from 'fs'; import * as github from '@actions/github'; import * as core from '@actions/core'; import { GetResponseDataTypeFromEndpointMethod } from '@octokit/types'; @@ -9,12 +11,32 @@ type Reaction = typeof REACTIONS[number]; async function run() { try { const message: string = core.getInput('message'); + const filePath: string = core.getInput('filePath'); const github_token: string = core.getInput('GITHUB_TOKEN'); const pr_number: string = core.getInput('pr_number'); const comment_tag: string = core.getInput('comment_tag'); const reactions: string = core.getInput('reactions'); const mode: string = core.getInput('mode'); + if (!message && !filePath) { + core.setFailed('Either "filePath" or "message" should be provided as input'); + return; + } + + let content: string = message; + let _filePath: string; + if (!message && filePath) { + const { GITHUB_WORKSPACE } = process.env; + + if (!GITHUB_WORKSPACE) { + core.setFailed('"GITHUB_WORKSPACE" env variable is not defined.'); + return; + } + + _filePath = path.join(GITHUB_WORKSPACE, filePath); + content = fs.readFileSync(_filePath, 'utf8'); + } + const context = github.context; const issue_number = parseInt(pr_number) || context.payload.pull_request?.number || context.payload.issue?.number; @@ -45,7 +67,7 @@ async function run() { const comment_tag_pattern = comment_tag ? `` : null; - const body = comment_tag_pattern ? `${message}\n${comment_tag_pattern}` : message; + const body = comment_tag_pattern ? `${content}\n${comment_tag_pattern}` : content; if (comment_tag_pattern) { type ListCommentsResponseDataType = GetResponseDataTypeFromEndpointMethod<