How to read response of a JIRA REST API - bash

Using the Atlassian documentation, I'm able to create a new JIRA ticket using curl command. I want to understand how to read the response of this command into a variable? Essentially i want to store the JIRA ticket ID into a variable that can be used in other scripts.
Reference Link: https://developer.atlassian.com/server/jira/platform/jira-rest-api-examples/
_SAMPLE_CODE_
curl -D- -u charlie:charlie -X POST --data '{
"fields": {
"project":{
"key": "TEST"
},
"summary": "REST ye merry gentlemen.",
"description": "Creating of an issue using project keys and issue type names using the REST API",
"issuetype": {
"name": "Bug"
}
}
}' -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue/
_SAMPLE_RESPONSE_
{
"id":"39000",
"key":"TEST-101",
"self":"http://localhost:8080/rest/api/2/issue/39000"
}
From the above log, I want to understand how can we fetch the "key" (i.e. JIRA number or JIRA ID) from response (essentially stdout) into a variable.

you can use jq command line tool to extract value from json:
#!/bin/bash
key=$(curl -s -u charlie:charlie -X POST --data '{
"fields": {
"project":{
"key": "TEST"
},
"summary": "REST ye merry gentlemen.",
"description": "Creating of an issue using project keys and issue type names using the REST API",
"issuetype": {
"name": "Bug"
}
}
}' -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue/|jq -r .key)
echo "key: $key"
jq command is not installed by default on linux systems.
you will need to install it manually:
sudo apt install jq
sudo yum install jq

Related

Bash getting escaped variable key value with script

I want to use jq to get value's out of a the Traefik API service. The output of the service looks like this:
{
"loadBalancer": {
"servers": [
{
"url": "http://172.18.0.19:3000"
},
{
"url": "http://172.18.0.17:3000"
},
{
"url": "http://172.20.0.3:3000"
}
],
"healthCheck": {
"path": "/health",
"interval": "10s",
"timeout": "10s",
"followRedirects": true
},
"passHostHeader": true
},
"status": "enabled",
"serverStatus": {
"http://172.18.0.17:3000": "UP",
"http://172.18.0.19:3000": "UP",
"http://172.20.0.3:3000": "DOWN"
},
"provider": "docker",
"type": "loadbalancer"
}
I want to get the value of the serverStatus dynamically. First assigning a variable with the correct IP address from docker inspect. I'm using curl with jq to check if the container is being serviced.
The problem is when getting it with a variable I cannot seem to escape the special characters in the url key. Nothing is returned.
This is the command I use is:
TRAEFIK_URL="http://someurl.com/"
TRAEFIK_URL="'Authorization: Basic hashkey"
NEW_IPADDRESS=$(docker container inspect some_container_name | jq -r .[].NetworkSettings.Networks.proxy.IPAddress)
NEW_ENV_URL="http://${NEW_IPADDRESS}:3000"
curl --location --request GET "$TRAEFIK_URL" --header "$TRAEFIK_AUTH_HEADER" | jq -r '.serverStatus["${NEW_ENV_URL}"]'
I've tried using this command and some other obvious options but all didn't work.
jq -r .serverStatus[$NEW_ENV_URL]
I do get the correct value when I'm not using a variable to get the status using:
jq -r '.serverStatus["http://172.18.0.17:3000"]'
Any help is welcome.
Use the option --arg to pass arguments to jq.
jq --arg url http://172.18.0.17:3000 -r '.serverStatus[$url]'
Always use this option to avoid security problems by injections.
One of the preferred ways to pass specific environment variables into jq is using the --arg command-line option; in your case:
jq -r --arg url "${NEW_ENV_URL}" '.serverStatus[$url]'

Gerrit Checks plugin - How to create/update Checker for a gerrit change with API

