使用 python 运行 bitbucket rest api 命令时出现问题

发布于 2025-01-13 12:07:06 字数 1335 浏览 2 评论 0原文

我正在构建一个脚本来使用其余 api 更新 Bitbucket 上的文件。

我的问题是:

使用 subprocess lib 运行命令和直接在命令行上运行命令会产生两种不同的结果。 如果我使用命令行运行该命令,当我检查 Bit 存储桶应用程序上的提交时,我可以看到一条提交消息和一个问题。 如果我使用子进程库的帮助运行命令,我最终不会收到提交消息和问题。提交消息默认将自身设置为“由 bitbucket 编辑”,并且问题为空。

这是命令:

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)

另一个问题是我需要将文件传递给内容才能更新它。 如果我像上面那样通过它,它就会起作用。问题是我正在将文件内容生成为原始字符串,并使用该内容创建一个临时文件。 当我将文件作为变量传递时,它不会获取文件的内容。

我的代码:

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)

基本上我像以前一样传递文件,只是将名称传递给内容,但它不起作用。 如果我打印命令,一切看起来都很好,所以我不知道为什么提交消息和文件内容都没有设置。

更新: 我能够传递该文件,我的错误是我没有像 temp_file.name 那样传递它。 但我无法解决消息的问题。 我发现该消息只会采用第一个单词。如果后面有一个空格和一个单词,它会忽略它。 该空间引起了一些问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢望 2025-01-20 12:07:06

我找到了解决方案,如果有人发现自己遇到这个问题,我们需要在 message= 之前使用 \ 。

示例:'-F message=\"使用最新依赖项更新"'

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文