public GitObject(GitContent entry) { this.name = entry.name; this.type = entry.type; this.size = entry.size; this.path = entry.path; }
public static List <GitContent> GetContents(string owner, string repository, string contentPath) { if (string.IsNullOrEmpty(owner) || string.IsNullOrEmpty(repository)) { throw new ArgumentNullException(); } // GET /repos/:owner/:repo/contents string url = string.Format("https://{0}/repos/{1}/{2}/contents/{3}", github_root_url, owner, repository, contentPath); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); string jsonResponse = null; try { jsonResponse = getResponse(request); } catch (WebException wex) { log.Warn(wex); return(null); } catch (Exception ex) { log.Error(ex); return(null); } List <GitContent> contents = new List <GitContent>(); // Json.Net can't tell difference between a json result with a // single entry result (non-array) or a json with multiple entries (array). // This hack handles it for now... if (jsonResponse.StartsWith("[")) { contents = JsonConvert.DeserializeObject <List <GitContent> >(jsonResponse); } else { GitContent content = JsonConvert.DeserializeObject <GitContent>(jsonResponse); contents.Add(content); } // sort contents by type and then name contents.Sort(); log.DebugFormat("Returning {0} content items", contents.Count); return(contents); }
/// <summary> /// Ex. /// Console.WriteLine(GitHubClient.GetFileContents("hello.txt")); /// </summary> /// <param name="contentPath">The path of the file in the repository (ex. 'hello.txt' or 'folder/hello.txt')</param> /// <returns>Plain text output of the Base64 encoded contents of the requested file</returns> public static string GetFileContents(string owner, string repository, string contentPath) { if (string.IsNullOrEmpty(owner) || string.IsNullOrEmpty(repository) || string.IsNullOrEmpty(contentPath)) { throw new ArgumentNullException(); } GitContent jsonResponse = GitHubClient.GetContent(owner, repository, contentPath); if (null == jsonResponse) { log.DebugFormat("No file matching '{0}' was found in the Repository", contentPath); return(null); } return(jsonResponse.DecodedContent); }
/// <summary> /// /// </summary> /// <param name="SourceFile">The full file path to upload (ex. c:\temp\hello.txt)</param> /// <param name="Content">Content object</param> /// <returns>True/False</returns> public static bool UpdateFile(string SourceFile, GitContent Content) { try { if (string.IsNullOrEmpty(SourceFile) || Content == null) { throw new ArgumentNullException(); } log.Info("Requesting an update to a file in the Repository"); // PUT /repos/:owner/:repo/contents/:path string url = string.Format("https://api.github.com/repos/{0}/{1}/contents/{2}", repository_owner, repository_name, Content.path); #region Build Request HttpWebRequest request = buildWebRequest(method.PUT, url); using (StreamWriter streamWriter = new StreamWriter(request.GetRequestStream())) { GitUpdateFile updateFile = new GitUpdateFile(); updateFile.message = commit_message; updateFile.author = new GitAuthor("Chris B", "*****@*****.**"); //TODO: Replace author info with authenticated user info updateFile.committer = new GitCommitter(committer_name, committer_email); updateFile.content = Utils.EncodeFile(SourceFile); updateFile.sha = Content.sha; // create json from object string json = JsonConvert.SerializeObject(updateFile, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); streamWriter.Write(json); streamWriter.Flush(); streamWriter.Close(); } #endregion // Build Request string jsonResult = getResponse(request); log.Info("File sucessfully updated in the Repository"); return true; } catch (FileNotFoundException fex) { log.Warn(fex.Message); return false; } catch (Exception ex) { log.Error(ex); return false; } }
/// <summary> /// Ex. /// Console.Write(GitHubClient.GetFileContents(Content)); /// </summary> /// <param name="Content">Content object</param> /// <returns>Plain text output of the Base64 encoded contents of the requested file</returns> public static string GetFileContents(GitContent Content) { if (Content == null) { throw new ArgumentNullException(); } return Utils.Base64Decode(Content.content); }