Gitlab API to upload whole project from local file system - gitlab

I need to upload using API whole project folder which contains multiple text files, image files, source files, etc.
I had gone through Gitlab Commits and upload API's but none of them matches requirement.
Is there any API to upload full project folder form API?
I used below API to create a project
POST : https://repo.**.com/api/v4/projects
{
"name": "test",
"default_branch": "master"
}

You can use repository files API to create files.
https://docs.gitlab.com/ee/api/repository_files.html#create-new-file-in-repository
POST /projects/:id/repository/files/:file_path
curl --request POST --header 'PRIVATE-TOKEN: <your_access_token>' --header "Content-Type: application/json" \
--data '{"branch": "master", "author_email": "author#example.com", "author_name": "Firstname Lastname", \
"content": "some content", "commit_message": "create a new file"}' \
'https://gitlab.example.com/api/v4/projects/13083/repository/files/app%2Fproject%2Erb'

After creating new project with gitlab api, e.g.:
curl --fail -d '{"name":"test","default_branch": "master"}' -H "Content-Type: application/json" -X POST https://repo.**.com/api/v4/projects?access_token=<your access token> --output project_details.json
You could assume your new project url based on post data name -> https://repo.**.com/<gitlab user or gitlab group>/test.git or you could parse response json with some tool, e.g. python:
http_url_to_repo=$(python -c "import json; print(json.load(open('project_details.json'))['http_url_to_repo'])")
Then you could follow new project instruction for existing folder:
#Push an existing folder
cd existing_folder
git init
git remote add origin $http_url_to_repo
git add .
git commit -m "Initial commit"
git push -u origin master
Where existing_folder is folder with your source files which you want to upload as a commit to new gitlab project.

Had the same problem and ended up walking the folder like this:
import os
for (dirpath, dirnames, files) in os.walk(my_folder):
for file in files:
# do the api call to commit for dirpath, file
It works nicely...

Related

Create new git project: Is not valid HTTP Git repository

I try create new git projects with files from the existing project: https://docs.gitlab.com/ee/api/projects.html#create-project
curl --request POST --header "PRIVATE-TOKEN: <your-token>" \
--header "Content-Type: application/json" --data '{
"name": "new_project", "description": "New Project", "path": "new_project",
"import_url": "https://user:token#gitlab.com/<path>.git",
"namespace_id": "42", "initialize_with_readme": "false"}' \
--url 'https://gitlab.example.com/api/v4/projects/'
But I got the error:
{"message":"https://user:token#gitlab.com/<path>.git is not a valid HTTP Git repository"}
I was found the issue https://gitlab.com/gitlab-org/gitlab/-/issues/360266 with same error, but I set "initialize_with_readme": "false"
Gitlab version is 14.9.2
Also I tried use a broken url as import_url and got this error too
The issue you mentioned used:
https://user:token#gitlab.example.com/templates/someproject.git
^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^
| |- replace with your project/repo name
|
|- needed if the import project is private
and need authentication to be accessed
That type of URL might work better in your case.
It this is somehow not supported, as in this thread (Apr. 2022) mentioned by the OP, the workaround is:
In any case we just worked around it by creating a blank project in the new GitLab, then pulling and pushing the project from the old one and into the new one.
This coding works well.
gitLabApi.getProjectApi().createProject(pro,"https://username:password#host/group/front-template.git");

How can I download a binary file submitted to gitlab over a trigger?

