Skip to content

Commit 0795898

Browse files
authored
Merge pull request #29 from apiiro/alexp/get-org-last-audit-log-date
Get org last audit log date
2 parents 24986a3 + e4e104d commit 0795898

5 files changed

Lines changed: 61 additions & 36 deletions

File tree

Octokit.Reactive/Clients/ObservableAuditOrganizationsClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ public ObservableAuditOrganizationsClient(IGitHubClient client)
1313

1414
public IObservable<DateTime?> GetUserLastActivityDate(string organization, AuditLogPhraseOptions phraseOptions)
1515
{
16-
return _client.GetUserLastActivityDate(organization, phraseOptions).ToObservable();
16+
return _client.GetUserLastActivityForRepositoryDate(organization, phraseOptions).ToObservable();
1717
}
1818
}

Octokit/Clients/AuditOrganizationsClient.cs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,22 @@ public AuditOrganizationsClient(IApiConnection apiConnection) : base(apiConnecti
1616
{
1717
}
1818

19+
[ManualRoute("GET", "/organizations/{org}")]
20+
public async Task<DateTime?> GetLastActivityDate(string organization)
21+
{
22+
Ensure.ArgumentNotNullOrEmptyString(organization, nameof(organization));
23+
return await GetLastActivityDateImpl(organization);
24+
}
25+
1926
[ManualRoute("GET", "/organizations/{org}/audit-log?phrase={phrase}")]
20-
public async Task<DateTime?> GetUserLastActivityDate(string organization, AuditLogPhraseOptions auditLogPhraseOptions)
27+
public async Task<DateTime?> GetUserLastActivityForRepositoryDate(string organization, AuditLogPhraseOptions auditLogPhraseOptions)
2128
{
2229
Ensure.ArgumentNotNullOrEmptyString(organization, nameof(organization));
2330
Ensure.ArgumentNotNull(auditLogPhraseOptions, nameof(auditLogPhraseOptions));
2431
Ensure.ArgumentNotNullOrEmptyString(auditLogPhraseOptions.Repository, nameof(AuditLogPhraseOptions.Repository));
2532
Ensure.ArgumentNotNullOrEmptyString(auditLogPhraseOptions.User, nameof(AuditLogPhraseOptions.User));
2633

27-
var options = new ApiOptions()
28-
{
29-
PageSize = 1
30-
};
31-
IDictionary<string, string> parameters = new Dictionary<string, string>();
32-
Pagination.Setup(parameters, options);
33-
34-
var phrase = auditLogPhraseOptions.BuildPhrase(organization);
35-
var auditLogs = await ApiConnection.Get<List<AuditLogEvent>>(ApiUrls.AuditLog(organization, phrase), parameters);
36-
37-
if (!auditLogs.Any())
38-
{
39-
return null;
40-
}
41-
42-
var auditLog = auditLogs.Single();
43-
44-
var dateTimeOffSet = DateTimeOffset.FromUnixTimeMilliseconds(auditLog.CreatedAt);
45-
var dateTime = dateTimeOffSet.DateTime;
46-
47-
return dateTime;
34+
return await GetLastActivityDateImpl(organization, auditLogPhraseOptions);
4835
}
4936

5037
[ManualRoute("GET", "/organizations/{org}/audit-log?phrase={phrase}")]
@@ -129,6 +116,31 @@ public AuditOrganizationsClient(IApiConnection apiConnection) : base(apiConnecti
129116
return forkRepositoryCreatedEvent;
130117
}
131118

119+
private async Task<DateTime?> GetLastActivityDateImpl(string organization, AuditLogPhraseOptions? auditLogPhraseOptions = null)
120+
{
121+
var options = new ApiOptions
122+
{
123+
PageSize = 1
124+
};
125+
IDictionary<string, string> parameters = new Dictionary<string, string>();
126+
Pagination.Setup(parameters, options);
127+
128+
var phrase = auditLogPhraseOptions?.BuildPhrase(organization);
129+
var auditLogs = await ApiConnection.Get<List<AuditLogEvent>>(ApiUrls.AuditLog(organization, phrase), parameters);
130+
131+
if (!auditLogs.Any())
132+
{
133+
return null;
134+
}
135+
136+
var auditLog = auditLogs.Single();
137+
138+
var dateTimeOffSet = DateTimeOffset.FromUnixTimeMilliseconds(auditLog.CreatedAt);
139+
var dateTime = dateTimeOffSet.DateTime;
140+
141+
return dateTime;
142+
}
143+
132144
private static ForkRepositoryCreatedEvent? GetRepositoryCreatedByForkEvent(AuditLogEvent auditLog)
133145
{
134146
if (auditLog.From != "tree#fork")

Octokit/Clients/IAuditOrganizationsClient.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ namespace Octokit
66
{
77
public interface IAuditOrganizationsClient
88
{
9+
/// <summary>
10+
/// Gets last activity date for an organization
11+
/// </summary>
12+
/// <param name="organization">The organization</param>
13+
Task<DateTime?> GetLastActivityDate(string organization);
14+
915
/// <summary>
1016
/// Gets user last activity date for a repository
1117
/// </summary>
@@ -14,7 +20,7 @@ public interface IAuditOrganizationsClient
1420
/// </remarks>
1521
/// <param name="organization">The organization</param>
1622
/// <param name="auditLogPhraseOptions">The query phrase options</param>
17-
Task<DateTime?> GetUserLastActivityDate(string organization, AuditLogPhraseOptions auditLogPhraseOptions);
23+
Task<DateTime?> GetUserLastActivityForRepositoryDate(string organization, AuditLogPhraseOptions auditLogPhraseOptions);
1824

1925
/// <summary>
2026
/// Gets last visibility change event for a given repository

Octokit/Helpers/ApiUrls.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public static Uri OrganizationRepositorySecrets(string organization)
9797
/// <returns></returns>
9898
public static Uri OrganizationRepositorySecret(string organization, string secret)
9999
{
100-
return "orgs/{0}/actions/secrets/{1}".FormatUri(organization,secret);
100+
return "orgs/{0}/actions/secrets/{1}".FormatUri(organization, secret);
101101
}
102102

103103
/// <summary>
@@ -423,7 +423,7 @@ public static Uri RepoInstallation(long repositoryId)
423423
/// </summary>
424424
public static Uri OrganizationInstallation(string organization)
425425
{
426-
return "orgs/{0}/installation".FormatUri(organization); ;
426+
return "orgs/{0}/installation".FormatUri(organization);
427427
}
428428

429429
/// <summary>
@@ -1581,6 +1581,7 @@ public static Uri ReceivedEvents(string user, bool isPublic)
15811581
{
15821582
usersReceivedEvents += "/public";
15831583
}
1584+
15841585
return usersReceivedEvents.FormatUri(user);
15851586
}
15861587

@@ -1607,6 +1608,7 @@ public static Uri PerformedEvents(string user, bool isPublic)
16071608
{
16081609
usersEvents += "/public";
16091610
}
1611+
16101612
return usersEvents.FormatUri(user);
16111613
}
16121614

@@ -1780,7 +1782,6 @@ public static Uri PullRequestReviewCommentReactions(long repositoryId, int numbe
17801782
return "repositories/{0}/pulls/comments/{1}/reactions".FormatUri(repositoryId, number);
17811783
}
17821784

1783-
17841785
/// <summary>
17851786
/// Returns the <see cref="Uri"/> for the reaction of a specified pull request review comment.
17861787
/// </summary>
@@ -1842,6 +1843,7 @@ public static Uri Blob(string owner, string name, string reference)
18421843
{
18431844
blob += "/{2}";
18441845
}
1846+
18451847
return blob.FormatUri(owner, name, reference);
18461848
}
18471849

@@ -1931,7 +1933,7 @@ public static Uri Teams(int id)
19311933
/// <returns></returns>
19321934
public static Uri TeamsByOrganizationAndSlug(string org, string teamSlug)
19331935
{
1934-
return "orgs/{0}/teams/{1}".FormatUri(org,teamSlug);
1936+
return "orgs/{0}/teams/{1}".FormatUri(org, teamSlug);
19351937
}
19361938

19371939
/// <summary>
@@ -2311,7 +2313,7 @@ public static Uri RepoBranch(string owner, string name, string branchName)
23112313
{
23122314
return "repos/{0}/{1}/branches/{2}".FormatUri(owner, name, branchName);
23132315
}
2314-
2316+
23152317
/// <summary>
23162318
/// Returns the <see cref="Uri"/> for a repository branches rules.
23172319
/// </summary>
@@ -3100,6 +3102,7 @@ public static Uri Blob(long repositoryId, string reference)
31003102
{
31013103
blob += "/{1}";
31023104
}
3105+
31033106
return blob.FormatUri(repositoryId, reference);
31043107
}
31053108

