Field 'dependencyGraphManifests' doesn't exist on type 'Repository' - github

I am trying to make a react project which can fetch data from a github repo and list all the project dependencies, their current version in the project, latest version available, outdated or not and whether vulnerable. I am trying to use the github graphQL to fetch data but keeps on throwing this error.
Field 'dependencyGraphManifests' doesn't exist on type 'Repository'
. Can anyone help me with this? How can I get these data from github? Is it even possible?
Thanks in advance

Read https://docs.github.com/en/graphql/overview/schema-previews#access-to-a-repositories-dependency-graph-preview.
If you set the following header it will be there:
Accept: application/vnd.github.hawkgirl-preview+json

gh api -H 'Accept: application/vnd.github.hawkgirl-preview+json' graphql
--paginate -f query='query {
repository(owner:"octocat",name:"Hello-World") {
dependencyGraphManifests {
totalCount
nodes {
filename
}
edges {
node {
blobPath
dependencies {
totalCount
nodes {
packageName
requirements
hasDependencies
packageManager
}
}
}
}
}
}
}

Related

How to get GitHub repository Default Branch name with GraphQL?

Is there a way to use GitHub's GraphQL API to retrieve the default branch name of a repository? I can build a query to get all the branch names, but none of the properties I see reference the default branch name. Some repositories I deal with have 'master' for their default, and others have 'main'.
I am aware I can use the HEAD in some commands to workaround this. Such as "HEAD:README.md" for fetching the README.md file, like in this example query below. This repository's default branch is 'main', and I only know that from manual trial and error.
query {
repository(owner: "newrelic", name: "newrelic-ruby-agent") {
object(expression: "HEAD:README.md") {
... on Blob {
text
}
}
}
}
But when I check the local cloned copy of that repository, the .git/HEAD file points to ref: refs/heads/dev. So it seems I am actually pulling the README.md file from the dev branch, and not the default branch?
So is there a nice way to get the name of the default branch for a GitHub repository using their GraphQL API?
There's a field defaultBranchRef on the repository object:
query {
repository(owner: "newrelic", name: "newrelic-ruby-agent") {
defaultBranchRef {
name
}
}
}

Getting the list of all branches in a GitHub organisation without triggering Rate Limit, using Bash?

