internal async Task <ProjectTemplate> UpdateProjectTemplateAsync(ProjectTemplate projectTemplate) { if (projectTemplate is null) { throw new ArgumentNullException(nameof(projectTemplate)); } var repository = projectTemplate.Repository; var creds = new VssBasicCredential(string.Empty, repository.Token); using var connection = new VssConnection(new Uri(repository.BaselUrl), creds); using var client = connection.GetClient <GitHttpClient>(); var commit = await client .GetCommitAsync(project : repository.Project, repository.Ref, repository.Repository) .ConfigureAwait(false); var result = await client .GetTreeAsync(project : repository.Project, repository.Repository, commit.TreeId, recursive : true) .ConfigureAwait(false); var projectYamlFile = await client .GetItemAsync(project : repository.Project, repository.Repository, Constants.ProjectYaml, download : true, versionDescriptor : repository.VersionDescriptor()) .ConfigureAwait(false); var projectYaml = projectYamlFile.Content; var projectJson = new Deserializer().ToJson(projectYaml); TeamCloudSerialize.PopulateObject(projectJson, projectTemplate, new ProjectTemplateConverter(projectTemplate, projectYamlFile.Path)); projectTemplate.Description = await CheckAndPopulateFileContentAsync(client, repository, result.TreeEntries, projectTemplate.Description) .ConfigureAwait(false); return(projectTemplate); }
async Task <IActionResult> IAdapterAuthorize.HandleAuthorizeAsync(DeploymentScope deploymentScope, HttpRequest request, IAuthorizationEndpoints authorizationEndpoints) { if (deploymentScope is null) { throw new ArgumentNullException(nameof(deploymentScope)); } if (request is null) { throw new ArgumentNullException(nameof(request)); } if (authorizationEndpoints is null) { throw new ArgumentNullException(nameof(authorizationEndpoints)); } var queryParams = Url.ParseQueryParams(request.QueryString.ToString()); var queryState = queryParams.GetValueOrDefault("state"); var queryCode = queryParams.GetValueOrDefault("code"); var queryError = queryParams.GetValueOrDefault("error"); var session = await SessionClient .GetAsync <GitHubSession>(deploymentScope) .ConfigureAwait(false); session ??= await SessionClient .SetAsync(new GitHubSession(deploymentScope)) .ConfigureAwait(false); var data = string.IsNullOrWhiteSpace(deploymentScope.InputData) ? default : TeamCloudSerialize.DeserializeObject <GitHubData>(deploymentScope.InputData); var token = await TokenClient .GetAsync <GitHubToken>(deploymentScope) .ConfigureAwait(false); if (string.IsNullOrEmpty(queryError) && Guid.TryParse(queryState, out var stateId)) { if (!stateId.ToString().Equals(session.SessionId, StringComparison.OrdinalIgnoreCase)) { return(new RedirectResult(authorizationEndpoints.AuthorizationUrl.SetQueryParam("error", "Session timed out.").ToString())); } else if (string.IsNullOrWhiteSpace(queryCode)) { return(new RedirectResult(authorizationEndpoints.AuthorizationUrl.SetQueryParam("error", "Missing GitHub handshake information.").ToString())); } token ??= new GitHubToken(deploymentScope); // Using Flurl as Octokit doesn't support this API yet // https://github.com/octokit/octokit.net/issues/2138 var url = $"https://api.github.com/app-manifests/{queryCode}/conversions"; var response = await url .WithHeader("User-Agent", GitHubConstants.ProductHeader.ToString()) .PostStringAsync(string.Empty) .ConfigureAwait(false); if (!response.IsSuccessStatusCode()) { return(new RedirectResult(authorizationEndpoints.AuthorizationUrl.SetQueryParam("error", $"Failed to get application token ({response.StatusCode} - {response.ResponseMessage.ReasonPhrase}).").ToString())); } var json = await response .GetStringAsync() .ConfigureAwait(false); TeamCloudSerialize.PopulateObject(json, token); if (string.IsNullOrEmpty(token.OwnerLogin)) { token.OwnerLogin = JToken.Parse(json) .SelectToken("owner.login") .ToString(); } token = await TokenClient .SetAsync(token, true) .ConfigureAwait(false); return(new ContentResult { StatusCode = (int)HttpStatusCode.OK, ContentType = "text/html", Content = Assembly.GetExecutingAssembly().GetManifestResourceTemplate($"{GetType().FullName}_Install.html", GetFormContext()) }); } else { return(new ContentResult { StatusCode = (int)HttpStatusCode.OK, ContentType = "text/html", Content = Assembly.GetExecutingAssembly().GetManifestResourceTemplate($"{GetType().FullName}_Register.html", GetFormContext()) }); } object GetFormContext() => new { deploymentScope = deploymentScope, authorizationEndpoints = authorizationEndpoints, token = token, data = data, session = session, error = queryError ?? string.Empty, succeeded = queryParams.Contains("succeeded") }; }