@@ -4696,7 +4699,7 @@ public static Uri AllOrganizationCredentials(string org, string login)
46964699
{
46974700
return "orgs/{0}/credential-authorizations?login={1}".FormatUri(org, login);
46984701
}
4699-
4702+
47004703
/// <summary>
47014704
/// Returns the <see cref="Uri"/> for the Packages request
47024705
/// </summary>
@@ -5219,19 +5222,23 @@ public static Uri ActionsListWorkflowRuns(string owner, string repo, string work
52195222
{
52205223
return "repos/{0}/{1}/actions/workflows/{2}/runs".FormatUri(owner, repo, workflowFileName.UriEncode());
52215224
}
5222-
5223-
5225+
52245226
/// <summary>
52255227
/// Creates the relative <see cref="Uri"/> for getting the contents of the specified repository and path
52265228
/// </summary>
52275229
/// <param name="organization">The organization</param>
5228-
/// <param name="phrase">The query phrase to filter results</param>
5230+
/// <param name="phrase">The query phrase to filter results (optional)</param>
52295231
/// <returns>The <see cref="Uri"/> for getting the contents of the specified repository and path</returns>
5230-
public static Uri AuditLog(string organization, string phrase)
5232+
public static Uri AuditLog(string organization, string phrase = null)
52315233
{
5234+
if (string.IsNullOrWhiteSpace(phrase))
5235+
{
5236+
return "organizations/{0}/audit-log".FormatUri(organization);
5237+
}
5238+
52325239
return "organizations/{0}/audit-log?phrase={1}".FormatUri(organization, phrase);
52335240
}
5234-
5241+
52355242
/// <summary>
52365243
/// Returns the <see cref="Uri"/> that returns all of the copilot seats of the organization
52375244
/// </summary>

Octokit/Octokit.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<Description>An async-based GitHub API client library for .NET and .NET Core</Description>
55
<AssemblyTitle>Octokit</AssemblyTitle>
66
<Authors>GitHub</Authors>
7-
<Version>1.0.24</Version>
7+
<Version>1.0.25</Version>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99
<AssemblyName>Octokit</AssemblyName>
1010
<PackageId>Apiiro.Octokit</PackageId>

0 commit comments

Comments
 (0)