using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Octokit.Tests.Integration.Helpers { internal class OrganizationRepositoryWithTeamContext : IDisposable { internal RepositoryContext RepositoryContext { get; set; } internal TeamContext TeamContext { get; set; } public void Dispose() { if (RepositoryContext != null) RepositoryContext.Dispose(); if (TeamContext != null) TeamContext.Dispose(); } } internal static class RepositoryProtectedBranchHelperExtensions { internal async static Task CreateRepositoryWithProtectedBranch(this IGitHubClient client) { // Create user owned repo var userRepo = new NewRepository(Helper.MakeNameWithTimestamp("protected-repo")) { AutoInit = true }; var contextUserRepo = await client.CreateRepositoryContext(userRepo); // Protect master branch var update = new BranchProtectionSettingsUpdate( new BranchProtectionRequiredStatusChecksUpdate(true, new[] { "build", "test" }), new BranchProtectionRequiredReviewsUpdate(true, true, 3), null, true); await client.Repository.Branch.UpdateBranchProtection(contextUserRepo.RepositoryOwner, contextUserRepo.RepositoryName, "master", update); return contextUserRepo; } internal async static Task CreateOrganizationRepositoryWithProtectedBranch(this IGitHubClient client) { // Create organization owned repo var orgRepo = new NewRepository(Helper.MakeNameWithTimestamp("protected-org-repo")) { AutoInit = true }; var contextOrgRepo = await client.CreateRepositoryContext(Helper.Organization, orgRepo); // Create team in org var contextOrgTeam = await client.CreateTeamContext(Helper.Organization, new NewTeam(Helper.MakeNameWithTimestamp("team"))); // Grant team push access to repo await client.Organization.Team.AddRepository( contextOrgTeam.TeamId, contextOrgRepo.RepositoryOwner, contextOrgRepo.RepositoryName, new RepositoryPermissionRequest(Permission.Push)); // Protect master branch var protection = new BranchProtectionSettingsUpdate( new BranchProtectionRequiredStatusChecksUpdate(true, new[] { "build", "test" }), new BranchProtectionRequiredReviewsUpdate(new BranchProtectionRequiredReviewsDismissalRestrictionsUpdate(new BranchProtectionTeamCollection { contextOrgTeam.TeamName }), true, true, 3), new BranchProtectionPushRestrictionsUpdate(new BranchProtectionTeamCollection { contextOrgTeam.TeamName }), true); await client.Repository.Branch.UpdateBranchProtection(contextOrgRepo.RepositoryOwner, contextOrgRepo.RepositoryName, "master", protection); return new OrganizationRepositoryWithTeamContext { RepositoryContext = contextOrgRepo, TeamContext = contextOrgTeam }; } } }