Curl POST gitlab API gives 404 - rest

I am trying to push a file to my folder called "collections" in my repository using curl.
I've spent almost 2 days investigating problem and I am not sure what is the exact problem.
curl -D- -k -X GET -H "PRIVATE-TOKEN: faNFKoC4-opiDJ0FJSk" https://gitlab.example.com/api/v4/projects/592/repository/tree?path=collections
Get request works properly and I get list of files in collections folder.
The collections folder is a folder in my gitlab repository
But when I try to POST a file to that exact same folder I get 404:
curl -D- -k -X POST -H "PRIVATE-TOKEN: faNFKoC4-opiDJ0FJSk" -F "file=#C:/Documents/Folder_A/bp30_QA.csv" https://gitlab.example.com/api/v4/projects/592/repository/tree?path=collections
Am I missing some parameter? also gitlab API didn't help me very much.
Edit: Solution from Bertrand Martel helped me solve the issue
Also for everyone on windows having trouble installing jq
jq is a lightweight and flexible command-line JSON processor.
Install choco: https://chocolatey.org/install
Open powershell as administrator and run:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
After installation, run:
choco install jq

To create a new file called bp30_QA.csv in the collections folder, you can use the following :
curl -H 'PRIVATE-TOKEN: YOUR_PRIVATE_TOKEN' \
-H "Content-Type: application/json" \
-d '{
"branch": "master",
"author_email": "johndoe#gmail.com",
"author_name": "John Doe",
"content": '"$(jq -Rs '.' bp30_QA.csv)"',
"commit_message": "create a new file"
}' "https://gitlab.com/api/v4/projects/592/repository/files/collections%2Fbp30_QA.csv"
It uses jq to wrap the content of the file in a single JSON field (check this post)

Related

Remote server Run CMD in Powershell

I am trying to upload a Zip file via RestAPI in powershell and Powershell version I am using is 5.1.
since Invoke-restmethod in powershell 5.1 doesn't have -form option,I tried to run curl command in CMD using powershell.When I run it locally it's working correctly. But when i try to run it in a server(azure devops agent) I am getting error saying curl command is not recognized.
What am i missing here it's runs as expected when i try in vs code, but not when i run it from a server.
$header1 = "accept: application/json"
$header2 = "X-Authorization: $($token)"
$header3 = "Content-Type: multipart/form-data"
$body1 = "upload=#Name.zip;type=application/x-zip-compressed"
$body2 = "actionIfExisting=Existing"
$body3 = "publicWorkspace=Public"
cmd.exe /c curl -X POST $uri -H $header1 -H $header2 -H $header3 -F $body1 -F $body2 -F $body3
Luckily I had git installed in the server and I read in this stack overflow page that Git comes with preInstalled curl.exe. So I called the path where git has curl.exe and using & operator in powershell to execute the curl.exe directly in powershell itself.
& 'C:\users\git\mingw64\bin\curl.exe' -X POST $uri -H $header1 -H $header2 -H $header3 -F $body1 -F $body2 -F $body3

Unable to import grafana dashboard using exported json through CLI

I was trying to import dashboard using exported json file from one server to another server of grafana, but getting bellow error;
{"message":"Dashboard not found","status":"not-found"}
My curl command:
curl -X POST --insecure -H "Authorization: Bearer {API KEY}" -H "Content-Type: application/json" --data-binary #'{JSON file name}' http://{Host ip}:3000/api/dashboards/db
To export dashboard, I am using following curl command;
curl -f -k -H "Authorization: Bearer {API key}" "http://{Host IP}:3000/api/dashboards/db/mydashboard" | jq 'del(.overwrite,.dashboard.version,.meta.created,.meta.createdBy,.meta.updated,.meta.updatedBy,.meta.expires,.meta.version)' > {JSON file name}"
I am unable to find exact missing field OR value which is missing OR incorrectly passed through JSON file while importing. Is there any way to debug?
Is there any issue with my Export and import command.
As mentioned in this issue,
you must replace the "id": num field of the dashboard with null.
so you can change it for all of your dashboards (.json files) by command below: (works with GNU sed only)
sed -i '0,/"id": .*/{s/"id": .*/"id": null,/}' *.json
The simplest solution is to set the 'id' from root of the json to null.
"id": null,
Be aware, it's not the same field as uid in the end of the json (also root). That one must be unique, but not null.

