Skip to content

Commit 18dc97e

Browse files
committed
Added Client.searchRepositories
1 parent 6e97bb1 commit 18dc97e

File tree

6 files changed

+93
-11
lines changed

6 files changed

+93
-11
lines changed

Git.hub.Demo/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ static void Main(string[] args)
4949
apitest_pr.ToIssue().CreateComment("This is a sample comment from the API");
5050
//Console.WriteLine(apitest_pr.CreatePullRequest("mabako:tex", "master", "title", "body"));
5151
*/
52+
53+
Console.WriteLine();
54+
Console.WriteLine("Query for Git Extensions?");
55+
var search = client.searchRepositories("Git Extensions");
56+
search.ForEach(repo =>
57+
{
58+
Console.WriteLine(" {0} by {1}", repo.Name, repo.Owner.Login);
59+
Console.WriteLine(" -> {0}", repo.GitUrl);
60+
});
61+
5262
Console.ReadLine();
5363
}
5464
}

Git.hub/APIv2/RepositoryV2.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using RestSharp;
6+
7+
namespace Git.hub.APIv2
8+
{
9+
internal class RepositoryV2
10+
{
11+
public string Name { get; private set; }
12+
public string Description { get; private set; }
13+
public string Homepage { get; private set; }
14+
15+
// not in v3 api
16+
public string Owner { get; private set; }
17+
18+
public bool Fork { get; private set; }
19+
public int Forks { get; private set; }
20+
public bool Private { get; private set; }
21+
public Repository ToV3(RestClient client)
22+
{
23+
return new Repository
24+
{
25+
Name = Name,
26+
Description = Description,
27+
Homepage = Homepage,
28+
Owner = new User
29+
{
30+
Login = Owner
31+
},
32+
Fork = Fork,
33+
Forks = Forks,
34+
Private = Private,
35+
36+
GitUrl = string.Format("git://github.com/{0}/{1}.git", Owner, Name),
37+
SshUrl = string.Format("[email protected]:{0}/{1}.git", Owner, Name),
38+
CloneUrl = string.Format("https://github.com/{0}/{1}.git", Owner, Name),
39+
Detailed = false,
40+
_client = client
41+
};
42+
}
43+
}
44+
45+
internal class RepositoryListV2
46+
{
47+
public List<RepositoryV2> Repositories { get; private set; }
48+
}
49+
}

Git.hub/Client.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using RestSharp;
45

56
namespace Git.hub
@@ -144,5 +145,26 @@ public User getCurrentUser()
144145

145146
return user.Data;
146147
}
148+
149+
/// <summary>
150+
/// Searches through all of Github's repositories, similar to the search on the website itself.
151+
/// </summary>
152+
/// <param name="query">what to search for</param>
153+
/// <returns>(limited) list of matching repositories</returns>
154+
public List<Repository> searchRepositories(string query)
155+
{
156+
var request = new RestRequest("/legacy/repos/search/{query}");
157+
request.AddUrlSegment("query", query);
158+
159+
var repos = client.Get<APIv2.RepositoryListV2>(request);
160+
if (repos.Data == null || repos.Data.Repositories == null)
161+
{
162+
Console.WriteLine(repos.Content);
163+
throw new Exception(string.Format("Could not search for {0}", query));
164+
}
165+
166+
List<Repository> convertedRepos = repos.Data.Repositories.Select(r => r.ToV3(client)).ToList();
167+
return convertedRepos;
168+
}
147169
}
148170
}

Git.hub/Git.hub.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<Reference Include="System" />
3939
</ItemGroup>
4040
<ItemGroup>
41+
<Compile Include="APIv2\RepositoryV2.cs" />
4142
<Compile Include="Branch.cs" />
4243
<Compile Include="Client.cs" />
4344
<Compile Include="Commit.cs" />

Git.hub/Repository.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ namespace Git.hub
99
{
1010
public class Repository
1111
{
12-
public string Name { get; private set; }
13-
public string Description { get; private set; }
14-
public string Homepage { get; private set; }
15-
public User Owner { get; private set; }
16-
public bool Fork { get; private set; }
17-
public int Forks { get; private set; }
18-
public bool Private { get; private set; }
12+
public string Name { get; internal set; }
13+
public string Description { get; internal set; }
14+
public string Homepage { get; internal set; }
15+
public User Owner { get; internal set; }
16+
public bool Fork { get; internal set; }
17+
public int Forks { get; internal set; }
18+
public bool Private { get; internal set; }
1919
public Organization Organization { get; internal set; }
2020

2121
private Repository _Parent;
@@ -37,19 +37,19 @@ private set
3737
/// Read-only clone url
3838
/// git://github.com/{user}/{repo}.git
3939
/// </summary>
40-
public string GitUrl { get; private set; }
40+
public string GitUrl { get; internal set; }
4141

4242
/// <summary>
4343
/// Read/Write clone url via SSH
4444
/// [email protected]/{user}/{repo.git}
4545
/// </summary>
46-
public string SshUrl { get; private set; }
46+
public string SshUrl { get; internal set; }
4747

4848
/// <summary>
4949
/// Read/Write clone url via HTTPS
5050
/// https://github.com/{user}/{repo}.git
5151
/// </summary>
52-
public string CloneUrl { get; private set; }
52+
public string CloneUrl { get; internal set; }
5353

5454
internal RestClient _client;
5555

Git.hub/User.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class User
55
/// <summary>
66
/// The GitHub username
77
/// </summary>
8-
public string Login { get; private set; }
8+
public string Login { get; internal set; }
99

1010
public User()
1111
{

0 commit comments

Comments
 (0)