Skip to content

Commit

Permalink
feat: allow single image only (#6)
Browse files Browse the repository at this point in the history
* chore: remove husky temporarily

Remove husky as it was failing to commit multiple times, will need to look into why.

* feat: start adding ability to update single image

Add some debug lines to pull the current comments on a PR, this is to start making it so we can
update a single comment with new information

* feat: begin to try to update the comment if we see an old comment

If we see an old comment defined by an invisible tag, then try to update it. For now this seems the
best way to ensure that it is unique.

* feat: add new flag to control if it should be an update

Add new flag to denote if the comment should be updated on each execution of the github action, or
if a new comment should be created every time its run.
  • Loading branch information
Chesire authored Apr 9, 2021
1 parent f3ba3a1 commit f147a33
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 24 deletions.
1 change: 0 additions & 1 deletion .husky/.gitignore

This file was deleted.

4 changes: 0 additions & 4 deletions .husky/commit-msg

This file was deleted.

2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ To get started with the project, please run `npm install` to get the required de

## Commiting

Vercel must be installed, to do so run
`npm install -g @vercel/[email protected]`.
Run `ncc build index.js --license licenses.txt` before commiting files, add the `dist` directory to the commit as well.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ Choices are:

Defaults to `shiba`

### `update-image`

Flag to denote if a single image be updated on each execution of the action, or a new image posted each time.

- If set to `true`, then a single comment will be created, then updated each time.
- If set to `false`, then a new comment will be created each time.

Defaults to `true`

## Example usage

```yaml
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ inputs:
description: 'What animal type image to post to the pull request'
required: false
default: 'shiba'
update-image:
description: 'Update a single comment each time this action is executed'
required: false
default: true
runs:
using: 'node12'
main: 'dist/index.js'
Expand Down
33 changes: 25 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ const core = __nccwpck_require__(9698)
const github = __nccwpck_require__(1695)
const apis = __nccwpck_require__(9692)

const actionTag = "<!--okami-action-->"

async function run() {
const context = github.context
if (context.payload.pull_request == null) {
Expand All @@ -113,19 +115,34 @@ async function run() {

async function postImageToPR(context, picture) {
const token = core.getInput('okami-token')
const shouldUpdateImage = core.getInput('update-image')
const octokit = github.getOctokit(token)
const commentBody = `${actionTag}\n![](${picture})`
const prNumber = context.payload.pull_request.number
const commentBody = `![](${picture})`

console.log("Posting comment " + commentBody)

octokit.issues.createComment({
const commentsResponse = await octokit.issues.listComments({
...context.repo,
issue_number: prNumber,
body: commentBody
issue_number: prNumber
})

console.log("Finished createComment")
const findResult = commentsResponse.data.find(element => element.body.includes(actionTag))

if (shouldUpdateImage && findResult != undefined) {
console.log("Updating comment with: " + commentBody)
await octokit.issues.updateComment({
...context.repo,
comment_id: findResult.id,
body: commentBody
})
console.log("Finished updateComment")
} else {
console.log("Creating comment with: " + commentBody)
await octokit.issues.createComment({
...context.repo,
issue_number: prNumber,
body: commentBody
})
console.log("Finished createComment")
}
}

run()
Expand Down
31 changes: 24 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const core = require('@actions/core')
const github = require('@actions/github')
const apis = require('./apis')

const actionTag = "<!--okami-action-->"

async function run() {
const context = github.context
if (context.payload.pull_request == null) {
Expand All @@ -20,19 +22,34 @@ async function run() {

async function postImageToPR(context, picture) {
const token = core.getInput('okami-token')
const shouldUpdateImage = core.getInput('update-image')
const octokit = github.getOctokit(token)
const commentBody = `${actionTag}\n![](${picture})`
const prNumber = context.payload.pull_request.number
const commentBody = `![](${picture})`

console.log("Posting comment " + commentBody)

octokit.issues.createComment({
const commentsResponse = await octokit.issues.listComments({
...context.repo,
issue_number: prNumber,
body: commentBody
issue_number: prNumber
})
const findResult = commentsResponse.data.find(element => element.body.includes(actionTag))

console.log("Finished createComment")
if (shouldUpdateImage && findResult != undefined) {
console.log("Updating comment with: " + commentBody)
await octokit.issues.updateComment({
...context.repo,
comment_id: findResult.id,
body: commentBody
})
console.log("Finished updateComment")
} else {
console.log("Creating comment with: " + commentBody)
await octokit.issues.createComment({
...context.repo,
issue_number: prNumber,
body: commentBody
})
console.log("Finished createComment")
}
}

run()
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"@commitlint/config-conventional": "^12.1.1",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^7.23.0",
"husky": "^6.0.0",
"prettier": "2.2.1"
},
"config": {
Expand All @@ -38,8 +37,5 @@
"extends": [
"@commitlint/config-conventional"
]
},
"scripts": {
"prepare": "husky install"
}
}

0 comments on commit f147a33

Please sign in to comment.