Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of getting authenticated user assigned issues #22

Merged
merged 4 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 25 additions & 4 deletions Git.hub/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void setOAuth2Token(string token)
/// Lists all repositories for the logged in user
/// </summary>
/// <returns>list of repositories</returns>
public IList<Repository> getRepositories()
public IReadOnlyList<Repository> getRepositories()
{
if (_client.Authenticator == null)
throw new ArgumentException("no authentication details");
Expand All @@ -73,7 +73,7 @@ public IList<Repository> getRepositories()
/// </summary>
/// <param name="username">username</param>
/// <returns>list of repositories</returns>
public IList<Repository> getRepositories(string username)
public IReadOnlyList<Repository> getRepositories(string username)
{
var request = new RestRequest("/users/{name}/repos")
.AddUrlSegment("name", username);
Expand Down Expand Up @@ -112,7 +112,7 @@ public Repository getRepository(string username, string repositoryName)
/// </summary>
/// <param name="organization">name of the organization</param>
/// <returns></returns>
public IList<Repository> getOrganizationRepositories(string organization)
public IReadOnlyList<Repository> getOrganizationRepositories(string organization)
{
var request = new RestRequest("/orgs/{org}/repos")
.AddUrlSegment("org", organization);
Expand Down Expand Up @@ -160,7 +160,7 @@ public async Task<User> GetUserAsync(string userName)
/// </summary>
/// <param name="query">what to search for</param>
/// <returns>(limited) list of matching repositories</returns>
public List<Repository> searchRepositories(string query)
public IReadOnlyList<Repository> searchRepositories(string query)
{
var request = new RestRequest("/legacy/repos/search/{query}");
request.AddUrlSegment("query", query);
Expand All @@ -174,6 +174,27 @@ public List<Repository> searchRepositories(string query)
return repos.Repositories.Select(r => r.ToV3(_client)).ToList();
}

/// <summary>
/// Get the issues assigned to the authenticated user.
/// <see href="https://docs.github.com/fr/rest/issues/issues?apiVersion=2022-11-28#list-issues-assigned-to-the-authenticated-user">GitHub issue api</see>
/// </summary>
/// <param name="state">state of the issues returned. Choice: "open", "closed", "all"</param>
/// <param name="includePullRequests">true to include pull requests; otherwise, false.</param>
/// <returns>a list of issues assigned to the authenticated user.</returns>
public IReadOnlyList<Issue> GetAssignedIssues(string state = "open", bool includePullRequests = false)
{
var request = new RestRequest("/issues");
request.AddQueryParameter("state", state);
request.AddQueryParameter("filter", "assigned");
request.AddQueryParameter("pulls", includePullRequests ? "true" : "false");

List<Issue> issues = _client.GetList<Issue>(request);

issues?.ForEach(i => i._client = _client);

return issues;
}

private T DoRequest<T>(IRestRequest request, bool throwOnError = true) where T : new()
{
var response = _client.Get<T>(request);
Expand Down
17 changes: 11 additions & 6 deletions Git.hub/Issue.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using RestSharp;

namespace Git.hub
{
[DebuggerDisplay("{Number}:{Title}")]
public class Issue
{
public int Number;

internal RestClient _client;
public Repository Repository { get; internal set; }

public int Number { get; internal set; }
public string Title { get; internal set; }
public string Body { get; internal set; }
public DateTime CreatedAt { get; internal set; }
public DateTime UpdatedAt { get; internal set; }
public string Url { get; internal set; }
public User User { get; internal set; }
public Repository Repository { get; internal set; }

public List<IssueComment> GetComments()
public IReadOnlyList<IssueComment> GetComments()
{
var request = new RestRequest("/repos/{user}/{repo}/issues/{issue}/comments");
request.AddUrlSegment("user", Repository.Owner.Login);
Expand Down
4 changes: 2 additions & 2 deletions Git.hub/PullRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class PullRequest
/// Retrieves all Commits associated with this pull request.
/// </summary>
/// <returns></returns>
public List<PullRequestCommit> GetCommits()
public IReadOnlyList<PullRequestCommit> GetCommits()
{
var request = new RestRequest("repos/{user}/{repo}/pulls/{pull}/commits");
request.AddUrlSegment("user", Repository.Owner.Login);
Expand All @@ -94,7 +94,7 @@ public Issue ToIssue()
return new Issue { _client = _client, Repository = Repository, Number = Number };
}

public List<IssueComment> GetIssueComments()
public IReadOnlyList<IssueComment> GetIssueComments()
{
return ToIssue().GetComments();
}
Expand Down
4 changes: 2 additions & 2 deletions Git.hub/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Repository CreateFork()
/// </summary>
/// <remarks>Not really sure if that's even useful, mind the 'git branch'</remarks>
/// <returns>list of all branches</returns>
public IList<Branch> GetBranches()
public IReadOnlyList<Branch> GetBranches()
{
RestRequest request = new RestRequest("/repos/{user}/{repo}/branches");
request.AddUrlSegment("user", Owner.Login);
Expand Down Expand Up @@ -104,7 +104,7 @@ public string GetDefaultBranch()
/// Lists all open pull requests
/// </summary>
/// <returns>list of all open pull requests</returns>
public IList<PullRequest> GetPullRequests()
public IReadOnlyList<PullRequest> GetPullRequests()
{
var request = new RestRequest("/repos/{user}/{repo}/pulls");
request.AddUrlSegment("user", Owner.Login);
Expand Down
9 changes: 9 additions & 0 deletions Git.hub/RestClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using RestSharp;
Expand All @@ -16,6 +17,14 @@ public static List<T> GetList<T>(this IRestClient client, IRestRequest request)
while (true)
{
IRestResponse<List<T>> pageResponse = client.Get<List<T>>(request);

if (!pageResponse.IsSuccessful)
{
var fullUrl = client.BuildUri(request);
Trace.WriteLine($"GitHub request error ({fullUrl}): {pageResponse.StatusCode:D} ({pageResponse.StatusCode:G}) - {pageResponse.StatusDescription}");
return null;
}

if (pageResponse.Data == null)
return null;

Expand Down
70 changes: 70 additions & 0 deletions GitExtensions.settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<dictionary>
<item>
<key>
<string>RevisionLinkDefs</string>
</key>
<value>
<string>&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;ArrayOfGitExtLinkDef&gt;
&lt;GitExtLinkDef&gt;
&lt;Enabled&gt;true&lt;/Enabled&gt;
&lt;LinkFormats&gt;
&lt;GitExtLinkFormat&gt;
&lt;Caption&gt;View commit in GitHub&lt;/Caption&gt;
&lt;Format&gt;https://github.com/gitextensions/Git.hub/commit/%COMMIT_HASH%&lt;/Format&gt;
&lt;/GitExtLinkFormat&gt;
&lt;GitExtLinkFormat&gt;
&lt;Caption&gt;View project in GitHub&lt;/Caption&gt;
&lt;Format&gt;https://github.com/gitextensions/Git.hub&lt;/Format&gt;
&lt;/GitExtLinkFormat&gt;
&lt;/LinkFormats&gt;
&lt;Name&gt;GitHub - Code&lt;/Name&gt;
&lt;RemoteSearchInParts /&gt;
&lt;SearchInParts&gt;
&lt;RevisionPart&gt;Message&lt;/RevisionPart&gt;
&lt;/SearchInParts&gt;
&lt;SearchPattern&gt;.*&lt;/SearchPattern&gt;
&lt;UseOnlyFirstRemote&gt;false&lt;/UseOnlyFirstRemote&gt;
&lt;/GitExtLinkDef&gt;
&lt;GitExtLinkDef&gt;
&lt;Enabled&gt;true&lt;/Enabled&gt;
&lt;LinkFormats&gt;
&lt;GitExtLinkFormat&gt;
&lt;Caption&gt;#{0}&lt;/Caption&gt;
&lt;Format&gt;https://github.com/gitextensions/Git.hub/issues/{0}&lt;/Format&gt;
&lt;/GitExtLinkFormat&gt;
&lt;/LinkFormats&gt;
&lt;Name&gt;GitHub - Issues&lt;/Name&gt;
&lt;NestedSearchPattern&gt;\d+&lt;/NestedSearchPattern&gt;
&lt;RemoteSearchInParts /&gt;
&lt;SearchInParts&gt;
&lt;RevisionPart&gt;Message&lt;/RevisionPart&gt;
&lt;RevisionPart&gt;LocalBranches&lt;/RevisionPart&gt;
&lt;/SearchInParts&gt;
&lt;SearchPattern&gt;(?i)(?&amp;lt;!pull request |pr[ _]?)(#|(((feat(ure)?)|fix)[/_-]))\d+&lt;/SearchPattern&gt;
&lt;UseOnlyFirstRemote&gt;false&lt;/UseOnlyFirstRemote&gt;
&lt;/GitExtLinkDef&gt;
&lt;GitExtLinkDef&gt;
&lt;Enabled&gt;true&lt;/Enabled&gt;
&lt;LinkFormats&gt;
&lt;GitExtLinkFormat&gt;
&lt;Caption&gt;PR #{0}&lt;/Caption&gt;
&lt;Format&gt;https://github.com/gitextensions/Git.hub/pull/{0}&lt;/Format&gt;
&lt;/GitExtLinkFormat&gt;
&lt;/LinkFormats&gt;
&lt;Name&gt;GitHub - Pull Requests&lt;/Name&gt;
&lt;NestedSearchPattern&gt;\d+&lt;/NestedSearchPattern&gt;
&lt;RemoteSearchInParts /&gt;
&lt;SearchInParts&gt;
&lt;RevisionPart&gt;Message&lt;/RevisionPart&gt;
&lt;RevisionPart&gt;LocalBranches&lt;/RevisionPart&gt;
&lt;RevisionPart&gt;RemoteBranches&lt;/RevisionPart&gt;
&lt;/SearchInParts&gt;
&lt;SearchPattern&gt;(?i)(pull request |pr[ _]?)#?\d+&lt;/SearchPattern&gt;
&lt;UseOnlyFirstRemote&gt;false&lt;/UseOnlyFirstRemote&gt;
&lt;/GitExtLinkDef&gt;
&lt;/ArrayOfGitExtLinkDef&gt;</string>
</value>
</item>
</dictionary>