Skip to content
Open
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
43 changes: 42 additions & 1 deletion src/code/PublishHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,12 @@ private bool PushNupkg(string outputNupkgDir, string repoName, string repoUri, N

try
{
using (var packageReader = new PackageArchiveReader(fullNupkgFile))
{
var packageIdentity = packageReader.GetIdentity();
_pkgName = packageIdentity.Id;
_pkgVersion = packageIdentity.Version;
}
PushRunner.Run(
settings: Settings.LoadDefaultSettings(root: null, configFileName: null, machineWideSettings: null),
sourceProvider: sourceProvider,
Expand All @@ -699,6 +705,8 @@ private bool PushNupkg(string outputNupkgDir, string repoName, string repoUri, N
}
catch (HttpRequestException e)
{
string normalizedRepoUri = repoUri.TrimEnd('/');
bool isPSGallery = normalizedRepoUri.Equals(RepositorySettings.PSGalleryRepoUri, StringComparison.OrdinalIgnoreCase);
_cmdletPassedIn.WriteVerbose(string.Format("Not able to publish resource to '{0}'", repoUri));
// look in PS repo for how httpRequestExceptions are handled

Expand Down Expand Up @@ -735,6 +743,14 @@ private bool PushNupkg(string outputNupkgDir, string repoName, string repoUri, N
ErrorCategory.AuthenticationError,
this);
}
else if (isPSGallery && e.Message.Contains("Unauthorized"))
{
// For AKS Exception
error = new ErrorRecord(new ArgumentException($"Could not publish to repository '{repoName}'. The Credential provided was incorrect. Exception: Response status code does not indicate success: 401 (An API key must be provided)."),
Comment on lines +746 to +749
"401Error",
ErrorCategory.AuthenticationError,
this);
}
else
{
// For ADO repository feeds that are public feeds, when the credentials are incorrect.
Expand Down Expand Up @@ -764,6 +780,15 @@ private bool PushNupkg(string outputNupkgDir, string repoName, string repoUri, N
ErrorCategory.PermissionDenied,
this);
}
else if (isPSGallery && e.Message.Contains("Forbidden"))
{
// For AKS Exception
error = new ErrorRecord(
new ArgumentException($"Could not publish to repository '{repoName}'. Response status code does not indicate success: 403 (The specified API key is invalid, has expired, or does not have permission to access the specified package.)."),
"403Error",
ErrorCategory.PermissionDenied,
this);
}
else
{
error = new ErrorRecord(
Expand All @@ -775,10 +800,26 @@ private bool PushNupkg(string outputNupkgDir, string repoName, string repoUri, N
}
else if (e.Message.Contains("409"))
{
error = new ErrorRecord(
if (isPSGallery && e.Message.Contains("Conflict"))
{
// For AKS Exception
string packageName = _pkgName ?? "unknown";
string packageVersion = _pkgVersion?.ToNormalizedString() ?? "unknown";
error = new ErrorRecord(
new ArgumentException(
$"Repository '{repoName}': Response status code does not indicate success: 409 " +
$"(A package with id '{packageName}' and version '{packageVersion}' already exists and cannot be modified.).",
e),
"409Error",
ErrorCategory.PermissionDenied, this);
}
else
{
error = new ErrorRecord(
ex,
"409Error",
ErrorCategory.PermissionDenied, this);
}
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/code/RepositorySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal static class RepositorySettings
// File name for a user's repository store file is 'PSResourceRepository.xml'
// The repository store file's location is currently only at '%LOCALAPPDATA%\PSResourceGet' for the user account.
private const string PSGalleryRepoName = "PSGallery";
private const string PSGalleryRepoUri = "https://www.powershellgallery.com/api/v2";
internal const string PSGalleryRepoUri = "https://www.powershellgallery.com/api/v2";
private const string MARRepoName = "MicrosoftArtifactRegistry";
private const string MARRepoUri = "https://mcr.microsoft.com";
private const int DefaultPriority = 50;
Expand Down
Loading