Download released asset from private github repo - github

I'm trying to download a single added asset from a release in a private repository but all I get is a 404 response. the download url of the asset is https://github.com/<user>/<repo>/releases/download/20211022/file.json
I've tried several different methods of specifying the username and private access token but all give the same result. When I use the access token to access api.github.com then it seems to work.
I've tried the following formats in curl
curl -i -u <user>:<token> <url>
curl -i "https://<user>:<token>#github.com/ ...."
curl -i -H "Authorization: token <token>" <url>
I can download the source code (zip) from the release, but this has a different url: https://github.com/<user>/<repo>/archive/refs/tags/20211022.zip
What am I doing wrong?

As in the document here
You have to use url: /repos/{owner}/{repo}/releases/assets/{asset_id} not browser_download_url and set request header to accept application/octet-stream to download.
And don't forget to add Authorization header too.

The alternative is to use the GitHub CLI gh, with the command gh release download.
Since gh 2.19.0 (Nov. 2022), you can download a single asset with gh release download --output (after a gh auth login, for authentication):
In your case:
gh release download --pattern 'file.json' --output file.json

Related

How do we access a file in github repo inside our azure databricks notebook

We have a requirement where we need to access a file hosted on our github private repo in our Azure Databricks notebook.
Currently we are doing it using curl command using the Personal Access Token of a user.
curl -H 'Authorization: token INSERTACCESSTOKENHERE' -H 'Accept:
application/vnd.github.v3.raw' -O -L
https://api.github.com/repos/*owner*/*repo*/contents/*path*
Is there a way we can avoid the use of PAT and use deploy keys or anything?
From summer 2021 databricks has introduced integration of git repos functionality.
More info here: https://learn.microsoft.com/en-us/azure/databricks/repos
If you add your file (excel, json etc.) in the repo, then you can use a relative path to access it and read it.
e.g. pd.read_excel("./test_data.xlsx")
Be aware that you need a cluster with a databricks version 8.4+ (or 9.1+?)
You can also test what is your current working directory by executing the following command. os.getcwd()
If you have correctly integrated the repo then your result should be something like:
/Workspace/Repos/george#myemail.com/REPO_FOLDER/analysis
otherwise it will be something like: /databricks/driver
Integrate Git and azure databricks.
This documentation shows how to integrate Git and azure databricks
Step1: Get raw URL of the File.
Step2: Use wget to access the file:
wget https://github.com/githubtraining/hellogitworld/blob/master/resources/labels.properties

I have a code in Github repo and wants to clone/copy it to the Azure repository, Is there any way to do it via Rest

I have a code in Github repo and wants to clone/copy it to the Azure repository, Is there any way to do it via Curl
Is there any way to do it via Curl
Yes. The method exists.
You need to use curl to run the following Rest API to create Other Git Service connection.
Post https://dev.azure.com/{Organization Name}/_apis/serviceendpoint/endpoints?api-version=6.0-preview.4
curl example:
curl -X POST \
-u USERName:PAT "https://dev.azure.com/org/_apis/serviceendpoint/endpoints?api-version=6.0-preview.4" \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"authorization":{"scheme":"UsernamePassword","parameters":{"username":"{User name}","password":"{Password}"}},
"data":{"accessExternalGitServer":"true"},
"name":"{name}",
"serviceEndpointProjectReferences":[{"description":"","name":"{Service connection name}","projectReference":{"id":"{Project Id}","name":"{Project Name}"}}],
"type":"git",
"url":"{Target Git URL}",
"isShared":false,
"owner":"library"
}'
Then you could use the importRequests Rest API to import the Github Repo to Azure Repo.
Post https://dev.azure.com/{Organization Name}/{Project Name}/_apis/git/repositories/{Repo Name}/importRequests?api-version=5.0-preview.1
For more detailed info, you could refer to my another ticket: Get github repository using Azure devops REST API
This method is more complicated. I suggest that you could directly import the github repo with the Import repository option in Azure Repo.
You could import the clone URL and auth info, then the repo will be directly cloned to Azure Repo.

How to correctly set a Github App committer to show the app created the commit?

