Skip to content

Commit

Permalink
Merge branch 'release/0.2.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalberger committed Nov 25, 2018
2 parents 6b13ff8 + dee1497 commit 35f04aa
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# TFS / Azure DevOps support for Cake
# Azure DevOps and Azure DevOps Server support for Cake

This addin for Cake allows you to work with Team Foundation Server or Azure DevOps.
This addin for Cake allows you to work with Azure DevOps or Azure DevOps Server.

[![License](http://img.shields.io/:license-mit-blue.svg)](https://github.com/cake-contrib/Cake.Tfs/blob/feature/build/LICENSE)

Expand Down
15 changes: 15 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pool:
vmImage: 'vs2017-win2016'

trigger:
- develop
- master
- release/*
- hotfix/*

pr:
- develop

steps:
- powershell: ./build.ps1
displayName: 'Cake Build'
2 changes: 1 addition & 1 deletion docs/input/docs/usage/vote-pullrequest.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Description: Example how to approve or vote for pull requests using the Cake.Tfs
---
The [Cake.Tfs addin] provides an alias for approving or voting on pull requests.

The following example will approve a pull request on a Team Foundation Server:
The following example will approve a pull request on a Azure DevOps Server:

```csharp
#addin "Cake.Tfs"
Expand Down
2 changes: 1 addition & 1 deletion docs/input/index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ NoGutter: true
<div class="container">
<h1>What is it?</h1>
<p>
The Tfs Addin for <a href="http://cakebuild.net/" target="_blank">Cake</a> allows you to access Team Foundation Server or Azure DevOps from Cake build scripts.
The Azure DevOps Addin for <a href="http://cakebuild.net/" target="_blank">Cake</a> allows you to access Azure DevOps or Azure DevOps Server from Cake build scripts.
</p>
</div>
6 changes: 3 additions & 3 deletions nuspec/nuget/Cake.Tfs.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<version>0.0.0</version>
<authors>Pascal Berger</authors>
<owners>pascalberger, cake-contrib</owners>
<summary>Addin for working with Team Foundation Server or Azure DevOps for the Cake build automation system</summary>
<summary>Addin for working with Azure DevOps or Azure DevOps Server for the Cake build automation system</summary>
<description>
This addin for Cake allows you to work with Team Foundation Server or Azure DevOps.
This addin for Cake allows you to work with Azure DevOps or Azure DevOps Server.
</description>
<licenseUrl>https://github.com/cake-contrib/Cake.Tfs/blob/develop/LICENSE</licenseUrl>
<projectUrl>http://cake-contrib.github.io/Cake.Tfs/</projectUrl>
Expand All @@ -17,7 +17,7 @@
<repository type="git" url="https://github.com/cake-contrib/Cake.Tfs"/>
<copyright>Copyright © Pascal Berger</copyright>
<tags>Cake Script Team-Foundation-Server TFS Azure-DevOps</tags>
<releaseNotes>https://github.com/cake-contrib/Cake.Tfs/releases/tag/0.2.3</releaseNotes>
<releaseNotes>https://github.com/cake-contrib/Cake.Tfs/releases/tag/0.2.4</releaseNotes>
</metadata>
<files>
<file src="net461\Cake.Tfs.dll" target="lib\net461" />
Expand Down
13 changes: 4 additions & 9 deletions src/Cake.Tfs.Tests/Cake.Tfs.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Cake.Core" Version="0.28.0" />
<PackageReference Include="Cake.Testing" Version="0.28.0" />
<PackageReference Include="Moq" Version="4.10.0" />
<PackageReference Include="Shouldly" Version="3.0.0" />
<PackageReference Include="Shouldly" Version="3.0.2" />
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.abstractions" Version="2.0.2" />
<PackageReference Include="xunit.assert" Version="2.4.0" />
<PackageReference Include="xunit.core" Version="2.4.0" />
<PackageReference Include="xunit.extensibility.core" Version="2.4.0" />
<PackageReference Include="xunit.extensibility.execution" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
</ItemGroup>

<ItemGroup>
Expand Down
37 changes: 37 additions & 0 deletions src/Cake.Tfs.Tests/PullRequest/Fakes/FakeAllSetGitClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,43 @@ protected override Mock<GitHttpClient> Setup(Mock<GitHttpClient> m)
State = status.State
});

// Setup CommitDiffs object
var gitChanges = new List<GitChange>
{
new GitChange
{
ChangeId = 1,
ChangeType = VersionControlChangeType.Edit,
Item = new GitItem("/src/project/myclass.cs", "ID1", GitObjectType.Commit, "6b13ff8", 0)
},
null,
new GitChange
{
ChangeId = 2,
ChangeType = VersionControlChangeType.Edit,
Item = new GitItem("/tools/folder", "ID2", GitObjectType.Tree, "6b13ff8", 0)
}
};

var gitCommitDiffs = new GitCommitDiffs
{
ChangeCounts = new Dictionary<VersionControlChangeType, int> { { VersionControlChangeType.Edit, 2 } },
Changes = gitChanges
};

m.Setup(arg => arg.GetCommitDiffsAsync(
It.IsAny<string>(),
It.IsAny<Guid>(),
true,
null,
null,
It.IsAny<GitBaseVersionDescriptor>(),
It.IsAny<GitTargetVersionDescriptor>(),
null,
CancellationToken.None))
.ReturnsAsync((string prj, Guid rId, bool? b, int? t, int? s, GitBaseVersionDescriptor bvd, GitTargetVersionDescriptor tvd, object o1, CancellationToken c1)
=> gitCommitDiffs);

return m;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Cake.Tfs.Tests.PullRequest.Fakes
{
using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Moq;
Expand All @@ -15,6 +16,10 @@ protected override Mock<GitHttpClient> Setup(Mock<GitHttpClient> m)
m.Setup(arg => arg.CreatePullRequestStatusAsync(It.IsAny<GitPullRequestStatus>(), It.IsAny<Guid>(), It.IsAny<int>(), It.IsAny<object>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(() => null);

m.Setup(arg => arg.GetCommitDiffsAsync(It.IsAny<string>(), It.IsAny<Guid>(), true, null, null, It.IsAny<GitBaseVersionDescriptor>(), It.IsAny<GitTargetVersionDescriptor>(), null, CancellationToken.None))
.ReturnsAsync(()
=> new GitCommitDiffs { ChangeCounts = new Dictionary<VersionControlChangeType, int>(), Changes = new List<GitChange>() });

return m;
}
}
Expand Down
70 changes: 70 additions & 0 deletions src/Cake.Tfs.Tests/PullRequest/TfsPullRequestTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace Cake.Tfs.Tests.PullRequest
{
using System.Collections.Generic;
using System.Linq;
using Cake.Core.IO;
using Cake.Tfs.PullRequest;
using Cake.Tfs.Tests.PullRequest.Fakes;
using Microsoft.VisualStudio.Services.Common;
Expand All @@ -23,6 +26,19 @@ public void Should_Throw_If_Log_Is_Null()
result.IsArgumentNullException("log");
}

[Fact]
public void Should_Throw_If_Log_Is_Null_Overload()
{
// Given
var fixture = new PullRequestFixture(PullRequestFixture.ValidTfsUrl, "foo") { Log = null };

// When
var result = Record.Exception(() => new TfsPullRequest(fixture.Log, fixture.Settings));

// Then
result.IsArgumentNullException("log");
}

[Fact]
public void Should_Throw_If_Settings_Are_Null()
{
Expand All @@ -36,6 +52,19 @@ public void Should_Throw_If_Settings_Are_Null()
result.IsArgumentNullException("settings");
}

[Fact]
public void Should_Throw_If_Settings_Are_Null_Overload()
{
// Given
var fixture = new PullRequestFixture(PullRequestFixture.ValidTfsUrl, 42) { Settings = null };

// When
var result = Record.Exception(() => new TfsPullRequest(fixture.Log, fixture.Settings));

// Then
result.IsArgumentNullException("settings");
}

[Fact]
public void Should_Throw_If_Git_Client_Factory_Is_Null()
{
Expand Down Expand Up @@ -421,5 +450,46 @@ public void Should_Throw_If_Null_Is_Returned_On_Tfs_Pull_Request()
result.IsTfsPullRequestNotFoundException();
}
}

public sealed class GetModifiedFiles
{
[Fact]
public void Should_Return_Empty_Collection_If_No_Changes_Found()
{
// Given
var fixture = new PullRequestFixture(PullRequestFixture.ValidTfsUrl, 42) { GitClientFactory = new FakeNullForMethodsGitClientFactory() };
var pullRequest = new TfsPullRequest(fixture.Log, fixture.Settings, fixture.GitClientFactory);

// When
var files = pullRequest.GetModifiedFiles();

// Then
files.ShouldBeOfType<List<FilePath>>();
files.ShouldNotBeNull();
files.ShouldBeEmpty();
}

[Fact]
public void Should_Return_Valid_Collection_Of_Modified_Files()
{
// Given
var fixture = new PullRequestFixture(PullRequestFixture.ValidTfsUrl, 42);
var pullRequest = new TfsPullRequest(fixture.Log, fixture.Settings, fixture.GitClientFactory);

// When
var files = pullRequest.GetModifiedFiles();

// Then
files.ShouldNotBeNull();
files.ShouldNotBeEmpty();
files.ShouldHaveSingleItem();

var filePath = files.First();
filePath.ShouldBeOfType<FilePath>();
filePath.ShouldNotBeNull();
filePath.FullPath.ShouldNotBeEmpty();
filePath.FullPath.ShouldBe("src/project/myclass.cs");
}
}
}
}
10 changes: 5 additions & 5 deletions src/Cake.Tfs/Cake.Tfs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@

<ItemGroup>
<PackageReference Include="Cake.Core" Version="0.28.0" />
<PackageReference Include="Costura.Fody" Version="3.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.6.1" />
<PackageReference Include="Microsoft.TeamFoundationServer.Client" Version="15.112.1" />
<PackageReference Include="Microsoft.VisualStudio.Services.InteractiveClient" Version="15.112.1" />
<PackageReference Include="Costura.Fody" Version="3.1.6" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.6.2" />
<PackageReference Include="Microsoft.TeamFoundationServer.Client" Version="15.131.1" />
<PackageReference Include="Microsoft.VisualStudio.Services.InteractiveClient" Version="15.131.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" />
<PackageReference Include="TfsUrlParser" Version="1.2.0" />
<PackageReference Include="TfsUrlParser" Version="1.3.0" />
</ItemGroup>

</Project>
69 changes: 69 additions & 0 deletions src/Cake.Tfs/PullRequest/TfsPullRequest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace Cake.Tfs.PullRequest
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Cake.Tfs;
using Microsoft.TeamFoundation.SourceControl.WebApi;
using TfsUrlParser;
Expand All @@ -19,6 +21,18 @@ public sealed class TfsPullRequest
private readonly RepositoryDescription repositoryDescription;
private readonly GitPullRequest pullRequest;

/// <summary>
/// Initializes a new instance of the <see cref="TfsPullRequest"/> class.
/// </summary>
/// <param name="log">The Cake log context.</param>
/// <param name="settings">Settings for accessing TFS.</param>
/// <exception cref="TfsPullRequestNotFoundException">If <see cref="TfsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/>
/// is set to <c>true</c> and no pull request could be found.</exception>
public TfsPullRequest(ICakeLog log, TfsPullRequestSettings settings)
: this(log, settings, new GitClientFactory())
{
}

/// <summary>
/// Initializes a new instance of the <see cref="TfsPullRequest"/> class.
/// </summary>
Expand Down Expand Up @@ -380,6 +394,61 @@ public void SetStatus(TfsPullRequestStatus status)
}
}

/// <summary>
/// Gets the files modified by the pull request.
/// </summary>
/// <returns>The collection of the modified files paths.</returns>
public IEnumerable<FilePath> GetModifiedFiles()
{
if (!this.ValidatePullRequest())
{
return new List<FilePath>();
}

var targetVersionDescriptor = new GitTargetVersionDescriptor
{
VersionType = GitVersionType.Commit,
Version = this.LastSourceCommitId
};

var baseVersionDescriptor = new GitBaseVersionDescriptor
{
VersionType = GitVersionType.Commit,
Version = this.LastTargetCommitId
};

using (var gitClient = this.gitClientFactory.CreateGitClient(this.CollectionUrl, this.settings.Credentials))
{
var commitDiffs = gitClient.GetCommitDiffsAsync(
this.ProjectName,
this.RepositoryId,
true, // bool? diffCommonCommit
null, // int? top
null, // int? skip
baseVersionDescriptor,
targetVersionDescriptor,
null, // object userState
CancellationToken.None).Result;

this.log.Verbose(
"Found {0} changed file(s) in the pull request",
commitDiffs.Changes.Count());

if (!commitDiffs.ChangeCounts.Any())
{
return new List<FilePath>();
}

return
from change in commitDiffs.Changes
where
change != null &&
!change.Item.IsFolder
select
new FilePath(change.Item.Path.TrimStart('/'));
}
}

/// <summary>
/// Validates if a pull request could be found.
/// Depending on <see cref="TfsPullRequestSettings.ThrowExceptionIfPullRequestCouldNotBeFound"/>
Expand Down

0 comments on commit 35f04aa

Please sign in to comment.