Skip to content

Commit

Permalink
fix: Corrected null reference exception when release version wasn't s…
Browse files Browse the repository at this point in the history
…pecified (#239)
  • Loading branch information
slewis74 authored Aug 24, 2022
1 parent 0db67c4 commit e8ca274
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ jobs:
- name: Install Octopus CLI 🐙
uses: OctopusDeploy/install-octopus-cli-action@v1
with:
version: latest
version: 9.1.5

- name: Push a package to Octopus Deploy 🐙
uses: OctopusDeploy/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion source/Octo.Tests/Commands/CreateReleaseCommandFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void ShouldLoadOptionsFromFile()
});

Assert.AreEqual("Test Project", createReleaseCommand.ProjectNameOrId);
Assert.AreEqual("1.0.0", createReleaseCommand.VersionNumber);
Assert.AreEqual("1.0.0", createReleaseCommand.VersionNumberParameterValue);
Assert.AreEqual("Test config file.", createReleaseCommand.ReleaseNotes);
}

Expand Down
41 changes: 23 additions & 18 deletions source/Octopus.Cli/Commands/Releases/CreateReleaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
using Octopus.CommandLine.Commands;
using Serilog.Events;

#nullable enable

namespace Octopus.Cli.Commands.Releases
{
[Command("create-release", Description = "Creates (and, optionally, deploys) a release.")]
public class CreateReleaseCommand : DeploymentCommandBase, ISupportFormattedOutput
{
readonly IReleasePlanBuilder releasePlanBuilder;
ReleaseResource release;
ProjectResource project;
ReleasePlan plan;
string versionNumber;
ReleaseResource? release;
ProjectResource? project;
ReleasePlan? plan;
string? versionNumber;

public CreateReleaseCommand(IOctopusAsyncRepositoryFactory repositoryFactory,
IOctopusFileSystem fileSystem,
Expand All @@ -45,7 +47,7 @@ public CreateReleaseCommand(IOctopusAsyncRepositoryFactory repositoryFactory,
options.Add<string>("defaultPackageVersion=|packageVersion=", "Default version number of all packages to use for this release. Override per-package using --package.", versionResolver.Default);
options.Add<string>("gitCommit=", "[Optional] Git commit to use when creating the release. Use in conjunction with the --gitRef parameter to select any previous commit.", v => GitCommit = v);
options.Add<string>("ref=|gitRef=", "[Optional] Git reference to use when creating the release.", v => GitReference = v);
options.Add<string>("version=|releaseNumber=", "[Optional] Release number to use for the new release.", v => VersionNumber = v);
options.Add<string>("version=|releaseNumber=", "[Optional] Release number to use for the new release.", v => VersionNumberParameterValue = v);
options.Add<string>("channel=", "[Optional] Name or ID of the channel to use for the new release. Omit this argument to automatically select the best channel.", v => ChannelNameOrId = v);
options.Add<string>("package=", "[Optional] Version number to use for a package in the release. Format: StepName:Version or PackageID:Version or StepName:PackageName:Version. StepName, PackageID, and PackageName can be replaced with an asterisk. An asterisk will be assumed for StepName, PackageID, or PackageName if they are omitted.", v => versionResolver.Add(v), allowsMultiple: true);
options.Add<string>("packagesFolder=",
Expand All @@ -67,19 +69,19 @@ public CreateReleaseCommand(IOctopusAsyncRepositoryFactory repositoryFactory,
options.Add<string>("deployTo=", "[Optional] Name or ID of the environment to automatically deploy to, e.g., 'Production' or 'Environments-1'; specify this argument multiple times to deploy to multiple environments.", v => DeployToEnvironmentNamesOrIds.Add(v), allowsMultiple: true);
}

public string GitReference { get; set; }
public string GitCommit { get; set; }
public string ChannelNameOrId { get; set; }
public string VersionNumber { get; set; }
public string ReleaseNotes { get; set; }
public string? GitReference { get; set; }
public string? GitCommit { get; set; }
public string? ChannelNameOrId { get; set; }
public string? VersionNumberParameterValue { get; set; }
public string? ReleaseNotes { get; set; }
public bool IgnoreIfAlreadyExists { get; set; }
public bool IgnoreChannelRules { get; set; }
public string VersionPreReleaseTag { get; set; }
public string? VersionPreReleaseTag { get; set; }
public bool WhatIf { get; set; }

protected override async Task ValidateParameters()
{
if (VersionNumber.Contains(' ')) throw new CommandException($"Release version '{VersionNumber}' is invalid, version cannot contain whitespace.");
if (VersionNumberParameterValue?.Contains(' ') == true) throw new CommandException($"Release version '{VersionNumberParameterValue}' is invalid, version cannot contain whitespace.");
if (!string.IsNullOrWhiteSpace(ChannelNameOrId) && !await Repository.SupportsChannels().ConfigureAwait(false))
throw new CommandException("Your Octopus Server does not support channels, which was introduced in Octopus 3.2. Please upgrade your Octopus Server, or remove the --channel argument.");

Expand All @@ -97,9 +99,9 @@ public async Task Request()

plan = await BuildReleasePlan().ConfigureAwait(false);

if (!string.IsNullOrWhiteSpace(VersionNumber))
if (!string.IsNullOrWhiteSpace(VersionNumberParameterValue))
{
versionNumber = VersionNumber;
versionNumber = VersionNumberParameterValue;
commandOutputProvider.Debug("Using version number provided on command-line: {Version:l}", versionNumber);
}
else if (!string.IsNullOrWhiteSpace(plan.ReleaseTemplate.NextVersionIncrement))
Expand Down Expand Up @@ -312,10 +314,10 @@ async Task ReleaseNotesFallBackToDeploymentSettings()

// Continue using deprecated property on DeploymentSettings that exposes project for older server backwards compatibility
#pragma warning disable 618
var projectReleaseNotes = project.ReleaseNotesTemplate;
var projectReleaseNotes = project?.ReleaseNotesTemplate;
#pragma warning restore 618

if (project.IsVersionControlled)
if (project?.IsVersionControlled == true)
{
var deploymentSettings = await Repository.DeploymentSettings
.Get(project, GitCommit ?? GitReference)
Expand Down Expand Up @@ -350,6 +352,9 @@ public void PrintDefaultOutput()

public void PrintJsonOutput()
{
if (release is null) throw new CommandException("Release resource was not set");
if (project is null) throw new CommandException("Project resource was not set");
if (plan is null) throw new CommandException("Release plan was not set");
commandOutputProvider.Json(new
{
ReleaseId = release.Id,
Expand All @@ -371,12 +376,12 @@ public void PrintJsonOutput()
void ValidateProjectPersistenceRequirements()
{
var wasGitRefProvided = !string.IsNullOrEmpty(GitReference);
if (project.PersistenceSettings is GitPersistenceSettingsResource && !wasGitRefProvided)
if (project?.PersistenceSettings is GitPersistenceSettingsResource && !wasGitRefProvided)
{
throw new CommandException("No gitRef value provided. Please provide the --gitRef argument passing the branch name or commit that contains the project details for this release.");
}

if (!project.IsVersionControlled && wasGitRefProvided)
if (project?.IsVersionControlled == false && wasGitRefProvided)
throw new CommandException("Since the provided project is not a version controlled project,"
+ " the --gitCommit and --gitRef arguments are not supported for this command.");
}
Expand Down

0 comments on commit e8ca274

Please sign in to comment.