Skip to content

Commit

Permalink
Update for application-scoped credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
InedoJohn committed Aug 12, 2019
1 parent 564fa23 commit 325562d
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 10 deletions.
27 changes: 26 additions & 1 deletion Git/GitHub.InedoExtension/Credentials/GitHubCredentials.cs
@@ -1,6 +1,9 @@
using System.ComponentModel;
using Inedo.Documentation;
using Inedo.Extensibility;
using Inedo.Extensibility.Credentials;
using Inedo.Extensibility.ListVariableSources;
using Inedo.Extensibility.Operations;
using Inedo.Extensions.Credentials;
using Inedo.Extensions.GitHub.Clients;
using Inedo.Extensions.GitHub.SuggestionProviders;
Expand All @@ -9,12 +12,14 @@

namespace Inedo.Extensions.GitHub.Credentials
{
[ScriptAlias("GitHub")]
[ScriptAlias(GitHubCredentials.TypeName)]
[DisplayName("GitHub")]
[Description("Credentials for GitHub.")]
[PersistFrom("Inedo.Extensions.Credentials.GitHubCredentials,GitHub")]
public sealed class GitHubCredentials : GitCredentialsBase
{
public const string TypeName = "GitHub";

[Persistent]
[DisplayName("API URL")]
[PlaceholderText(GitHubClient.GitHubComUrl)]
Expand Down Expand Up @@ -47,5 +52,25 @@ public override RichDescription GetDescription()

return desc;
}

internal static GitHubCredentials TryCreate(string name, ValueEnumerationContext context)
{
return (GitHubCredentials)ResourceCredentials.TryCreate(GitHubCredentials.TypeName, name, environmentId: null, applicationId: context.ProjectId, inheritFromParent: false);
}

internal static GitHubCredentials TryCreate(string name, IComponentConfiguration config)
{
int? projectId = AH.ParseInt(AH.CoalesceString(config["ProjectId"], config["ApplicationId"]));
int? environmentId = AH.ParseInt(config["EnvironmentId"]);

return (GitHubCredentials)ResourceCredentials.TryCreate(GitHubCredentials.TypeName, name, environmentId: environmentId, applicationId: projectId, inheritFromParent: false);
}
internal static GitHubCredentials TryCreate(string name, IOperationConfiguration config)
{
int? projectId = AH.ParseInt(AH.CoalesceString(config["ProjectId"], config["ApplicationId"]));
int? environmentId = AH.ParseInt(config["EnvironmentId"]);

return (GitHubCredentials)ResourceCredentials.TryCreate(GitHubCredentials.TypeName, name, environmentId: environmentId, applicationId: projectId, inheritFromParent: false);
}
}
}
Expand Up @@ -124,7 +124,7 @@ public override async Task ExecuteAsync(IOperationExecutionContext context)

