Skip to content

Commit

Permalink
feat: ability to specify pr_number for manual action (#44)
Browse files Browse the repository at this point in the history
Co-authored-by: mvarrieur
  • Loading branch information
thollander authored Mar 19, 2022
1 parent 9845eaa commit e7099b3
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"printWidth": 80,
"printWidth": 120,
"endOfLine": "lf",
"bracketSpacing": true
}
29 changes: 23 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ You can even put dynamic data thanks to [Contexts and expression syntax](https:/

## Usage

### Classic usage

```
on: pull_request
Expand All @@ -16,26 +18,41 @@ jobs:
name: An example job to comment a PR
steps:
- name: Checkout
uses: actions/checkout@v1
uses: actions/checkout@v3
- name: Comment PR
uses: thollander/actions-comment-pull-request@v1
with:
message: 'Example of message !'
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

See examples in [opened PR](https://github.com/thollander/actions-comment-pull-request/pulls) !

:information_source: : Add `if: ${{ github.event_name == 'pull_request' }}` to this Action's step if your workflow is not only triggered by a `pull_request` event. It will ensure that you don't throw an error on this step.
:information_source: : Add `if: ${{ github.event_name == 'pull_request' }}` to this Action's step if your workflow is not only triggered by a `pull_request` event. It will ensure that you don't throw an error on this step.

### Specifying which pull request to comment on

You can explicitly input which pull request should be commented on by passing the `pr_number` input.
That is particularly useful for manual workflow for instance (`workflow_run`).

```
...
- name: Comment PR
uses: thollander/actions-comment-pull-request@v1
with:
message: 'Example of message !'
pr_number: 123 # This will comment on pull request #123
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

## Contributing

### Build
### Build

The build steps transpiles the `src/main.ts` to `lib/main.js` which is used in the Docker container.
It is handled by Typescript compiler.
The build steps transpiles the `src/main.ts` to `lib/main.js` which is used in the Docker container.
It is handled by Typescript compiler.

```sh
$ npm run build
Expand Down
7 changes: 4 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: 'Comment Pull Request'
branding:
icon: 'message-circle'
icon: 'message-circle'
color: 'blue'
description: 'Comments a pull request with the provided message'
inputs:
inputs:
message:
description: 'Message that should be printed in the pull request'
required: true
Expand All @@ -15,4 +15,5 @@ runs:
image: 'Dockerfile'
args:
- ${{ inputs.message }}
- ${{ inputs.GITHUB_TOKEN }}
- ${{ inputs.GITHUB_TOKEN }}
- ${{ inputs.pr_number }}
44 changes: 35 additions & 9 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
Expand All @@ -8,21 +31,24 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const github = require('@actions/github');
const core = require('@actions/core');
Object.defineProperty(exports, "__esModule", { value: true });
const github = __importStar(require("@actions/github"));
const core = __importStar(require("@actions/core"));
function run() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
const message = core.getInput("message");
const github_token = core.getInput("GITHUB_TOKEN");
const message = core.getInput('message');
const github_token = core.getInput('GITHUB_TOKEN');
const pr_number = core.getInput('pr_number');
const context = github.context;
if (context.payload.pull_request == null) {
core.setFailed("No pull request found.");
const pull_number = parseInt(pr_number) || ((_a = context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.number);
const octokit = github.getOctokit(github_token);
if (!pull_number) {
core.setFailed('No pull request in input neither in current context.');
return;
}
const pull_request_number = context.payload.pull_request.number;
const octokit = github.getOctokit(github_token);
const new_comment = yield octokit.rest.issues.createComment(Object.assign(Object.assign({}, context.repo), { issue_number: pull_request_number, body: message }));
yield octokit.rest.issues.createComment(Object.assign(Object.assign({}, context.repo), { issue_number: pull_number, body: message }));
}
catch (error) {
if (error instanceof Error) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "actions-comment-pull-request",
"version": "1.0.5",
"version": "1.1.0",
"description": "GitHub action for commenting pull-request",
"main": "lib/main.js",
"scripts": {
Expand Down
15 changes: 9 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ async function run() {
try {
const message: string = core.getInput('message');
const github_token: string = core.getInput('GITHUB_TOKEN');
const pr_number: string = core.getInput('pr_number');

const context = github.context;
if (context.payload.pull_request == null) {
core.setFailed('No pull request found.');
const pull_number = parseInt(pr_number) || context.payload.pull_request?.number;

const octokit = github.getOctokit(github_token);

if (!pull_number) {
core.setFailed('No pull request in input neither in current context.');
return;
}
const pull_request_number = context.payload.pull_request.number;

const octokit = github.getOctokit(github_token);
const new_comment = await octokit.rest.issues.createComment({
await octokit.rest.issues.createComment({
...context.repo,
issue_number: pull_request_number,
issue_number: pull_number,
body: message,
});
} catch (error) {
Expand Down

0 comments on commit e7099b3

Please sign in to comment.