Skip to contents

This function creates a new file or updates an existing file in a GitHub repository. For updating existing files, the SHA of the current file must be provided.

Usage

file_update(
  repo,
  path,
  content,
  message,
  branch = NULL,
  sha = NULL,
  quiet = FALSE
)

Arguments

repo

Character string specifying the full name of the repository (format: "owner/repo")

path

Character string specifying the path to the file within the repository

content

Character string with the new content of the file

message

Character string with the commit message

branch

Character string specifying the branch name. Default is NULL (uses default branch).

sha

Character string with the blob SHA of the file being replaced. Required for updating existing files; omit for creating new files. Default is NULL.

quiet

Logical; if TRUE, suppresses progress and status messages. Default is FALSE.

Value

When successful, returns a list containing the GitHub API response with details about the commit, including:

content

Information about the updated file

commit

Details about the created commit

When the operation fails (e.g., permission issues, invalid SHA), returns NULL.

Examples

if (FALSE) { # interactive()
# Create a new file
result <- file_update(
  repo = "username/repository", 
  path = "path/to/new_file.R", 
  content = "# New R script\n\nprint('Hello world')", 
  message = "Add new script file"
)
# Check if operation was successful
if (!is.null(result)) {
  # Access commit information
  commit_sha <- result$commit$sha
}

# Update an existing file (requires SHA)
file_info <- file_content("username/repository", "path/to/existing_file.R")
if (!is.null(file_info)) {
  result <- file_update(
    repo = "username/repository", 
    path = "path/to/existing_file.R", 
    content = "# Updated content\n\nprint('Hello updated world')", 
    message = "Update file content",
    sha = file_info$sha
  )
}
}