Skip to content

Instantly share code, notes, and snippets.

@pseudomuto
Last active December 24, 2015 22:59
Show Gist options
  • Save pseudomuto/6876762 to your computer and use it in GitHub Desktop.
Save pseudomuto/6876762 to your computer and use it in GitHub Desktop.
Blog Code: Pushing Files to GitHub Programmatically
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GitHubPushLib
{
public sealed class ContentService
{
private ContentRepo _repo;
private string _authToken;
public ContentService(string authToken)
: this(authToken, new GitHubContentRepo())
{
}
public ContentService(string authToken, ContentRepo repo = null)
{
Guard.AgainstNullOrEmpty("authToken", authToken);
this._repo = repo;
this._authToken = authToken;
}
public File PushFile(File file, FileTarget target, string message)
{
Guard.AgainstNull("file", file);
Guard.AgainstNull("target", target);
Guard.AgainstNullOrEmpty("message", message);
var existingFile = this._repo.GetFile(this._authToken, target);
if (existingFile != null)
{
// set the hash to the existing one...
file.SHA = existingFile.SHA;
}
return existingFile == null ?
this._repo.CreateFile(this._authToken, file, target, message) :
this._repo.UpdateFile(this._authToken, file, target, message);
}
}
}
// get the api token from sonewhere...
var service = new ContentService(token);
var file = new DiskFile("Files/content_file.gif");
var target = new FileTarget(owner, repo, file.Name);
service.PushFile(file, target, "pushing file via GitHubPushLib");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment