script: |\n require(process.env.GITHUB_ACTION_PATH + '/comment.js');// File: comment.js\nconst { data: list_comments } = await github.rest.issues.listComments({\n issue_number: context.issue.number,\n owner: context.repo.owner,\n per_page: 100,\n repo: context.repo.repo,\n});\nconst get_comment = list_comments\n .sort((a, b) => b.id - a.id)\n .find((comment) => /^keyword/.test(comment.body));\nreturn {\n body: get_comment.body,\n id: get_comment.id,\n};With this, I get: \"SyntaxError: await is only valid in async functions and the top level bodies of modules.\"
\nIf I drop the await, then I get: \"ReferenceError: github is not defined.\"
I'm sure I'm missing something obvious with module.exports = ({ github, context }) => { ... }, but I'm not sure how best to address this particular script which: makes an API call, processes the response, and returns the output in that specific order.
Really appreciate any thoughts/inputs, thanks for your time.
","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"I shared an example script here that got @rdhar on the right path.
\nThe crux of the solution is that:
\nrequireed in the script step that needs re-usable code as an exported function.awaited in the calling script step.-
|
Hi, I would like to strip out snippets of JS code into separate files outside of YAML for ease of legibility. However, I'm struggling to understand how best to achieve something this. Before/Currentscript: |
const { data: list_comments } = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
per_page: 100,
repo: context.repo.repo,
});
const get_comment = list_comments
.sort((a, b) => b.id - a.id)
.find((comment) => /^keyword/.test(comment.body));
return {
body: get_comment.body,
id: get_comment.id,
};After/Proposedscript: |
require(process.env.GITHUB_ACTION_PATH + '/comment.js');// File: comment.js
const { data: list_comments } = await github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
per_page: 100,
repo: context.repo.repo,
});
const get_comment = list_comments
.sort((a, b) => b.id - a.id)
.find((comment) => /^keyword/.test(comment.body));
return {
body: get_comment.body,
id: get_comment.id,
};With this, I get: "SyntaxError: await is only valid in async functions and the top level bodies of modules." I'm sure I'm missing something obvious with Really appreciate any thoughts/inputs, thanks for your time. |
Beta Was this translation helpful? Give feedback.
-
|
I shared an example script here that got @rdhar on the right path. The crux of the solution is that:
|
Beta Was this translation helpful? Give feedback.
I shared an example script here that got @rdhar on the right path.
The crux of the solution is that:
requireed in the script step that needs re-usable code as an exported function.awaited in the calling script step.