Skip to content

Commit

Permalink
feat: Task summary for deployments (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
benPearce1 authored May 17, 2023
1 parent ef76948 commit bd08fb7
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 34 deletions.
26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"yargs": "^17.5.1"
},
"dependencies": {
"@octopusdeploy/api-client": "^3.0.7",
"@octopusdeploy/api-client": "^3.0.8",
"azure-devops-node-api": "11.2.0",
"azure-pipelines-task-lib": "3.3.1",
"azure-pipelines-tool-lib": "1.3.2",
Expand Down
33 changes: 18 additions & 15 deletions source/tasks/CreateOctopusRelease/CreateOctopusReleaseV6/release.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Client, CreateReleaseCommandV1, Logger, Project, ProjectRepository, Space, SpaceRepository } from "@octopusdeploy/api-client";
import {
Client,
CreateReleaseCommandV1,
Logger,
Project,
ProjectRepository,
resolveSpaceId,
} from "@octopusdeploy/api-client";
import { OctoServerConnectionDetails } from "../../Utils/connection";
import { createReleaseFromInputs } from "./createRelease";
import { createCommandFromInputs } from "./inputCommandBuilder";
Expand Down Expand Up @@ -33,20 +40,16 @@ export class Release {
}

private async tryCreateSummary(client: Client, command: CreateReleaseCommandV1, version: string) {
const spaceRepo = new SpaceRepository(client);
const spaces = await spaceRepo.list({ partialName: command.spaceName });
const matchedSpaces = spaces.Items.filter((s: Space) => s.Name.localeCompare(command.spaceName) === 0);
if (matchedSpaces.length === 1) {
const projectRepo = new ProjectRepository(client, command.spaceName);
const projects = await projectRepo.list({ partialName: command.ProjectName });
const matchedProjects = projects.Items.filter((p: Project) => p.Name.localeCompare(command.ProjectName) === 0);
if (matchedProjects.length === 1) {
const link = `${this.connection.url}app#/${matchedSpaces[0].Id}/projects/${matchedProjects[0].Id}/deployments/releases/${version}`;
const markdown = `[Release ${version} created for '${matchedProjects[0].Name}'](${link})`;
const markdownFile = path.join(getVstsEnvironmentVariables().defaultWorkingDirectory, `${uuidv4()}.md`);
tasks.writeFile(markdownFile, markdown);
tasks.addAttachment("Distributedtask.Core.Summary", "Octopus Deploy", markdownFile);
}
const spaceId = await resolveSpaceId(client, command.spaceName);
const projectRepo = new ProjectRepository(client, command.spaceName);
const projects = await projectRepo.list({ partialName: command.ProjectName });
const matchedProjects = projects.Items.filter((p: Project) => p.Name.localeCompare(command.ProjectName) === 0);
if (matchedProjects.length === 1) {
const link = `${this.connection.url}app#/${spaceId}/projects/${matchedProjects[0].Id}/deployments/releases/${version}`;
const markdown = `[Release ${version} created for '${matchedProjects[0].Name}'](${link})`;
const markdownFile = path.join(getVstsEnvironmentVariables().defaultWorkingDirectory, `${uuidv4()}.md`);
tasks.writeFile(markdownFile, markdown);
tasks.addAttachment("Distributedtask.Core.Summary", "Octopus Create Release", markdownFile);
}
}
}
1 change: 0 additions & 1 deletion source/tasks/Deploy/DeployV6/createDeployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { TaskWrapper } from "../../Utils/taskInput";
import { ExecutionResult } from "../../Utils/executionResult";

export async function createDeploymentFromInputs(client: Client, command: CreateDeploymentUntenantedCommandV1, task: TaskWrapper, logger: Logger): Promise<ExecutionResult[]> {

logger.info?.("🐙 Deploying a release in Octopus Deploy...");

try {
Expand Down
38 changes: 36 additions & 2 deletions source/tasks/Deploy/DeployV6/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { Logger } from "@octopusdeploy/api-client";
import { Client, CreateDeploymentUntenantedCommandV1, Logger, resolveSpaceId, ServerTask, SpaceServerTaskRepository } from "@octopusdeploy/api-client";
import { OctoServerConnectionDetails } from "../../Utils/connection";
import { createDeploymentFromInputs } from "./createDeployment";
import { createCommandFromInputs } from "./inputCommandBuilder";
import os from "os";
import { TaskWrapper } from "tasks/Utils/taskInput";
import { getClient } from "../../Utils/client";
import path from "path";
import { getVstsEnvironmentVariables } from "../../../tasksLegacy/Utils/environment";
import { v4 as uuidv4 } from "uuid";
import { ExecutionResult } from "../../Utils/executionResult";
import * as tasks from "azure-pipelines-task-lib";

export class Deploy {
constructor(readonly connection: OctoServerConnectionDetails, readonly task: TaskWrapper, readonly logger: Logger) {}
Expand All @@ -14,7 +19,8 @@ export class Deploy {
const command = createCommandFromInputs(this.logger, this.task);
const client = await getClient(this.connection, this.logger, "release", "deploy", 6);

createDeploymentFromInputs(client, command, this.task, this.logger);
const results = await createDeploymentFromInputs(client, command, this.task, this.logger);
await this.tryCreateSummary(client, command, results);

this.task.setSuccess("Deployment succeeded.");
} catch (error: unknown) {
Expand All @@ -26,4 +32,32 @@ export class Deploy {
throw error;
}
}

private async tryCreateSummary(client: Client, command: CreateDeploymentUntenantedCommandV1, results: ExecutionResult[]) {
if (results.length === 0) {
return;
}

const spaceId = await resolveSpaceId(client, command.spaceName);
const taskRepo = new SpaceServerTaskRepository(client, command.spaceName);
const allTasks = await taskRepo.getByIds<{ DeploymentId: string }>(results.map((t) => t.serverTaskId));
const taskLookup = new Map<string, ServerTask<{ DeploymentId: string }>>();
allTasks.forEach(function (t) {
taskLookup.set(t.Id, t);
});

const url = this.connection.url;
let markdown = `${results[0].type} tasks\n\n`;
results.forEach(function (result) {
const task = taskLookup.get(result.serverTaskId);
if (task != null) {
const link = `${url}app#/${spaceId}/deployments/${task.Arguments.DeploymentId}`;
markdown += `[${result.environmentName}](${link})\n`;
}
});

const markdownFile = path.join(getVstsEnvironmentVariables().defaultWorkingDirectory, `${uuidv4()}.md`);
tasks.writeFile(markdownFile, markdown);
tasks.addAttachment("Distributedtask.Core.Summary", "Octopus Deploy", markdownFile);
}
}
39 changes: 37 additions & 2 deletions source/tasks/DeployTenant/TenantedDeployV6/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { Logger } from "@octopusdeploy/api-client";
import { CreateDeploymentTenantedCommandV1, Logger, Client, resolveSpaceId, SpaceServerTaskRepository, ServerTask } from "@octopusdeploy/api-client";
import { OctoServerConnectionDetails } from "../../Utils/connection";
import { createDeploymentFromInputs } from "./createDeployment";
import { createCommandFromInputs } from "./inputCommandBuilder";
import os from "os";
import { TaskWrapper } from "tasks/Utils/taskInput";
import { getClient } from "../../Utils/client";
import { ExecutionResult } from "../../Utils/executionResult";
import path from "path";
import { getVstsEnvironmentVariables } from "../../../tasksLegacy/Utils/environment";
import { v4 as uuidv4 } from "uuid";
import * as tasks from "azure-pipelines-task-lib";

export class Deploy {
constructor(readonly connection: OctoServerConnectionDetails, readonly task: TaskWrapper, readonly logger: Logger) {}
Expand All @@ -14,7 +19,9 @@ export class Deploy {
const command = createCommandFromInputs(this.logger, this.task);
const client = await getClient(this.connection, this.logger, "release", "deploy-tenanted", 6);

createDeploymentFromInputs(client, command, this.task, this.logger);
const results = await createDeploymentFromInputs(client, command, this.task, this.logger);

await this.tryCreateSummary(client, command, results);

this.task.setSuccess("Deployment succeeded.");
} catch (error: unknown) {
Expand All @@ -26,4 +33,32 @@ export class Deploy {
throw error;
}
}

private async tryCreateSummary(client: Client, command: CreateDeploymentTenantedCommandV1, results: ExecutionResult[]) {
if (results.length === 0) {
return;
}

const spaceId = await resolveSpaceId(client, command.spaceName);
const taskRepo = new SpaceServerTaskRepository(client, command.spaceName);
const allTasks = await taskRepo.getByIds<{ DeploymentId: string }>(results.map((t) => t.serverTaskId));
const taskLookup = new Map<string, ServerTask<{ DeploymentId: string }>>();
allTasks.forEach(function (t) {
taskLookup.set(t.Id, t);
});

const url = this.connection.url;
let markdown = `${results[0].type} tasks for '${results[0].environmentName}' environment\n\n`;
results.forEach(function (result) {
const task = taskLookup.get(result.serverTaskId);
if (task != null) {
const link = `${url}app#/${spaceId}/deployments/${task.Arguments.DeploymentId}`;
markdown += `[${result.tenantName}](${link})\n`;
}
});

const markdownFile = path.join(getVstsEnvironmentVariables().defaultWorkingDirectory, `${uuidv4()}.md`);
tasks.writeFile(markdownFile, markdown);
tasks.addAttachment("Distributedtask.Core.Summary", "Octopus Deploy Tenants", markdownFile);
}
}

0 comments on commit bd08fb7

Please sign in to comment.