Last active
December 24, 2015 22:59
-
-
Save pseudomuto/6876762 to your computer and use it in GitHub Desktop.
Blog Code: Pushing Files to GitHub Programmatically
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
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); | |
} | |
} | |
} |
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
// 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