internal async static Task <OrganizationRepositoryWithTeamContext> 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 BranchProtectionPushRestrictionsUpdate(new BranchProtectionTeamCollection { contextOrgTeam.TeamName }), true); await client.Repository.Branch.UpdateBranchProtection(contextOrgRepo.RepositoryOwner, contextOrgRepo.RepositoryName, "master", protection); return(new OrganizationRepositoryWithTeamContext { RepositoryContext = contextOrgRepo, TeamContext = contextOrgTeam }); }
public async Task EnsuresNonNullArguments() { var client = new RepositoryBranchesClient(Substitute.For <IApiConnection>()); var update = new BranchProtectionSettingsUpdate( new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" })); await Assert.ThrowsAsync <ArgumentNullException>(() => client.UpdateBranchProtection(null, "repo", "branch", update)); await Assert.ThrowsAsync <ArgumentNullException>(() => client.UpdateBranchProtection("owner", null, "branch", update)); await Assert.ThrowsAsync <ArgumentNullException>(() => client.UpdateBranchProtection("owner", "repo", null, update)); await Assert.ThrowsAsync <ArgumentNullException>(() => client.UpdateBranchProtection("owner", "repo", "branch", null)); await Assert.ThrowsAsync <ArgumentNullException>(() => client.UpdateBranchProtection(1, null, update)); await Assert.ThrowsAsync <ArgumentNullException>(() => client.UpdateBranchProtection(1, "branch", null)); await Assert.ThrowsAsync <ArgumentException>(() => client.UpdateBranchProtection("", "repo", "branch", update)); await Assert.ThrowsAsync <ArgumentException>(() => client.UpdateBranchProtection("owner", "", "branch", update)); await Assert.ThrowsAsync <ArgumentException>(() => client.UpdateBranchProtection("owner", "repo", "", update)); await Assert.ThrowsAsync <ArgumentException>(() => client.UpdateBranchProtection(1, "", update)); }
internal async static Task<OrganizationRepositoryWithTeamContext> 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, true, new[] { "build", "test" }), new BranchProtectionPushRestrictionsUpdate(new BranchProtectionTeamCollection { contextOrgTeam.TeamName })); await client.Repository.Branch.UpdateBranchProtection(contextOrgRepo.RepositoryOwner, contextOrgRepo.RepositoryName, "master", protection); return new OrganizationRepositoryWithTeamContext { RepositoryContext = contextOrgRepo, TeamContext = contextOrgTeam }; }
/// <summary> /// Add the default branch protections to the provided branch in the specified repository /// </summary> /// <param name="repositoryName">New repository created that needs branch protections</param> public async Task AddBranchProtections(string repositoryName) { // The webhook payload does not seem to have the correct Default Branch name, its defaulted to master // Also the repository doesn't seem to report the correct default branch consistently either var repo = await client.Repository.Get(Organization, repositoryName); if (Configuration["DefaultBranch"] != repo.DefaultBranch) { Console.WriteLine("Specified default did not match the repo"); } if (await AwaitDefaultBranch(repositoryName, Configuration["DefaultBranch"])) { // Adding Branch protections // Required PR Reviews: Dismiss stale reviews and requiring at least 1 required approver // Enforce Admins to follow branch policy as well var requiredPullRequestReviews = new BranchProtectionRequiredReviewsUpdate(true, false, 1); var branchProtections = new BranchProtectionSettingsUpdate(null, requiredPullRequestReviews, null, true); var result = await client.Repository.Branch.UpdateBranchProtection(Organization, repositoryName, repo.DefaultBranch, branchProtections); // Create and close an issue in this repository denoting the actions we've taken await CreateIssue(repositoryName); } else { Console.WriteLine("Default branch not detected"); } }
public async Task UpdatesBranchProtectionForOrgRepo() { var repoOwner = _orgRepoContext.RepositoryContext.RepositoryOwner; var repoName = _orgRepoContext.RepositoryContext.RepositoryName; var update = new BranchProtectionSettingsUpdate( new BranchProtectionRequiredStatusChecksUpdate(false, new[] { "new" }), new BranchProtectionRequiredReviewsUpdate(new BranchProtectionRequiredReviewsDismissalRestrictionsUpdate(false), false, false, 2), new BranchProtectionPushRestrictionsUpdate(), false); var protection = await _client.UpdateBranchProtection(repoOwner, repoName, "master", update); Assert.False(protection.RequiredStatusChecks.Strict); Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count); Assert.Null(protection.RequiredPullRequestReviews.DismissalRestrictions); Assert.False(protection.RequiredPullRequestReviews.DismissStaleReviews); Assert.False(protection.RequiredPullRequestReviews.RequireCodeOwnerReviews); Assert.Equal(2, protection.RequiredPullRequestReviews.RequiredApprovingReviewCount); Assert.Empty(protection.Restrictions.Teams); Assert.Empty(protection.Restrictions.Users); Assert.False(protection.EnforceAdmins.Enabled); }
public override async Task <Command> HandleAsync(Command command, CancellationToken cancellationToken = new CancellationToken()) { var statusChecksUpdate = new BranchProtectionSettingsUpdate( new BranchProtectionRequiredStatusChecksUpdate(true, new List <string>())); await _client.Repository.Branch.UpdateBranchProtection(command.RepositoryId, command.BranchName, statusChecksUpdate); return(await base.HandleAsync(command, cancellationToken)); }
private async Task UpdateBranchProtection(string owner, string repositoryName, string branchName) { var update = new BranchProtectionSettingsUpdate( new BranchProtectionRequiredStatusChecksUpdate(false, new[] { "new" }), new BranchProtectionRequiredReviewsUpdate(false, true, 2), false ); await Client.Repository.Branch.UpdateBranchProtection(owner, repositoryName, branchName, update); }
public override async Task <Command> HandleAsync(Command command, CancellationToken cancellationToken = new CancellationToken()) { var protectionSettingsUpdate = new BranchProtectionSettingsUpdate(new BranchProtectionRequiredReviewsUpdate(true, false)); await _client.Repository.Branch.UpdateBranchProtection(command.RepositoryId, command.BranchName, protectionSettingsUpdate); return(await base.HandleAsync(command, cancellationToken)); }
public void RequestsTheCorrectUrlWithRepositoryId() { var gitHubClient = Substitute.For <IGitHubClient>(); var client = new ObservableRepositoryBranchesClient(gitHubClient); var update = new BranchProtectionSettingsUpdate( new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" })); client.UpdateBranchProtection(1, "branch", update); gitHubClient.Repository.Branch.Received() .UpdateBranchProtection(1, "branch", update); }
public void RequestsTheCorrectUrlWithRepositoryId() { var connection = Substitute.For <IApiConnection>(); var client = new RepositoryBranchesClient(connection); var update = new BranchProtectionSettingsUpdate( new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" })); const string previewAcceptsHeader = "application/vnd.github.loki-preview+json"; client.UpdateBranchProtection(1, "branch", update); connection.Received() .Put <BranchProtectionSettings>(Arg.Is <Uri>(u => u.ToString() == "repositories/1/branches/branch/protection"), Arg.Any <BranchProtectionSettingsUpdate>(), null, previewAcceptsHeader); }
internal async static Task<RepositoryContext> 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, true, new[] { "build", "test" })); await client.Repository.Branch.UpdateBranchProtection(contextUserRepo.RepositoryOwner, contextUserRepo.RepositoryName, "master", update); return contextUserRepo; }
public async Task UpdatesBranchProtectionWithRepositoryId() { var repoId = _userRepoContext.RepositoryId; var update = new BranchProtectionSettingsUpdate( new BranchProtectionRequiredStatusChecksUpdate(false, false, new[] { "new" })); var protection = await _client.UpdateBranchProtection(repoId, "master", update); Assert.False(protection.RequiredStatusChecks.IncludeAdmins); Assert.False(protection.RequiredStatusChecks.Strict); Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count); Assert.Null(protection.Restrictions); }
public async Task UpdatesBranchProtection() { var repoOwner = _userRepoContext.RepositoryOwner; var repoName = _userRepoContext.RepositoryName; var update = new BranchProtectionSettingsUpdate( new BranchProtectionRequiredStatusChecksUpdate(false, new[] { "new" })); var protection = await _client.UpdateBranchProtection(repoOwner, repoName, "master", update); Assert.False(protection.EnforceAdmins.Enabled); Assert.False(protection.RequiredStatusChecks.Strict); Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count); Assert.Null(protection.Restrictions); }
internal async static Task <RepositoryContext> 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" }), null, true); await client.Repository.Branch.UpdateBranchProtection(contextUserRepo.RepositoryOwner, contextUserRepo.RepositoryName, "master", update); return(contextUserRepo); }
public async Task UpdatesBranchProtectionForOrgRepoWithRepositoryId() { var repoId = _orgRepoContext.RepositoryContext.RepositoryId; var update = new BranchProtectionSettingsUpdate( new BranchProtectionRequiredStatusChecksUpdate(false, new[] { "new" }), new BranchProtectionPushRestrictionsUpdate(), false); var protection = await _client.UpdateBranchProtection(repoId, "master", update); Assert.False(protection.EnforceAdmins.Enabled); Assert.False(protection.RequiredStatusChecks.Strict); Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count); Assert.Empty(protection.Restrictions.Teams); Assert.Empty(protection.Restrictions.Users); }
public async Task UpdatesBranchProtectionForOrgRepo() { var repoOwner = _orgRepoContext.RepositoryContext.RepositoryOwner; var repoName = _orgRepoContext.RepositoryContext.RepositoryName; var update = new BranchProtectionSettingsUpdate( new BranchProtectionRequiredStatusChecksUpdate(false, false, new[] { "new" }), new BranchProtectionPushRestrictionsUpdate()); var protection = await _client.UpdateBranchProtection(repoOwner, repoName, "master", update); Assert.False(protection.RequiredStatusChecks.IncludeAdmins); Assert.False(protection.RequiredStatusChecks.Strict); Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count); Assert.Empty(protection.Restrictions.Teams); Assert.Empty(protection.Restrictions.Users); }
public async Task UpdatesBranchProtectionWithRepositoryId() { var repoId = _userRepoContext.RepositoryId; var update = new BranchProtectionSettingsUpdate( new BranchProtectionRequiredStatusChecksUpdate(false, new[] { "new" }), new BranchProtectionRequiredReviewsUpdate(false, true), false); var protection = await _client.UpdateBranchProtection(repoId, "master", update); Assert.False(protection.RequiredStatusChecks.Strict); Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count); Assert.Null(protection.RequiredPullRequestReviews.DismissalRestrictions); Assert.False(protection.RequiredPullRequestReviews.DismissStaleReviews); Assert.True(protection.RequiredPullRequestReviews.RequireCodeOwnerReviews); Assert.Null(protection.Restrictions); Assert.False(protection.EnforceAdmins.Enabled); }
/// <summary> /// /// </summary> /// <param name="orgNm">Organization Name</param> /// <param name="id">Repository ID</param> private async void ExecuteProtection(Dictionary <string, object> result) { // get repository name string orgNm = (JObject.FromObject(result["organization"]).ToObject <Dictionary <string, object> >())["login"].ToString(); // get repository ID long id = long.Parse((JObject.FromObject(result["repository"]).ToObject <Dictionary <string, object> >())["id"].ToString()); // Login Credentials cre = new Credentials(_setting.Value.Email, _setting.Value.Pwd); GitHubClient ghc = new GitHubClient(new ProductHeaderValue(orgNm)); ghc.Credentials = cre; // Check for branches amount var repoBranches = await ghc.Repository.Branch.GetAll(id); if (repoBranches.Count > 0) // Check if there is at least a branch { // get the master branch var master = repoBranches.Where(br => br.Name.Equals("master")).FirstOrDefault(); if (!master.Protected) { // set protection BranchProtectionRequiredReviewsUpdate bprru = new BranchProtectionRequiredReviewsUpdate(true, true, 1); BranchProtectionSettingsUpdate Setting = new BranchProtectionSettingsUpdate(bprru); await ghc.Repository.Branch.UpdateBranchProtection(id, "master", Setting); // mentioning oneself var ic = ghc.Issue; var newIssue = new NewIssue("Master Branch Protection Validation") { Body = "Hi @" + _setting.Value.MentionAccount + " , Please check if the master branch protection is valid!" }; var issue = await ic.Create(id, newIssue); } } }
public async Task <ProtectBranchResponse> ProtectBranch(string branchName, string owner, string repoName) { var responseModel = new ProtectBranchResponse(); try { //Apply the branch protections below var update = new BranchProtectionSettingsUpdate( new BranchProtectionRequiredStatusChecksUpdate(true, new[] { "build", "test" }), new BranchProtectionRequiredReviewsUpdate(false, true, 2), true); var updateBranchResponse = await _githubClient.Repository.Branch.UpdateBranchProtection(owner, repoName, branchName, update); responseModel.IsSuccess = true; responseModel.Message = "Branch protection applied succesfully"; return(responseModel); } catch (Exception ex) { responseModel.IsSuccess = false; responseModel.Message = ex.Message; return(responseModel); } }
public void RequestsTheCorrectUrlWithRepositoryId() { var connection = Substitute.For<IApiConnection>(); var client = new RepositoryBranchesClient(connection); var update = new BranchProtectionSettingsUpdate( new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" })); const string previewAcceptsHeader = "application/vnd.github.loki-preview+json"; client.UpdateBranchProtection(1, "branch", update); connection.Received() .Put<BranchProtectionSettings>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/branches/branch/protection"), Arg.Any<BranchProtectionSettingsUpdate>(), null, previewAcceptsHeader); }
/// <summary> /// Update the branch protection settings for the specified branch /> /// </summary> /// <remarks> /// See the <a href="https://developer.github.com/v3/repos/branches/#update-branch-protection">API documentation</a> for more details /// </remarks> /// <param name="owner">The owner of the repository</param> /// <param name="name">The name of the repository</param> /// <param name="branch">The name of the branch</param> /// <param name="update">Branch protection settings</param> public Task<BranchProtectionSettings> UpdateBranchProtection(string owner, string name, string branch, BranchProtectionSettingsUpdate update) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNullOrEmptyString(branch, "branch"); Ensure.ArgumentNotNull(update, "update"); return ApiConnection.Put<BranchProtectionSettings>(ApiUrls.RepoBranchProtection(owner, name, branch), update, null, AcceptHeaders.ProtectedBranchesApiPreview); }
/// <summary> /// Update the branch protection settings for the specified branch /// </summary> /// <remarks> /// See the <a href="https://developer.github.com/v3/repos/branches/#update-branch-protection">API documentation</a> for more details /// </remarks> /// <param name="repositoryId">The Id of the repository</param> /// <param name="branch">The name of the branch</param> /// <param name="update">Branch protection settings</param> public IObservable<BranchProtectionSettings> UpdateBranchProtection(long repositoryId, string branch, BranchProtectionSettingsUpdate update) { Ensure.ArgumentNotNullOrEmptyString(branch, "branch"); Ensure.ArgumentNotNull(update, "update"); return _client.UpdateBranchProtection(repositoryId, branch, update).ToObservable(); }
/// <summary> /// Update the branch protection settings for the specified branch /// </summary> /// <remarks> /// See the <a href="https://developer.github.com/v3/repos/branches/#update-branch-protection">API documentation</a> for more details /// </remarks> /// <param name="owner">The owner of the repository</param> /// <param name="name">The name of the repository</param> /// <param name="branch">The name of the branch</param> /// <param name="update">Branch protection settings</param> public IObservable<BranchProtectionSettings> UpdateBranchProtection(string owner, string name, string branch, BranchProtectionSettingsUpdate update) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNullOrEmptyString(branch, "branch"); Ensure.ArgumentNotNull(update, "update"); return _client.UpdateBranchProtection(owner, name, branch, update).ToObservable(); }
/// <summary> /// Update the branch protection settings for the specified branch /// </summary> /// <remarks> /// See the <a href="https://developer.github.com/v3/repos/branches/#update-branch-protection">API documentation</a> for more details /// </remarks> /// <param name="owner">The owner of the repository</param> /// <param name="name">The name of the repository</param> /// <param name="branch">The name of the branch</param> /// <param name="update">Branch protection settings</param> public IObservable <BranchProtectionSettings> UpdateBranchProtection(string owner, string name, string branch, BranchProtectionSettingsUpdate update) { Ensure.ArgumentNotNullOrEmptyString(owner, "owner"); Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNullOrEmptyString(branch, "branch"); Ensure.ArgumentNotNull(update, "update"); return(_client.UpdateBranchProtection(owner, name, branch, update).ToObservable()); }
public void RequestsTheCorrectUrlWithRepositoryId() { var gitHubClient = Substitute.For<IGitHubClient>(); var client = new ObservableRepositoryBranchesClient(gitHubClient); var update = new BranchProtectionSettingsUpdate( new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" })); client.UpdateBranchProtection(1, "branch", update); gitHubClient.Repository.Branch.Received() .UpdateBranchProtection(1, "branch", update); }
public async Task UpdatesBranchProtectionForOrgRepoWithRepositoryId() { var repoId = _orgRepoContext.RepositoryContext.RepositoryId; var update = new BranchProtectionSettingsUpdate( new BranchProtectionRequiredStatusChecksUpdate(false, false, new[] { "new" }), new BranchProtectionPushRestrictionsUpdate()); var protection = await _client.UpdateBranchProtection(repoId, "master", update); Assert.False(protection.RequiredStatusChecks.IncludeAdmins); Assert.False(protection.RequiredStatusChecks.Strict); Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count); Assert.Empty(protection.Restrictions.Teams); Assert.Empty(protection.Restrictions.Users); }
public async Task UpdatesBranchProtection() { var repoOwner = _userRepoContext.RepositoryOwner; var repoName = _userRepoContext.RepositoryName; var update = new BranchProtectionSettingsUpdate( new BranchProtectionRequiredStatusChecksUpdate(false, false, new[] { "new" })); var protection = await _client.UpdateBranchProtection(repoOwner, repoName, "master", update); Assert.False(protection.RequiredStatusChecks.IncludeAdmins); Assert.False(protection.RequiredStatusChecks.Strict); Assert.Equal(1, protection.RequiredStatusChecks.Contexts.Count); Assert.Null(protection.Restrictions); }
public async Task EnsuresNonNullArguments() { var client = new ObservableRepositoryBranchesClient(Substitute.For<IGitHubClient>()); var update = new BranchProtectionSettingsUpdate( new BranchProtectionRequiredStatusChecksUpdate(true, true, new[] { "test" })); Assert.Throws<ArgumentNullException>(() => client.UpdateBranchProtection(null, "repo", "branch", update)); Assert.Throws<ArgumentNullException>(() => client.UpdateBranchProtection("owner", null, "branch", update)); Assert.Throws<ArgumentNullException>(() => client.UpdateBranchProtection("owner", "repo", null, update)); Assert.Throws<ArgumentNullException>(() => client.UpdateBranchProtection("owner", "repo", "branch", null)); Assert.Throws<ArgumentNullException>(() => client.UpdateBranchProtection(1, null, update)); Assert.Throws<ArgumentNullException>(() => client.UpdateBranchProtection(1, "branch", null)); Assert.Throws<ArgumentException>(() => client.UpdateBranchProtection("", "repo", "branch", update)); Assert.Throws<ArgumentException>(() => client.UpdateBranchProtection("owner", "", "branch", update)); Assert.Throws<ArgumentException>(() => client.UpdateBranchProtection("owner", "repo", "", update)); Assert.Throws<ArgumentException>(() => client.UpdateBranchProtection(1, "", update)); }
/// <summary> /// Update the branch protection settings for the specified branch /// </summary> /// <remarks> /// See the <a href="https://developer.github.com/v3/repos/branches/#update-branch-protection">API documentation</a> for more details /// </remarks> /// <param name="repositoryId">The Id of the repository</param> /// <param name="branch">The name of the branch</param> /// <param name="update">Branch protection settings</param> public IObservable <BranchProtectionSettings> UpdateBranchProtection(long repositoryId, string branch, BranchProtectionSettingsUpdate update) { Ensure.ArgumentNotNullOrEmptyString(branch, "branch"); Ensure.ArgumentNotNull(update, "update"); return(_client.UpdateBranchProtection(repositoryId, branch, update).ToObservable()); }
/// <summary> /// Update the branch protection settings for the specified branch /> /// </summary> /// <remarks> /// See the <a href="https://developer.github.com/v3/repos/branches/#update-branch-protection">API documentation</a> for more details /// </remarks> /// <param name="repositoryId">The Id of the repository</param> /// <param name="branch">The name of the branch</param> /// <param name="update">Branch protection settings</param> public Task<BranchProtectionSettings> UpdateBranchProtection(int repositoryId, string branch, BranchProtectionSettingsUpdate update) { Ensure.ArgumentNotNullOrEmptyString(branch, "branch"); Ensure.ArgumentNotNull(update, "update"); return ApiConnection.Put<BranchProtectionSettings>(ApiUrls.RepoBranchProtection(repositoryId, branch), update, null, AcceptHeaders.ProtectedBranchesApiPreview); }