Created
August 28, 2023 22:33
-
-
Save zkamvar/56bb1910dcad4da6945c09354c45f357 to your computer and use it in GitHub Desktop.
fix a single file in a repo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library("gh") | |
library("base64enc") | |
# get a file from a repository | |
get_repo_file <- function(org = "carpentries", lesson = "sandpaper-docs", file = "CONTRIBUTING.md", ...) { | |
gh("GET /repos/{org}/{lesson}/contents/{file}", | |
.params = list( | |
org = org, | |
lesson = lesson, | |
file = file | |
), | |
... | |
) | |
} | |
# create a branch or force-update it if it exists | |
create_branch <- function(org = "carpentries", lesson = "sandpaper-docs", branch = "core/updates", ...) { | |
# https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28 | |
HEAD <- gh("GET /repos/{org}/{lesson}/commits", | |
.params = list( | |
org = org, | |
lesson = lesson | |
), | |
per_page = 1, page = 1, ...) | |
tryCatch({ | |
# https://docs.github.com/en/rest/git/refs?apiVersion=2022-11-28#get-a-reference | |
ref <- gh("GET /repos/{org}/{lesson}/git/refs/heads/{branch}", | |
.params = list( | |
org = org, | |
lesson = lesson, | |
branch = branch | |
)) | |
if (ref$object$sha == HEAD[[1]]$sha) { | |
message("branch exists and is up to date") | |
return(TRUE) | |
} | |
# https://docs.github.com/en/rest/git/refs?apiVersion=2022-11-28#update-a-reference | |
message("updating branch") | |
gh("PATCH /repos/{org}/{lesson}/git/refs/heads/{branch}", | |
.params = list( | |
org = org, | |
lesson = lesson, | |
branch = branch | |
), | |
sha = HEAD[[1]]$sha, | |
force = TRUE | |
) | |
}, | |
http_error_404 = function(e) { | |
# https://docs.github.com/en/rest/git/refs?apiVersion=2022-11-28#create-a-reference | |
message("creating branch") | |
gh("POST /repos/{org}/{lesson}/git/refs", | |
.params = list( | |
org = org, | |
lesson = lesson | |
), | |
ref = paste0("refs/heads/", branch), | |
sha = HEAD[[1]]$sha | |
) | |
}) | |
} | |
# base 64 to text | |
to_text <- function(b64) { | |
rawToChar(base64enc::base64decode(b64)) | |
} | |
# text to base 64 | |
to_b64 <- function(txt) { | |
base64enc::base64encode(charToRaw(txt)) | |
} | |
# update the contents of a file in a branch and create a pull request | |
update_repo_file <- function(contents = NULL, message = "updating CONTRIBUTING", org = "carpentries", lesson = "sandpaper-docs", file = "CONTRIBUTING.md", branch = "core/updates", body = "An update to the slack URL", ...) { | |
if (is.null(contents)) { | |
return(NULL) | |
} | |
# note: this is not working because I do not know how to calculate the sha for the modified content | |
gh("PUT /repos/{owner}/{repo}/contents/{path}", | |
.params = list( | |
owner = org, | |
repo = lesson, | |
path = file | |
), | |
branch = branch, | |
message = message, | |
content = contents$content, | |
sha = contents$sha, | |
... | |
) | |
gh("POST /repos/{owner}/{repo}/pulls", | |
.params = list( | |
owner = org, | |
repo = lesson | |
), | |
title = message, | |
head = paste0(org, ":", branch), | |
base = "main", | |
body = body, | |
... | |
) | |
} | |
if (FALSE) { | |
# manually run this to test | |
ctb <- get_repo_file("zkamvar", "r-ecology-lesson", "CONTRIBUTING.Rmd") | |
create_branch("zkamvar", "r-ecology-lesson") | |
ctb$content <- ctb$content |> | |
to_text() |> | |
stringr::str_replace_all("swc-slack-invite.herokuapp.com", "slack-invite.carpentries.org") |> | |
to_b64() | |
# this part still does not work | |
update_repo_file(ctb, "update slack invite", "zkamvar", "r-ecology-lesson", "CONTRIBUTING.Rmd") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment