linqpad samples

Linqpad samples
This commit is contained in:
Naveen
2015-09-30 21:20:37 -04:00
parent 0053069eab
commit 4f89ce394d
10 changed files with 459 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
<Query Kind="Program">
<NuGetReference>Octokit.Reactive</NuGetReference>
<NuGetReference>Rx-Main</NuGetReference>
<Namespace>Octokit</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;
#if CMD
owner = args[0];
reponame = args[1];
#else
owner = "octokit";
reponame = "octokit.net";
#endif
var client = new GitHubClient(new Octokit.ProductHeaderValue("octokit.samples"));
var repository = await client.Repository.Get(owner, reponame);
Console.WriteLine(String.Format("Octokit.net can be found at {0}\n", repository.HtmlUrl));
Console.WriteLine("It currently has {0} watchers and {1} forks\n",
repository.StargazersCount,
repository.ForksCount);
Console.WriteLine("It has {0} open issues\n", repository.OpenIssuesCount);
Console.WriteLine("And GitHub thinks it is a {0} project", repository.Language);
}

View File

@@ -0,0 +1,69 @@
<Query Kind="Program">
<NuGetReference>Octokit.Reactive</NuGetReference>
<NuGetReference>Rx-Main</NuGetReference>
<Namespace>Octokit</Namespace>
<Namespace>Octokit.Reactive</Namespace>
<Namespace>System</Namespace>
<Namespace>System.Reactive.Linq</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
async Task Main()
{
var owner = string.Empty;
var reponame = string.Empty;
//Search Issues with xamarin keyword and get the results
GitHubClient client = new GitHubClient(
new Octokit.ProductHeaderValue("Octokit.samples"));
#if CMD
owner = args[0];
reponame = args[1];
// For integration testing
client.Credentials = new Credentials(
Environment.GetEnvironmentVariable("OCTOKIT_GITHUBUSERNAME"),
Environment.GetEnvironmentVariable("OCTOKIT_GITHUBPASSWORD"));
#else
owner = "octokit";
reponame = "octokit.net";
// 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"));
#endif
var issue = new SearchIssuesRequest("xamarin");
issue.Repos.Add(owner,reponame);
issue.SortField = IssueSearchSort.Updated;
var searchresults = await client.Search.SearchIssues(issue);
//For every issue get the comments for it
var commentsclient = client.Issue.Comment;
var comments = searchresults.Items.Select(async i =>
new { IssueNumber = i.Number,
Comments = await commentsclient
.GetAllForIssue(owner, reponame, i.Number)});
var issueComments = await Task.WhenAll( comments);
//Combine the comments with Issue and then dump it.
searchresults.Items.Select(i => new
{
Number = Util.RawHtml(new XElement("a",
new XAttribute("href", i.HtmlUrl.ToString()), i.Number)),
i.Title,
i.Body,
i.State,
Comments = issueComments.FirstOrDefault(c => c.IssueNumber == i.Number)
.Comments.Select(c =>
new { User = c.User.Id,
Name = c.User.Login,
Content = c.Body,
Date = c.CreatedAt,
Id = c.Id, c.Body})
} ).Dump();
}

View File

@@ -0,0 +1,45 @@
<Query Kind="Program">
<NuGetReference>Octokit.Reactive</NuGetReference>
<NuGetReference>Rx-Main</NuGetReference>
<Namespace>Octokit</Namespace>
<Namespace>System.Net</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
async Task Main(string[] args)
{
var owner = string.Empty;
var reponame = string.Empty;
#if CMD
owner = args[0];
reponame = args[1];
#else
owner = "octokit";
reponame = "octokit.net";
#endif
var client = new GitHubClient(new Octokit.ProductHeaderValue("octokit.samples"));
//Get releases for the octokit
var releases = await client.Release.GetAll(owner, reponame);
releases.Select(r => new { r.Name, r.Body }).Dump("Releases");
//Don't want draft release and because we are using reactive the first one is the latest one.
var latestrelease = releases.First(r => r.Draft == false).Dump("Latest Octokit");
//Gets the assets for the latest relase
var assets = await client.Release.GetAllAssets(owner,reponame,latestrelease.Id);
assets.Dump("Assets");
var latestreleaseassetid = assets.First(a => a.Name.Contains("Reactive")).Id;
var asset = await client.Release.GetAsset(owner,reponame,latestreleaseassetid);
asset.DownloadCount.Dump("Download Count for this release");
//Download the release
var wc = new WebClient();
var filename = Path.Combine( Path.GetTempPath(),"Octokit-Reactive.nupkg");
await wc.DownloadFileTaskAsync(asset.BrowserDownloadUrl,filename);
filename.Dump();
}
// Define other methods and classes here

