Github API cannot tell the number of Python files in a public repo - github-api

When I try to get the number of python files in a public github repo using the following query:
https://api.github.com/search/code?q=*%20extension:py%20repo:VismithaMona/codedeploytest2
I got the response:
{ "total_count": 0, "incomplete_results": false, "items": [] }
However, that repo has at least 2 py files, as you may find from the following link
https://github.com/VismithaMona/codedeploytest2
Then why Github API not working here? Is it because the repo is too old?
I tried searching for related questions using Google, but no similar questions were ever asked.

This is explained by an announcement on the Github blog from December 2020:
Starting today, GitHub Code Search will only index repositories that have had recent activity within the last year. Recent activity for a repository means that it has had a commit or has shown up in a search result. If the repository does not have any activity for an entire year, the repository will be purged from the Code Search index. This change will enable the most relevant content for developers to surface in the code search index as well as keeping code search queries fast for all customers.
So you don't find results because the repository has not been updated recently enough.
However, the API endpoint https://api.github.com/repos/VismithaMona/codedeploytest2/contents/ is still available. This lists the files and folders starting in the root folder of the repository. Small excerpt of the json response, with various files and properties removed:
[
{
"name": "scripts",
"url": "https://api.github.com/repos/VismithaMona/codedeploytest2/contents/scripts?ref=master",
"type": "dir",
},
{
"name": "templates",
"url": "https://api.github.com/repos/VismithaMona/codedeploytest2/contents/templates?ref=master",
"type": "dir",
},
{
"name": "test_app.py",
"type": "file",
},
{
"name": "web.py",
"type": "file",
}
]
Using this response you can count the number of .py files in the root folder. If you also want to search in subfolders then you can write a small recursive function to also access the provided urls, for example https://api.github.com/repos/VismithaMona/codedeploytest2/contents/scripts?ref=master for the scripts subfolder.
For example in Python:
import requests
def checkfiles(url, extension):
print(f"check {url}")
files_found = 0
response = requests.request("GET", url)
if response.status_code == 200:
contents = response.json()
for item in contents:
if item['type'] == 'file' and item['name'][-len(extension):] == extension:
files_found += 1
print(f"found: {item['name']}")
if item['type'] == 'dir':
# recursive call here
files_found += checkfiles(item['url'], extension)
return(files_found)
total_files = checkfiles("https://api.github.com/repos/VismithaMona/codedeploytest2/contents", "py")
print("number of files with extension .py:", total_files)
Output:
check https://api.github.com/repos/VismithaMona/codedeploytest2/contents
check https://api.github.com/repos/VismithaMona/codedeploytest2/contents/scripts?ref=master
found: stop_flask.py
check https://api.github.com/repos/VismithaMona/codedeploytest2/contents/templates?ref=master
found: test_app.py
found: web.py
number of files with extension .py: 3

Related

How to build JSON from remote Swagger documentation (websocket based API)

