Skip to content

Commit

Permalink
#104 GitRepositoryMonitor: added support to override secure resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Hessinger committed Mar 4, 2021
1 parent ccce550 commit 8776608
Showing 1 changed file with 46 additions and 14 deletions.
60 changes: 46 additions & 14 deletions Git/Git.InedoExtension/RepositoryMonitors/GitRepositoryMonitor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security;
using System.Threading.Tasks;
using Inedo.Agents;
using Inedo.Diagnostics;
Expand All @@ -22,7 +23,6 @@ namespace Inedo.Extensions.Git.RepositoryMonitors
[Description("Monitors a Git repository for new commits.")]
public sealed class GitRepositoryMonitor : RepositoryMonitor, IMissingPersistentPropertyHandler
{
[Required]
[Persistent]
[DisplayName("From Git resource")]
[SuggestableValue(typeof(SecureResourceSuggestionProvider<GitSecureResourceBase>))]
Expand All @@ -34,6 +34,22 @@ void IMissingPersistentPropertyHandler.OnDeserializedMissingProperties(IReadOnly
this.ResourceName = value;
}

[Persistent]
[DisplayName("Repository URL")]
[Category("Connection/Identity")]
public string RepositoryUrl { get; set; }

[Persistent]
[DisplayName("User name")]
[Category("Connection/Identity")]
public string UserName { get; set; }

[Persistent]
[DisplayName("Password")]
[Category("Connection/Identity")]
public SecureString Password { get; set; }


[Persistent]
[Category("Advanced")]
[DisplayName("Git executable path")]
Expand All @@ -57,28 +73,44 @@ public override RichDescription GetDescription()
{
return new RichDescription(
"Git repository at ",
new Hilite(this.ResourceName)
new Hilite(string.IsNullOrWhiteSpace(this.RepositoryUrl) ? this.ResourceName : this.RepositoryUrl)
);
}

private async Task<GitClient> CreateClientAsync(IRepositoryMonitorContext context)
{
if(string.IsNullOrWhiteSpace(this.ResourceName) && string.IsNullOrWhiteSpace(this.RepositoryUrl))
{
throw new InvalidOperationException("You must specify either a From Git resource or a Repository URL");
}

var credctx = (ICredentialResolutionContext)context;
var resource = (GitSecureResourceBase)SecureResource.Create(this.ResourceName, credctx);
var credential = resource?.GetCredentials(credctx);
if (resource == null)
GitSecureResourceBase resource = null;
SecureCredentials credential = null;
Extensions.Credentials.UsernamePasswordCredentials upcreds = null;
if (!string.IsNullOrWhiteSpace(this.ResourceName))
{
var rc = SecureCredentials.Create(this.ResourceName, credctx) as GitCredentialsBase;
resource = (GitSecureResourceBase)rc?.ToSecureResource();
credential = rc?.ToSecureCredentials();
resource = (GitSecureResourceBase)SecureResource.Create(this.ResourceName, credctx);
credential = resource?.GetCredentials(credctx);
if (resource == null)
{
var rc = SecureCredentials.Create(this.ResourceName, credctx) as GitCredentialsBase;
resource = (GitSecureResourceBase)rc?.ToSecureResource();
credential = rc?.ToSecureCredentials();
}

upcreds = credential as Extensions.Credentials.UsernamePasswordCredentials;
if (credential is GitSecureCredentialsBase gitcreds)
upcreds = gitcreds.ToUsernamePassword();
else if (credential != null && upcreds == null)
throw new InvalidOperationException("Invalid credential type for Git repository monitor.");
}
var repositoryUrl = !string.IsNullOrWhiteSpace(this.RepositoryUrl) ? this.RepositoryUrl : await resource.GetRepositoryUrlAsync(credctx, context.CancellationToken);

var repositoryUrl = await resource.GetRepositoryUrlAsync(credctx, context.CancellationToken);
var upcreds = credential as Extensions.Credentials.UsernamePasswordCredentials;
if (credential is GitSecureCredentialsBase gitcreds)
upcreds = gitcreds.ToUsernamePassword();
else if (credential != null && upcreds == null)
throw new InvalidOperationException("Invalid credential type for Git repository monitor.");
if(!string.IsNullOrWhiteSpace(this.UserName) || this.Password != null)
{
upcreds = new Extensions.Credentials.UsernamePasswordCredentials { UserName = this.UserName, Password = this.Password };
}

if (!string.IsNullOrEmpty(this.GitExePath))
{
Expand Down

0 comments on commit 8776608

Please sign in to comment.