-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create github action to comment pull requests
- Loading branch information
Showing
329 changed files
with
69,275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<img src="https://beakyn.com/assets/beakyn-logo-v2-color.png" height="40" /> | ||
|
||
# Close Milestone | ||
|
||
Action to close milestone | ||
|
||
## Pre-requisites | ||
|
||
Create a workflow .yml file in your .github/workflows directory. An example workflow is available below. For more information, reference the GitHub Help Documentation for Creating a workflow file. | ||
|
||
## Inputs | ||
|
||
`repository`: Github repository. Add the Github context value: `github.repository`. (**required**) | ||
|
||
Either `milestone-number` or `milestone-title` is required. | ||
|
||
## Examples | ||
|
||
```yaml | ||
- name: Close milestone | ||
uses: Beakyn/gha-close-milestone@master | ||
env: | ||
GITHUB_TOKEN: ${{ github.token }} | ||
with: | ||
repository: ${{ github.repository }} | ||
milestone-number: 1 | ||
``` | ||
```yaml | ||
- name: Close milestone | ||
uses: Beakyn/gha-close-milestone@master | ||
env: | ||
GITHUB_TOKEN: ${{ github.token }} | ||
with: | ||
repository: ${{ github.repository }} | ||
milestone-title: v1.1.1 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: "Comment Pull Request" | ||
description: "Add comments to pull request. It could also append text to description." | ||
inputs: | ||
pull-request-number: | ||
description: "Pull request number. This is required if not running in a pull_request event" | ||
required: false | ||
description-message: | ||
description: "Append message to pull request description" | ||
required: false | ||
comment-message: | ||
description: "Add comment to pull request" | ||
required: false | ||
delete-previous-comment: | ||
description: "Delete previous comment" | ||
required: false | ||
runs: | ||
using: "node12" | ||
main: "./index.js" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
const github = require("@actions/github"); | ||
const core = require("@actions/core"); | ||
|
||
function getInputs() { | ||
const pullRequestNumber = core.getInput("pull-request-number"); | ||
const descriptionMessage = core.getInput("description-message"); | ||
const commentMessage = core.getInput("comment-message"); | ||
const deletePreviousComment = core.getInput("delete-previous-comment"); | ||
const token = process.env.GITHUB_TOKEN; | ||
|
||
return { | ||
pullRequestNumber, | ||
descriptionMessage, | ||
commentMessage, | ||
deletePreviousComment, | ||
token, | ||
}; | ||
} | ||
|
||
async function run() { | ||
try { | ||
const { | ||
pullRequestNumber, | ||
descriptionMessage, | ||
commentMessage, | ||
deletePreviousComment, | ||
token, | ||
} = getInputs(); | ||
const { pull_request } = github.context.payload; | ||
const { owner, repo } = github.context.repo; | ||
const octokit = github.getOctokit(token); | ||
if (!pull_request && !pullRequestNumber) { | ||
console.log('Not a pull_request event or pull-request-number informed. Ignoring action.') | ||
return; | ||
} | ||
const pull_number = pull_request && pull_request.number || pullRequestNumber | ||
|
||
/** | ||
* @param {string} message | ||
* @returns {string} | ||
*/ | ||
const placeholderWrap = message => `\n<!-- Replace -->\n${message}\n<!-- Replace -->\n`.replace('\\n', '\n') | ||
|
||
/** | ||
* @param {string} text | ||
* @param {string} placeholder | ||
* @returns {string} | ||
*/ | ||
const replacePlaceholder = (text, placeholder) => | ||
text.replace(/(<!-- Replace -->)(.*)(<!-- Replace -->)/, placeholder); | ||
|
||
if (descriptionMessage) { | ||
const descriptionWithPlaceholder = placeholderWrap(descriptionMessage) | ||
const currentPullRequest = await octokit.pulls.get({ | ||
owner, | ||
repo, | ||
pull_number, | ||
}); | ||
const newBody = replacePlaceholder( | ||
currentPullRequest.data.body, | ||
'' | ||
).concat(`\n${descriptionWithPlaceholder}`) | ||
await octokit.pulls.update({ | ||
owner, | ||
repo, | ||
pull_number, | ||
body: newBody | ||
}); | ||
console.log(newBody) | ||
console.log("Updated pull request description succesfully"); | ||
} | ||
|
||
if (commentMessage) { | ||
if (deletePreviousComment) { | ||
const comments = await octokit.issues.listComments({ | ||
owner, | ||
repo, | ||
issue_number: pull_number, | ||
}); | ||
const commentsToDelete = | ||
comments.data.filter(c => c.body.match(/(<!-- Replace -->)/)) | ||
await Promise.all( | ||
commentsToDelete.map(c => octokit.issues.deleteComment({ | ||
owner, | ||
repo, | ||
comment_id: c.id, | ||
}))) | ||
console.log("Deleted previous comments succesfully."); | ||
} | ||
|
||
const commentWithPlaceholder = placeholderWrap(commentMessage) | ||
|
||
const body = commentWithPlaceholder | ||
|
||
await octokit.issues.createComment({ | ||
owner, | ||
repo, | ||
issue_number: pull_number, | ||
body, | ||
}); | ||
console.log("Added new comment to pull request succesfully."); | ||
} | ||
} catch (error) { | ||
console.log('Error => ', error); | ||
core.setFailed(error.message); | ||
} | ||
} | ||
|
||
run(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.