public async Task ExecuteAsync(OctoContext context) { var json = await new StreamReader(context.Request.Body).ReadToEndAsync(); var request = JsonConvert.DeserializeObject <JObject>(json); var baseUrl = request.GetValue("BaseUrl").ToString(); var username = request.GetValue("Username").ToString(); // If password here is null, it could be that they're clicking the test connectivity button after saving // the configuration as we won't have the value of the password on client side, so we need to retrieve it // from the database var password = string.IsNullOrEmpty(request.GetValue("Password").ToString()) ? configurationStore.GetJiraPassword() : request.GetValue("Password").ToString(); if (string.IsNullOrEmpty(baseUrl) || string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) { context.Response.AsOctopusJson(ConnectivityCheckResponse.Failure( string.IsNullOrEmpty(baseUrl) ? "Please provide a value for Jira Base Url." : null, string.IsNullOrEmpty(username) ? "Please provide a value for Jira Username." : null, string.IsNullOrEmpty(password) ? "Please provide a value for Jira Password." : null)); return; } var jiraRestClient = new JiraRestClient(baseUrl, username, password, log); var connectivityCheckResult = jiraRestClient.GetServerInfo().Result; context.Response.AsOctopusJson(connectivityCheckResult); }
public async Task ExecuteAsync(OctoContext context) { var json = await new StreamReader(context.Request.Body).ReadToEndAsync(); var request = JsonConvert.DeserializeObject <JObject>(json); var baseUrl = request.GetValue("BaseUrl").ToString(); var username = installationIdProvider.GetInstallationId().ToString(); // If password here is null, it could be that they're clicking the test connectivity button after saving // the configuration as we won't have the value of the password on client side, so we need to retrieve it // from the database var password = string.IsNullOrEmpty(request.GetValue("Password").ToString()) ? configurationStore.GetConnectAppPassword() : request.GetValue("Password").ToString(); if (string.IsNullOrEmpty(baseUrl) || string.IsNullOrEmpty(password)) { context.Response.AsOctopusJson(ConnectivityCheckResponse.Failure( string.IsNullOrEmpty(baseUrl) ? "Please provide a value for Jira Base Url." : null, string.IsNullOrEmpty(password) ? "Please provide a value for Jira Connect App Password." : null) ); return; } var token = connectAppClient.GetAuthTokenFromConnectApp(username, password); if (token is null) { context.Response.AsOctopusJson(ConnectivityCheckResponse.Failure("Failed to get authentication token from Jira Connect App.")); return; } using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); var connectivityCheckPayload = JsonConvert.SerializeObject(new JiraConnectAppConnectivityCheckRequest { BaseHostUrl = baseUrl, OctopusInstallationId = username }); var result = await client.PostAsync( $"{configurationStore.GetConnectAppUrl()}/relay/connectivitycheck", new StringContent(connectivityCheckPayload, Encoding.UTF8, "application/json")); if (!result.IsSuccessStatusCode) { context.Response.AsOctopusJson(ConnectivityCheckResponse.Failure(result.StatusCode == HttpStatusCode.NotFound ? $"Failed to find an installation for Jira host {configurationStore.GetBaseUrl()}. Please ensure you have installed the Octopus Deploy for Jira plugin from the [Atlassian Marketplace](https://marketplace.atlassian.com/apps/1220376/octopus-deploy-for-jira). [Learn more](https://g.octopushq.com/JiraIssueTracker)." : $"Failed to check connectivity to Jira. Response code: {result.StatusCode}, Message: {result.Content.ReadAsStringAsync().GetAwaiter().GetResult()}") ); return; } context.Response.AsOctopusJson(ConnectivityCheckResponse.Success); } }
public async Task <ConnectivityCheckResponse> GetServerInfo() { using (var client = CreateHttpClient()) { var response = await client.GetAsync($"{baseUrl}/{baseApiUri}/serverInfo"); if (response.IsSuccessStatusCode) { return(ConnectivityCheckResponse.Success); } return(ConnectivityCheckResponse.Failure( $"Failed to connect to {baseUrl}. Response code: {response.StatusCode}{(!string.IsNullOrEmpty(response.ReasonPhrase) ? $"Reason: {response.ReasonPhrase}" : "")}")); } }