Skip to content

Commit

Permalink
fix datetime format bug (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-smith authored Jun 21, 2022
1 parent 2fa69a4 commit 9ea87ed
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
3 changes: 2 additions & 1 deletion RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- fixed a bug where `generate-script` would not properly handle Team Projects with spaces (or other invalid for github chars) in the name
- fixed bug where `inventory-report` would fail if your computer's datetime format settings were dd/mm/yyyy
- Support excluding releases when `--skip-releases` flag is provided in `gh gei migrate-repo` command for GHES migration path. Previously releases were excluded by default but now they are going to be included unless `--skip-releases` is provided.
- fixed a bug where `generate-script` would not properly handle Team Projects with spaces (or other invalid for github chars) in the name
9 changes: 5 additions & 4 deletions src/Octoshift/AdoApi.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -63,20 +64,20 @@ public virtual async Task<DateTime> GetLastPushDate(string org, string teamProje
var response = await _client.GetAsync(url);

var data = JObject.Parse(response);
var pushDate = data.TryGetValue("value", out var dataValue) && dataValue.Any() ? (string)dataValue.First()["date"] : DateTime.MinValue.ToString();
var pushDate = data.TryGetValue("value", out var dataValue) && dataValue.Any() ? (DateTime)dataValue.First()["date"] : DateTime.MinValue;

return DateTime.Parse(pushDate);
return pushDate.Date;
}

public virtual async Task<int> GetCommitCountSince(string org, string teamProject, string repo, DateTime fromDate)
{
var url = $"{_adoBaseUrl}/{org}/{teamProject}/_apis/git/repositories/{repo}/commits?searchCriteria.fromDate={fromDate.ToShortDateString()}&api-version=7.1-preview.1";
var url = $"{_adoBaseUrl}/{org}/{teamProject}/_apis/git/repositories/{repo}/commits?searchCriteria.fromDate={fromDate.ToString("d", CultureInfo.InvariantCulture)}&api-version=7.1-preview.1";
return await _client.GetCountUsingSkip(url);
}

public virtual async Task<IEnumerable<string>> GetPushersSince(string org, string teamProject, string repo, DateTime fromDate)
{
var url = $"{_adoBaseUrl}/{org}/{teamProject}/_apis/git/repositories/{repo}/pushes?searchCriteria.fromDate={fromDate.ToShortDateString()}&api-version=7.1-preview.1";
var url = $"{_adoBaseUrl}/{org}/{teamProject}/_apis/git/repositories/{repo}/pushes?searchCriteria.fromDate={fromDate.ToString("d", CultureInfo.InvariantCulture)}&api-version=7.1-preview.1";
var response = await _client.GetWithPagingTopSkipAsync(url, x => $"{x["pushedBy"]["displayName"]} ({x["pushedBy"]["uniqueName"]})");

return response;
Expand Down
83 changes: 81 additions & 2 deletions src/OctoshiftCLI.Tests/AdoApiTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -531,15 +532,56 @@ public async Task GetLastPushDate_Should_Return_MinDate_When_No_Pushes()
result.Should().Be(DateTime.MinValue);
}

[Fact]
public async Task GetLastPushDate_Should_Be_Locale_Independent()
{
var endpoint = $"https://dev.azure.com/{ADO_ORG}/{ADO_TEAM_PROJECT}/_apis/git/repositories/{ADO_REPO}/pushes?$top=1&api-version=7.1-preview.2";
var expectedDate = new DateTime(2016, 4, 22);

var response = new
{
value = new[]
{
new { date = "2016-04-22T23:39:04.2658909Z" }
}
};

_mockAdoClient.Setup(x => x.GetAsync(endpoint)).ReturnsAsync(response.ToJson());

CultureInfo.CurrentCulture = new CultureInfo("en-AT"); // Austrian culture has reversed datetime format

var result = await sut.GetLastPushDate(ADO_ORG, ADO_TEAM_PROJECT, ADO_REPO);

result.Should().Be(expectedDate);
}

[Fact]
public async Task GetCommitCountSince_Should_Return_Commit_Count()
{
var fromDate = new DateTime(2022, 2, 14);
var endpoint = $"https://dev.azure.com/{ADO_ORG}/{ADO_TEAM_PROJECT}/_apis/git/repositories/{ADO_REPO}/commits?searchCriteria.fromDate={fromDate.ToShortDateString()}&api-version=7.1-preview.1";
var fromDateIso = "02/14/2022";
var endpoint = $"https://dev.azure.com/{ADO_ORG}/{ADO_TEAM_PROJECT}/_apis/git/repositories/{ADO_REPO}/commits?searchCriteria.fromDate={fromDateIso}&api-version=7.1-preview.1";
var expectedCount = 12;

_mockAdoClient.Setup(x => x.GetCountUsingSkip(endpoint)).ReturnsAsync(expectedCount);

var result = await sut.GetCommitCountSince(ADO_ORG, ADO_TEAM_PROJECT, ADO_REPO, fromDate);

result.Should().Be(expectedCount);
}

[Fact]
public async Task GetCommitCountSince_Should_Be_Locale_Independent()
{
var fromDate = new DateTime(2022, 2, 14);
var fromDateIso = "02/14/2022";
var endpoint = $"https://dev.azure.com/{ADO_ORG}/{ADO_TEAM_PROJECT}/_apis/git/repositories/{ADO_REPO}/commits?searchCriteria.fromDate={fromDateIso}&api-version=7.1-preview.1";
var expectedCount = 12;

_mockAdoClient.Setup(x => x.GetCountUsingSkip(endpoint)).ReturnsAsync(expectedCount);

CultureInfo.CurrentCulture = new CultureInfo("en-AT"); // Austrian culture has reversed datetime format

var result = await sut.GetCommitCountSince(ADO_ORG, ADO_TEAM_PROJECT, ADO_REPO, fromDate);

result.Should().Be(expectedCount);
Expand All @@ -549,7 +591,8 @@ public async Task GetCommitCountSince_Should_Return_Commit_Count()
public async Task GetPushersSince_Should_Return_Pushers()
{
var fromDate = new DateTime(2022, 2, 14);
var endpoint = $"https://dev.azure.com/{ADO_ORG}/{ADO_TEAM_PROJECT}/_apis/git/repositories/{ADO_REPO}/pushes?searchCriteria.fromDate={fromDate.ToShortDateString()}&api-version=7.1-preview.1";
var fromDateIso = "02/14/2022";
var endpoint = $"https://dev.azure.com/{ADO_ORG}/{ADO_TEAM_PROJECT}/_apis/git/repositories/{ADO_REPO}/pushes?searchCriteria.fromDate={fromDateIso}&api-version=7.1-preview.1";
var pusher1DisplayName = "Dylan";
var pusher1UniqueName = "dsmith";
var pusher2DisplayName = "Tom";
Expand Down Expand Up @@ -578,6 +621,42 @@ public async Task GetPushersSince_Should_Return_Pushers()
result.Last().Should().Be("Tom (tcruise)");
}

[Fact]
public async Task GetPushersSince_Should_Be_Locale_Independent()
{
var fromDate = new DateTime(2022, 2, 14);
var fromDateIso = "02/14/2022";
var endpoint = $"https://dev.azure.com/{ADO_ORG}/{ADO_TEAM_PROJECT}/_apis/git/repositories/{ADO_REPO}/pushes?searchCriteria.fromDate={fromDateIso}&api-version=7.1-preview.1";
var pusher1DisplayName = "Dylan";
var pusher1UniqueName = "dsmith";
var pusher2DisplayName = "Tom";
var pusher2UniqueName = "tcruise";

var response = new[]
{
new
{
pushedBy = new { displayName = pusher1DisplayName, uniqueName = pusher1UniqueName }
},
new
{
pushedBy = new { displayName = pusher2DisplayName, uniqueName = pusher2UniqueName }
}
}.ToJson();

var responseArray = JArray.Parse(response);

_mockAdoClient.Setup(x => x.GetWithPagingTopSkipAsync(endpoint, It.IsAny<Func<JToken, string>>()))
.ReturnsAsync((string url, Func<JToken, string> selector) => responseArray.Select(selector));

CultureInfo.CurrentCulture = new CultureInfo("en-AT"); // Austrian culture has reversed datetime format

var result = await sut.GetPushersSince(ADO_ORG, ADO_TEAM_PROJECT, ADO_REPO, fromDate);

result.First().Should().Be("Dylan (dsmith)");
result.Last().Should().Be("Tom (tcruise)");
}

[Fact]
public async Task GetPipelines_Should_Return_All_Pipelines()
{
Expand Down

0 comments on commit 9ea87ed

Please sign in to comment.