Closed
Description
Discussed in #433
Originally posted by rdhar November 13, 2023
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/Current
script: |
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/Proposed
script: |
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."
If 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.
Metadata
Metadata
Assignees
Labels
No labels