Get files attached in issue

I have a GitHub action that processes information given in issues created (private repo).
Sometimes, there are chances that a zip file may be attached in the issue body. Is there any way via actions or GitHub API which can help me access those attachment(s) in action run?

When you open an issue, a workflow triggered by this issue event, you could see the issue body in ${{github.event.issue.body}} .


And then you could use shell scripts to get the attachment url and it could be downloaded with curl command to github workspace.
Please check my example workflow:

on:
  issues:
      types: [opened, edited]

jobs:
  get-attachment:
    runs-on: ubuntu-latest
    steps:
    
    - uses: actions/checkout@v2
    - name: Dump GitHub context
      env:
        GITHUB_CONTEXT: ${{ toJson(github) }}
      run: echo "$GITHUB_CONTEXT"
    - name: get attachment
      run: |
        attachments=($(echo $url | awk -v RS=[\(] -v FS=[\)] 'NR>1{print $1}' ))
        echo ${attachments[1]}
        for i in "${attachments[@]}"
        do 
          :
          echo $i
          curl -O $i
        done
        ls
      env:
        url: ${{github.event.issue.body}}
1 Like

Hey @yanjingzhu, thanks for the action code, I did try something similar, but it does not work for private repos. This GitHub url does not download in case it is a private repo.

Also, for the public repos, in the example that you have shared, you will have to replace:

          curl -O $i

With,

          curl -OL $i

Need to follow the link redirection. Otherwise, you will not get the actual zip file, just a HTML text containing redirection message as the file content of the zip file.

Please cat as well as unzip the files after downloading them to replicate the exact issue.

Do let me know if there is any way in which I get this zip file URL to download properly in case of private repos. I am just getting Not Found in case of zip file link when issue is from private repo.

I tried in a private repo. The workflow and the issue are in the same private repo.

What’s your zip url? Does it need authentication? What about add username and password when execute curl command?

Do not just run ls, please try to unzip the downloaded file and you will understand the issue.

Oh, you are right. I am sorry for ignoring that you are using a private repo. The method I provided before could not work in private repo.
There is no GitHub API to access attachments in issue currently. And the attachment url could not be accessed by curl command.