Skip to content

How to reference a separate script file which processes and returns output? #438

Closed
@rdhar

Description

@rdhar

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions