GitHub API, posting new comment using a variable - bash

I have a file with a bunch of output from some performance tests. It looks similar to the following:
index | master | performance-fix | change %
--- | --- | --- | ---
load | 26212.8 | 28223.6 | 7.67%
type | 67.5 | 75.41 | 11.72%
minType | 56.91 | 59.6 | 4.73%
maxInserterSearch | 185.45 | 283.25 | 52.74%
minInserterHover | 25.97 | 27.55 | 6.08%
maxInserterHover | 44.47 | 44.7 | 0.52%
I am trying to submit a new comment on a Github issue using that table data. Standard text works fine, but when I try and pass the table along I'm getting the error:
{
"message": "Problems parsing JSON",
"documentation_url": "https://docs.github.com/rest/reference/issues#update-an-issue-comment"
}
My cURL request is as follows:
NEW_COMMENT=$(curl -sS \
-X PATCH \
-u $GH_LOGIN:$GH_AUTH_TOKEN \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/issues/comments/$COMMENT_ID" \
-d '{"body": "Results: <br />'"$TEST_RESULTS"'"}')
I have also tried creating the {"body": ...} using jq, and using the --data-urlencode flag. Both return the same "Problems parsing JSON" error.

It looks like $TEST_RESULTS contains characters that make the JSON not what you think it is, like including quotation marks and newlines
Maybe escaping the JSON output like this will help
escaped="$(printf '%s' "$TEST_RESULTS" | jq -Rs '.')"
... \
-d '{"body": "Results: <br />'"$escaped"'"}')

Related

Using jq and outputting specific columns with formatting

Can anyone help me to understand how I can print countryCode followed by connectionName and load with a percentage symbol all on one line nicely formatted - all using jq - not using sed, column or any other unix external command. I cannot seem print anything other than the one column
curl --silent "https://api.surfshark.com/v3/server/clusters" | jq -r -c "map(select(.countryCode == "US" and .load <= "99")) | sort_by(.load) | limit(20;.[]) | [.countryCode, .connectionName, .load] | (.[1])
Is this what you wanted ?
curl --silent "https://api.surfshark.com/v3/server/clusters" |
jq -r -c 'map(select(.countryCode == "US" and .load <= 99)) |
sort_by(.load) |
limit(20;.[]) |
"\(.countryCode) \(.connectionName) \(.load)%"'

Using an "if" statement with curl

I am using below curl command to restart the Connectors/tasks. I have scheduled this curl command in Crontab. The purpose of this curl command is to restart the Connectors/Tasks, if their status is "Failure". I would like to incorporate "if" statement in this curl command, so that it attempts only 3 times in order to restart the Connectors/Tasks. After attempting 3 times, it has to stop the restarting the connectors/tasks and send an Email.
curl -s "http://localhost:8083/connectors?expand=status" | \
jq -c -M 'map({name: .status.name } + {tasks: .status.tasks}) | .[] | {task: ((.tasks[]) + {name: .name})} | select(.task.state=="FAILED") | {name: .task.name, task_id: .task.id|tostring} | ("/connectors/"+ .name + "/tasks/" + .task_id + "/restart")' | \
xargs -I{connector_and_task} curl -v -X POST "http://localhost:8083"\{connector_and_task\}
Could you please give me the solution

update values defined in specific section of config file using bash

I am looking to update one config file using bash.
Config file is having multiple section like
[SECTION1]
action.email.useNSSubject = 1
dispatch.earliest_time = 1578927600
dispatch.latest_time = 1579016736
search = | inputlookup KPI_MASTER_LIST.csv | search TYPE="MTE_GENERIC" \
| table ALERT Order\
| map maxsearches=21 search="| savedsearch "$$ALERT$$" host_token=$host_token$ SERVICE_EARLIEST_TIME=$SERVICE_EARLIEST_TIME$ time_token.earliest=$time_token.earliest$ time_token.latest=$time_token.latest$ | appendcols [ | makeresults | eval Order="$$Order$$" | fillnull count ] | table ALERT count Order "\
| sort Order \
[SECTION2]
action.email.useNSSubject = 1
alert.track = 0
dispatch.earliest_time = 153437300
dispatch.latest_time = 1549013433
display.general.timeRangePicker.show = 0
search = | inputlookup KPI_MASTER_LIST.csv | search TYPE="MTE_GENERIC" \
| table ALERT Order\
| map maxsearches=21 search="| savedsearch "$$ALERT$$" host_token=$host_token$ SERVICE_EARLIEST_TIME=$SERVICE_EARLIEST_TIME$ time_token.earliest=$time_token.earliest$ time_token.latest=$time_token.latest$ | appendcols [ | makeresults | eval Order="$$Order$$" | fillnull count ] | table ALERT count Order "\
| sort Order \
I am looking to update value of "dispatch.earliest_time" and "dispatch.latest_time" in specific section only(not all occurrence of this file)
You could use an address range specifying your delimiter
sed '/\[SECTION_NAME\]/,/^\[/ s/dispatch.earliest_time/new_value_here/'
You can find thorough documentation about sed here

Merge multiple jq invocations to sort and limit the content of a stream of objects

I have a json stream of updates for products, and I'm trying to get the last X versions sorted by version (they are sorted by release date currently).
It looks like jq can't sort a stream of objects directly, sort_by only works on arrays, and I couldn't find a way to collect a stream into an array that doesn't involve piping the output of jq -c to jq -s.
My current solution:
< xx \
jq -c '.[] | select(.platform | contains("Unix"))' \
| jq -cs 'sort_by(.version) | reverse | .[]' \
| head -5 \
| jq -C . \
| less
I expected to be able to use
jq '.[] | select(...) | sort_by(.version) | limit(5) | reverse'
but I couldn't find a thing that limits and sort_by doesn't work on non arrays.
I am testing this on atlassian's json for releases: https://my.atlassian.com/download/feeds/archived/bamboo.json
In jq you can always contain the results to an array using the [..] that put the results to an array for the subsequent functions to operate on. Your given requirement could be simply done as
jq '[.[] | select(.platform | contains("Unix"))] | sort_by(.version) | limit(5;.[])'
See it working on jq playground tested on v1.6
and with added reverse() function, introduce an another level of array nesting. Use reverse[] to dump the objects alone
jq '[[.[] | select(.platform | contains("Unix"))] | sort_by(.version) | limit(5;.[]) ] | reverse'

An issue occurs when i attempt to run batch this file. is it a syntax error?

i dont see why it isnt working.
if you could help me with this it would be greatly appreciated.
i run the file and it just exits out, as if the code is missing.
cls
echo //========================================//
echo \\ --- --- ---------- ---------- \\
echo // | \ | | | | | | //
echo \\ | \ \ | | -- -- -- -- \\
echo // | |\ \| | | | | | //
echo \\ | | \ | | | | | \\
echo // |__| \____| |__| |__| //
echo \\========================================\\
echo press any key to begin
pause
You need to escape the pipes | with a carret (^|).
However you should store ascii art in a txt file and use type my_banner.txt in order to display it. This is easier ...

Resources