cURL command failed to delete pull-request from bit bucket with 400 error

I am trying to delete a pull-request from bitbucket. I am using the following command,
curl -v -u {username}:{password} -X DELETE -H "Content-Type:application/json" https://{bitbucket.com}/rest/api/1.0/projects/{project_name}/repos/{repo_name}/pull-requests/{pull-request_id}
but the command fails with an error code 400. Please find the error:
{"errors":[{"context":null,"message":"No content to map to Object due to end of input","exceptionName":"java.io.EOFException"}]}.
I think I am missing something in the rest url. Please help me to resolve the issue. Thanks in advance
You need to pass the "version" of the pull-request (remember that the pull-request version starts in 0). Add the "-d #-" option to send the version in a here-document.
curl -v -u {username}:{password} -d #- -X DELETE -H "Content-Type:application/json" https://{bitbucket.com}/rest/api/1.0/projects/{project_name}/repos/{repo_name}/pull-requests/{pull-request_id} <<EOF
{
"version": {version}
}
EOF

How to deploy *.jrxml reports to Jasper Server using scripting

I would like to be able to deploy reports to jasperserver using some kind of scripts, not via the Jaspersoft Studio GUI
I tried
js-export --uris /reports --output-dir myExportedReports
hoping to re-import the same.
However, while exploring the myExportedReports folder, there were only *.xml and associated *.data files without the original *.jrxml.
Did anyone deploy new reports (report folders) to Jasper Server without using Jaspersoft Studio?
So I checked out the Jasper Server REST interface.
It looks like a two steps process
Upload the JRXML file
JRXML_DATA=`cat "report.jrxml"`
curl -X POST http://localhost:8080/jasperserver/rest_v2/resources/reports/ \
-H "Content-Type:application/jrxml" \
-H "Content-Disposition:attachment; filename=My-report" \
-H "Content-Description:My-report-description" \
-d "$JRXML_DATA" \
--user jasperadmin:jasperadmin
Create the Report Unit and assign a data source
curl -X POST http://localhost:8080/jasperserver/rest_v2/resources/reports \
-H "Content-Type:application/repository.reportUnit+json" \
-d '{"uri": "/reports/my_report","label": "my-report","description": "My-report description", "permissionMask": "0", "version": "0" , "alwaysPromptControls": "true","controlsLayout": "popupScreen", "jrxml": {"jrxmlFileReference": { "uri": "/reports/my-report"} },"dataSource": {"dataSourceReference": { "uri": "/datasources/Reporting_DB"}}}' \
--user jasperadmin:jasperadmin

Create jobs and execute them in jenkins using REST

