You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用GitHub REST API上传文件并迁移Bitbucket Java代码至GitHub

从Bitbucket迁移至GitHub的文件提交与PR实现问题

我有一段向Bitbucket提交文件并创建Pull Request(PR)的Java代码,需要迁移到GitHub环境。在Bitbucket中,文件提交采用表单POST方式实现,相关代码如下:

public void storeMessageInBitbucket(final Vertx vertx, final String message,
        final String accessToken) {

    // Convert to HTTP Form format as used by Bitbucket API
    final String messagePath = "/src/newStuff/randomFilename.json";

    final MultiMap form = MultiMap.caseInsensitiveMultiMap();
    form.set("author", "john.doe@unknown.com");
    form.set("message", "new Comment");
    form.set("branch", "comments-new");
    form.set(messagePath, message);

    final WebClientOptions options = new WebClientOptions()
            .setUserAgent("CommentService 1.0.4")
            .setSsl(true)
            .setKeepAlive(true);

    final WebClient wc = WebClient.create(vertx, options);
    final String target = "/2.0/repositories/myuser/myrepo/src";
    wc.post(443, "api.bitbucket.org", target).ssl(true)
            .putHeader("Content-Type", "application/x-www-form-urlencoded")
            .putHeader("Authorization", "Bearer " + accessToken)
            .sendForm(form)
            .onSuccess(resp -> System.out.printf("Posted to %s%n", messagePath))
            .onFailure(err -> System.err.println(err.getMessage()));

}

创建PR的代码如下:

public void createPullRequest(final Vertx vertx, final String message,
        final String accessToken) {

    // Compose pull request data
    final JsonObject source = new JsonObject()
            .put("branch", new JsonObject().put("name", "comments-new"))
            .put("repository", new JsonObject().put("full_name", "myuser/myrepo"));

    final JsonObject destination = new JsonObject()
            .put("branch", new JsonObject().put("name", "master"));

    // Final assembly
    final JsonObject body = new JsonObject();
    body.put("title", "Comment from John Doe");
    body.put("source", source);
    body.put("destination", destination);
    body.put("close_source_branch", true);

    final WebClientOptions options = new WebClientOptions()
            .setUserAgent("CommentService 1.0.4")
            .setSsl(true)
            .setKeepAlive(true);

    final WebClient wc = WebClient.create(vertx, options);

    final String target = "/2.0/repositories/myuser/myrepo/pullrequests/";

    wc.post(443, "api.bitbucket.org", target).ssl(true)
            .putHeader("Content-Type", "application/json")
            .putHeader("Authorization", "Bearer " + accessToken)
            .sendJson(body)
            .onSuccess(res -> System.out.println("Pull request is on its way"))
            .onFailure(err -> System.err.println(err.getMessage()));
}

我已查阅GitHub CLI及GitHub REST API文档,了解到创建PR的接口,但未找到文件上传的相关文档及示例。请问在哪里可以找到对应的文档或示例?


回答

GitHub REST API中,文件上传(创建或更新仓库中的文件)对应的是创建内容接口,核心逻辑是通过PUT请求将文件内容以Base64编码的形式提交。

核心规则如下:

  • 接口路径:/repos/{owner}/{repo}/contents/{path}
  • 请求方式:PUT
  • 请求体必填字段:
    • message:提交说明文本
    • content:文件内容的Base64编码字符串
  • 可选字段:
    • branch:目标分支名(非默认分支时需指定)
    • committer/author:提交者的姓名、邮箱信息

适配Vertx环境的示例代码:

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public void storeMessageInGitHub(final Vertx vertx, final String message,
        final String accessToken) {
    final String filePath = "newStuff/randomFilename.json";
    // 将文件内容转为Base64编码
    String base64Content = Base64.getEncoder().encodeToString(message.getBytes(StandardCharsets.UTF_8));

    JsonObject body = new JsonObject()
            .put("message", "new Comment")
            .put("content", base64Content)
            .put("branch", "comments-new")
            .put("committer", new JsonObject()
                    .put("name", "John Doe")
                    .put("email", "john.doe@unknown.com"));

    WebClientOptions options = new WebClientOptions()
            .setUserAgent("CommentService 1.0.4")
            .setSsl(true)
            .setKeepAlive(true);

    WebClient wc = WebClient.create(vertx, options);
    String target = String.format("/repos/myuser/myrepo/contents/%s", filePath);

    wc.put(443, "api.github.com", target).ssl(true)
            .putHeader("Content-Type", "application/json")
            .putHeader("Authorization", "Bearer " + accessToken)
            .sendJson(body)
            .onSuccess(resp -> System.out.printf("Posted to %s%n", filePath))
            .onFailure(err -> System.err.println(err.getMessage()));
}

另外,创建PR的GitHub接口路径为/repos/{owner}/{repo}/pulls,请求体需包含title(PR标题)、head(源分支名)、base(目标分支名)等字段,逻辑与Bitbucket类似,只需调整请求结构即可。


内容的提问来源于stack exchange,提问作者stwissel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 19:22:52