Search code examples
python-3.xbitbucket-api

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.


Solution

  • 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"'