I want to push a file to a Gitlab pipeline from an external process using curl or a similar tool.
Uploading the file can be accomplished with a Gitlab Trigger API request:
curl -X POST \
-F "token=$(cat .gitlab-trigger)" \
-F "ref=develop" \
-F "variables[env]=qua" \
-F "bundle=#bundle.zip" \
https://gitlab.company.com/api/v4/projects/1234/trigger/pipeline
The pipeline job can then access a TRIGGER_PAYLOAD file similar to:
{
"ref": "develop",
"variables": {
"env": "qua"
},
"bundle": {
"filename": "bundle.zip",
"type": "application/octet-stream",
"name": "bundle",
"tempfile": "#\u003cFile:0x00007fcc8b7581e0\u003e",
"head": "Content-Disposition: form-data; name=\"bundle\"; filename=\"bundle.zip\"\r\nContent-Type: application/octet-stream\r\n"
},
"id": "1228"
}
Judging from the file content it would appear that the bundle.zip file is uploaded to the Gitlab server.
How can I get hold of the bundle.zip file? Is it even possible?
Please note that
Neither the bundle nor the temp file is found in the current dir or in the temporary parent dir of the TRIGGER_PAYLOAD file.
Specifying the payload file as a variables[bundle] form param makes Gitlab reject the request as only strings and map variables are supported.
Submitting the token and variables[env] variables as query params and adding the ZIP file as the binary only payload (no form params) makes the upload fail.
It would appear that uploading a file over the trigger API is not possible. A new feature request has been made to Gitlab.
Feel free to push it if you agree that this feature is needed!

How can I use the GitHub Rest API to update a repository that is part of an organization?