While trying to establish a list of incoming GitHub commits I've stumbled accross the GitHub rate api limits, of 60 calls per hour. As explained in this answer, one can get the lists of branches with an API call using:
https://api.github.com/repos/{username}/{repo-name}/branches
However, that triggers the rate limit for the average GitHub organisation/user. So I thought I'd try a different approach, using RSS/atom format. However, as that same answer explains, the atom format/rss feed seems to depend on the user having a list of all branches in a repository. This question asks for an overview of all commits in a repository, yet instead it is given an answer for all commits in the default branch of the repository. And this question receives a working answer that triggers the rate limit, as it relies on at least 1 API call per repository.
Hence, I would like to ask: How could one get a list of all branches of a GitHub user, using at most 1 GitHub API call?
Note, using atom views would be perfectly fine, however, I have not found an atom view like: https://github.com/:owner/:repo/commits.atom or https://github.com/:owner/:repo/branches.atom that displays all branches in a repository. I would strongly prefer a solution that does not rely on a third party like: https://rsshub.app/github/repos/yanglr as I imagine, they too will at some point start rate-limiting.
My current approach is to scrape the source code of https://github.com/:user/:repo/branches using bash. However, I imagine there might exist a more efficient solution to this.
MWE
Thanks to the comments, I was ble to find a bash MWE to perform a GraphQL query using terminal. It is given in this answer, where bearer is not a variable, it is the means of identification and the ...... should be your personal GitHub Access token. I am currently looking into how to get the repositories beyond the 1st hundred. Then I'll look at how to get the branches of those repositories.
Attempt I
The following query yields a json with the repositories and first 4 branches in each repository of a user!
name:examplequery.gql.
query {
repositoryOwner(login: "somegithubuser") {
repositories(first: 40) {
edges {
node {
nameWithOwner
refs(
refPrefix: "refs/heads/"
orderBy: { direction: DESC, field: TAG_COMMIT_DATE }
first: 4
) {
edges {
node {
... on Ref {
name
}
}
}
}
}
}
}
}
}
Next, a bash script is made that runs the query:
#!/usr/bin/env bash
# Runs graphql query on GitHub. Execute with:
# ./run_graphql_query.sh examplequery1.gql
GITHUB_PERSONAL_ACCESS_TOKEN_GLOBAL="your_github_personal_access_token"
if [ $# -ne 1 ]; then
echo "usage of this script is incorrect."
exit 1
fi
if [ ! -f $1 ];then
echo "usage of this script is incorrect."
exit 1
fi
# Form query JSON
QUERY=$(jq -n \
--arg q "$(cat $1 | tr -d '\n')" \
'{ query: $q }')
curl -s -X POST \
-H "Content-Type: application/json" \
-H "Authorization: bearer $GITHUB_PERSONAL_ACCESS_TOKEN_GLOBAL" \
--data "$QUERY" \
https://api.github.com/graphql
It can be ran with:
./run_graphql_query.sh examplequery1.gql
There are two more issues to resolve before I can answer the question. How I can iterate over all repositories instead of only the first 100. How I can parse the json into a list of branches per repository.

How to get a list of submodules through github API?

I'm trying to get a list of submodules of a repository through GitHub API. After reading Github API docs, I did the following things: In Order to access the submodules of the Jquery, I use the following link to get a list of submodules, however, I cannot see any submodules from it. Could anyone please tell me what field should I use to get a list of submodules of a repository from GitHub API?
You can get the list of git submodules using Github GraphQL API v4 with the submodules connection property :
{
repository(owner: "bertrandmartel", name: "javacard-tutorial") {
submodules(first: 100) {
nodes {
name
path
}
}
}
}
Try this in the explorer
Output :
{
"data": {
"repository": {
"submodules": {
"nodes": [
{
"name": "oracle_javacard_sdks",
"path": "oracle_javacard_sdks"
}
]
}
}
}
}
Note that this will give only submodules on default branch's HEAD, from the documentation :
Returns a list of all submodules in this repository parsed from the .gitmodules file as of the default branch's HEAD commit.
Note that the jquery repository doesn't have any git submodules in its default branch's HEAD (and I don't see any in its other branches)
If you use v3 REST API you can
Get the HEAD commit SHA of the branch.
Use the returned commit SHA as the tree_sha to get tree. GET /repos/{owner}/{repo}/git/trees/{tree_sha}
Iterate the returned tree object for entry that has mode 160000 (submodule)

How can we check in Jenkins home directory to Github?

We want to check in the Jenkins home directory into Github, certainly ignoring some files/folders. As we want to have everything defined by code, we try to achieve this using Job DSL & Pipeline DSL (not writing the pipeline script inside Jenkins GUI but having it read from a workspace file - see below).
My problem right now is that, being not very proficient in both DSL's yet, I don't know how to force git to do an initial clone of the remote repo (and later push) inside the home directory - which is a parent directory of the job's directory.
I tried this pipeline:
node('master') {
dir('../..') {
scm {
git {
remote {
github('company/repo', 'https')
credentials('xyz')
}
}
}
}
}
The job itself is defined like this:
pipelineJob('backup') {
definition {
cps {
script(readFileFromWorkspace('pipelines/backup.groovy'))
sandbox(true)
}
}
}
The job fails with this error message:
ERROR: ‘checkout scm’ is only available when using “Multibranch Pipeline” or “Pipeline script from SCM”
So I guess the above used 'pipelineJob(backup)' does not fit. Should I change this, and if, how, or should I take another approach?
Another shot at this was trying to rewrite the pipeline like this:
node {
dir('../..') {
git url: 'https://github.com/company/repo.git'
}
}
But then it won't work because credentials are missing...

Getting the issues from a certain milestone in Github

All I'm looking for is a way to get a list of issues for a given milestone. It looks like Github treats milestones a bit like labels in that you can ask for the labels for an issue, but not the issues for a given label.
I know that I can filter my issues by milestone on the Github website, but this traverses multiple pages and I wanted an easy way to see all of the issues for a milestone in a more printer friendly version.
Any tips?
You could use GitHub's API for this. See here on how to get the list of issues for a repo and notice the milestone parameter. The response you will get is a big JSON document, so you would have to create a small script to pull only the titles of the issues, or use grep, or smething like jq.
Notice also that API responses are also paged, but you can set the paging to be 100 entries per page, which is usually enough. If not, you would again have to create a small script to fetch all the pages (or do it manually).
You can use the GraphQL API which is V4. and do something like:
{
repository(owner: "X", name: "X") {
milestone(number: X) {
id
issues(first: 100) {
edges {
node {
id,
title
}
}
}
}
}
}
I was not able to find any easy methods. This worked a treat for me:
brew install hub (on OSX). Hub is created by GitHub
cd to the local repo you want to access the origin for.
hub issue -M 21 -f "%I,%t,%L,%b,%au,%as" > save_here.csv
profit.
Find the issue # (21 in the example above) in the URL on GitHub when you are viewing the milestone.
Docs for hub and in particular the format (-f) flag can be found here: https://hub.github.com/hub-issue.1.html
First find the list of milestones using this
Then query this api by milestone number for each milestone
Given a milestone $title in $owner/$repo, we can list the issues in this milestone using curl and jq:
api_url="https://api.github.com/repos/$owner/$repo"
MS=$(curl -s "$api_url/milestones" | jq '.[] | select(.title == "QA")')
MS_number=$(echo "$MS" | jq .number -r)
MS_state=$(echo "$MS" | jq .state -r)
echo "Found $title milestone with state=$MS_state"
echo ""
issues=$(curl -s "$api_url/issues?milestone=$MS_number" | jq '.[].number' -r)
echo "The following issues are in the QA milestone:"
for i in $issues; do
issue_title=$(curl -s "$api_url/issues/$i" | jq '.title' -r)
echo " https://github/$owner/$repo/issues/$i - $issue_title"
done
echo ""