Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOCS]: What's the right way to pass JSON from one step to another? #227

Closed
1 task done
peytondmurray opened this issue Jul 25, 2023 · 1 comment
Closed
1 task done
Labels
Status: Up for grabs Issues that are ready to be worked on by anyone Type: Documentation Improvements or additions to documentation Type: Support Any questions, information, or general needs around the SDK or GitHub APIs

Comments

@peytondmurray
Copy link

peytondmurray commented Jul 25, 2023

Describe the need

I'm trying to parse the comments of issues, and label an issue if certain words are found. I'm using a query to get the comments of issues in the first step of my workflow, and a second step applies the label using the output.

The documentation for this action says that if I want to pass the output of a graphql query to another step, I should

To access deep values of outputs.data, use fromJSON()

but I can't figure out a way to do this that doesn't break in some way. Here's what I have currently:

  apply-issue-label:
    name: Apply label if needed
    steps:
      - uses: octokit/graphql-action@v2.x
        name: Get the text of the issue and any of its comments
        id: get_issue_text
        with:
          query: |
            query getIssues($number: Int!, $owner: String!, $repo: String!) {
              repository(name: $repo, owner: $owner) {
                issue(number: $number) {
                  body
                  comments(last: 100) {
                    nodes {
                      body
                    }
                  }
                }
              }
            }
          variables: |
            owner: ${{ github.event.repository.owner.login }}
            repo: ${{ github.event.repository.name }}
            number: ${{ github.event.issue.number }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - uses: actions/github-script@v6
        name: Parse issue comments
        with:
          script: |
            # This fails because the ${{ }} is literally substituted with the word `Array` before execution. Not sure why this is...
            const comments = ${{ fromJSON(steps.get_issue_text.outputs.data).repository.issue.comments.nodes }}
            console.log(comments)

            # This will also most likely fail because again the ${{ }} literally replaces what is in the issue body. If a malicious user puts in the right snippet of code, they can execute it here.
            const issueBody = ${{ fromJSON(steps.get_issue_text.outputs.data).repository.issue.body }}
            console.log(issueBody)

            let text = comments.map(({body}) => body)
            text.push(issueBody)

What's the right way to pass the JSON output from the first step to the second step for handling? I'd really like just the first step's JSON output as an Object in the second step.

Other stuff I've tried

  • Using JSON.parse(${{ steps.get_issue_text.outputs.data }}) fails because the argument is replaced with the literal output of get_issue_text, which isn't valid JS.
  • Using JSON.parse(`${{ steps.get_issue_text.outputs.data }}`) almost works, but will fail if there are newlines in the data, which aren't allowed in JSON, and will also fail if the data contains `.
  • Setting an environment variable to be ${{ steps.get_issue_text.outputs.data }} doesn't work; spaces aren't allowed
  • Setting an environment variable to be "${{ steps.get_issue_text.outputs.data }}" doesn't work because the GraphQL response contains ", causing a syntax error

SDK Version

No response

API Version

No response

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@peytondmurray peytondmurray added Status: Triage This is being looked at and prioritized Type: Documentation Improvements or additions to documentation labels Jul 25, 2023
@kfcampbell kfcampbell added Type: Support Any questions, information, or general needs around the SDK or GitHub APIs Status: Up for grabs Issues that are ready to be worked on by anyone and removed Status: Triage This is being looked at and prioritized labels Jul 25, 2023
@peytondmurray
Copy link
Author

Closing this issue as solved. Inside the github-script action, you can get the JSON output from the graphql-action into a JS object by passing it through JSON.stringify, like this:

const result = JSON.parse(JSON.stringify(${{ steps.get_issue_text.outputs.data }}))

Then you can manipulate it however you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Up for grabs Issues that are ready to be worked on by anyone Type: Documentation Improvements or additions to documentation Type: Support Any questions, information, or general needs around the SDK or GitHub APIs
Projects
Archived in project
Development

No branches or pull requests

2 participants