Using GitHub's REST API to update a release with a multi-line description - release

I am using JSON to update the description of a GitHub release with a list of the commits for that release.
The problem is that if the description has a newline the command fails with:
Stdout: {
Stdout: "message": "Problems parsing JSON",
Stdout: "documentation_url": "https://developer.github.com/v3"
Stdout: }
The only way I got it to work is to replace the newlines with a space:
def API_JSON = sprintf ('{"body": "%s"}', description.replace('\n',' '));
However, without the newlines the description is difficult to read. Is there a way to set the description for a GitHub release using the API/JSON and keep newlines?

Figured out the answer, replace with a line break:
def API_JSON = sprintf ('{"body": "%s"}', description.replace('\n','<br />'));
Hope this helps someone else :)

You can use \r\n as well, only \n is not supported
curl -i -X 'POST' https://api.github.com/repos/:owner/:repo/releases/?access_token=:token' -d
{
"body": "My\r\nName"
}

Related

parsing the JSON string with only mandatory POSIX tools [duplicate]

This question already has answers here:
Parsing JSON with Unix tools
(45 answers)
Closed 24 days ago.
I have json string like this
{"state":{"stateId":0,"nextPollingTime":null,"updateState":null},"config":{"privateIPs":null,"64Bit":false,"silent":false,"iid":14,"selfp":null,"sevlfp":null,"av":14,"aid":null,"aty":2,"sev":0,"seci":0,"sti":false,"scto":60000,"sci":5000,"stkd":5000,"sud":5000,"mpfa":3}}
I need to get "state" and "config" keys content to a shell custom variable or to a file.
tried with a command:
$RESPONSE is the API response, the above json string
echo $RESPONSE | sed 's/{"$state":"*\([0-9a-zA-Z]*\)"*,*.*}/\1/'
this prints nothing but suppose to get this output:
{"stateId":0,"nextPollingTime":null,"updateState":null}
Tried with saving the response to tmp file and executed this command
cat /tmp/a.json | grep -o -e "{"state":.*}"
this also print empty string but expected result:
{"stateId":0,"nextPollingTime":null,"updateState":null}
Am new to shell script and trying with various options available in the internet, please help me to write the command for the same.
This uses any sed:
$ sed 's/.*"state":\({[^}]*}\).*/\1/' file
{"stateId":0,"nextPollingTime":null,"updateState":null}
That will work for your posted input but fail given other json input (e.g. with "state" as a value or with } inside a value), just like any other solution that uses mandatory POSIX tools as none of them have a JSON parser (unless you write one with awk, but I expect that's more work than you or anyone here would put into this).

Problem running bitbucket rest api command using python

I,am building a script to update files on Bitbucket using the rest api.
My problems are:
Running the command using subprocess lib and running the command directly on the command line gives two different results.
If I run the command using the command line, when I inspect my commits on the Bit bucket app I can see a Commit message and a Issue.
If I run the command using the help of the subprocess lib I don't have a commit message and a Issue in the end. The commit message sets itself by default to "edited by bitbucket" and the issue is null.
This is the command:
curl -X PUT -u user:pass -F content=#conanfile_3_f62hu.py -F 'message= test 4' -F branch=develop -F sourceCommitId={} bitbucket_URL".format(latest_commit)
The other problem is that I need to pass a file to the content in order to update it.
If I pass it like above it works. The problem is that I am generating the file content as raw string and creating a temporary file with that content.
And when I pass the file as a variable, it does not get the content of the file.
My code:
content = b'some content'
current_dir = os.getcwd()
temp_file=tempfile.NamedTemporaryFile(suffix=".py",prefix="conanfile", dir=current_dir)
temp_file.name = temp_file.name.split("\\")
temp_file.name = [x for x in temp_file.name if x.startswith("conanfile")][0]
temp_file.name = "#" + temp_file.name
temp_file.write(content)
temp_file.seek(0)
update_file_url = "curl -X PUT -u user:pass -F content={} -F 'message=test 4' -F branch=develop -F sourceCommitId={} bitbucket_url".format(temp_file.name, latest_commit)
subprocess.run(update_file_url)
Basically I'am passing the file like before, just passing the name to the content, but it does not work.
If I print the command everything looks good, so I don't know why the commit message does not get set and the file content as well.
Updated:
I was able to pass the file, My mistake was that I was not passing it like temp_file.name.
But I could not solve the problem of the message.
What I found is that the message will only take the first word. If there is a space and one more word after, it will ignore it.
The space is causing some problem.
I found the solution, if someone found himself with this problem we need to use a \ before the message= .
Example: '-F message=\" Updated with latest dependencies"'

npm command doesn't update json

This command works fine:
json -I -f ./src/environments/build.json -e 'this.patch++'
I'm trying to create a custom build NPM command in my package.json file that runs this command before the actual build, but first I just tried to run the json command, just to see if it's working, but it doesn't :/
package.json
{
...
"scripts": {
...
"svrge-build-dev": "json -I -f ./src/environments/build.json -e 'this.patch++'",
...
}
then I get this output (which is exactly the same as when I run the JSON code by itself) which means that the command is definitely running
> web-client#1.0.0 svrge-build-dev D:\repos\test\web-client
> json -I -f ./src/environments/build.json -e 'this.patch++
json: updated "./src/environments/build.json" in-place //<- this is exactly the same'
However, the build.json file is not being updated
No errors in the terminal
any idea how I can get it to work? I can't seem to find anything regarding that.
I would appreciate any help, been scratching my head for hours
Tom
Re-stating what I said in the comment: try to replace the single quotes with double quotes:
{
// ...
"scripts": {
// ...
"svrge-build-dev": "json -I -f ./src/environments/build.json -e \"this.patch++\"",
// ...
}
What triggered that thought was this line in the output:
> json -I -f ./src/environments/build.json -e 'this.patch++
After doing some digging, I think it depends on the OS and/or command line interpreter. It shows the command that was run, but not with the finishing single quote.
After some searching, it appears that is indeed a bug: see this issue and this issue. You might want to give the maintainers a heads up about it: the last issue I've linked to, makes it seem like it's a Windows issue.

How do I parse output from result of CURL command?

I have a Jenkins console output that looks like this:
Started by remote host 10.16.17.13
Building remotely on ep9infrajen201 (ep9) in workspace d:\Jenkins\workspace\Tools\Provision
[AWS-NetProvision] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\Users\user\AppData\Local\Temp\jenkins12345.ps1'"
Request network range: 10.1.0.0/13
{
    "networks":  [
                     "10.1.0.0/24"
                 ]
}
Finished: SUCCESS
I get this from a curl command that I run. to check the JENKINS_JOB_URL/lastBuild/consoleText
My question is, for the sake of some other automation I am doing, how do I get just "10.1.0.0/24" so I can assign it to a shell variable using LINUX tools?
Thank you
Since you listed jq among the tags of your duplicate question, I'll assume you have jq installed. You have to clean up your output to get JSON first, then get to the part of JSON you need. awk does the former, jq the latter.
.... | awk '/^{$/{p=1}{if(p){print}}/^}$/{p=0}' | jq -r .networks[0]
The AWK script looks for { on its own on a line to turn on a flag p; prints the current line if the flag is set; and switches off the flag when it encounters } all by itself.
EDIT: Since this output was generated on a DOS machine, it has DOS line endings (\r\n). To convert those before awk, additionally pipe through dos2unix.

Sending a post json with curl get error

Im trying to create a bash script to make github pull requests here its my code:
Gist Code
I get this error:
curl: (6) Could not resolve host: on
curl: (3) [globbing] unmatched close brace/bracket in column 63
{
"message": "Problems parsing JSON",
"documentation_url": "https://developer.github.com/v3"
}
Please help
More/proper quoting:
body=$(printf '{"title":"%s","body":"%s","head":"clamour:%s","base":"%s"}' "$TITLE" "$DESCRIPTION" "$TARGET" "$SOURCE")
curl -H "$auth" -d "$body" "https://api.github.com/repos/clamour/$PROJECT/pulls"
All user-supplied variables must be quoted (unless you know exactly why you want to leave then unquoted).
Enclosing a variable name in ${braces} is not the same as "$quoting".
I find using printf tends to be more clear than mixing double and single quotes with variable interpolation.
Get out of the habit of using ALLCAPS variable names: one day you'll use PATH=... and then wonder why your script is broken
you are using a lot of variables
try to do it like this
BODY="{\"title\":\"$TITLE\",\"body\":\"$DESCRIPTION\",\"head\":\"clamour\":\"$TARGET\",\"base\":\"$SOURCE\"}"
"https://api.github.com/repos/clamour/$PROJECT/pulls"
read also this Difference between single and double quotes in Bash

Resources