forked from octokit/octokit.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-create-repository.linq
90 lines (72 loc) · 2.82 KB
/
06-create-repository.linq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<Query Kind="Program">
<Reference><RuntimeDirectory>\System.Net.Http.dll</Reference>
<Reference><RuntimeDirectory>\System.Runtime.dll</Reference>
<NuGetReference>Octokit</NuGetReference>
<NuGetReference>Octokit.Reactive</NuGetReference>
<Namespace>Octokit</Namespace>
<Namespace>System.Net.Http.Headers</Namespace>
<Namespace>System.Reactive.Linq</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
async Task Main(string[] args)
{
var owner = string.Empty;
var reponame = string.Empty;
GitHubClient client = new GitHubClient(
new Octokit.ProductHeaderValue("Octokit.samples"));
owner = "naveensrinivasan";
reponame = "my-awesome-repo-" + Environment.TickCount;
// or if you don't want to give an app your creds
// you can use a token from an OAuth app
// Here is the URL to get tokens https://github.com/settings/tokens
// and save the token using Util.SetPassword("github","CHANGETHIS")
client.Credentials = new Credentials(Util.GetPassword("github"));
var email = "[email protected]";
// 1 - create a repository through the API
var newRepo = new NewRepository(reponame)
{
AutoInit = true // very helpful!
};
var repository = await client.Repository.Create(newRepo);
Console.WriteLine("Browse the repository at: " + repository.HtmlUrl);
// 2 - create a blob containing the contents of our README
var newBlob = new NewBlob() {
Content = "#MY AWESOME REPO\rthis is some code\rI made it on: "
+ DateTime.Now.ToString(),
Encoding = EncodingType.Utf8
};
var createdBlob = await client.Git.Blob
.Create(owner, reponame, newBlob);
createdBlob.Dump();
// 3 - create a tree which represents just the README file
var newTree = new NewTree();
newTree.Tree.Add(new NewTreeItem() {
Path = "README.md",
Mode = Octokit.FileMode.File,
Sha = createdBlob.Sha,
Type = TreeType.Blob
});
var createdTree = await client.Git.Tree
.Create(owner, reponame, newTree);
createdTree.Dump();
// 4 - this commit should build on the current master branch
var master = await client.Git.Reference
.Get(owner, reponame, "heads/master");
var newCommit = new NewCommit(
"Hello World!",
createdTree.Sha,
new[] { master.Object.Sha })
{ Author = new Committer(owner,email,DateTime.UtcNow)};
var createdCommit = await client.Git.Commit
.Create(owner, reponame, newCommit);
createdCommit.Dump();
// 5 - create a reference for the master branch
var updateReference = new ReferenceUpdate(createdCommit.Sha);
var updatedReference = await client.Git
.Reference.Update(owner, reponame, "heads/master", updateReference);
updatedReference.Dump();
// Deleting a repository requires admin access.
//If OAuth is used, the `delete_repo` scope is required.
await client.Repository.Delete(owner, reponame);
"Repo Clean up!".Dump(reponame + " has been deleted");
}