Skip to content

Commit

Permalink
feat: add stdout and stderr (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesclark-Zapata authored Apr 19, 2024
1 parent 8ab46f3 commit fc671f7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 44 deletions.
26 changes: 12 additions & 14 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches:
- main
- 'releases/*'
- "releases/*"

jobs:
# test action works running from the graph
Expand All @@ -13,16 +13,14 @@ jobs:
pull-requests: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./
with:
command: echo hello123
template: "Some comment\n%command%"
update-text: "Some comment"
- uses: ./
with:
command: echo "hello123 asdf"
template: "Some comment2\n%command%"
update-text: "Some comment2"


- uses: actions/checkout@v4
- uses: ./
with:
command: echo "printing from stdout"
template: "Testing command\n%command%"
update-text: "Testing command"
- uses: ./
with:
command: node -e "process.stdout.write('testing Out');process.stderr.write('testing Error')"
template: "Testing stdout/stderr\n```\n%stdout%\n```\n```\n%stderr%\n```"
update-text: "Testing stdout/stderr"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ You can now consume the action by referencing the v1 branch
uses: zapatacomputing/command-pr-comment@v1
with:
command: make show-coverage-text-report
template: "🚀 Code Coverage\n```\n%command%```"
template: "🚀 Code Coverage\n```\n%stdout%```"
update-text: "🚀 Code Coverage"
```
Expand Down
35 changes: 23 additions & 12 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

43 changes: 27 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
const github = require('@actions/github');
const core = require('@actions/core');
const parse = require('shell-quote/parse');
const { spawnSync } = require('node:child_process');

const github = require("@actions/github");
const core = require("@actions/core");
const parse = require("shell-quote/parse");
const { spawnSync } = require("node:child_process");

// most @actions toolkit packages have async methods
async function run() {
const cmd = core.getInput('command');
const messageTemplate = core.getInput('template');
const updateText = core.getInput('update-text');
const githubToken = core.getInput('github-token');
const cmd = core.getInput("command");
const messageTemplate = core.getInput("template");
const updateText = core.getInput("update-text");
const githubToken = core.getInput("github-token");

const octokit = github.getOctokit(githubToken);
const context = github.context;
Expand All @@ -23,36 +22,46 @@ async function run() {
var message = "";

// If we have text to check, we will search the PR for a comment with this text
if (updateText){
if (updateText) {
try {
for await (const response of octokit.paginate.iterator(
octokit.rest.issues.listComments,
{
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
}
},
)) {
comment = response.data.find((comment) => comment.body.includes(updateText));
comment = response.data.find((comment) =>
comment.body.includes(updateText),
);
if (comment) break;
}
} catch (_) {
// Unable to find the comment, or an error occurred
// Continue and make a new comment
core.info("No comment found, will create a new one");
}
}

// Next, we execute the user's command
const splitCmd = parse(cmd);
const proc = spawnSync(splitCmd[0], splitCmd.slice(1));
const cmdOut = proc.stdout.toString();
const cmdErr = proc.stderr.toString();

if (proc.status === null || proc.status !== 0) {
core.error(`Command failed: ${cmd}\nstdout:${cmdOut}\nstderr:${cmdErr}`);
}

// If there's a template, we replace %command% with the output of the command
// Otherwise, we return a code block with the command output
if(messageTemplate){
if (messageTemplate) {
message = messageTemplate.replace("%command%", cmdOut);
message = message.replace("%stdout%", cmdOut);
message = message.replace("%stderr%", cmdErr);
} else {
message = `\n\`\`\`${cmdOut}\n\`\`\``;
message = `\n\`\`\`${cmdOut}\n\`\`\`<details>\n<summary>stderr</summary>\n\`\`\`${cmdErr}\`\`\`\n</details>`;
}
console.log(message);

Expand All @@ -71,11 +80,13 @@ async function run() {
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: message
body: message,
});
}
} catch (error) {
core.setFailed(`Error with making comment - ${error.name}: ${error.message}`);
core.setFailed(
`Error with making comment - ${error.name}: ${error.message}`,
);
}
}

Expand Down

0 comments on commit fc671f7

Please sign in to comment.