Create or replace a repository's README.md with a GitHub commit on the branch you choose.
dotnet add package Soenneker.GitHub.Repositories.Readmes{
"GH": {
"Token": "github-token"
}
}The token must have repository contents write access.
services.AddGitHubRepositoriesReadmesUtilAsSingleton();Use AddGitHubRepositoriesReadmesUtilAsScoped() for a scoped consumer.
public sealed class RepositoryReadmeService
{
private readonly IGitHubRepositoriesReadmesUtil _readmes;
public RepositoryReadmeService(IGitHubRepositoriesReadmesUtil readmes)
{
_readmes = readmes;
}
public ValueTask Publish(
string markdown,
CancellationToken cancellationToken = default)
{
return _readmes.Upsert(
owner: "soenneker",
name: "example-repository",
commitMessage: "Update README",
content: markdown,
branch: "main",
cancellationToken);
}
}Createfails whenREADME.mdalready exists.Updatereads the file's current SHA from the requested branch, then replaces its contents.Upsertupdates the file when present and creates it only when GitHub reports it missing.
Each successful call creates a commit. The supplied content replaces the entire file; it is not merged with the existing Markdown. The branch must already exist.