protected override ExtendedRichDescription GetDescription(IOperationConfiguration config)
{
var credentials = string.IsNullOrEmpty(config[nameof(CredentialName)]) ? null : ResourceCredentials.Create<GitHubCredentials>(config[nameof(CredentialName)]);
var credentials = string.IsNullOrEmpty(config[nameof(CredentialName)]) ? null : GitHubCredentials.TryCreate(config[nameof(CredentialName)], config);
var repositoryOwner = AH.CoalesceString(config[nameof(OrganizationName)], credentials?.OrganizationName, config[nameof(UserName)], credentials?.UserName, "(unknown)");
var repositoryName = AH.CoalesceString(config[nameof(RepositoryName)], credentials?.RepositoryName, "(unknown)");
return new ExtendedRichDescription(
Expand Down
Expand Up @@ -3,7 +3,6 @@
using System.Threading;
using System.Threading.Tasks;
using Inedo.Extensibility;
using Inedo.Extensibility.Credentials;
using Inedo.Extensions.GitHub.Clients;
using Inedo.Extensions.GitHub.Credentials;
using Inedo.Web;
Expand All @@ -19,7 +18,9 @@ public async Task<IEnumerable<string>> GetSuggestionsAsync(IComponentConfigurati
if (string.IsNullOrEmpty(credentialName))
return Enumerable.Empty<string>();

var credentials = ResourceCredentials.Create<GitHubCredentials>(credentialName);
var credentials = GitHubCredentials.TryCreate(credentialName, config);
if (credentials == null)
return Enumerable.Empty<string>();

string ownerName = AH.CoalesceString(credentials.OrganizationName, credentials.UserName);
string repositoryName = AH.CoalesceString(config["RepositoryName"], credentials.RepositoryName);
Expand Down
Expand Up @@ -19,7 +19,9 @@ public async Task<IEnumerable<string>> GetSuggestionsAsync(IComponentConfigurati
if (string.IsNullOrEmpty(credentialName))
return Enumerable.Empty<string>();

var credentials = ResourceCredentials.Create<GitHubCredentials>(credentialName);
var credentials = GitHubCredentials.TryCreate(credentialName, config);
if (credentials == null)
return Enumerable.Empty<string>();

string ownerName = AH.CoalesceString(credentials.OrganizationName, credentials.UserName);

Expand Down
Expand Up @@ -19,7 +19,9 @@ public async Task<IEnumerable<string>> GetSuggestionsAsync(IComponentConfigurati
if (string.IsNullOrEmpty(credentialName))
return Enumerable.Empty<string>();

var credentials = ResourceCredentials.Create<GitHubCredentials>(credentialName);
var credentials = GitHubCredentials.TryCreate(credentialName, config);
if (credentials == null)
return Enumerable.Empty<string>();

string ownerName = AH.CoalesceString(credentials.OrganizationName, credentials.UserName);

Expand Down
27 changes: 26 additions & 1 deletion Git/GitLab.InedoExtension/Credentials/GitLabCredentials.cs
Expand Up @@ -2,6 +2,9 @@
using System.Security;
using Inedo.Documentation;
using Inedo.Extensibility;
using Inedo.Extensibility.Credentials;
using Inedo.Extensibility.ListVariableSources;
using Inedo.Extensibility.Operations;
using Inedo.Extensions.Credentials;
using Inedo.Extensions.GitLab.Clients;
using Inedo.Extensions.GitLab.SuggestionProviders;
Expand All @@ -10,12 +13,14 @@

namespace Inedo.Extensions.GitLab.Credentials
{
[ScriptAlias("GitLab")]
[ScriptAlias(GitLabCredentials.TypeName)]
[DisplayName("GitLab")]
[Description("Credentials for GitLab.")]
[PersistFrom("Inedo.Extensions.Credentials.GitLabCredentials,GitLab")]
public sealed class GitLabCredentials : GitCredentialsBase
{
public const string TypeName = "GitLab";

[Persistent]
[DisplayName("API URL")]
[PlaceholderText(GitLabClient.GitLabComUrl)]
Expand Down Expand Up @@ -57,5 +62,25 @@ public override RichDescription GetDescription()

return desc;
}

internal static GitLabCredentials TryCreate(string name, ValueEnumerationContext context)
{
return (GitLabCredentials)ResourceCredentials.TryCreate(GitLabCredentials.TypeName, name, environmentId: null, applicationId: context.ProjectId, inheritFromParent: false);
}

internal static GitLabCredentials TryCreate(string name, IComponentConfiguration config)
{
int? projectId = AH.ParseInt(AH.CoalesceString(config["ProjectId"], config["ApplicationId"]));
int? environmentId = AH.ParseInt(config["EnvironmentId"]);

return (GitLabCredentials)ResourceCredentials.TryCreate(GitLabCredentials.TypeName, name, environmentId: environmentId, applicationId: projectId, inheritFromParent: false);
}
internal static GitLabCredentials TryCreate(string name, IOperationConfiguration config)
{
int? projectId = AH.ParseInt(AH.CoalesceString(config["ProjectId"], config["ApplicationId"]));
int? environmentId = AH.ParseInt(config["EnvironmentId"]);

return (GitLabCredentials)ResourceCredentials.TryCreate(GitLabCredentials.TypeName, name, environmentId: environmentId, applicationId: projectId, inheritFromParent: false);
}
}
}
Expand Up @@ -19,7 +19,9 @@ public async Task<IEnumerable<string>> GetSuggestionsAsync(IComponentConfigurati
if (string.IsNullOrEmpty(credentialName))
return Enumerable.Empty<string>();

var credentials = ResourceCredentials.Create<GitLabCredentials>(credentialName);
var credentials = GitLabCredentials.TryCreate(credentialName, config);
if (credentials == null)
return Enumerable.Empty<string>();

string ownerName = AH.CoalesceString(credentials.GroupName, credentials.UserName);

Expand Down
Expand Up @@ -19,7 +19,9 @@ public async Task<IEnumerable<string>> GetSuggestionsAsync(IComponentConfigurati
if (string.IsNullOrEmpty(credentialName))
return Enumerable.Empty<string>();

var credentials = ResourceCredentials.Create<GitLabCredentials>(credentialName);
var credentials = GitLabCredentials.TryCreate(credentialName, config);
if (credentials == null)
return Enumerable.Empty<string>();

string ownerName = AH.CoalesceString(credentials.GroupName, credentials.UserName);
string repositoryName = AH.CoalesceString(config["ProjectName"], credentials.ProjectName);
Expand Down
Expand Up @@ -19,7 +19,9 @@ public async Task<IEnumerable<string>> GetSuggestionsAsync(IComponentConfigurati
if (string.IsNullOrEmpty(credentialName))
return Enumerable.Empty<string>();

var credentials = ResourceCredentials.Create<GitLabCredentials>(credentialName);
var credentials = GitLabCredentials.TryCreate(credentialName, config);
if (credentials == null)
return Enumerable.Empty<string>();

string ownerName = AH.CoalesceString(credentials.GroupName, credentials.UserName);

Expand Down

0 comments on commit 325562d

Please sign in to comment.