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

(GH-264) Add an alias to get all projects for a collection #268

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/Cake.AzureDevOps/AzureDevOpsAliases.Projects.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace Cake.AzureDevOps
{
using System.Collections.Generic;
using Cake.AzureDevOps.Collections;
using Cake.AzureDevOps.Projects;
using Cake.Core;
using Cake.Core.Annotations;

/// <content>
/// Contains functionality related to Azure DevOps projects.
/// </content>
public static partial class AzureDevOpsAliases
{
/// <summary>
/// Gets all projects or a collection.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="settings">Settings for getting the projects.</param>
/// <example>
/// <para>Get the projects associated with an Azure DevOps collection:</para>
/// <code>
/// <![CDATA[
/// var collectionSettings =
/// new AzureDevOpsCollectionSettings(
/// new Uri("http://myserver:8080/defaultcollection"),
/// AzureDevOpsAuthenticationNtlm());
///
/// var projects =
/// AzureDevOpsProjects(
/// collectionSettings);
/// ]]>
/// </code>
/// </example>
/// <returns>The projects or an empty list of projects if no projects were found for the <paramref name="settings"/>.</returns>
[CakeMethodAlias]
[CakeAliasCategory("Azure Pipelines")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Category should be Projects

[CakeNamespaceImport("Cake.AzureDevOps.Projects")]
[CakeNamespaceImport("Cake.AzureDevOps.Collections")]
public static IEnumerable<AzureDevOpsProject> AzureDevOpsProjects(
this ICakeContext context,
AzureDevOpsCollectionSettings settings)
{
context.NotNull(nameof(context));
settings.NotNull(nameof(settings));

return AzureDevOpsProjectsHelper.GetAzureDevOpsProjects(context.Log, settings);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace Cake.AzureDevOps.Collections
{
using Cake.AzureDevOps.Authentication;

/// <summary>
/// Settings for aliases handling collections.
/// </summary>
public class AzureDevOpsCollectionSettings : BaseAzureDevOpsCollectionSettings
{
/// <summary>
/// Initializes a new instance of the <see cref="AzureDevOpsCollectionSettings"/> class
/// based on the instance of a <see cref="AzureDevOpsCollectionSettings"/> class.
/// </summary>
/// <param name="settings">Settings containing the parameters.</param>
public AzureDevOpsCollectionSettings(AzureDevOpsCollectionSettings settings)
: base(settings)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="AzureDevOpsCollectionSettings"/> class using environment variables
/// as set by an Azure Pipelines build.
/// </summary>
/// <param name="credentials">Credentials to use to authenticate against Azure DevOps.</param>
public AzureDevOpsCollectionSettings(IAzureDevOpsCredentials credentials)
: base(credentials)
{
}

/// <summary>
/// Constructs the settings object using the access token provided by Azure Pipelines.
/// </summary>
/// <returns>The instance of <see cref="AzureDevOpsCollectionSettings"/> class.</returns>
public static AzureDevOpsCollectionSettings UsingAzurePipelinesOAuthToken()
{
var accessToken = EnvironmentVariableHelper.GetSystemAccessToken();
return new AzureDevOpsCollectionSettings(new AzureDevOpsOAuthCredentials(accessToken));
}
}
}
30 changes: 30 additions & 0 deletions src/Cake.AzureDevOps/IProjectClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace Cake.AzureDevOps
{
using System;
using Cake.AzureDevOps.Authentication;
using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.VisualStudio.Services.Identity;

/// <summary>
/// The interface for a Git client factory.
/// </summary>
internal interface IProjectClientFactory
{
/// <summary>
/// Creates the instance of the <see cref="ProjectHttpClient"/> class.
/// </summary>
/// <param name="collectionUrl">The URL of the Azure DevOps team project collection.</param>
/// <param name="credentials">The credentials to connect to Azure DevOps.</param>
/// <returns>The instance of <see cref="ProjectHttpClient"/> class.</returns>
ProjectHttpClient CreateProjectClient(Uri collectionUrl, IAzureDevOpsCredentials credentials);

/// <summary>
/// Creates the instance of the <see cref="ProjectHttpClient"/> class.
/// </summary>
/// <param name="collectionUrl">The URL of the Azure DevOps team project collection.</param>
/// <param name="credentials">The credentials to connect to Azure DevOps.</param>
/// <param name="authorizedIdentity">Returns identity which is authorized.</param>
/// <returns>The instance of <see cref="ProjectHttpClient"/> class.</returns>
ProjectHttpClient CreateProjectClient(Uri collectionUrl, IAzureDevOpsCredentials credentials, out Identity authorizedIdentity);
}
}
35 changes: 35 additions & 0 deletions src/Cake.AzureDevOps/ProjectClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Cake.AzureDevOps
{
using System;
using Cake.AzureDevOps.Authentication;
using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.VisualStudio.Services.Identity;
using Microsoft.VisualStudio.Services.WebApi;

/// <inheritdoc />
internal class ProjectClientFactory : IProjectClientFactory
{
/// <inheritdoc/>
public ProjectHttpClient CreateProjectClient(Uri collectionUrl, IAzureDevOpsCredentials credentials)
{
return this.CreateProjectClient(collectionUrl, credentials, out _);
}

/// <inheritdoc/>
public ProjectHttpClient CreateProjectClient(Uri collectionUrl, IAzureDevOpsCredentials credentials, out Identity authorizedIdentity)
{
var connection =
new VssConnection(collectionUrl, credentials.ToVssCredentials());

authorizedIdentity = connection.AuthorizedIdentity;

var projectClient = connection.GetClient<ProjectHttpClient>();
if (projectClient == null)
{
throw new AzureDevOpsException("Could not retrieve the ProjectHttpClient object");
}

return projectClient;
}
}
}
20 changes: 20 additions & 0 deletions src/Cake.AzureDevOps/Projects/AzureDevOpsProject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Cake.AzureDevOps.Projects
{
using System;

/// <summary>
/// Class for writing issues to Azure DevOps pull requests.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong comment

/// </summary>
public sealed class AzureDevOpsProject
{
/// <summary>
/// Gets the project identifier.
/// </summary>
public Guid Id { get; internal set; }

/// <summary>
/// Gets the project name.
/// </summary>
public string Name { get; internal set; }
}
}
41 changes: 41 additions & 0 deletions src/Cake.AzureDevOps/Projects/AzureDevOpsProjectsHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace Cake.AzureDevOps.Projects
{
using System.Collections.Generic;
using System.Linq;
using Cake.AzureDevOps.Collections;
using Cake.Core.Diagnostics;

/// <summary>
/// Provides functions for AzureDevOps projects.
/// </summary>
internal static class AzureDevOpsProjectsHelper
{
/// <summary>
/// Gets the projects for the parameter <paramref name="settings"/>.
/// </summary>
/// <param name="log">The Cake log context.</param>
/// <param name="settings">Settings for getting the collection.</param>
/// <returns>The projects or an empty list of projects if no projects were found for the <paramref name="settings"/>.</returns>
internal static IEnumerable<AzureDevOpsProject> GetAzureDevOpsProjects(
ICakeLog log,
AzureDevOpsCollectionSettings settings)
{
log.NotNull(nameof(log));
settings.NotNull(nameof(settings));

using (var projectHttpClient = new ProjectClientFactory().CreateProjectClient(settings.CollectionUrl, settings.Credentials))
{
var projects =
projectHttpClient
.GetProjects()
.ConfigureAwait(false)
.GetAwaiter()
.GetResult()
.Select(x => x.ToAzureDevOpsProject())
.ToList();

return projects;
}
}
}
}
27 changes: 27 additions & 0 deletions src/Cake.AzureDevOps/Projects/TeamProjectReferenceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Cake.AzureDevOps.Projects
{
using Microsoft.TeamFoundation.Core.WebApi;

/// <summary>
/// Extensions for the <see cref="TeamProjectReference"/> class.
/// </summary>
internal static class TeamProjectReferenceExtensions
{
/// <summary>
/// Converts a <see cref="TeamProjectReference"/> to an <see cref="AzureDevOpsProject"/>.
/// </summary>
/// <param name="teamProjectReference">Team project to convert.</param>
/// <returns>Converted team project.</returns>
public static AzureDevOpsProject ToAzureDevOpsProject(this TeamProjectReference teamProjectReference)
{
teamProjectReference.NotNull(nameof(teamProjectReference));

return
new AzureDevOpsProject
{
Id = teamProjectReference.Id,
Name = teamProjectReference.Name,
};
}
}
}