When using the GitHub Api you can supply the author and committer information, such as when you PUT a content file.
I want to be able to use the GitHub App as the committer so that people understand the commit was done using the third-party tool and the author information is kept as the authenticated user.
Currently I am using the app name as the name of the committer and an email for the apps' domain. But that does not tie the commit to the app at all (ex: I would have to create a bot account with that email to get it show any kind of profile, etc but that would not really be tied to my Github App).
What is the correct way to do this? Or should I not be trying to use the App as the committer and just use the authenticated user as the committer?
You could use the same content api you mentioned, to figure out what you need. The response you get by creating a file is a json that will tell you the user-id that was assigned to your github app. Pay attention to the author object under commit, you should see something like this:
curl \
-X PUT \
-H "Authorization: token ${TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/msgongora/fubectl/contents/test-file \
-d '{"message":"message","content":"Y29udGVudA==","branch":"mrsg"}'
{
"content": {
...
"commit": {
...
"author": {
"name": "myciapp[bot]",
"email": "1234545665+myciapp[bot]#users.noreply.github.com",
"date": "2021-04-07T06:28:18Z"
},
...
}
Once you have that, you can set the name and email for your app as if you were setting it for a regular user:
git config --global user.name "myciapp[bot]"
git config --global user.email 1234545665+myciapp[bot]#users.noreply.github.com
While you need to use a token if you want to commit through GitHub's API, you do not actually need to go through the hassle of dealing with your app's private key and creating a JWT when all you need is the ID for...
git config --global user.name "myciapp[bot]"
git config --global user.email 123456789+myciapp[bot]#users.noreply.github.com
To get that ID (123456789 above), simply call:
https://api.github.com/users/myciapp[bot]
Replacing myciapp above with the name of your app, but leaving the [bot] suffix in place. You do not even need to run this request with an auth token - it returns public info, and your app's "user" ID is part of that.
If you are confused as to how to get TOKEN from Marcel's example, as I was, here's a command that you can run from shell. You'll need your app id (numbers), app private key, and your repo name.
export APP_ID=123456
export APP_SECRET="$(cat app.private-key.pem)"
export GH_REPO=owner/repo
export TOKEN=$(bash get_github_app_token.sh)
curl -X PUT \
    -H "Authorization: token ${TOKEN}" \
    -H "Accept: application/vnd.github.v3+json" \
    https://api.github.com/repos/$GH_REPO/contents/test-file \
    -d '{"message":"message","content":"Y29udGVudA==","branch":"test"}'
This will create a commit on branch test. The branch must exist.
The command will return a JSON that has wanted credentials.
This needs open-ssl, jq, curl, and this neat script.

How to search using GitHub API in private organizations

I have a private organization GitHub (enterprise) repository, e.g. github.mycompany.com.
I want to search strings in code using the GitHub API. How do I search in mycompany-specific GitHub repositories?
I tried the query below and received 404:
curl -i -H "Authorization: token <token>" https://github.mycompany.com/api/v3/search/code?q=class
It seems to be authenticating fine, since I tried curl -u <userName> and
If I give the wrong password, I get an authentication error.
If I give the right password, it goes through, but returns 404 for the search query.
Currently, the Search API is still in preview on GitHub Enterprise. As a result, you need to specify a special media type in the Accept header to get things working.
Give this a try:
curl -i -H "Accept: application/vnd.github.preview+json" -H "Authorization: token <token>" https://github.mycompany.com/api/v3/search/code?q=class

Deployment from private github repository

I am new to git and github (I used Subversion before). I can not find a solution how to export master branch only from my private repository to my production server.
I need to prepare an automated solution (called via fabric). I found that git has the archive command but this doesn't work with github (I set up SSH keys):
someuser#ews1:~/sandbox$ git archive --format=tar --remote=git#github.com:someuser/somerepository.git master
Invalid command: 'git-upload-archive 'someuser/somerepository.git''
You appear to be using ssh to clone a git:// URL.
Make sure your core.gitProxy config option and the
GIT_PROXY_COMMAND environment variable are NOT set.
fatal: The remote end hung up unexpectedly
So I will need an another way how to do this. I don't want any meta files from git in the export. When I do clone, I will have all these files in .git directory (what I don't want) and this downloads more data than I really need.
Is there a way how to do this over ssh? Or I have to download the zip over HTTPS only?
I'm not sure I fully understood your question.
I use this command to pull the current master version to my server:
curl -sL --user "user:pass" https://github.com/<organisation>/<repository>/archive/master.zip > master.zip
Does this help?
As I explained in SO answer on downloading GitHub archives from a private repo, after creating a GitHub access token, you can use wget or curl to obtain the desired <version> of your repo (e.g. master, HEAD, commit SHA-1, ...).
Solution with wget:
wget --output-document=<version>.tar.gz \
https://api.github.com/repos/<owner>/<repo>/tarball/<version>?access_token=<OAUTH-TOKEN>
Solution with curl:
curl -L https://api.github.com/repos/<owner>/<repo>/tarball/<version>?access_token=<OAUTH-TOKEN> \
> <version>.tar.gz