Skip to content
\n
After/Proposed
\n
script: |\n  require(process.env.GITHUB_ACTION_PATH + '/comment.js');
\n
// 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};
\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.\"

\n

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.

\n

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.

\n

The crux of the solution is that:

\n
    \n
  1. The script to be referenced is requireed in the script step that needs re-usable code as an exported function.
  2. \n
  3. Async functions need to be awaited in the calling script step.
  4. \n
","upvoteCount":2,"url":"https://github.com/actions/github-script/discussions/433#discussioncomment-8533599"}}}
Discussion options

You must be logged in to vote

I shared an example script here that got @rdhar on the right path.

The crux of the solution is that:

  1. The script to be referenced is requireed in the script step that needs re-usable code as an exported function.
  2. Async functions need to be awaited in the calling script step.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@rdhar
Comment options

Answer selected by rdhar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants