如何使用REST API将文件上传到GitHub仓库。

huangapple go评论65阅读模式
英文:

How to upload a file to a GitHub repos using REST API

问题

Here is the translated code for submitting a file to GitHub and creating a pull request:

Submitting a file to GitHub:

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

    // Convert to HTTP Form format as used by GitHub API
    final String messagePath = "/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("path", messagePath); // Use "path" instead of messagePath

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

    final WebClient wc = WebClient.create(vertx, options);
    final String target = "/repos/myuser/myrepo/contents" + messagePath; // Adjust the target URL

    wc.put(443, "api.github.com", target).ssl(true)
            .putHeader("Authorization", "Bearer " + accessToken)
            .sendForm(form)
            .onSuccess(resp -> System.out.printf("Posted to %s%n", messagePath))
            .onFailure(err -> System.err.println(err.getMessage()));
}

Creating a pull request on GitHub:

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

    // Compose pull request data
    final JsonObject base = new JsonObject()
            .put("ref", "master")
            .put("repo", new JsonObject().put("full_name", "myuser/myrepo"));

    final JsonObject head = new JsonObject()
            .put("ref", "comments-new");

    // Final assembly
    final JsonObject body = new JsonObject();
    body.put("title", "Comment from John Doe");
    body.put("base", base);
    body.put("head", head);

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

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

    final String target = "/repos/myuser/myrepo/pulls";

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

Please make sure to adjust the target URLs according to GitHub's API documentation, and note that the GitHub API uses a different structure for file uploads and pull request creation compared to Bitbucket.

英文:

I have Java code that submits a file to Bitbucket and creates a pull request. I need to port it to GitHub. In Bitbucket the API takes a form post for a file submission:

    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()));

    }

Similar the submission of a 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()));
    }

I looked at the github CLI and the GitHub REST API which does address creating a pull request, but I couldn't find how to upload a file.

Where could I find the matching documentation and/or a sample?s

答案1

得分: 1

上传文件到代码库,请参阅 https://docs.github.com/en/rest/repos/contents?apiVersion=2022-11-28#create-or-update-file-contents

英文:

To upload a file to a repository see https://docs.github.com/en/rest/repos/contents?apiVersion=2022-11-28#create-or-update-file-contents

huangapple
  • 本文由 发表于 2023年6月26日 16:11:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定