Skip to content

GitLab

  • Menu
    • Projects Groups Snippets
      Help
Projects Groups Snippets
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in
  • G gitlabhq1
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 21
    • Issues 21
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 12
    • Merge requests 12
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Monitor
    • Monitor
    • Incidents
  • Packages & Registries
    • Packages & Registries
    • Package Registry
    • Infrastructure Registry
  • Analytics
    • Analytics
    • CI/CD
    • Repository
    • Value stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • gpt
  • large_projects
  • gitlabhq1
  • Merge requests
  • !2839

Merged
Created 12 years ago by Administrator@rootOwner

Added methods to protect and unprotect branches in from the API

  • Overview 18
  • Commits 1
  • Changes 4

Created by: m4tthumphrey

Provides methods in the API to manage protected branches. See documentation for usage.

Request to merge github/fork/screenpages/protected-branches-api into master
  • Download as
  • Email patches

  • Plain diff

Approval is optional

Merged by (Jun 19, 2025 9:14pm UTC)

The changes were merged into master with 2c7554e8

The source branch has been deleted


  • Administrator
    Administrator @root · 12 years ago
    Owner

    Created by: m4tthumphrey

    Fixes #1947 (closed)

    By Administrator on 2013-01-29T21:13:01 (imported from GitLab project)

  • Administrator
    Administrator @root started a thread on commit 2c7554e8 12 years ago
    lib/api/projects.rb
    218 218 # Example Request:
    219 219 # GET /projects/:id/repository/branches
    220 220 get ":id/repository/branches" do
    221 present user_project.repo.heads.sort_by(&:name), with: Entities::RepoObject
    221 present user_project.repo.heads.sort_by(&:name), with: Entities::RepoObject, project: user_project
    • Administrator
      Administrator @root · 12 years ago
      Owner

      Created by: AlexDenisov

      project: user_project

      By Administrator on 2013-01-31T09:28:29 (imported from GitLab project)

  • Administrator
    Administrator @root started a thread on commit 2c7554e8 12 years ago
    lib/api/projects.rb
    230 230 # GET /projects/:id/repository/branches/:branch
    231 231 get ":id/repository/branches/:branch" do
    232 232 @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
    233 present @branch, with: Entities::RepoObject
    233 present @branch, with: Entities::RepoObject, project: user_project
    • Administrator
      Administrator @root · 12 years ago
      Owner

      Created by: AlexDenisov

      project: user_project

      By Administrator on 2013-01-31T09:28:29 (imported from GitLab project)

  • Administrator
    Administrator @root started a thread on commit 2c7554e8 12 years ago
    lib/api/projects.rb
    236 # Protect a single branch
    237 #
    238 # Parameters:
    239 # id (required) - The ID of a project
    240 # branch (required) - The name of the branch
    241 # Example Request:
    242 # PUT /projects/:id/repository/branches/:branch/protect
    243 put ":id/repository/branches/:branch/protect" do
    244 @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
    245 protected = user_project.protected_branches.find_by_name(@branch.name)
    246
    247 unless protected
    248 user_project.protected_branches.create(:name => @branch.name)
    249 end
    250
    251 present @branch, with: Entities::RepoObject, project: user_project
    • Administrator
      Administrator @root · 12 years ago
      Owner

      Created by: AlexDenisov

      project: user_project

      By Administrator on 2013-01-31T09:28:29 (imported from GitLab project)

  • Administrator
    Administrator @root started a thread on commit 2c7554e8 12 years ago
    lib/api/projects.rb
    254 # Unprotect a single branch
    255 #
    256 # Parameters:
    257 # id (required) - The ID of a project
    258 # branch (required) - The name of the branch
    259 # Example Request:
    260 # PUT /projects/:id/repository/branches/:branch/unprotect
    261 put ":id/repository/branches/:branch/unprotect" do
    262 @branch = user_project.repo.heads.find { |item| item.name == params[:branch] }
    263 protected = user_project.protected_branches.find_by_name(@branch.name)
    264
    265 if protected
    266 protected.destroy
    267 end
    268
    269 present @branch, with: Entities::RepoObject, project: user_project
    • Administrator
      Administrator @root · 12 years ago
      Owner

      Created by: AlexDenisov

      project: user_project

      By Administrator on 2013-01-31T09:28:29 (imported from GitLab project)

  • Administrator
    Administrator @root started a thread on commit 2c7554e8 12 years ago
    doc/api/repositories.md
    73 74 },
    74 75 "authored_date": "2012-06-27T05:51:39-07:00",
    75 76 "committed_date": "2012-06-28T03:44:20-07:00"
    76 }
    77 },
    78 "protected": true
    79 }
    80 ```
    81
    82 ## Protect a project repository branch
    83
    84 Protect a single project repository branch.
    85
    86 ```
    87 PUT /projects/:id/repository/branches/:branch/protect
    • Administrator
      Administrator @root · 12 years ago
      Owner

      Created by: vsizov

      you've added post request as i understood.

      By Administrator on 2013-01-31T09:28:29 (imported from GitLab project)

  • Administrator
    Administrator @root started a thread on commit 2c7554e8 12 years ago
    doc/api/repositories.md
    112 "name": "John Smith",
    113 "email": "john@example.com"
    114 },
    115 "authored_date": "2012-06-27T05:51:39-07:00",
    116 "committed_date": "2012-06-28T03:44:20-07:00"
    117 },
    118 "protected": true
    119 }
    120 ```
    121
    122 ## Unprotect a project repository branch
    123
    124 Unprotect a single project repository branch.
    125
    126 ```
    127 PUT /projects/:id/repository/branches/:branch/unprotect
    • Administrator
      Administrator @root · 12 years ago
      Owner

      Created by: vsizov

      the same

      By Administrator on 2013-01-31T09:28:29 (imported from GitLab project)

  • Administrator
    Administrator @root started a thread on commit 2c7554e8 12 years ago
    lib/api/projects.rb
    218 218 # Example Request:
    219 219 # GET /projects/:id/repository/branches
    220 220 get ":id/repository/branches" do
    221 present user_project.repo.heads.sort_by(&:name), with: Entities::RepoObject
    221 present user_project.repo.heads.sort_by(&:name), with: Entities::RepoObject, project: user_project
    • Administrator
      Administrator @root · 12 years ago
      Owner

      Created by: vsizov

      why?

      By Administrator on 2013-01-31T09:28:29 (imported from GitLab project)

  • Administrator
    Administrator @root · 12 years ago
    Owner

    Created by: vsizov

    nice one. But there is one issue. I think that update single branch must be implemented through put request according to rest. Don't you think so?

    By Administrator on 2013-01-30T09:09:36 (imported from GitLab project)

  • Administrator
    Administrator @root · 12 years ago
    Owner

    Created by: AlexDenisov

    @vsizov, according to rest there should be something like that

    GET /projects/:id/repository/protected_branches
    POST /projects/:id/repository/protected_branches
    DELETE /projects/:id/repository/protected_branches

    imho.

    By Administrator on 2013-01-30T09:15:53 (imported from GitLab project)

  • Administrator
    Administrator @root · 12 years ago
    Owner

    Created by: vsizov

    @AlexDenisov I agree with you

    By Administrator on 2013-01-30T09:19:55 (imported from GitLab project)

  • Administrator
    Administrator @root started a thread on commit 2c7554e8 12 years ago
    lib/api/projects.rb
    218 218 # Example Request:
    219 219 # GET /projects/:id/repository/branches
    220 220 get ":id/repository/branches" do
    221 present user_project.repo.heads.sort_by(&:name), with: Entities::RepoObject
    221 present user_project.repo.heads.sort_by(&:name), with: Entities::RepoObject, project: user_project
    • Administrator
      Administrator @root · 12 years ago
      Owner

      Created by: m4tthumphrey

      So that the branches can contain the protected attribute.

      By Administrator on 2013-01-31T09:28:29 (imported from GitLab project)

  • Administrator
    Administrator @root · 12 years ago
    Owner

    Created by: vsizov

    The main idea: Every request that going to change single item must be PUT.

    By Administrator on 2013-01-30T09:22:26 (imported from GitLab project)

  • Administrator
    Administrator @root · 12 years ago
    Owner

    Created by: m4tthumphrey

    Would you accept both methods? Ie keeping the /protect /unprotect as well as adding the proper REST style:

    GET /projects/:id/repository/protected_branches
    POST /projects/:id/repository/protected_branches
    DELETE /projects/:id/repository/protected_branches

    By Administrator on 2013-01-30T09:36:33 (imported from GitLab project)

  • Administrator
    Administrator @root started a thread on commit 2c7554e8 12 years ago
    doc/api/repositories.md
    73 74 },
    74 75 "authored_date": "2012-06-27T05:51:39-07:00",
    75 76 "committed_date": "2012-06-28T03:44:20-07:00"
    76 }
    77 },
    78 "protected": true
    79 }
    80 ```
    81
    82 ## Protect a project repository branch
    83
    84 Protect a single project repository branch.
    85
    86 ```
    87 PUT /projects/:id/repository/branches/:branch/protect
    • Administrator
      Administrator @root · 12 years ago
      Owner

      Created by: m4tthumphrey

      Yes I did. Woops!

      By Administrator on 2013-01-31T09:28:29 (imported from GitLab project)

  • Administrator
    Administrator @root · 12 years ago
    Owner

    Created by: dzaporozhets

    Please squash the commits and we will accept this one

    By Administrator on 2013-01-31T06:27:26 (imported from GitLab project)

  • Administrator
    Administrator @root · 12 years ago
    Owner

    Created by: m4tthumphrey

    @randx done!

    By Administrator on 2013-01-31T09:29:07 (imported from GitLab project)

  • Administrator
    Administrator @root · 12 years ago
    Owner

    Created by: dzaporozhets

    Thank you

    By Administrator on 2013-01-31T18:44:14 (imported from GitLab project)

  • You're only seeing other activity in the feed. To add a comment, switch to one of the following options.
Please register or sign in to reply
0 Assignees
None
Assign to
0 Reviewers
None
Request review from
Milestone
No milestone
None
None
Time tracking
No estimate or time spent
0
Labels
None
Assign labels
  • No matching results
  • Manage project labels
Lock merge request
Unlocked
1
1 participant
user avatar
Reference: gpt/large_projects/gitlabhq1!2839
Source branch: github/fork/screenpages/protected-branches-api

    0 pending comments

Menu

Projects Groups Snippets
Help