How to format request for github api - rest

I'm having trouble formatting my request for the github api.
I have tried looking at the documentation and doing:
https://api.github.com/repos/facebook/react/commits/master?since=2019-01-01&until=2019-12-30
But this only returns one result instead of all commits for the year. I am using the requests library in python, and have also tried it natively in the browser.
Any help appreciated, Thanks.

You are using the Single Commit API with GET /repos/:owner/:repo/commits/:ref
You need List commits API : GET /repos/:owner/:repo/commits :
https://api.github.com/repos/facebook/react/commits?since=2019-01-01&until=2019-12-30&sha=master
You can specify the branch with sha param. If you want the default branch, you can omit this field:
https://api.github.com/repos/facebook/react/commits?since=2019-01-01&until=2019-12-30

Related

Looking for VSTS REST API to set the compare branch on a given repo

I am looking for help to find the REST API to set "compare" branch on Azure DevOps.
I have found one below API that is only modifying the default branch.
https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{RepositoryID}?api-version=6.0
Could someone please help in finding the similar API for modifying the compare branch?
Thanks in advance
To modify the compare branch, you can use the following Rest API:
API URL:
PATCH https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/Settings/Repository/{RepoID}/Entries/me?api-version=4.1-preview.1
Request Body:
{
"Branches.Compare":"refs/heads/branchname"
}

Azure DevOps/VSTS REST API does not get changes of a changeset

I'm trying to to get the changes of a changeset but it returns 404. I used this:
https://<myname>.visualstudio.com/<projectname>/_apis/tfvc/changesets/291/changes
changeset exists
without the '/changes' it works, returns the changeset info but I also need the merge sources
tried to specify the API version (e.g.: api-version-5.0)
I created a full control Personal Access Token for the client app but no luck. I tried to use this link in the browser and I got the same result: it works only without '/changes'.
What did I wrong?
As this is an old Q, this is for anyone else who has same problem, The projectname needs to be removed from the request.
https://<myname>.visualstudio.com/_apis/tfvc/changesets/291/changes
You look at the docs and sure enough it's not there but most other REST calls require a project name, so it can be confusing.
Also the docs are not very clear that you can interchange https://{myName}.visualstudio.com/ for the documented https://dev.azure.com/{organization}

Get the pull request that introduced a commit

In github, when I click on a commit, I can see the pull request that introduced it.
You can see this commit was introduced by PR 15.
How can I get the associated pull request for a particular commit from the Github API?
the #15 indicates the issue/PR number. If you have a commit sha it is possible to find the associated PR# using the GitHub search API (v3).
For example, suppose you have a commit sha - 7dd1bcf5f2f5eeed34cc2ec63053098fba302b6c. To find the PR# from this sha, your query using the GitHub APi can look something like this- https://api.github.com/search/issues?q=sha:7dd1bcf5f2f5eeed34cc2ec63053098fba302b6c. From JSON response, the field 'number' represents the PR# (in this case 16).
Ref. Search Issues

Github API to get the author/committer of specific line of code

I'm using Github code search API to get search for some text in a given repository. The response doesn't seem to contain any information related to the content that matched, sha/hash of the commit or the author/committer info:
https://api.github.com/search/code?q=addClass+repo:jquery/jquery
Is there any way to get the author/committer of the given line of code in a repo using the Github API?
Thanks!
For searching commits we can use the new Commit Search API which is currently available for developers for preview . During the preview period we need to explicitly specify a custom media type in the 'Accept' header. For example your search using this API could be - curl -H 'Accept: application/vnd.github.cloak-preview' \https://api.github.com/search/commits?q=addClass+repo:jquery/jquery
. ref: Search Commits

How to search for code in GitHub with GitHub API?

I'm trying to search for some piece of code using the GitHub API V3 given only the keyword, not limiting by user, organization, or repository.
For example, if I want to search for all pieces of code that contain the keyword "addClass", the results would be
https://github.com/search?q=addClass&type=Code&ref=searchresults without using GitHub API.
But how can I do the same thing through GitHub API? I tried https://api.github.com/search/code?q=addClass
It says "Must include at least one user, organization, or repository". How can I fix this?
You can do a code search without specifying a user/org/repo if you authenticate.
First, generate a personal access token for use for this purpose, from your Profile on GitHub's website:
Settings -> Developer Settings -> Personal Access Token -> Generate New Token (you can leave all access options unticked, since you're just using to make web requests)
Now, your original GET request will work and return results, if you append the token to it:
https://api.github.com/search/code?q=addClass&access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
UPDATE: OCT 2021
As pointed out by a comment below, passing the token in via a query parameter (like above) is deprecated. You must now add it as an Authorization header.
e.g.
curl --location --request GET 'https://api.github.com/search/code?q=addClass +in:file +language:csharp' \
--header 'Authorization: Token xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
or in Python:
import requests
url = "https://api.github.com/search/code?q=addClass +in:file +language:csharp"
headers = {
'Authorization': 'Token xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
}
response = requests.request("GET", url, headers=headers)
print(response.text)
2020: As detailed in Mark Z.'s answer, using an authentication (Authorization': 'Token xxxx') allows for a code search.
get /search/code
You can use:
either a dedicated command-line tool like feinoujc/gh-search-cli
ghs code --extension js "import _ from 'lodash'"
or the official GitHub CLI gh, (after a gh auth login) as show in issue 5117:
gh api --method=GET "search/code?q=filename:test+extension:yaml+org:new-org"
Or even:
gh api --method=GET search/code -f q='filename:test extension:yaml org:new-org' \
--jq '.items[] | [.repository.full_name,.path,.sha] | #tsv'
That would get a line-based, tab-separated list of fields in this order: repo name, file path, git sha. (see gh help formatting)
2014 (original answer): That seems related to the new restriction "New Validation Rule for Beta Code Search API" (Oct. 2013)
In order to support the expected volume of requests, we’re applying a new validation rule to the Code Search API. Starting today, you will need to scope your code queries to a specific set of users, organizations, or repositories.
So, the example of the API search code mentions now:
Suppose you want to find the definition of the addClass function inside jQuery. Your query would look something like this:
https://api.github.com/search/code?q=addClass+in:file+language:js+repo:jquery/jquery
While Gihub does not currently support code search without repo, user, or organization (see VonC's answer), codesearch does index some sources from Github via the codesearch API, albeit with an API less fully featured out than Github's.
For example, to search for wget invocations indexed from Github, call
curl "https://searchcode.com/api/codesearch_I/?q=wget&src=2"
The optional src parameter picks the code source (e.g., Github, BitBucket) that should be searched, and you can find its integer value for a source by altering the parameters of faceted search in the codesearch UI. The current value of src for Github is 2.
You can verify that the returned results from the above example come from github.com by viewing the the repo property of results items.