View File

@@ -0,0 +1,39 @@
<Query Kind="Program">
<NuGetReference>Octokit.Reactive</NuGetReference>
<NuGetReference>Rx-Main</NuGetReference>
<Namespace>Octokit</Namespace>
<Namespace>Octokit.Reactive</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
async Task Main(string[] args)
{
var userName = string.Empty;
GitHubClient client = new GitHubClient(new Octokit.ProductHeaderValue("octokit.samples"));
#if CMD
userName = args[0];
// For integration testing
client.Credentials = new Credentials(
Environment.GetEnvironmentVariable("OCTOKIT_GITHUBUSERNAME"),
Environment.GetEnvironmentVariable("OCTOKIT_GITHUBPASSWORD"));
#else
userName = "naveensrinivasan";
// basic authentication
//client.Credentials = new Credentials("username", "password");
// 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"));
#endif
var repositories = await client.Repository.GetAllForUser(userName);
repositories.Select(r => new { r.Name }).Dump(userName + "Repos");
// and then fetch the repositories for the current user
var repos = await client.Repository.GetAllForCurrent();
repos.Select(r => r.Name).Dump("Your Repos");
}

View File

@@ -0,0 +1,22 @@
<Query Kind="Program">
<NuGetReference>Octokit.Reactive</NuGetReference>
<NuGetReference>Rx-Main</NuGetReference>
<Namespace>Octokit</Namespace>
<Namespace>Octokit.Reactive</Namespace>
<Namespace>System</Namespace>
<Namespace>System.Reactive.Linq</Namespace>
</Query>
void Main()
{
var owner = string.Empty;
#if CMD
owner = args[0];
#else
owner = "octokit";
#endif
var client = new ObservableGitHubClient(new Octokit.ProductHeaderValue("Octokit.samples"));
client.Repository.GetAllForUser(owner).Select(r => r.Name).Dump();
}

View File

@@ -0,0 +1,39 @@
<Query Kind="Program">
<NuGetReference>Octokit</NuGetReference>
<NuGetReference>Rx-Main</NuGetReference>
<Namespace>Octokit</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"));
#if CMD
owner = args[0];
reponame = args[1];
#else
owner = "octokit";
reponame = "octokit.net";
#endif
var releases = await client.Release.GetAll(owner, reponame);
// we have to build up this tag because release tags
// are just lightweight tags. you can read more about
// the differences between lightweight tags and annotated tags
// here: http://git-scm.com/book/en/Git-Basics-Tagging#Creating-Tags
// we can fetch the tag for this release
var reference = "tags/" + releases[0].TagName;
var tag = await client.GitDatabase.Reference.Get(owner, reponame, reference);
tag.Dump();
// and we can fetch the commit associated with this release
var commit = await client.GitDatabase.Commit.Get(owner, reponame, tag.Object.Sha);
commit.Dump();
}

View File

@@ -0,0 +1,99 @@
<Query Kind="Program">
<Reference>&lt;RuntimeDirectory&gt;\System.Net.Http.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Runtime.dll</Reference>
<NuGetReference>Octokit</NuGetReference>
<NuGetReference>Octokit.Reactive</NuGetReference>
<NuGetReference>Rx-Main</NuGetReference>
<Namespace>System.Reactive.Linq</Namespace>
<Namespace>System.Net.Http.Headers</Namespace>
<Namespace>Octokit</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"));
#if CMD
owner = args[0];
reponame = args[1];
// For integration testing
client.Credentials = new Credentials(
Environment.GetEnvironmentVariable("OCTOKIT_GITHUBUSERNAME"),
Environment.GetEnvironmentVariable("OCTOKIT_GITHUBPASSWORD"));
#else
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"));
#endif
var email = "person@cooldomain.com";
// 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.GitDatabase.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.GitDatabase.Tree
.Create(owner, reponame, newTree);
createdTree.Dump();
// 4 - this commit should build on the current master branch
var master = await client.GitDatabase.Reference
.Get(owner, reponame, "heads/master");
var newCommit = new NewCommit(
"Hello World!",
createdTree.Sha,
new[] { master.Object.Sha })
{ Author = new SignatureResponse(owner,email,DateTime.UtcNow)};
var createdCommit = await client.GitDatabase.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.GitDatabase
.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");
}

