/// <summary> /// Check that the user has access to the org and repo then /// return the settings for that repository, create settings if none exists /// </summary> /// <param name="user"></param> /// <param name="ownerId"></param> /// <param name="repoId"></param> /// <returns></returns> public async Task <RepoSettings> CheckRepoSettingsAsync(ClaimsPrincipal user, string ownerId, string repoId) { var repo = await CheckGitHubRepository(user.Identity, ownerId, repoId); try { var repoSettings = await _repoSettingsStore.GetRepoSettingsAsync(ownerId, repoId); if (string.IsNullOrEmpty(repoSettings.CloneUrl)) { repoSettings.CloneUrl = repo.CloneUrl; await _repoSettingsStore.CreateOrUpdateRepoSettingsAsync(repoSettings); } return(repoSettings); } catch (RepoSettingsNotFoundException) { var newRepoSettings = new RepoSettings { OwnerId = ownerId, RepositoryId = repoId, CloneUrl = repo.CloneUrl, OwnerIsOrg = !user.Identity.Name.Equals(ownerId, StringComparison.InvariantCultureIgnoreCase), OwnerAvatar = repo.Owner?.AvatarUrl }; await _repoSettingsStore.CreateOrUpdateRepoSettingsAsync(newRepoSettings); return(newRepoSettings); } catch (Exception e) { Log.Error($"Error checking repo settings for '{ownerId}/{repoId}", e); throw; } }
public async Task <IActionResult> Settings(string ownerId, string repoId, RepoSettingsViewModel settingsViewModel) { if (ModelState.IsValid) { try { settingsViewModel.LastModified = DateTime.UtcNow; settingsViewModel.LastModifiedBy = User.Identity.Name; if (!ownerId.Equals(User.Identity.Name)) { settingsViewModel.OwnerIsOrg = true; } var repoSettings = settingsViewModel.AsRepoSettings(); await _repoSettingsStore.CreateOrUpdateRepoSettingsAsync(repoSettings); ViewBag.StatusMessage = GetSettingsStatusMessage(ManageMessageId.ChangeSettingSuccess); TempData["ModelState"] = null; } catch (Exception e) { ViewBag.StatusMessage = GetSettingsStatusMessage(ManageMessageId.Error); Log.Error(e, "Error updating repo settings for '{0}/{1}'", ownerId, repoId); throw; } } else { // pass errors to the ViewComponent ViewBag.StatusMessage = GetSettingsStatusMessage(ManageMessageId.ValidationError); TempData["ModelState"] = ModelState; TempData["ViewModel"] = settingsViewModel; } DashboardViewModel.Area = "settings"; DashboardViewModel.Title = string.Format("{0} Settings", DashboardViewModel.SelectedOwnerId, DashboardViewModel.SelectedRepoId); return(View("Settings", DashboardViewModel)); }