My Gerrit server installed Checks plugin and I also try to create many checkers for my repo.
I can get information about Checkers of a gerrit change by admin account (configured Administrate Checkers and password was generated for "HTTP Credentials" in settings menu) with curl command:
url="http://my_gerrit_ip:8081"
curl -k -X GET \
-u admin:Jy7xK+oJ2tA2yzc+jG5n7yttsFUNmXDNKEDh+PoygQ \
--header "Content-Type: application/json; charset=UTF-8" \
--data gerrit-field-content.json \
$url/changes/66/revisions/1/checks/
The output looks like:
[
{
"repository": "test2",
"change_number": 66,
"patch_set_id": 1,
"checker_uuid": "a:a",
"state": "NOT_STARTED",
"created": "2020-10-28 02:56:55.000000000",
"updated": "2020-10-28 02:56:55.000000000",
"checker_name": "test2",
"checker_status": "ENABLED",
"blocking": [
"STATE_NOT_PASSING"
],
"submit_impact": {
"required": true
},
"checker_description": "test2"
},
{
"repository": "test2",
"change_number": 66,
"patch_set_id": 1,
"checker_uuid": "b:b",
"state": "NOT_STARTED",
"created": "2020-10-28 02:56:55.000000000",
"updated": "2020-10-28 02:56:55.000000000",
"checker_name": "test2_2",
"checker_status": "ENABLED",
"blocking": [],
"submit_impact": {},
"checker_description": "test2_2"
}
]
But now, i want to update the state of a Checker of my gerrit change via curl command too but it throw message "Authentication required".
I tried with:
curl -k -X POST \
-u admin:Jy7xK+oJ2tA2yzc+jG5n7yttsFUNmXDNKEDh+PoygQ \
--header "Content-Type: application/json; charset=UTF-8" \
--data gerrit-field-content.json \
$url/changes/66/revisions/1/checks/
The gerrit-field-content.json content:
{
"checker_uuid": "a:a",
"state": "SUCCESSFUL",
"url": "",
"started": ""
}
How can I update state of a checker via api? Or are there any better way to do this?
Result of checker state maybe look like a sample in this one https://gerrit-review.googlesource.com/c/gerrit/+/285580
Note: the it's not able update checker when i leave password for admin empty (curl -u admin:) because my Gerrit server is not require password for logging in.
Afraid I am just poking around myself, but in the zuul change it says:
* Access control for the `checks` API in Gerrit depends on a single
global administrative permission, ``administrateCheckers``. This is
required in order to use the `checks` API and can not be restricted
by project. This means that any system using the `checks` API can
interfere with any other.
Hope that is helpful, as I deploy this I'll update if I gain any other useful experience

Elasticsearch 7.7 how to read several documents, not fetch all the documents

I am using ElasticSearch 7.7 in CentOS 8 box. I could creat index, type by REST format by command curl. For example, I could use
curl -X PUT "localhost:9200/testindex2"
curl -H "Content-Type: application/json" -XPOST "http://localhost:9200/testindex2/man/1/" -d '{ "name" : "shiny2", "age": 28}'
curl -XGET "localhost:9200/testindex2/man/1/"
curl -XGET "localhost:9200/testindex2/man/_search?pretty"
But if I have inserted many documents, how could I do query by REST command line using command curl to find particular age = 28's documents?
curl -XGET "localhost:9200/testindex2/_search?pretty&q=age:28"
that is the simplest way to query.
more option and documentation:
https://www.elastic.co/guide/en/elasticsearch/reference/7.8/search-search.html
also you can use Match or Term query with JSON body format.
curl -XGET 'localhost:9200/testindex2/_search?pretty' -d '
{
"query": {
"term": {
"age": {
"value": "28"
}
}
}
}'
more documentation:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html

Trigger build with parameters

