async Task <OctopusJiraPayloadData> PrepareOctopusJiraPayload(string eventType,
                                                                      string serverUri,
                                                                      DeploymentResource deployment,
                                                                      IJiraApiDeployment jiraApiDeployment,
                                                                      CancellationToken cancellationToken,
                                                                      EnvironmentResource deploymentEnvironment,
                                                                      DeploymentEnvironmentSettingsMetadataProvider.JiraDeploymentEnvironmentSettings
                                                                      environmentSettings)
        {
            var project = (await mediator.Request(new GetProjectRequest(deployment.ProjectId), cancellationToken)).Project;

            var release    = (await mediator.Request(new GetReleaseRequest(deployment.ReleaseId), cancellationToken)).Release;
            var serverTask = (await mediator.Request(new GetServerTaskRequest(deployment.TaskId), cancellationToken)).Task;

            TenantResource?tenant = null;

            if (deployment.TenantId?.Value is not null)
            {
                tenant = (await mediator.Request(new GetTenantByIdOrNameRequest(deployment.TenantId.Value.ToTenantIdOrName()), cancellationToken)).Resource;
            }

            return(new OctopusJiraPayloadData
            {
                InstallationId = installationIdProvider.GetInstallationId().ToString(),
                BaseHostUrl = store.GetBaseUrl() ?? string.Empty,
                DeploymentsInfo = new JiraPayloadData
                {
                    Deployments = new[]
        public async Task PublishToJira(string eventType,
                                        DeploymentResource deployment,
                                        IJiraApiDeployment jiraApiDeployment,
                                        ITaskLog taskLog,
                                        CancellationToken cancellationToken)
        {
            if (JiraIntegrationUnavailable(deployment))
            {
                jiraApiDeployment.HandleJiraIntegrationIsUnavailable();
                return;
            }

            var serverUri = serverConfigurationStore.GetServerUri()?.TrimEnd('/');

            if (string.IsNullOrWhiteSpace(serverUri))
            {
                taskLog.Warn("To use Jira integration you must have the Octopus server's external url configured (see the Configuration/Nodes page)");
                return;
            }

            if (string.IsNullOrWhiteSpace(store.GetConnectAppUrl()) || string.IsNullOrWhiteSpace(store.GetConnectAppPassword()?.Value))
            {
                taskLog.Warn("Jira integration is enabled but settings are incomplete, ignoring deployment events");
                return;
            }

            var taskLogBlock = taskLogFactory.CreateBlock(taskLog, $"Sending Jira state update - {eventType}");

            // get token from connect App
            var token = await connectAppClient.GetAuthTokenFromConnectApp(taskLogBlock);

            if (token is null)
            {
                taskLogFactory.Finish(taskLogBlock);
                return;
            }

            var getDeploymentEnvironmentResponse = await mediator.Request(new GetDeploymentEnvironmentRequest(deployment.EnvironmentId), cancellationToken);

            var deploymentEnvironment = getDeploymentEnvironmentResponse.DeploymentEnvironment;
            var environmentSettings   =
                deploymentEnvironmentSettingsProvider
                .GetSettings <DeploymentEnvironmentSettingsMetadataProvider.JiraDeploymentEnvironmentSettings?>(
                    JiraConfigurationStore.SingletonId,
                    deployment.EnvironmentId.Value)
                ?? new DeploymentEnvironmentSettingsMetadataProvider.JiraDeploymentEnvironmentSettings();

            var data = await PrepareOctopusJiraPayload(
                eventType,
                serverUri,
                deployment,
                jiraApiDeployment,
                cancellationToken,
                deploymentEnvironment,
                environmentSettings);

            // Push data to Jira
            await SendToJira(
                token,
                data,
                deployment,
                taskLogBlock,
                deploymentEnvironment,
                environmentSettings);

            taskLogFactory.Finish(taskLogBlock);
        }