Skip to content

Commit

Permalink
feat: filePath for comment (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
thollander committed Dec 16, 2022
1 parent 318fcea commit e55342f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 6 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/ci.yaml
Expand Up @@ -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
16 changes: 15 additions & 1 deletion README.md
Expand Up @@ -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

Expand Down Expand Up @@ -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 | | |
Expand Down
3 changes: 2 additions & 1 deletion action.yml
Expand Up @@ -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 }}
Expand Down
25 changes: 24 additions & 1 deletion lib/index.js
Expand Up @@ -9535,19 +9535,42 @@ 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
const REACTIONS = ['+1', '-1', 'laugh', 'confused', 'heart', 'hooray', 'rocket', 'eyes'];
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);
Expand All @@ -9571,7 +9594,7 @@ async function run() {
const comment_tag_pattern = comment_tag
? `<!-- thollander/actions-comment-pull-request "${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, {
Expand Down
24 changes: 23 additions & 1 deletion 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';
Expand All @@ -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;

Expand Down Expand Up @@ -45,7 +67,7 @@ async function run() {
const comment_tag_pattern = comment_tag
? `<!-- thollander/actions-comment-pull-request "${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<
Expand Down

0 comments on commit e55342f

Please sign in to comment.