英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论