private async Task <RepoToCreate> GetRepoToCreate(long chatId, long userId) { var data = new { chatId, userId }.ToJson(); try { var repoToCreate = new RepoToCreate { ChatId = chatId, UserId = userId }; var questionsToAnswer = new[] { _chooseTeam, _questionEnterName, _questionEnterDesc }; foreach (var question in questionsToAnswer) { // var history = await _telegramBotHistoryRepository.GetLatestAsync(x => x.Question == question && x.ChatId == chatId && x.UserId == userId); var history = _history.FirstOrDefault(x => x.Question == question); if (history == null) { continue; } var answer = history.Answer; switch (question) { case _questionEnterName: repoToCreate.RepoName = answer; break; case _questionEnterDesc: repoToCreate.Description = answer; break; } repoToCreate.TeamId = 0; repoToCreate.AddSecurityTeam = false; repoToCreate.AddCoreTeam = true; } if (repoToCreate.TeamId == 0) { repoToCreate.TeamId = await GetUserTeamId(chatId, userId); } repoToCreate.MenuAction = await GetMenuAction(chatId, userId); return(repoToCreate); } catch (Exception ex) { await _log.WriteErrorAsync("TelegramBotActions.GetRepoToCreate", data, ex); throw; } }
public async Task <TelegramBotActionResult> CreateRepo(RepoToCreate repoToCreate) { try { var commonDevelopersTeam = await GetTeamByName(_teamName); var message = $"Repository \"{repoToCreate.RepoName}\" successfully created."; //Creating new repo var newRepo = new NewRepository(repoToCreate.RepoName) { AutoInit = true, TeamId = commonDevelopersTeam.Id, Description = repoToCreate.Description }; var repositoryToEdit = await client.Repository.Create(_organisation, newRepo); await client.Organization.Team.AddRepository(commonDevelopersTeam.Id, _organisation, repositoryToEdit.Name, new RepositoryPermissionRequest(Permission.Admin)); //var branchTeams = new BranchProtectionTeamCollection(); //if (team != null) //{ // branchTeams.Add(teamName); // message += $"\n Teams: \n \"{team.Name}\""; //} //if (repoToCreate.AddSecurityTeam) //{ // var securityTeam = RepositoryTgBotJobSettings.SecurityTeam.ToLower().Replace(' ', '-'); // branchTeams.Add(securityTeam); // codeOwnersFile += $"@{_organisation}/{securityTeam} "; // message += $"\n \"{RepositoryTgBotJobSettings.SecurityTeam}\""; //} //if (repoToCreate.AddCoreTeam) //{ // var coreTeam = RepositoryTgBotJobSettings.CoreTeam.ToLower().Replace(' ', '-'); // branchTeams.Add(coreTeam); // codeOwnersFile += $"@{_organisation}/{coreTeam} "; // message += $"\n \"{RepositoryTgBotJobSettings.CoreTeam}\""; //} //creating Code Owners file //await client.Repository.Content.CreateFile(repositoryToEdit.Id, "CODEOWNERS", new CreateFileRequest("Added CODEOWNERS file", codeOwnersFile)); //creating "dev" and "test" branches from master //var masterSha = await client.Repository.Commit.GetSha1(repositoryToEdit.Id, "refs/heads/master"); //await client.Git.Reference.Create(repositoryToEdit.Id, new NewReference("refs/heads/dev", masterSha)); //await client.Git.Reference.Create(repositoryToEdit.Id, new NewReference("refs/heads/test", masterSha)); //Permitions to "test" branch //var masterProtSett = new BranchProtectionSettingsUpdate(new BranchProtectionPushRestrictionsUpdate(new BranchProtectionTeamCollection(new List<string>() { teamName }))); //var testProtSett = new BranchProtectionSettingsUpdate(null, new BranchProtectionRequiredReviewsUpdate(new BranchProtectionRequiredReviewsDismissalRestrictionsUpdate(false), true, true), new BranchProtectionPushRestrictionsUpdate(branchTeams), true); //await client.Repository.Branch.UpdateBranchProtection(repositoryToEdit.Id, "test", testProtSett); //await client.Repository.Branch.UpdateBranchProtection(repositoryToEdit.Id, "master", masterProtSett); var link = repositoryToEdit.CloneUrl; message += "\n Clone url: " + link; return(new TelegramBotActionResult { Success = true, Message = message }); } catch (Exception ex) { await TelegramBotService._log.WriteErrorAsync("TelegramBotActions.CreateRepo", repoToCreate.ToJson(), ex); return(new TelegramBotActionResult { Success = false, Message = ex.Message }); } }
public async Task <TelegramBotActionResult> CreateLibraryRepo(RepoToCreate repoToCreate) { try { var team = await client.Organization.Team.Get(repoToCreate.TeamId); var teamName = team.Name.ToLower().Replace(' ', '-'); var codeOwnersFile = (team != null) ? $"* @{_organisation}/{teamName} " : "* "; var commonDevelopersTeam = await GetTeamByName(_teamName); var message = $"Library repository \"{repoToCreate.RepoName}\" successfully created."; // Creating new repo var newRepo = new NewRepository(repoToCreate.RepoName) { AutoInit = true, Description = repoToCreate.Description }; var repositoryToEdit = await client.Repository.Create(_organisation, newRepo); await client.Organization.Team.AddRepository(commonDevelopersTeam.Id, _organisation, repositoryToEdit.Name, new RepositoryPermissionRequest(Permission.Push)); //var branchTeams = new BranchProtectionTeamCollection(); //if (team != null) //{ // branchTeams.Add(teamName); // message += $"\n Teams: \n \"{team.Name}\""; //} //var architectureTeam = RepositoryTgBotJobSettings.ArchitectureTeam.ToLower().Replace(' ', '-'); //branchTeams.Add(architectureTeam); //codeOwnersFile += $"@{_organisation}/{architectureTeam} "; //message += $"\n \"{RepositoryTgBotJobSettings.ArchitectureTeam}\""; ////creating Code Owners file //await client.Repository.Content.CreateFile(repositoryToEdit.Id, "CODEOWNERS", new CreateFileRequest("Added CODEOWNERS file", codeOwnersFile)); //var protSett = new BranchProtectionSettingsUpdate(null, new BranchProtectionRequiredReviewsUpdateExtention(new BranchProtectionRequiredReviewsDismissalRestrictionsUpdate(false), true, true, 2), new BranchProtectionPushRestrictionsUpdate(branchTeams), true); ////this method throws null reference ecxeption because responce it's OK //try //{ // await client.Connection.Put<BranchProtectionSettingsUpdate>(ApiUrls.RepoBranchProtection(repositoryToEdit.Id, "master"), protSett, null, "application/vnd.github.luke-cage-preview+json"); //} //catch (Exception e) //{ // //Console.WriteLine(e); //} var link = repositoryToEdit.CloneUrl; message += "\n Clone url: " + link; return(new TelegramBotActionResult { Success = true, Message = message }); } catch (Exception ex) { Console.WriteLine(ex); await TelegramBotService._log.WriteErrorAsync("TelegramBotActions.CreateLibraryRepo", repoToCreate.ToJson(), ex); return(new TelegramBotActionResult { Success = false, Message = ex.Message }); } }