I am using the GitHub's GraphQL API to search for files/code containing a particular word. A simple (contrived) example of a search which in this case is to find the term "beef" in files located in "recipes" (the repo) for "someuser" (the owner for the repo) is shown below:
{
search(query: "beef repo:someuser/recipes", type: REPOSITORY, first: 10) {
repositoryCount
edges {
node {
... on Repository {
name
}
}
}
}
}
I tried this in GitHub's GraphQL Explorer (https://developer.github.com/v4/explorer/) and receive zero results from the search which is incorrect as I can confirm that the word ("beef" in the example above) is in the files in the repo:
{
"data": {
"search": {
"repositoryCount": 0,
"edges": []
}
}
}
When I try this using GitHub's REST API (v3) via curl, I definitely get results:
curl --header 'Accept: application/vnd.github.v3.raw' https://api.github.com/search/code?q=beef+repo:someuser/recipes
... So I know that the query (REST v3 API) is valid, and my understanding is that the query string in the GraphQL (v4) API is identical to that for the REST (v3) API.
My questions are:
Am I incorrectly using the GitHub GraphQL (v4) API or am I specifying the query string improperly, or am I trying to use functionality that is not yet supported?
Is there an example of how to do this that someone can provide (or link to) that illustrates how to search code for specific words?
Type: CODE is not supported yet. There is no way you can search the code using graphql right now.
Your understanding is right. Just that you are missing one piece.
The search you are doing is happening against the type: REPOSITORY.
if you replace your search with
search(query: "beef", type: REPOSITORY, first: 10) {
you will get all the repos having beef in their name.
You can add qualifiers, if you want to search "beef" just in names than change the query like
search(query: "beef in:name", type: REPOSITORY, first: 10) {
for further detail you can look at https://help.github.com/en/articles/searching-for-repositories#search-based-on-the-contents-of-a-repository
Related
Attempting to use the search query doesn't appear to give results and I'm not even sure it has the data I want, here's an example
query RepoFiles {
search(query: "filename:Dockerfile repo:apache/druid", type:REPOSITORY) {
codeCount
}
}
This REST api call does what I'd like, but I'd like to do this in graphql if possible
https://api.github.com/search/code?q=repo:apache/druid+filename:Dockerfile
Is there a way to get the website link via github api? Currently I don't see it when using the following:
curl
-H "Accept: application/vnd.github.v3+json"
https://api.github.com/orgs/ORG/repos
I can get the topics and the description text but not the website (winterbe.com/projects...).
You'll need to use the GraphQL API, and you're looking for the homepageUrl field under a given repo. For example, the query below will give all public repositories for a given user, and for each repository, it will give its name, description, and homepage:
query MyQuery {
repositoryOwner(login: "username") {
repositories(orderBy: {field: CREATED_AT, direction: DESC}, first: 100, privacy: PUBLIC) {
nodes {
name
description
homepageUrl
}
}
}
}
Unlike the REST api, the GraphQL api requires authentication. You can tinker around with the query here.
For a given repository, we can view its forks by replacing 'username' and 'repo' in the following URL:
https://github.com/username/repo/network/members
Example: https://github.com/veracrypt/VeraCrypt/network/members
Question
Using the GitHub API, how can we order a repository's forks by
stars
watchers
commits (since forking)
Note: accessible wrappers are fine (e.g. using python library for GitHub API)
What I know so far
We could write a script to individually retrieve the metrics for each of the ~500 forks. This may be the best/simplest solution, although I do know GitHub has a lot of built in (hidden) features, so I do wonder if collecting the most active forks may have been considered and built into the API by GitHub. Iterating through hundreds/thousands of forks could also invoke the GitHub API's rate limiting, so please be mindful if using this approach.
Not a full answer, but it is possible to order by stargazers using GraphQL API (try GitHub GraphQL API Explorer for quick start!):
{
repository(owner: "vuejs", name: "vue") {
forks(orderBy: {field: STARGAZERS, direction: DESC}, first: 3) {
nodes {
nameWithOwner
stargazerCount
}
}
}
}
Result:
{
"data": {
"repository": {
"forks": {
"nodes": [
{
"nameWithOwner": "SmallComfort/react-vue",
"stargazerCount": 1198
},
{
"nameWithOwner": "supergaojian/vue",
"stargazerCount": 20
},
{
"nameWithOwner": "developedbyed/vue",
"stargazerCount": 13
}
]
}
}
}
}
Possible fields are:
enum RepositoryOrderField {
"""
Order repositories by creation time
"""
CREATED_AT
"""
Order repositories by name
"""
NAME
"""
Order repositories by push time
"""
PUSHED_AT
"""
Order repositories by number of stargazers
"""
STARGAZERS
"""
Order repositories by update time
"""
UPDATED_AT
}
I found a project for that: Useful Forks.
I have tried it and it works perfect as long as the fork didn't change its original name.
Using a Github graphql query below I can see which merge options a repository has available:
{
node(id: "<id>") {
... on PullRequest {
number
repository {
mergeCommitAllowed
squashMergeAllowed
rebaseMergeAllowed
}
}
}
}
that returns:
{
"data": {
"node": {
"number": 666,
"repository": {
"mergeCommitAllowed": false,
"squashMergeAllowed": true,
"rebaseMergeAllowed": true
}
}
}
}
But, I don't see a way to know which is the default. When I look at the pull request on Github (see below), it knows that rebase is the preferred method for merging my pull request. Perhaps there is some kind of "sticky" data attached to my user? (I'm sure the last pull request I closed was using rebase)
Is it possible to know which merge method is the default with a graphql query?
Here's an example query which returns the default merge method for the authenticated user. replace owner and name as appropriate.
{
repository(owner: "TeamCodeStream", name: "codestream") {
viewerDefaultMergeMethod
}
}
I believe this is sticky to your user and possibly the repository. I've used a non-default merge method on a repository in the past, and when I came back, the merge button suggested the same value, which was definitely not the value I typically used.
The API wouldn't show you this, since that information is not a property of the repository, but both the repository and the user. API responses are not supposed to change based on the person who makes them unless there's an access control issue. Anyway, when performing a merge through the GraphQL API, the API defaults to the merge method anyway unless you specify something different.
I am using the GitHub's GraphQL API to search for files/code containing a particular word. A simple (contrived) example of a search which in this case is to find the term "beef" in files located in "recipes" (the repo) for "someuser" (the owner for the repo) is shown below:
{
search(query: "beef repo:someuser/recipes", type: REPOSITORY, first: 10) {
repositoryCount
edges {
node {
... on Repository {
name
}
}
}
}
}
I tried this in GitHub's GraphQL Explorer (https://developer.github.com/v4/explorer/) and receive zero results from the search which is incorrect as I can confirm that the word ("beef" in the example above) is in the files in the repo:
{
"data": {
"search": {
"repositoryCount": 0,
"edges": []
}
}
}
When I try this using GitHub's REST API (v3) via curl, I definitely get results:
curl --header 'Accept: application/vnd.github.v3.raw' https://api.github.com/search/code?q=beef+repo:someuser/recipes
... So I know that the query (REST v3 API) is valid, and my understanding is that the query string in the GraphQL (v4) API is identical to that for the REST (v3) API.
My questions are:
Am I incorrectly using the GitHub GraphQL (v4) API or am I specifying the query string improperly, or am I trying to use functionality that is not yet supported?
Is there an example of how to do this that someone can provide (or link to) that illustrates how to search code for specific words?
Type: CODE is not supported yet. There is no way you can search the code using graphql right now.
Your understanding is right. Just that you are missing one piece.
The search you are doing is happening against the type: REPOSITORY.
if you replace your search with
search(query: "beef", type: REPOSITORY, first: 10) {
you will get all the repos having beef in their name.
You can add qualifiers, if you want to search "beef" just in names than change the query like
search(query: "beef in:name", type: REPOSITORY, first: 10) {
for further detail you can look at https://help.github.com/en/articles/searching-for-repositories#search-based-on-the-contents-of-a-repository