Skip to content

Commit 47f3432

Browse files
authored
Merge pull request #3 from NejcZdovc/file-input
Adds file input support
2 parents e1254e7 + f0961ed commit 47f3432

File tree

6 files changed

+156
-15
lines changed

6 files changed

+156
-15
lines changed

.github/workflows/comment.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# hi user
2+
3+
Current time is {DATE}!

.github/workflows/example.yml

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,48 @@ on:
44
pull_request:
55

66
jobs:
7-
example:
8-
name: Example
7+
body:
8+
name: Doing comment with message
99
runs-on: ubuntu-latest
1010
steps:
1111
- name: Checkout
1212
uses: actions/checkout@v2
13+
14+
- name: Get time
15+
uses: actions/github-script@v3
16+
id: get-time
17+
with:
18+
script: return new Date().toString()
19+
result-encoding: string
20+
21+
- name: Comment
22+
uses: ./
23+
with:
24+
message: >
25+
Hello world :wave:!
26+
${{steps.get-time.outputs.result}}
27+
identifier: 'GITHUB_COMMIT_BODY'
28+
env:
29+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
30+
file:
31+
name: Doing comment with file
32+
runs-on: ubuntu-latest
33+
steps:
34+
- name: Checkout
35+
uses: actions/checkout@v2
36+
37+
- name: Get time
38+
uses: actions/github-script@v3
39+
id: get-time
40+
with:
41+
script: return new Date().toString()
42+
result-encoding: string
43+
1344
- name: Comment
1445
uses: ./
1546
with:
16-
message: "Hello world :wave:!"
47+
file: 'comment.md'
48+
identifier: 'GITHUB_COMMIT_FILE'
1749
env:
1850
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
51+
DATE: ${{steps.get-time.outputs.result}}

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ You can do multiple comments during the workflow execution via different identif
88

99
| Name | Description | Required | Default |
1010
| ---- | ----------- | -------- | ------- |
11-
| message | Message that you want in the comment (markdown supported) | yes | |
11+
| message | Message that you want in the comment (markdown supported) | message or file | |
12+
| file | File name of the message (file needs to be placed in `.github/workflows/`) | message or file | |
1213
| single_comment | Would you like to update existing comment (if exists) instead of creating new one every time? | no | true |
1314
| identifier | Identifier that we put as comment in the comment, so that we can identify them | no | `GITHUB_ACTION_COMMENT_PR` |
1415
| github_token | Github token that we use to create/update commit | yes | |
1516

17+
It's required to provide `message` or `file` input. If both are provided `message` input will be used.
18+
1619
## Output
1720

