-
Notifications
You must be signed in to change notification settings - Fork 10
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
christianbumann
wants to merge
1
commit into
cake-contrib:develop
from
christianbumann:feature/gh-264
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")] | ||
[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); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/Cake.AzureDevOps/Collections/AzureDevOpsCollectionSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
41
src/Cake.AzureDevOps/Projects/AzureDevOpsProjectsHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
src/Cake.AzureDevOps/Projects/TeamProjectReferenceExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Category should be
Projects