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

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

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:

  1. public void storeMessageInGitHub(final Vertx vertx, final String message,
  2. final String accessToken) {
  3. // Convert to HTTP Form format as used by GitHub API
  4. final String messagePath = "/newStuff/randomFilename.json";
  5. final MultiMap form = MultiMap.caseInsensitiveMultiMap();
  6. form.set("author", "john.doe@unknown.com");
  7. form.set("message", "new Comment");
  8. form.set("branch", "comments-new");
  9. form.set("path", messagePath); // Use "path" instead of messagePath
  10. final WebClientOptions options = new WebClientOptions()
  11. .setUserAgent("CommentService 1.0.4")
  12. .setSsl(true)
  13. .setKeepAlive(true);
  14. final WebClient wc = WebClient.create(vertx, options);
  15. final String target = "/repos/myuser/myrepo/contents" + messagePath; // Adjust the target URL
  16. wc.put(443, "api.github.com", target).ssl(true)
  17. .putHeader("Authorization", "Bearer " + accessToken)
  18. .sendForm(form)
  19. .onSuccess(resp -> System.out.printf("Posted to %s%n", messagePath))
  20. .onFailure(err -> System.err.println(err.getMessage()));
  21. }

Creating a pull request on GitHub:

  1. public void createPullRequest(final Vertx vertx, final String message,
  2. final String accessToken) {
  3. // Compose pull request data
  4. final JsonObject base = new JsonObject()
  5. .put("ref", "master")
  6. .put("repo", new JsonObject().put("full_name", "myuser/myrepo"));
  7. final JsonObject head = new JsonObject()
  8. .put("ref", "comments-new");
  9. // Final assembly
  10. final JsonObject body = new JsonObject();
  11. body.put("title", "Comment from John Doe");
  12. body.put("base", base);
  13. body.put("head", head);
  14. final WebClientOptions options = new WebClientOptions()
  15. .setUserAgent("CommentService 1.0.4")
  16. .setSsl(true)
  17. .setKeepAlive(true);
  18. final WebClient wc = WebClient.create(vertx, options);
  19. final String target = "/repos/myuser/myrepo/pulls";
  20. wc.post(443, "api.github.com", target).ssl(true)
  21. .putHeader("Authorization", "Bearer " + accessToken)
  22. .sendJson(body)
  23. .onSuccess(res -> System.out.println("Pull request is on its way"))
  24. .onFailure(err -> System.err.println(err.getMessage()));
  25. }

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:

  1. public void storeMessageInBitbucket(final Vertx vertx, final String message,
  2. final String accessToken) {
  3. // Convert to HTTP Form format as used by Bitbucket API
  4. final String messagePath = "/src/newStuff/randomFilename.json";
  5. final MultiMap form = MultiMap.caseInsensitiveMultiMap();
  6. form.set("author", "john.doe@unknown.com");
  7. form.set("message", "new Comment");
  8. form.set("branch", "comments-new");
  9. form.set(messagePath, message);
  10. final WebClientOptions options = new WebClientOptions()
  11. .setUserAgent("CommentService 1.0.4")
  12. .setSsl(true)
  13. .setKeepAlive(true);
  14. final WebClient wc = WebClient.create(vertx, options);
  15. final String target = "/2.0/repositories/myuser/myrepo/src";
  16. wc.post(443, "api.bitbucket.org", target).ssl(true)
  17. .putHeader("Content-Type", "application/x-www-form-urlencoded")
  18. .putHeader("Authorization", "Bearer " + accessToken)
  19. .sendForm(form)
  20. .onSuccess(resp -> System.out.printf("Posted to %s%n", messagePath))
  21. .onFailure(err -> System.err.println(err.getMessage()));
  22. }

Similar the submission of a PR:

  1. public void createPullRequest(final Vertx vertx, final String message,
  2. final String accessToken) {
  3. // Compose pull request data
  4. final JsonObject source = new JsonObject()
  5. .put("branch", new JsonObject().put("name", "comments-new"))
  6. .put("repository", new JsonObject().put("full_name", "myuser/myrepo"));
  7. final JsonObject destination = new JsonObject()
  8. .put("branch", new JsonObject().put("name", "master"));
  9. // Final assembly
  10. final JsonObject body = new JsonObject();
  11. body.put("title", "Comment from John Doe");
  12. body.put("source", source);
  13. body.put("destination", destination);
  14. body.put("close_source_branch", true);
  15. final WebClientOptions options = new WebClientOptions()
  16. .setUserAgent("CommentService 1.0.4")
  17. .setSsl(true)
  18. .setKeepAlive(true);
  19. final WebClient wc = WebClient.create(vertx, options);
  20. final String target = "/2.0/repositories/myuser/myrepo/pullrequests/";
  21. wc.post(443, "api.bitbucket.org", target).ssl(true)
  22. .putHeader("Content-Type", "application/json")
  23. .putHeader("Authorization", "Bearer " + accessToken)
  24. .sendJson(body)
  25. .onSuccess(res -> System.out.println("Pull request is on its way"))
  26. .onFailure(err -> System.err.println(err.getMessage()));
  27. }

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:

确定