How to list packages and versions in github package registry?

What is the best way to list github packages in a script?

Thanks,

Tom

…after a little bit of hacking I came up with the following bash script, using GraphQL/curl/jq:

graphqlQuery() {
  local query="$1"; shift

  curl -s -H "Authorization: bearer $INPUT_TOKEN" -X POST -d '{"query":"'"$query"'"}' 'https://api.github.com/graphql'
}
listPackageVersions() {
  local g="$1"; shift
  local a="$1"; shift

  local query="$(cat <<EOF | sed 's/"/\\"/g' | tr '\n\r' ' '
query {
    repository(owner:"$USERNAME", name:"$REPOSNAME"){
        registryPackages(name:"$g.$a",first:1) {
            nodes {
                versions(last:100) {
                    nodes {
                        version
                    }
                }
            }
        }
    }
}
EOF
)"
  graphqlQuery "$query" | jq -r '.data.repository.registryPackages.nodes[0].versions.nodes[].version'
}

just thought I’d share it.

-Tom

3 Likes

Since the registryPackages seems to have been refactored to packages I had to change my script around. Again, just thought I’d share it… her is my new version:

graphqlQuery() {
  local query="$1"; shift

  curl -s -H "Authorization: bearer $INPUT_TOKEN" -X POST -d '{"query":"'"$query"'"}' 'https://api.github.com/graphql'
}
listPackageVersions() {
  local g="$1"; shift
  local a="$1"; shift

  local query="$(cat <<EOF | sed 's/"/\\"/g' | tr '\n\r' ' '
query {
    repository(owner:"$USERNAME", name:"$REPOSNAME"){
        packages(names:"$g.$a",first:1) {
            nodes {
                versions(first:100) {
                    nodes {
                        version
                    }
                }
            }
        }
    }
}
EOF
)"
  graphqlQuery "$query" | jq -r '.data.repository.packages.nodes[0].versions.nodes[].version'
}

Apparantly they removed the updatedAt aswell which is what I was using… Not sure how to get that date now can’t find anything.

Following does not work:

query {
          repository(owner: "User", name: "repo") {
              packages(first:100){
                nodes {
                    name,
                    packageType 
                    versions(first:100) {
                        nodes {
                            id,
                            version,
                            updatedAt
                        }
                    }
                }
              }
          }
      }

So it seems the only way to get a date that should be something close to the actual package date would be PackageFile object.

New query:

query {
          repository(owner: "User", name: "repo") {
              packages(first:100){
                nodes {
                    id
                    name
                    packageType 
                    versions(first:100) {
                        nodes {
                            id
                            version
                            files(first: 10) {
                                nodes {
                                    name
                                    updatedAt
                                }
                            }
                        }
                    }
                }
              }
          }
      }

I found the following on the GraphQL API v4 change log page:

reg your updatedAt:

On member RegistryPackageVersion.updatedAt: updatedAt will be removed. 
Use the PackageVersion object instead. Effective 2020-04-01.

and on my registryPackages:

On member Organization.registryPackages: registryPackages will be removed.
Use the PackageOwner object instead. Effective 2020-04-01.

So I guess we are using some moving targets :frowning:
On the other hand, improving APIs is also good! :slight_smile:

Yeah I found that as well, well seems like we’ll have to rewrite these things a few times before its out of beta :stuck_out_tongue:

1 Like