I have a large number of GitHub repositories that have been sorted into various organizations. I wish to change the visibility of the repos by batch to public using the following API call:
curl -u {user}:{pat} -H "Accept: application/vnd.github.v3+json" -X PATCH https://api.github.com/repos/{user}/angular.powershifter -d '{"private":false}'
Which as best as I can tell is the API call (see github docs). The {user] and {pat} in the sample above are real values, and the {pat} has every option selected. The {user} is both the owner of the repo and the organization.
What I get in response is is a 404 with the following body.
{
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/reference/repos#update-a-repository"
}
Is it possible to set the properties of repositories under an organization using the API?
What is the API call?
Bonus points if you can point me to the documentation!
Thanks.
Use Bearer Token based authentication. Github is deprecating password based authentications.
curl \
-H 'Authorization: Bearer ${GITHUB-TOKEN}' \
-X PATCH \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${OWNER}/${repo} \
-d '{"private":"false"}'
Link for the github documentation
https://docs.github.com/en/free-pro-team#latest/rest/overview/other-authentication-methods
Yes it is possible.
I've had some issues with this too.
Short answer use bearer token (As #shek said), and change false to "false":
curl -v -H "Authorization: Bearer ${GITHUB_TOKEN}"
-X PATCH https://api.github.com/repos/${ORGANIZATION}/${REPO}
-H "Accept: application/vnd.github.v3+json" -d '{"private": "false"}';
Notice that your token has full control of private repositories when creating the token:
full control of private repositories image
The link is quite confusing, because if for example you need to get all the repositories from GitHub you'll have to use:
https://api.github.com/orgs/${ORGANIZATION}/repos?${parameters}
Also when you need to change permissions you'll need to use:
https://api.github.com/orgs/${ORGANIZATION}/teams/${TEAM}/repos/${ORGANIZATION}/${REPO}
(Notice the orgs, which is missing in the PATCH command).
GitHub repositories documentation:
https://docs.github.com/en/rest/reference/repos#update-a-repository
GitHub auth methods (From #shek):
https://docs.github.com/en/free-pro-team#latest/rest/overview/other-authentication-methods

create repo using github api (only curl and no oauth)

I'm trying to create a github repo using the v3 api, but I always get a Not Found error. I want to do it only with curl and without oauth. This is what I'm trying:
curl -u myusername https://api.github.com/users/repos -d '{"name": "reponame"}'
What am I missing?
You can't do it without an Access Token.
Also, please feel free to look at my GitHub open source project Git-Captain.
I created a web-application with a Node.js back-end and HTML/JS front-end that you can setup to have an API do many of these calls for you. It has a step-by-step for a windows server and I'll be adding a Linux step-by-step soon.
It would only take a slight tweak to the project to add a new end-point to the source to do this for you.
To answer your question,
The GitHub API documentation explains exactly how to do what you are requesting on this link.
Giving this example:
as you requested in CURL and obviously replace the token "5199..." with your own:
curl -i -H "Authorization: token 5199831f4dd3b79e7c5b7e0ebe75d67aa66e79d4" \
-d '{ \
"name": "blog", \
"auto_init": true, \
"private": true, \
"gitignore_template": "nanoc" \
}' \
https://api.github.com/user/repos
OR
Not in CURL and according to this StackOverflow question you can do the following:
https://api.github.com/orgs/<organisation_name>/repos?access_token=<generated token>
or
https://api.github.com/users/<username>/repos?access_token=<generated token>
In body, pass this as a payload:
{
<br/>"name": "<Repo Name>",<br/>
"description": "<Whateveryour description is>",<br/>
"homepage": "https://github.com",<br/>
"private": false,<br/>
}
You can get a "personal access token in GitHub" by going to Settings->Developer Settings-> Personal Access Tokens->Generate new token
OR do all of the following
Write a script (let's call this script #1) that takes the username,password, and repoName as a parameter.
That script will call script #2, which is curl -u ' USER-NAME-HERE' https://api.github.com/user/repos -d '{"name": "REPO-NAME-HERE"}' which will prompt for your user password,
have your script #1 listen for script #2's response and then have it enter in the password which the user passed in as a parameter in script#1
Finally programmatically hit enter which fires off the curl to create your repo.
UPDATE*
So for some reason, the CURL won't work at all, but the Git-Hub API end point https://api.github.com/user/repos does indeed work. Using POSTMAN, I was able to create a new POST with the URL being https://api.github.com/user/repos and the BODY set to:
{
"name": "Hello-World",
"description": "This is your first repository",
"homepage": "https://github.com",
"private": false,
"has_issues": true,
"has_projects": true,
"has_wiki": true
}
Then I went to the 'Authorization' section of postman and under 'Type' I selected "Basic Auth" entered my username and password.
Clicked update request and then send and my repo was created!

How to get subfolders and files using gitlab api

I am using gitlab api to get the files and folders and succeded,
But I can able to get only directory names, not its subfolders and files.
So, how can i get full tree of my repository.
Please let me know.
Thanks in advance,
Mallikarjuna
According to the API, we can use
GET /projects/:id/repository/tree
to list files and directories in a project. But we can only get the files and directories in top-level of the repo in this way, and sub-directories of directories in top-level with param path.
If you wanna get directories of script/js/components, for example, you can use
GET /projects/:id/repository/tree?path=script/js/components
Rest API
You can use the recursive option to get the full tree using /projects/:id/repository/tree?recursive=true
For example : https://your_gitlab_host/api/v4/projects/:id/repository/tree?recursive=true&per_page=100
GraphQL API
You can also use the recently released Gitlab GraphQL API to get the trees in a recursive way :
{
project(fullPath: "project_name_here") {
repository {
tree(ref: "master", recursive: true){
blobs{
nodes {
name
type
flatPath
}
}
}
}
}
}
You can go to the following URL : https://$gitlab_url/-/graphql-explorer and past the above query
The Graphql endpoint is a POST on "https://$gitlab_url/api/graphql"
An example using curl & jq :
gitlab_url=<your gitlab host>
access_token=<your access token>
project_name=<your project name>
branch=master
curl -s -H "Authorization: Bearer $access_token" \
-H "Content-Type:application/json" \
-d '{
"query": "{ project(fullPath: \"'$project_name'\") { repository { tree(ref: \"'$branch'\", recursive: true){ blobs{ nodes { name type flatPath }}}}}}"
}' "https://$gitlab_url/api/graphql" | jq '.'
you should do url encoding to the full path of the file. for example lest assume that the path to file under your repository is: javascript/header.js
then you could use:
curl --head --header "PRIVATE-TOKEN: <your_access_token>" "https://<>/api/v4/projects//repository/files/javascript%2Fheader%2Ejs"
Of course, as mentioned in other responses, you have missed the path attribute of the gitlab repositories API which lets you browse the file hierarchy.
In addition, for simplicity, the python gitlab project exposes it through the projects API. Example:
# list the content of the root directory for the default branch
items = project.repository_tree()
# list the content of a subdirectory on a specific branch
items = project.repository_tree(path='docs', ref='branch1')
For getting the whole tree with sub-directories and files, you can pass a parameter called "recursive" to true
By default it's false
Api - {Gitlab_URl}/api/v4/projects/{Project_id}/repository/tree?recursive=true
Thanks!

Resources