I am currently grabbing the entire swagger.json contents:
swagger_link = "https://<SWAGGER_URL>/swagger/v1/swagger.json"
swagger_link_contents = requests.get(swagger_link)
#write the returned swagger to a file
swagger_return = swagger_link_contents.json()
with open ("swagger_raw.json", "w") as f:
f.write(json.dumps(swagger_return, indent=4))
The file is filled with beautiful JSON now. But there are a ton of objects in there, quite a few I don't need. Say I wanted to grab the command "UpdateCookieCommand" from my document:
"parameters": [
{
"name": "command",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/UpdateCookieCommand"
},
"x-nullable": false
}
],
but it also has additional mentions later in the document...
UpdateCookieCommand": {
"allOf": [
{
"$ref": "#/definitions/BaseCommand"
},
{
"type": "object",
"required": [
"command",
"id"
],
The latter object is really what I want to take from the document. It tells me what's required for a nominal API command to Update a Cookie. The hope is I could have a script that will look at every release and be able to absorb changes of the swagger and build new commands.
What methodology would you all recommend to accomplish this? I considered making subprocess Linux commands to just awk, sed, grep it to what I want but maybe there's a more elegant way? Also I won't know the amount of lines or location of anything as a new Swagger will be produced each release and my script will need to automatically run with a CI/CD pipeline.

Azure DevOps API TFS: How to get a file history if it was renamed and/or branched?

Is this possible somehow to get a file history (all related changesets) with API request if the file was branched or/and renamed?
For example, if I need to find a history of the object in Azure DevOps UI I can search this object in the project, in a certain path like this:
And if I need to find the first appearance of the object in a repository, I can get it by expanding a "branch and rename" history
There is a need to get this information via API requests.
I had tried to find some API requests which can do it, but found only the requests which can return only the changesets which are on the first picture, where the object has the same name and is located under the path defined in the search parameter - there is no information about renaming/branching operations.
GET https://dev.azure.com/Contoso/_apis/tfvc/changesets?api-version=6.0&searchCriteria.itemPath=$/Contoso/Trunk/Main/Metadata/Application_Ext_Contoso/Application_Ext_Contoso/AxSecurityPrivilege/Entity.xml
returns only 3 changesets - 2162, 2161, 391
POST https://dev.azure.com/Contoso/Contoso/_api/_versioncontrol/history?api-version=6.0
With the body request
{
"repositoryId":"",
"searchCriteria":"{\"itemPaths\":[\"$/Contoso/Trunk/Main/Metadata/Application_Ext_Contoso/Application_Ext_Contoso/AxSecurityPrivilege/Entity.xml\" ], \"followRenames\" : true ,\"top\":50}",
"includeSourceRename" : true
}
Also returns only 3 changesets, it only finds a specific item path, I tried to experiment with includeSourceRename and followRenames , but they do not work as I expected.
POST https://almsearch.dev.azure.com/Contoso/Contoso/_apis/search/codesearchresults?api-version=6.0-preview.1
with the body
{
"searchText": "Entity.xml",
"$skip": 0,
"$top": 25,
"filters": {
"Project": [
"Contoso"
],
"Repository": [
"$/Contoso"
],
"Path": [
"$/Contoso/"
]
},
"$orderBy": [
{
"field": "filename",
"sortOrder": "ASC"
}
],
"includeFacets": true
}
Also returns information only about 3 changesets.
Are there some approaches to get this information from the API request?

IBM Cloud Functions printing API response in Watson Assistant / node.js / json

With IBM cloud functions I am calling two Joke APIs. The first one gives me these results:
Results:
{
"response": {
"body": {
"body": [
{
"_id": "5f80ccd641785ba7c7d27bc0",
"punchline": "They always egg-cercise!",
"setup": "How do hens stay fit?",
"type": "general"
}
],
"success": true
},
I want to print the punchline and setup in Watson assistant so I tried this code:
$webhook_result_1.response.body.body.setup and $webhook_result_1.response.body.body.punchline but both gives me an error. When I use $webhook_result_1.response.body.body I get this:
[{"_id":"5f80ccd641785ba7c7d27c07","punchline":"A JOKE MACHINE!?","setup":"What do I look like?","type":"general"}]
So I guess I am on the right way. What am I doing wrong?
—
This is the response for the second joke API:
Results:
{
"response": [
{
"id": 299,
"punchline": "The meat-ball.",
"setup": "Where do hamburgers go to dance?",
"type": "general"
}
]
}
And I tried this: $webhook_result_2.response.punchline but it is not working as well.
How can I print the punchline and setup for each API?
The [] indicates an array, so you need to index it. Ideally you should check an array to see that it has at least one element, and then iterate through it, but your 1st element, if it exists, will be:
$webhook_result_1.response.body.body[0].setup
Based on the comments to your question, it appears that you are placing the opening bracket in the wrong place.

Get all entries with included images using Contentful REST API

I am using typescript,and the Contentful-ManagementAPI (that's the reason I'm not using the SDK client), and I want to retrieve all entries from a specific type. I am calling th Api like this:
axios.get("https://api.contentful.com/spaces/" +
space_id +
"/entries?content_type=" +
content_type+"&include="+2)
I am receiving all the entries requested, but in the image field I am getting this:
poster:en-US:
sys:
{type: "Link", linkType: "Asset", id: "222xxm9aaAu4GiAc2ESw0Q"}
So, How could I get the image URL?
I would appreciate any help. Thanks
From the Contentful docs:
The CMA does not support the include and locale parameters.
So, I might be better using the delivery api for retrieve content, and the management api for create/update/delete. That's the way it should be used.
The referenced assets and entries can be found in the includes object in the API response.
The response should look something like this:
{
"sys": {
"type": "Array"
},
"total": 9,
"skip": 0,
"limit": 100,
"items": [ // Your items here ],
"includes": {
"Asset": [ // Your included assets here, this is where you find the url to the image ]
"Entry": [ // Any referenced entries not included in the items array above ]
}
So to find the actual asset that includes all your data (including the url) you would have to find the correct asset, using the id of the reference (222xxm9aaAu4GiAc2ESw0Q), in the includes.Asset object.
Try include=10, If you are using multi locales provide fallback for each locale.
This worked for me.

get author from Confluence CQL Search String Version 3

How is it possible to get the author (from pages, comments, etc) for the search results with the Confluence CQL API Version 3.0?
For my search the API search Version 3.0 is necessary
This one gaves no Content snippet back:
https://myconfluence.site/rest/searchv3/1.0/search?queryString=SEARCHTEXT&startIndex=0&where=conf_all
The function "&expand=body.storage" doesn't work for me.
The only results i get are:
id,
title,
bodyTextHighlights,
url,
searchResultContainer
--name
--url
friendlyDate,
contentType,
metadata,
Anybody an idea? Thanks a lot.
I tried another search API to get the page creator and this works for me.
Instead of body.storage, you can expand history to get the creator of a page.
The rest call
/rest/api/content/search?cql=id=<pageId>&expand=history
leads to an output like this and you can walk through till the creator
{
"results": [{
"id": "123456",
...
"history": {
"latest": true,
"createdBy": {
...
"username": "Some.Name",
"displayName": "Some Name",
"userKey": "123456"
}
}
...
}

Resources