View File

@@ -0,0 +1,31 @@
<Query Kind="Program">
<Reference>&lt;RuntimeDirectory&gt;\System.Net.Http.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Runtime.dll</Reference>
<NuGetReference>Octokit</NuGetReference>
<NuGetReference>Octokit.Reactive</NuGetReference>
<NuGetReference>Rx-Main</NuGetReference>
<Namespace>System.Reactive.Linq</Namespace>
<Namespace>System.Net.Http.Headers</Namespace>
<Namespace>Octokit</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
async Task Main()
{
//This makes discovering code fun!!
var client = new GitHubClient(new Octokit.ProductHeaderValue("octokit"));
var gorepos = await client.Search.SearchRepo(new SearchRepositoriesRequest()
{Language = Language.Go});
gorepos.Items.OrderByDescending (i => i.CreatedAt)
.OrderByDescending (i => i.StargazersCount)
.Take(50)
.Select (i => new {
Name = i.Name,
Description = i.Description ,
LastUpdated = i.UpdatedAt,
Url = i.HtmlUrl,
WatchCount = i.StargazersCount
})
.Dump();
}

View File

@@ -0,0 +1,42 @@
<Query Kind="Program">
<NuGetReference>Octokit.Reactive</NuGetReference>
<NuGetReference>Rx-Main</NuGetReference>
<Namespace>Octokit</Namespace>
<Namespace>Octokit.Reactive</Namespace>
<Namespace>System</Namespace>
<Namespace>System.Reactive.Linq</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
async Task Main(string[] args)
{
var userName = string.Empty;
GitHubClient client = new GitHubClient(new Octokit.ProductHeaderValue("Octokit.Samples"));
#if CMD
userName = args[0];
// For integration testing
client.Credentials = new Credentials(
Environment.GetEnvironmentVariable("OCTOKIT_GITHUBUSERNAME"),
Environment.GetEnvironmentVariable("OCTOKIT_GITHUBPASSWORD"));
#else
userName = "naveensrinivasan";
client.Credentials = new Credentials(Util.GetPassword("github"));
#endif
var observableclient = new ObservableGitHubClient(client);
var gists = await observableclient.Gist.GetAllForUser(userName).Dump();
//Create A gist
var gist = new NewGist() { Description = "Gist from LinqPad", Public = true};
gist.Files.Add("test","Test file");
//Star a gist
var createdgist = await observableclient.Gist.Create(gist);
await observableclient.Gist.Star(createdgist.Id);
// Add a comment to the gist
var comment = await observableclient.Gist
.Comment.Create(createdgist.Id,"Comment from linqpad").Dump();
}

View File

@@ -0,0 +1,37 @@
<Query Kind="Program">
<NuGetReference>Octokit.Reactive</NuGetReference>
<NuGetReference>Rx-Main</NuGetReference>
<Namespace>Octokit</Namespace>
<Namespace>Octokit.Reactive</Namespace>
<Namespace>System</Namespace>
<Namespace>System.Reactive.Linq</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
async Task Main()
{
var userName = string.Empty;
GitHubClient client = new GitHubClient(new Octokit.ProductHeaderValue("Octokit.Samples"));
#if CMD
userName = args[0];
#else
userName = "naveensrinivasan";
client.Credentials = new Credentials(Util.GetPassword("github"));
#endif
client.Credentials = new Credentials(Util.GetPassword("github"));
IIssuesClient issuesclient = client.Issue;
var myissues = await issuesclient.GetAllForCurrent();
myissues.Select(m => new { m.Title, m.Body}).Dump();
//var issue = new NewIssue("Test");
//var newissue = await issuesclient.Create(owner,reponame,new NewIssue("Test"));
//
//newissue.Dump();
var allissues = await issuesclient.GetAllForRepository("octokit", "octokit.net");
allissues.Select(a => new { a.Title, a.Body}).Dump();
}