I am trying to create a WCF REST client that will communicate to Jenkins and create a job from an XML file and then build the job. My understanding is that you can do that with Jenkins.
Can some one please provide some commands that you can type on a browser's address bar to create and build jobs? ie: http:localhost/jenkins/createItem?name=TESTJOB something along those lines.
Usually, when parsing through the documentation, it can take one or two days. It is helpful to be able to access code or curl commands to get you up and running in one hour. That is my objective with a lot of third party software.
See the post at http://scottizu.wordpress.com/2014/04/30/getting-started-with-the-jenkins-api/ which lists several of the curl commands. You will have to replace my.jenkins.com (ie JENKINS_HOST) with the your own url.
To create a job, for instance, try:
curl -X POST -H "Content-Type:application/xml" -d "<project><builders/><publishers/><buildWrappers/></project>" "http://JENKINS_HOST/createItem?name=AA_TEST_JOB2"
This uses a generic config. You can also download a config from a manually created job and then use that as a template.
curl "http://JENKINS_HOST/job/MY_JOB_NAME/config.xml" > config.xml
curl -X POST -H "Content-Type:application/xml" -d #config.xml "http://JENKINS_HOST/createItem?name=AA_TEST_JOB3"
To execute the job (and set string parameters), use:
curl "http://JENKINS_HOST/job/MY_JOB_NAME/build"
curl "http://JENKINS_HOST/job/MY_JOB_NAME/buildWithParameters?PARAMETER0=VALUE0&PARAMETER1=VALUE1"
See the Jenkins API Wiki page (including the comments at the end). You can fill in the gaps using the documentation provided by Jenkins itself; for example, http://JENKINS_HOST/api will give you the URL for creating a job and http://JENKINS_HOST/job/JOBNAME/api will give you the URL to trigger a build.
I highly recommend avoiding the custom creation of job configuration XML files and looking at something like the Job DSL plugin instead. This gives you a nice Groovy-based DSL to create jobs programmatically - much more concise and less error-prone.
Thanks to a GIST - https://gist.github.com/stuart-warren/7786892
Check if job exists
curl -XGET 'http://jenkins/checkJobName?value=yourJobFolderName' --user user.name:YourAPIToken
With folder plugin
curl -s -XPOST 'http://jenkins/job/FolderName/createItem?name=yourJobName' --data-binary #config.xml -H "Content-Type:text/xml" --user user.name:YourAPIToken
Without folder plugin
curl -s -XPOST 'http://jenkins/createItem?name=yourJobName' --data-binary #config.xml -H "Content-Type:text/xml" --user user.name:YourAPIToken
Create folder
curl -XPOST 'http://jenkins/createItem?name=FolderName&mode=com.cloudbees.hudson.plugins.folder.Folder&from=&json=%7B%22name%22%3A%22FolderName%22%2C%22mode%22%3A%22com.cloudbees.hudson.plugins.folder.Folder%22%2C%22from%22%3A%22%22%2C%22Submit%22%3A%22OK%22%7D&Submit=OK' --user user.name:YourAPIToken -H "Content-Type:application/x-www-form-urlencoded"
If you want to create a job into a view given the view exists.
curl -X POST -H "Content-Type:application/xml" -d #build.xml "http://jenkins_host/view/viewName/createItem?name=itemName"
the build.xml filetemplate could be found in the root directory of a job's workspace
if you want to create a view:
curl -X POST -H "Content-Type:application/xml" -d #view.xml "http://jenkins_host/createView?name=viewName"
the content of the file view.xml could be:
<?xml version="1.0" encoding="UTF-8"?>
<hudson.model.ListView>
<name>viewName</name>
<filterExecutors>false</filterExecutors>
<filterQueue>false</filterQueue>
<properties class="hudson.model.View$PropertyList"/>
<jobNames>
<comparator class="hudson.util.CaseInsensitiveComparator"/>
</jobNames>
<jobFilters/>
<columns>
<hudson.views.StatusColumn/>
<hudson.views.WeatherColumn/>
<hudson.views.JobColumn/>
<hudson.views.LastSuccessColumn/>
<hudson.views.LastFailureColumn/>
<hudson.views.LastDurationColumn/>
<hudson.views.BuildButtonColumn/>
</columns>
</hudson.model.ListView>
and to check if a view exists:
curl -X POST -H "Content-Type:application/xml" "http://jenkins_host/checkViewName?value=viewName"
to check if a job exists:
curl -X POST -H "Content-Type:application/xml" "http://jenkins_host/checkJobName?value=jobName"
To create a job:
curl -X POST -H "Content-Type:application/xml" -d "<project><builders/><publishers/><buildWrappers/></project>" -u username: API_Token http://JENKINS_HOST/createItem?name=AA_TEST_JOB2
To build a job:
curl -X POST -u username:API_TOKEN http://JENKINS_HOST/job/MY_JOB_NAME/build
In case you need to make the same HTTP calls using the Python requests library, instead of CURL...
Download a job config:
import requests
auth = ("username", "api_token")
url = "http://" + JENKINS_HOST + "/job/" + JOB_NAME + "/config.xml"
response = requests.get(url, auth=auth)
open('config.xml', 'wt').write(response.text)
Create a new job using same config:
url = "http://" + JENKINS_HOST + "/createItem?name=" + NEW_JOB_NAME
headers = {'content-type': 'text/xml'}
data = response.text
response = requests.post(url, auth=auth, headers=headers, data=data)
Omit auth parameter when not needed.