Attempting to recreate the dashboard programatically with limitations

I want to recreate the octopus dashboard but only limit it to the specific project and channel and if possible, a specific release.
The idea is to give the use an option of choosing a release and then getting a single line of output of information.

I can use the following to get the information from the dashboard
string octoUrl = “Your Octopus URL” ;
string apiKey = “Your API Key”;
var client = new OctopusClient(new OctopusServerEndpoint(octoUrl, apiKey));
var repo = new OctopusRepository(client);
var Dashboard = repo.Dashboards.GetDashboard();
var Items = Dashboard.Items

I can then filter the items for the specific project and channel that I want.

How can I from here work out the status of a specific release on that channel to reproduce that line, ie success, fail, not deployed at all.
Thanks

Hi,

Thanks for getting in touch! From each item on the Dashboard has a ReleaseId, which is the first piece of the puzzle. This will let you know if the release is the the latest (Items.IsCurrent).

You probably need to attack this through the getting the release first, and finding all the associated deployments. eg:

var project = repository.Projects.FindByName("Test");
var env = repository.Environments.FindByName("Local");
//var release = repository.Projects.GetReleaseByVersion(project, "1.0.0");
var release = repository.Projects.GetReleases(project).Items.First();
var deployments = repository.Deployments.FindAll(new[] { project.Id }, new[] { env.Id });
if (deployments.Items.Any())
{
    var task = repository.Deployments.GetTask(deployments.Items.First());
    var state = task.State.ToString();
}
else
{
    // Not Deployed
}

Hope that helps!

Robert W

Thanks for getting back to me so quickly.
This works for getting the results for one environment in the project, but I need to get the information for all environments in a channel in a project.
Is is possible to query a project / channel for a list of all environments that I can then pass to the deployments.findall?

Thanks

Hi,

The environments available for a channel/project is determined by the lifecycle. Since the web UI is built on top of this same API, you can figure out the relationships between the entities through that. Also check the calls the web UI makes to the API.

This should do it:

var project = repository.Projects.Get("Test");
var channel = repository.Channels.FindByName(project, "Default");
var channelLifecycleId = channel.LifecycleId ?? project.LifecycleId; // If channel lifecycle is null, the project's lifecycle is used
var lifecycle = repository.Lifecycles.Get(channelLifecycleId);

if (lifecycle.Phases.Any(p => p.AutomaticDeploymentTargets.Union(p.OptionalDeploymentTargets).Count() == 0))
{
    // One of the phases is setup to deploy to remaining environments
    // therefore this lifecycle deploys to all environments
}
else
{
    var environments = lifecycle.Phases.SelectMany(p => p.AutomaticDeploymentTargets.Union(p.OptionalDeploymentTargets));
}

Robert W

That is a very clever way of getting the information.
I suspect that is what I need.
Thank you

Hi,
I thought I had this, but I still seem to be missing a crucial step.

I am trying to determine the state of past releases.

I would have though that that:
var dashboard = _octopus.Repository.Dashboards.GetDashboard();
var dashboardItems = dashboard.Items.Where(d=>d.ChannelId.Equals(channelID))
.Where(d=>d.ReleaseId.Equals(releaseID));

would give me the specific line in the dashboard for that release.

However, all of these items have the state of “Success” even when some of those environments have clearly failed. Similarly:
IsCompleted = true,
HasPendingInterruptions =false,
HasWarningsOrErrors =false

How do I determine if a task on an environment has been successful, failed or not deployed at all?

Thanks

Hi,

As sorry, I maybe misunderstood. The Dashboard will get you the latest deployment for a given environment and project. It sounds like you want the latest deployment for a release.

Octopus shows that in the UI, on the release details page (eg https://demo.octopusdeploy.com/app#/projects/octofx-rate-service/releases/3.2.99214) . Looking at the API calls, that is populated via the call https://demo.octopusdeploy.com/api/releases/Releases-191453/deployments?skip=0

That would map to the Octopus.Client method repository.Releases.GetDeployments(release). So if you then filter/group that by environment and then sort by Created descending, that should get you your answer. To get the time it actually started or completed, you should get the related task.

You could also search for events that relate to that release and (optionally) environment. repository.Events.List(regardingDocumentId: $"{environmentId},{releaseId}") should do it. That will perform a query like: https://demo.octopusdeploy.com/api/events?skip=0&regarding=Releases-191453,Environments-1

Hope that helps

Robert W