1821
| Name | Description | Return |
@@ -30,6 +33,35 @@ env:
3033
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
3134
```
3235
36+
### Simple comment via file
37+
```yaml
38+
uses: NejcZdovc/comment-pr@v1
39+
with:
40+
file: "comment.md"
41+
env:
42+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
43+
```
44+
45+
File should be placed in `.github/workflows` and it should be `.md`.
46+
47+
### Passing data in md file
48+
49+
```yaml
50+
uses: NejcZdovc/comment-pr@v1
51+
with:
52+
file: "comment.md"
53+
env:
54+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
55+
DATA: 'year 2020'
56+
```
57+
When you need to pass data from workflow info file you just define new env variable.
58+
That will be automatically replaced in the template.
59+
60+
Example of `comment.md` that uses `DATA` env variable.
61+
```md
62+
It's almost end of {DATA}!
63+
```
64+
3365

3466
### Multiple comments
3567
With specifying different `identifier` per step we will now track two different comments, and they will be updated accordingly.

action.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ description: 'Add comments in the PR with your GitHub Actions workflow.'
44
inputs:
55
message:
66
description: 'Message for the PR'
7-
required: true
7+
file:
8+
description: 'Name for comment md file'
89
single_comment:
910
description: 'Would you only like one comment from the action in the PR?'
1011
default: 'true'

dist/index.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ __webpack_require__.r(__webpack_exports__);
1111
/* harmony import */ var _actions_github__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_actions_github__WEBPACK_IMPORTED_MODULE_0__);
1212

1313
const core = __webpack_require__(186)
14+
const { promises: fs } = __webpack_require__(747)
15+
const path = __webpack_require__(622)
1416

1517
const getInputs = () => ({
1618
message: core.getInput('message'),
19+
file: core.getInput('file'),
1720
singleComment: core.getInput('single_comment') === 'true',
1821
identifier: core.getInput('identifier'),
1922
githubToken: core.getInput('github_token') || process.env.GITHUB_TOKEN
@@ -27,9 +30,44 @@ const getIdentifier = () => {
2730
return `<!-- ${identifier} -->`
2831
}
2932

30-
const getMessage = () => {
33+
const getFileContent = async () => {
34+
const { file } = getInputs()
35+
36+
if (!file) {
37+
return null
38+
}
39+
40+
const filePath = path.join(__dirname, `../.github/workflows/${file}`)
41+
const content = await fs.readFile(filePath, 'utf8')
42+
if (!content) {
43+
return null
44+
}
45+
46+
return content.replace(/{\w+}/g, (key) => {
47+
const envKey = key.substring(1, key.length - 1)
48+
if (process.env[envKey]) {
49+
return process.env[envKey]
50+
}
51+
52+
return key
53+
})
54+
}
55+
56+
const getMessage = async () => {
3157
const { message } = getInputs()
32-
return `${getIdentifier()}\n${message}`
58+
59+
let body
60+
if (!message) {
61+
body = await getFileContent()
62+
} else {
63+
body = message
64+
}
65+
66+
if (!body) {
67+
throw new Error('You need to provide message or file input')
68+
}
69+
70+
return `${getIdentifier()}\n${body}`
3371
}
3472

3573
const findComment = async (client) => {
@@ -76,9 +114,7 @@ const comment = async (client) => {
76114
commentId = await findComment(client)
77115
}
78116

79-
console.log(commentId)
80-
81-
const body = getMessage()
117+
const body = await getMessage()
82118
if (commentId) {
83119
await client.issues
84120
.updateComment({

index.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { context, getOctokit } from '@actions/github'
22
const core = require('@actions/core')
3+
const { promises: fs } = require('fs')
4+
const path = require('path')
35

46
const getInputs = () => ({
57
message: core.getInput('message'),
8+
file: core.getInput('file'),
69
singleComment: core.getInput('single_comment') === 'true',
710
identifier: core.getInput('identifier'),
811
githubToken: core.getInput('github_token') || process.env.GITHUB_TOKEN
@@ -16,9 +19,44 @@ const getIdentifier = () => {
1619
return `<!-- ${identifier} -->`
1720
}
1821

19-
const getMessage = () => {
22+
const getFileContent = async () => {
23+
const { file } = getInputs()
24+
25+
if (!file) {
26+
return null
27+
}
28+
29+
const filePath = path.join(__dirname, `../.github/workflows/${file}`)
30+
const content = await fs.readFile(filePath, 'utf8')
31+
if (!content) {
32+
return null
33+
}
34+
35+
return content.replace(/{\w+}/g, (key) => {
36+
const envKey = key.substring(1, key.length - 1)
37+
if (process.env[envKey]) {
38+
return process.env[envKey]
39+
}
40+
41+
return key
42+
})
43+
}
44+
45+
const getMessage = async () => {
2046
const { message } = getInputs()
21-
return `${getIdentifier()}\n${message}`
47+
48+
let body
49+
if (!message) {
50+
body = await getFileContent()
51+
} else {
52+
body = message
53+
}
54+
55+
if (!body) {
56+
throw new Error('You need to provide message or file input')
57+
}
58+
59+
return `${getIdentifier()}\n${body}`
2260
}
2361

2462
const findComment = async (client) => {
@@ -65,9 +103,7 @@ const comment = async (client) => {
65103
commentId = await findComment(client)
66104
}
67105

68-
console.log(commentId)
69-
70-
const body = getMessage()
106+
const body = await getMessage()
71107
if (commentId) {
72108
await client.issues
73109
.updateComment({

0 commit comments

Comments
 (0)