Skip to content

Commit d7134b7

Browse files
authored
Support users and teams in branch protection access restrictions (#2561)
Fixes: #2560.
1 parent 34cc7b4 commit d7134b7

File tree

2 files changed

+487
-23
lines changed

2 files changed

+487
-23
lines changed

github/repos.go

Lines changed: 203 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ type DismissalRestrictionsRequest struct {
11801180
Users *[]string `json:"users,omitempty"`
11811181
// The list of team slugs which can dismiss pull request reviews. (Required; use nil to disable dismissal_restrictions or &[]string{} otherwise.)
11821182
Teams *[]string `json:"teams,omitempty"`
1183-
// The list of apps which can dismiss pull request reviews. (Required; use nil to disable dismissal_restrictions or &[]string{} otherwise.)
1183+
// The list of app slugs which can dismiss pull request reviews. (Required; use nil to disable dismissal_restrictions or &[]string{} otherwise.)
11841184
Apps *[]string `json:"apps,omitempty"`
11851185
}
11861186

@@ -1676,6 +1676,8 @@ func (s *RepositoriesService) ReplaceAllTopics(ctx context.Context, owner, repo
16761676
// It requires the GitHub apps to have `write` access to the `content` permission.
16771677
//
16781678
// GitHub API docs: https://docs.github.com/en/rest/branches/branch-protection#get-apps-with-access-to-the-protected-branch
1679+
//
1680+
// Deprecated: Please use ListAppRestrictions instead.
16791681
func (s *RepositoriesService) ListApps(ctx context.Context, owner, repo, branch string) ([]*App, *Response, error) {
16801682
u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/apps", owner, repo, branch)
16811683
req, err := s.client.NewRequest("GET", u, nil)
@@ -1692,27 +1694,37 @@ func (s *RepositoriesService) ListApps(ctx context.Context, owner, repo, branch
16921694
return apps, resp, nil
16931695
}
16941696

1697+
// ListAppRestrictions lists the GitHub apps that have push access to a given protected branch.
1698+
// It requires the GitHub apps to have `write` access to the `content` permission.
1699+
//
1700+
// GitHub API docs: https://docs.github.com/en/rest/branches/branch-protection#get-apps-with-access-to-the-protected-branch
1701+
//
1702+
// Note: This is a wrapper around ListApps so a naming convention with ListUserRestrictions and ListTeamRestrictions is preserved.
1703+
func (s *RepositoriesService) ListAppRestrictions(ctx context.Context, owner, repo, branch string) ([]*App, *Response, error) {
1704+
return s.ListApps(ctx, owner, repo, branch)
1705+
}
1706+
16951707
// ReplaceAppRestrictions replaces the apps that have push access to a given protected branch.
16961708
// It removes all apps that previously had push access and grants push access to the new list of apps.
16971709
// It requires the GitHub apps to have `write` access to the `content` permission.
16981710
//
16991711
// Note: The list of users, apps, and teams in total is limited to 100 items.
17001712
//
17011713
// GitHub API docs: https://docs.github.com/en/rest/branches/branch-protection#set-app-access-restrictions
1702-
func (s *RepositoriesService) ReplaceAppRestrictions(ctx context.Context, owner, repo, branch string, slug []string) ([]*App, *Response, error) {
1714+
func (s *RepositoriesService) ReplaceAppRestrictions(ctx context.Context, owner, repo, branch string, apps []string) ([]*App, *Response, error) {
17031715
u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/apps", owner, repo, branch)
1704-
req, err := s.client.NewRequest("PUT", u, slug)
1716+
req, err := s.client.NewRequest("PUT", u, apps)
17051717
if err != nil {
17061718
return nil, nil, err
17071719
}
17081720

1709-
var apps []*App
1710-
resp, err := s.client.Do(ctx, req, &apps)
1721+
var newApps []*App
1722+
resp, err := s.client.Do(ctx, req, &newApps)
17111723
if err != nil {
17121724
return nil, resp, err
17131725
}
17141726

1715-
return apps, resp, nil
1727+
return newApps, resp, nil
17161728
}
17171729

17181730
// AddAppRestrictions grants the specified apps push access to a given protected branch.
@@ -1721,42 +1733,216 @@ func (s *RepositoriesService) ReplaceAppRestrictions(ctx context.Context, owner,
17211733
// Note: The list of users, apps, and teams in total is limited to 100 items.
17221734
//
17231735
// GitHub API docs: https://docs.github.com/en/rest/branches/branch-protection#add-app-access-restrictions
1724-
func (s *RepositoriesService) AddAppRestrictions(ctx context.Context, owner, repo, branch string, slug []string) ([]*App, *Response, error) {
1736+
func (s *RepositoriesService) AddAppRestrictions(ctx context.Context, owner, repo, branch string, apps []string) ([]*App, *Response, error) {
17251737
u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/apps", owner, repo, branch)
1726-
req, err := s.client.NewRequest("POST", u, slug)
1738+
req, err := s.client.NewRequest("POST", u, apps)
17271739
if err != nil {
17281740
return nil, nil, err
17291741
}
17301742

1731-
var apps []*App
1732-
resp, err := s.client.Do(ctx, req, &apps)
1743+
var newApps []*App
1744+
resp, err := s.client.Do(ctx, req, &newApps)
17331745
if err != nil {
17341746
return nil, resp, err
17351747
}
17361748

1737-
return apps, resp, nil
1749+
return newApps, resp, nil
17381750
}
17391751

1740-
// RemoveAppRestrictions removes the ability of an app to push to this branch.
1752+
// RemoveAppRestrictions removes the restrictions of an app from pushing to this branch.
17411753
// It requires the GitHub apps to have `write` access to the `content` permission.
17421754
//
17431755
// Note: The list of users, apps, and teams in total is limited to 100 items.
17441756
//
17451757
// GitHub API docs: https://docs.github.com/en/rest/branches/branch-protection#remove-app-access-restrictions
1746-
func (s *RepositoriesService) RemoveAppRestrictions(ctx context.Context, owner, repo, branch string, slug []string) ([]*App, *Response, error) {
1758+
func (s *RepositoriesService) RemoveAppRestrictions(ctx context.Context, owner, repo, branch string, apps []string) ([]*App, *Response, error) {
17471759
u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/apps", owner, repo, branch)
1748-
req, err := s.client.NewRequest("DELETE", u, slug)
1760+
req, err := s.client.NewRequest("DELETE", u, apps)
17491761
if err != nil {
17501762
return nil, nil, err
17511763
}
17521764

1753-
var apps []*App
1754-
resp, err := s.client.Do(ctx, req, &apps)
1765+
var newApps []*App
1766+
resp, err := s.client.Do(ctx, req, &newApps)
17551767
if err != nil {
17561768
return nil, resp, err
17571769
}
17581770

1759-
return apps, resp, nil
1771+
return newApps, resp, nil
1772+
}
1773+
1774+
// ListTeamRestrictions lists the GitHub teams that have push access to a given protected branch.
1775+
// It requires the GitHub teams to have `write` access to the `content` permission.
1776+
//
1777+
// GitHub API docs: https://docs.github.com/en/rest/branches/branch-protection#get-teams-with-access-to-the-protected-branch
1778+
func (s *RepositoriesService) ListTeamRestrictions(ctx context.Context, owner, repo, branch string) ([]*Team, *Response, error) {
1779+
u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/teams", owner, repo, branch)
1780+
req, err := s.client.NewRequest("GET", u, nil)
1781+
if err != nil {
1782+
return nil, nil, err
1783+
}
1784+
1785+
var teams []*Team
1786+
resp, err := s.client.Do(ctx, req, &teams)
1787+
if err != nil {
1788+
return nil, resp, err
1789+
}
1790+
1791+
return teams, resp, nil
1792+
}
1793+
1794+
// ReplaceTeamRestrictions replaces the team that have push access to a given protected branch.
1795+
// This removes all teams that previously had push access and grants push access to the new list of teams.
1796+
// It requires the GitHub teams to have `write` access to the `content` permission.
1797+
//
1798+
// Note: The list of users, apps, and teams in total is limited to 100 items.
1799+
//
1800+
// GitHub API docs: https://docs.github.com/en/rest/branches/branch-protection#set-team-access-restrictions
1801+
func (s *RepositoriesService) ReplaceTeamRestrictions(ctx context.Context, owner, repo, branch string, teams []string) ([]*Team, *Response, error) {
1802+
u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/teams", owner, repo, branch)
1803+
req, err := s.client.NewRequest("PUT", u, teams)
1804+
if err != nil {
1805+
return nil, nil, err
1806+
}
1807+
1808+
var newTeams []*Team
1809+
resp, err := s.client.Do(ctx, req, &newTeams)
1810+
if err != nil {
1811+
return nil, resp, err
1812+
}
1813+
1814+
return newTeams, resp, nil
1815+
}
1816+
1817+
// AddTeamRestrictions grants the specified teams push access to a given protected branch.
1818+
// It requires the GitHub teams to have `write` access to the `content` permission.
1819+
//
1820+
// Note: The list of users, apps, and teams in total is limited to 100 items.
1821+
//
1822+
// GitHub API docs: https://docs.github.com/en/rest/branches/branch-protection#add-team-access-restrictions
1823+
func (s *RepositoriesService) AddTeamRestrictions(ctx context.Context, owner, repo, branch string, teams []string) ([]*Team, *Response, error) {
1824+
u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/teams", owner, repo, branch)
1825+
req, err := s.client.NewRequest("POST", u, teams)
1826+
if err != nil {
1827+
return nil, nil, err
1828+
}
1829+
1830+
var newTeams []*Team
1831+
resp, err := s.client.Do(ctx, req, &newTeams)
1832+
if err != nil {
1833+
return nil, resp, err
1834+
}
1835+
1836+
return newTeams, resp, nil
1837+
}
1838+
1839+
// RemoveTeamRestrictions removes the restrictions of a team from pushing to this branch.
1840+
// It requires the GitHub teams to have `write` access to the `content` permission.
1841+
//
1842+
// Note: The list of users, apps, and teams in total is limited to 100 items.
1843+
//
1844+
// GitHub API docs: https://docs.github.com/en/rest/branches/branch-protection#remove-team-access-restrictions
1845+
func (s *RepositoriesService) RemoveTeamRestrictions(ctx context.Context, owner, repo, branch string, teams []string) ([]*Team, *Response, error) {
1846+
u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/teams", owner, repo, branch)
1847+
req, err := s.client.NewRequest("DELETE", u, teams)
1848+
if err != nil {
1849+
return nil, nil, err
1850+
}
1851+
1852+
var newTeams []*Team
1853+
resp, err := s.client.Do(ctx, req, &newTeams)
1854+
if err != nil {
1855+
return nil, resp, err
1856+
}
1857+
1858+
return newTeams, resp, nil
1859+
}
1860+
1861+
// ListUserRestrictions lists the GitHub users that have push access to a given protected branch.
1862+
// It requires the GitHub users to have `write` access to the `content` permission.
1863+
//
1864+
// GitHub API docs: https://docs.github.com/en/rest/branches/branch-protection#get-users-with-access-to-the-protected-branch
1865+
func (s *RepositoriesService) ListUserRestrictions(ctx context.Context, owner, repo, branch string) ([]*User, *Response, error) {
1866+
u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/users", owner, repo, branch)
1867+
req, err := s.client.NewRequest("GET", u, nil)
1868+
if err != nil {
1869+
return nil, nil, err
1870+
}
1871+
1872+
var users []*User
1873+
resp, err := s.client.Do(ctx, req, &users)
1874+
if err != nil {
1875+
return nil, resp, err
1876+
}
1877+
1878+
return users, resp, nil
1879+
}
1880+
1881+
// ReplaceUserRestrictions replaces the user that have push access to a given protected branch.
1882+
// It removes all users that previously had push access and grants push access to the new list of users.
1883+
// It requires the GitHub users to have `write` access to the `content` permission.
1884+
//
1885+
// Note: The list of users, apps, and teams in total is limited to 100 items.
1886+
//
1887+
// GitHub API docs: https://docs.github.com/en/rest/branches/branch-protection#set-team-access-restrictions
1888+
func (s *RepositoriesService) ReplaceUserRestrictions(ctx context.Context, owner, repo, branch string, users []string) ([]*User, *Response, error) {
1889+
u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/users", owner, repo, branch)
1890+
req, err := s.client.NewRequest("PUT", u, users)
1891+
if err != nil {
1892+
return nil, nil, err
1893+
}
1894+
1895+
var newUsers []*User
1896+
resp, err := s.client.Do(ctx, req, &newUsers)
1897+
if err != nil {
1898+
return nil, resp, err
1899+
}
1900+
1901+
return newUsers, resp, nil
1902+
}
1903+
1904+
// AddUserRestrictions grants the specified users push access to a given protected branch.
1905+
// It requires the GitHub users to have `write` access to the `content` permission.
1906+
//
1907+
// Note: The list of users, apps, and teams in total is limited to 100 items.
1908+
//
1909+
// GitHub API docs: https://docs.github.com/en/rest/branches/branch-protection#add-team-access-restrictions
1910+
func (s *RepositoriesService) AddUserRestrictions(ctx context.Context, owner, repo, branch string, users []string) ([]*User, *Response, error) {
1911+
u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/users", owner, repo, branch)
1912+
req, err := s.client.NewRequest("POST", u, users)
1913+
if err != nil {
1914+
return nil, nil, err
1915+
}
1916+
1917+
var newUsers []*User
1918+
resp, err := s.client.Do(ctx, req, &newUsers)
1919+
if err != nil {
1920+
return nil, resp, err
1921+
}
1922+
1923+
return newUsers, resp, nil
1924+
}
1925+
1926+
// RemoveUserRestrictions removes the restrictions of a user from pushing to this branch.
1927+
// It requires the GitHub users to have `write` access to the `content` permission.
1928+
//
1929+
// Note: The list of users, apps, and teams in total is limited to 100 items.
1930+
//
1931+
// GitHub API docs: https://docs.github.com/en/rest/branches/branch-protection#remove-team-access-restrictions
1932+
func (s *RepositoriesService) RemoveUserRestrictions(ctx context.Context, owner, repo, branch string, users []string) ([]*User, *Response, error) {
1933+
u := fmt.Sprintf("repos/%v/%v/branches/%v/protection/restrictions/users", owner, repo, branch)
1934+
req, err := s.client.NewRequest("DELETE", u, users)
1935+
if err != nil {
1936+
return nil, nil, err
1937+
}
1938+
1939+
var newUsers []*User
1940+
resp, err := s.client.Do(ctx, req, &newUsers)
1941+
if err != nil {
1942+
return nil, resp, err
1943+
}
1944+
1945+
return newUsers, resp, nil
17601946
}
17611947

17621948
// TransferRequest represents a request to transfer a repository.

0 commit comments

Comments
 (0)