Following a TC upgrade to 2018
My previous script of triggering a TC build with parameters is not working
The script we were using uses the following api:
https://[server]/httpAuth/action.html?add2Queue=[build name]&name=[param name]&value=[param value]
I'm trying to migrate to restApi
from (https://confluence.jetbrains.com/display/TCD18/REST+API#RESTAPI-QueuedBuilds):
I have tried
https://[server]/app/rest/buildQueue?locator=buildType:[build name],[param name]:[param value]
Currently I have 2 issues:
I get a build triggered successfully - but it has not been triggered
Documentation was not clear, how to I trigger the build with parameters ?
Can you please advise on how to trigger the build successfully with parameters (also could be more than 1)
Firstly you right TeamCity documentation is not clear. Respect to this link;
For triggering a build you must make POST request to this url and send buildType id through body.
http://localhost:8111/httpAuth/app/rest/buildQueue
Also you can pass configuration parameter into body.
XML body for trigger build with parameters:
<build><buildType id="YourBuildTypeId"/>
<properties><property name="PARAM1" value="VALUE1"/></properties>
</build>
JSON body for trigger build with parameters:
{
"buildType": {
"id": "YourBuildTypeId"
},
"properties": {
"property": [
{
"name": "PARAM1",
"value": "VALUE1"
},
{
"name": "PARAM2",
"value": "VALUE2"
}
]
}
}
You can use below curl script.
curl -X POST \
http://localhost:8111/httpAuth/app/rest/buildQueue \
-H 'Accept: application/json' \
-H 'Content-Type: application/xml' \
-d '<build><buildType id="YourBuildTypeId"/>
<properties><property name="PARAM1" value="VALUE1"/></properties>
</build>'

Bash Loop Through URLs Issue

I'm using cURL request to google safe browsing API to check for any of my requested site is malicious or not. When i'm issuing the following command
$curl -H "Content-Type: application/json" -X POST -d ' { "client": { "clientId": "Test", "clientVersion": "1.0.0" }, "threatInfo": { "threatTypes": ["MALWARE", "SOCIAL_ENGINEERING","THREAT_TYPE_UNSPECIFIED","UNWANTED_SOFTWARE","POTENTIALLY_HARMFUL_APPLICATION"], "platformTypes": ["WINDOWS","PLATFORM_TYPE_UNSPECIFIED","ANY_PLATFORM"], "threatEntryTypes": ["URL"], "threatEntries": [ {"url":"hxxp://bookmyroom.pk/assets/timepicker/f.exe"} ] } }' https://safebrowsing.googleapis.com/v4/threatMatches:find?key=AIzaSyD1IMgjaHEza6e9m_jwtjBgPmJX0IMKKIs
Caution: I have "xx"ed instead "tt" for the http string. This site might be a potential malicious site.So do not open it using browser.
I'm getting JSON response as
{
"matches": [
{
"threatType": "UNWANTED_SOFTWARE",
"platformType": "ANY_PLATFORM",
"threat": {
"url": "hxxp://bookmyroom.pk/assets/timepicker/f.exe"
},
"cacheDuration": "300.000s",
"threatEntryType": "URL"
}
]
}
Caution: I have "xx"ed instead "tt" for the http string. This site might be a potential malicious site.So do not open it using browser.
When i'm doing the same in loop
#check.sh
for i in $(cat malsite.txt); do
content="$(curl -H "Content-Type: application/json" -X POST -d ' { "client": { "clientId": "Test", "clientVersion": "1.0.0" }, "threatInfo": { "threatTypes": ["MALWARE", "SOCIAL_ENGINEERING","THREAT_TYPE_UNSPECIFIED","UNWANTED_SOFTWARE","POTENTIALLY_HARMFUL_APPLICATION"], "platformTypes": ["WINDOWS","PLATFORM_TYPE_UNSPECIFIED","ANY_PLATFORM"], "threatEntryTypes": ["URL"], "threatEntries": [ {"url":"$i"} ] } }' https://safebrowsing.googleapis.com/v4/threatMatches:find?key=AIzaSyD1IMgjaHEza6e9m_jwtjBgPmJX0IMKKIs)"
echo "$i"
echo "$content"
done
#malsite.txt
"hxxp://bookmyroom.pk/assets/timepicker/f.exe"
Caution: I have "xx"ed instead "tt"ing for the http string.This site might be a potential malicious site.So do not open it using browser.
I'm not getting any results. It just returns empty result.
#Result of /.check.sh :
{}
Not sure , Where i'm making mistakes. Any thoughts ?

Resources