You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

branch.go 26 kB

Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
6 years ago
9 years ago
Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
6 years ago
Move macaron to chi (#14293) Use [chi](https://github.com/go-chi/chi) instead of the forked [macaron](https://gitea.com/macaron/macaron). Since macaron and chi have conflicts with session share, this big PR becomes a have-to thing. According my previous idea, we can replace macaron step by step but I'm wrong. :( Below is a list of big changes on this PR. - [x] Define `context.ResponseWriter` interface with an implementation `context.Response`. - [x] Use chi instead of macaron, and also a customize `Route` to wrap chi so that the router usage is similar as before. - [x] Create different routers for `web`, `api`, `internal` and `install` so that the codes will be more clear and no magic . - [x] Use https://github.com/unrolled/render instead of macaron's internal render - [x] Use https://github.com/NYTimes/gziphandler instead of https://gitea.com/macaron/gzip - [x] Use https://gitea.com/go-chi/session which is a modified version of https://gitea.com/macaron/session and removed `nodb` support since it will not be maintained. **BREAK** - [x] Use https://gitea.com/go-chi/captcha which is a modified version of https://gitea.com/macaron/captcha - [x] Use https://gitea.com/go-chi/cache which is a modified version of https://gitea.com/macaron/cache - [x] Use https://gitea.com/go-chi/binding which is a modified version of https://gitea.com/macaron/binding - [x] Use https://github.com/go-chi/cors instead of https://gitea.com/macaron/cors - [x] Dropped https://gitea.com/macaron/i18n and make a new one in `code.gitea.io/gitea/modules/translation` - [x] Move validation form structs from `code.gitea.io/gitea/modules/auth` to `code.gitea.io/gitea/modules/forms` to avoid dependency cycle. - [x] Removed macaron log service because it's not need any more. **BREAK** - [x] All form structs have to be get by `web.GetForm(ctx)` in the route function but not as a function parameter on routes definition. - [x] Move Git HTTP protocol implementation to use routers directly. - [x] Fix the problem that chi routes don't support trailing slash but macaron did. - [x] `/api/v1/swagger` now will be redirect to `/api/swagger` but not render directly so that `APIContext` will not create a html render. Notices: - Chi router don't support request with trailing slash - Integration test `TestUserHeatmap` maybe mysql version related. It's failed on my macOS(mysql 5.7.29 installed via brew) but succeed on CI. Co-authored-by: 6543 <6543@obermui.de>
4 years ago
[Enhancement] Allow admin to merge pr with protected file changes (#12078) * [Enhancement] Allow admin to merge pr with protected file changes As tilte, show protected message in diff page and merge box. Signed-off-by: a1012112796 <1012112796@qq.com> * remove unused ver * Update options/locale/locale_en-US.ini Co-authored-by: Cirno the Strongest <1447794+CirnoT@users.noreply.github.com> * Add TrN * Apply suggestions from code review * fix lint * Update options/locale/locale_en-US.ini Co-authored-by: zeripath <art27@cantab.net> * Apply suggestions from code review * move pr proteced files check to TestPatch * Call TestPatch when protected branches settings changed * Apply review suggestion @CirnoT * move to service @lunny * slightly restructure routers/private/hook.go Adds a lot of comments and simplifies the logic Signed-off-by: Andrew Thornton <art27@cantab.net> * placate lint Signed-off-by: Andrew Thornton <art27@cantab.net> * skip duplicate protected files check * fix check logic * slight refactor of TestPatch Signed-off-by: Andrew Thornton <art27@cantab.net> * When checking for protected files changes in TestPatch use the temporary repository Signed-off-by: Andrew Thornton <art27@cantab.net> * fix introduced issue with hook Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove the check on PR index being greater than 0 as it unnecessary Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: Cirno the Strongest <1447794+CirnoT@users.noreply.github.com> Co-authored-by: zeripath <art27@cantab.net> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
5 years ago
9 years ago
9 years ago
Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
6 years ago
9 years ago
9 years ago
9 years ago
9 years ago
Move macaron to chi (#14293) Use [chi](https://github.com/go-chi/chi) instead of the forked [macaron](https://gitea.com/macaron/macaron). Since macaron and chi have conflicts with session share, this big PR becomes a have-to thing. According my previous idea, we can replace macaron step by step but I'm wrong. :( Below is a list of big changes on this PR. - [x] Define `context.ResponseWriter` interface with an implementation `context.Response`. - [x] Use chi instead of macaron, and also a customize `Route` to wrap chi so that the router usage is similar as before. - [x] Create different routers for `web`, `api`, `internal` and `install` so that the codes will be more clear and no magic . - [x] Use https://github.com/unrolled/render instead of macaron's internal render - [x] Use https://github.com/NYTimes/gziphandler instead of https://gitea.com/macaron/gzip - [x] Use https://gitea.com/go-chi/session which is a modified version of https://gitea.com/macaron/session and removed `nodb` support since it will not be maintained. **BREAK** - [x] Use https://gitea.com/go-chi/captcha which is a modified version of https://gitea.com/macaron/captcha - [x] Use https://gitea.com/go-chi/cache which is a modified version of https://gitea.com/macaron/cache - [x] Use https://gitea.com/go-chi/binding which is a modified version of https://gitea.com/macaron/binding - [x] Use https://github.com/go-chi/cors instead of https://gitea.com/macaron/cors - [x] Dropped https://gitea.com/macaron/i18n and make a new one in `code.gitea.io/gitea/modules/translation` - [x] Move validation form structs from `code.gitea.io/gitea/modules/auth` to `code.gitea.io/gitea/modules/forms` to avoid dependency cycle. - [x] Removed macaron log service because it's not need any more. **BREAK** - [x] All form structs have to be get by `web.GetForm(ctx)` in the route function but not as a function parameter on routes definition. - [x] Move Git HTTP protocol implementation to use routers directly. - [x] Fix the problem that chi routes don't support trailing slash but macaron did. - [x] `/api/v1/swagger` now will be redirect to `/api/swagger` but not render directly so that `APIContext` will not create a html render. Notices: - Chi router don't support request with trailing slash - Integration test `TestUserHeatmap` maybe mysql version related. It's failed on my macOS(mysql 5.7.29 installed via brew) but succeed on CI. Co-authored-by: 6543 <6543@obermui.de>
4 years ago
Move macaron to chi (#14293) Use [chi](https://github.com/go-chi/chi) instead of the forked [macaron](https://gitea.com/macaron/macaron). Since macaron and chi have conflicts with session share, this big PR becomes a have-to thing. According my previous idea, we can replace macaron step by step but I'm wrong. :( Below is a list of big changes on this PR. - [x] Define `context.ResponseWriter` interface with an implementation `context.Response`. - [x] Use chi instead of macaron, and also a customize `Route` to wrap chi so that the router usage is similar as before. - [x] Create different routers for `web`, `api`, `internal` and `install` so that the codes will be more clear and no magic . - [x] Use https://github.com/unrolled/render instead of macaron's internal render - [x] Use https://github.com/NYTimes/gziphandler instead of https://gitea.com/macaron/gzip - [x] Use https://gitea.com/go-chi/session which is a modified version of https://gitea.com/macaron/session and removed `nodb` support since it will not be maintained. **BREAK** - [x] Use https://gitea.com/go-chi/captcha which is a modified version of https://gitea.com/macaron/captcha - [x] Use https://gitea.com/go-chi/cache which is a modified version of https://gitea.com/macaron/cache - [x] Use https://gitea.com/go-chi/binding which is a modified version of https://gitea.com/macaron/binding - [x] Use https://github.com/go-chi/cors instead of https://gitea.com/macaron/cors - [x] Dropped https://gitea.com/macaron/i18n and make a new one in `code.gitea.io/gitea/modules/translation` - [x] Move validation form structs from `code.gitea.io/gitea/modules/auth` to `code.gitea.io/gitea/modules/forms` to avoid dependency cycle. - [x] Removed macaron log service because it's not need any more. **BREAK** - [x] All form structs have to be get by `web.GetForm(ctx)` in the route function but not as a function parameter on routes definition. - [x] Move Git HTTP protocol implementation to use routers directly. - [x] Fix the problem that chi routes don't support trailing slash but macaron did. - [x] `/api/v1/swagger` now will be redirect to `/api/swagger` but not render directly so that `APIContext` will not create a html render. Notices: - Chi router don't support request with trailing slash - Integration test `TestUserHeatmap` maybe mysql version related. It's failed on my macOS(mysql 5.7.29 installed via brew) but succeed on CI. Co-authored-by: 6543 <6543@obermui.de>
4 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
Move macaron to chi (#14293) Use [chi](https://github.com/go-chi/chi) instead of the forked [macaron](https://gitea.com/macaron/macaron). Since macaron and chi have conflicts with session share, this big PR becomes a have-to thing. According my previous idea, we can replace macaron step by step but I'm wrong. :( Below is a list of big changes on this PR. - [x] Define `context.ResponseWriter` interface with an implementation `context.Response`. - [x] Use chi instead of macaron, and also a customize `Route` to wrap chi so that the router usage is similar as before. - [x] Create different routers for `web`, `api`, `internal` and `install` so that the codes will be more clear and no magic . - [x] Use https://github.com/unrolled/render instead of macaron's internal render - [x] Use https://github.com/NYTimes/gziphandler instead of https://gitea.com/macaron/gzip - [x] Use https://gitea.com/go-chi/session which is a modified version of https://gitea.com/macaron/session and removed `nodb` support since it will not be maintained. **BREAK** - [x] Use https://gitea.com/go-chi/captcha which is a modified version of https://gitea.com/macaron/captcha - [x] Use https://gitea.com/go-chi/cache which is a modified version of https://gitea.com/macaron/cache - [x] Use https://gitea.com/go-chi/binding which is a modified version of https://gitea.com/macaron/binding - [x] Use https://github.com/go-chi/cors instead of https://gitea.com/macaron/cors - [x] Dropped https://gitea.com/macaron/i18n and make a new one in `code.gitea.io/gitea/modules/translation` - [x] Move validation form structs from `code.gitea.io/gitea/modules/auth` to `code.gitea.io/gitea/modules/forms` to avoid dependency cycle. - [x] Removed macaron log service because it's not need any more. **BREAK** - [x] All form structs have to be get by `web.GetForm(ctx)` in the route function but not as a function parameter on routes definition. - [x] Move Git HTTP protocol implementation to use routers directly. - [x] Fix the problem that chi routes don't support trailing slash but macaron did. - [x] `/api/v1/swagger` now will be redirect to `/api/swagger` but not render directly so that `APIContext` will not create a html render. Notices: - Chi router don't support request with trailing slash - Integration test `TestUserHeatmap` maybe mysql version related. It's failed on my macOS(mysql 5.7.29 installed via brew) but succeed on CI. Co-authored-by: 6543 <6543@obermui.de>
4 years ago
Move macaron to chi (#14293) Use [chi](https://github.com/go-chi/chi) instead of the forked [macaron](https://gitea.com/macaron/macaron). Since macaron and chi have conflicts with session share, this big PR becomes a have-to thing. According my previous idea, we can replace macaron step by step but I'm wrong. :( Below is a list of big changes on this PR. - [x] Define `context.ResponseWriter` interface with an implementation `context.Response`. - [x] Use chi instead of macaron, and also a customize `Route` to wrap chi so that the router usage is similar as before. - [x] Create different routers for `web`, `api`, `internal` and `install` so that the codes will be more clear and no magic . - [x] Use https://github.com/unrolled/render instead of macaron's internal render - [x] Use https://github.com/NYTimes/gziphandler instead of https://gitea.com/macaron/gzip - [x] Use https://gitea.com/go-chi/session which is a modified version of https://gitea.com/macaron/session and removed `nodb` support since it will not be maintained. **BREAK** - [x] Use https://gitea.com/go-chi/captcha which is a modified version of https://gitea.com/macaron/captcha - [x] Use https://gitea.com/go-chi/cache which is a modified version of https://gitea.com/macaron/cache - [x] Use https://gitea.com/go-chi/binding which is a modified version of https://gitea.com/macaron/binding - [x] Use https://github.com/go-chi/cors instead of https://gitea.com/macaron/cors - [x] Dropped https://gitea.com/macaron/i18n and make a new one in `code.gitea.io/gitea/modules/translation` - [x] Move validation form structs from `code.gitea.io/gitea/modules/auth` to `code.gitea.io/gitea/modules/forms` to avoid dependency cycle. - [x] Removed macaron log service because it's not need any more. **BREAK** - [x] All form structs have to be get by `web.GetForm(ctx)` in the route function but not as a function parameter on routes definition. - [x] Move Git HTTP protocol implementation to use routers directly. - [x] Fix the problem that chi routes don't support trailing slash but macaron did. - [x] `/api/v1/swagger` now will be redirect to `/api/swagger` but not render directly so that `APIContext` will not create a html render. Notices: - Chi router don't support request with trailing slash - Integration test `TestUserHeatmap` maybe mysql version related. It's failed on my macOS(mysql 5.7.29 installed via brew) but succeed on CI. Co-authored-by: 6543 <6543@obermui.de>
4 years ago
[Enhancement] Allow admin to merge pr with protected file changes (#12078) * [Enhancement] Allow admin to merge pr with protected file changes As tilte, show protected message in diff page and merge box. Signed-off-by: a1012112796 <1012112796@qq.com> * remove unused ver * Update options/locale/locale_en-US.ini Co-authored-by: Cirno the Strongest <1447794+CirnoT@users.noreply.github.com> * Add TrN * Apply suggestions from code review * fix lint * Update options/locale/locale_en-US.ini Co-authored-by: zeripath <art27@cantab.net> * Apply suggestions from code review * move pr proteced files check to TestPatch * Call TestPatch when protected branches settings changed * Apply review suggestion @CirnoT * move to service @lunny * slightly restructure routers/private/hook.go Adds a lot of comments and simplifies the logic Signed-off-by: Andrew Thornton <art27@cantab.net> * placate lint Signed-off-by: Andrew Thornton <art27@cantab.net> * skip duplicate protected files check * fix check logic * slight refactor of TestPatch Signed-off-by: Andrew Thornton <art27@cantab.net> * When checking for protected files changes in TestPatch use the temporary repository Signed-off-by: Andrew Thornton <art27@cantab.net> * fix introduced issue with hook Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove the check on PR index being greater than 0 as it unnecessary Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: Cirno the Strongest <1447794+CirnoT@users.noreply.github.com> Co-authored-by: zeripath <art27@cantab.net> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
5 years ago
Move macaron to chi (#14293) Use [chi](https://github.com/go-chi/chi) instead of the forked [macaron](https://gitea.com/macaron/macaron). Since macaron and chi have conflicts with session share, this big PR becomes a have-to thing. According my previous idea, we can replace macaron step by step but I'm wrong. :( Below is a list of big changes on this PR. - [x] Define `context.ResponseWriter` interface with an implementation `context.Response`. - [x] Use chi instead of macaron, and also a customize `Route` to wrap chi so that the router usage is similar as before. - [x] Create different routers for `web`, `api`, `internal` and `install` so that the codes will be more clear and no magic . - [x] Use https://github.com/unrolled/render instead of macaron's internal render - [x] Use https://github.com/NYTimes/gziphandler instead of https://gitea.com/macaron/gzip - [x] Use https://gitea.com/go-chi/session which is a modified version of https://gitea.com/macaron/session and removed `nodb` support since it will not be maintained. **BREAK** - [x] Use https://gitea.com/go-chi/captcha which is a modified version of https://gitea.com/macaron/captcha - [x] Use https://gitea.com/go-chi/cache which is a modified version of https://gitea.com/macaron/cache - [x] Use https://gitea.com/go-chi/binding which is a modified version of https://gitea.com/macaron/binding - [x] Use https://github.com/go-chi/cors instead of https://gitea.com/macaron/cors - [x] Dropped https://gitea.com/macaron/i18n and make a new one in `code.gitea.io/gitea/modules/translation` - [x] Move validation form structs from `code.gitea.io/gitea/modules/auth` to `code.gitea.io/gitea/modules/forms` to avoid dependency cycle. - [x] Removed macaron log service because it's not need any more. **BREAK** - [x] All form structs have to be get by `web.GetForm(ctx)` in the route function but not as a function parameter on routes definition. - [x] Move Git HTTP protocol implementation to use routers directly. - [x] Fix the problem that chi routes don't support trailing slash but macaron did. - [x] `/api/v1/swagger` now will be redirect to `/api/swagger` but not render directly so that `APIContext` will not create a html render. Notices: - Chi router don't support request with trailing slash - Integration test `TestUserHeatmap` maybe mysql version related. It's failed on my macOS(mysql 5.7.29 installed via brew) but succeed on CI. Co-authored-by: 6543 <6543@obermui.de>
4 years ago
Move macaron to chi (#14293) Use [chi](https://github.com/go-chi/chi) instead of the forked [macaron](https://gitea.com/macaron/macaron). Since macaron and chi have conflicts with session share, this big PR becomes a have-to thing. According my previous idea, we can replace macaron step by step but I'm wrong. :( Below is a list of big changes on this PR. - [x] Define `context.ResponseWriter` interface with an implementation `context.Response`. - [x] Use chi instead of macaron, and also a customize `Route` to wrap chi so that the router usage is similar as before. - [x] Create different routers for `web`, `api`, `internal` and `install` so that the codes will be more clear and no magic . - [x] Use https://github.com/unrolled/render instead of macaron's internal render - [x] Use https://github.com/NYTimes/gziphandler instead of https://gitea.com/macaron/gzip - [x] Use https://gitea.com/go-chi/session which is a modified version of https://gitea.com/macaron/session and removed `nodb` support since it will not be maintained. **BREAK** - [x] Use https://gitea.com/go-chi/captcha which is a modified version of https://gitea.com/macaron/captcha - [x] Use https://gitea.com/go-chi/cache which is a modified version of https://gitea.com/macaron/cache - [x] Use https://gitea.com/go-chi/binding which is a modified version of https://gitea.com/macaron/binding - [x] Use https://github.com/go-chi/cors instead of https://gitea.com/macaron/cors - [x] Dropped https://gitea.com/macaron/i18n and make a new one in `code.gitea.io/gitea/modules/translation` - [x] Move validation form structs from `code.gitea.io/gitea/modules/auth` to `code.gitea.io/gitea/modules/forms` to avoid dependency cycle. - [x] Removed macaron log service because it's not need any more. **BREAK** - [x] All form structs have to be get by `web.GetForm(ctx)` in the route function but not as a function parameter on routes definition. - [x] Move Git HTTP protocol implementation to use routers directly. - [x] Fix the problem that chi routes don't support trailing slash but macaron did. - [x] `/api/v1/swagger` now will be redirect to `/api/swagger` but not render directly so that `APIContext` will not create a html render. Notices: - Chi router don't support request with trailing slash - Integration test `TestUserHeatmap` maybe mysql version related. It's failed on my macOS(mysql 5.7.29 installed via brew) but succeed on CI. Co-authored-by: 6543 <6543@obermui.de>
4 years ago
[Enhancement] Allow admin to merge pr with protected file changes (#12078) * [Enhancement] Allow admin to merge pr with protected file changes As tilte, show protected message in diff page and merge box. Signed-off-by: a1012112796 <1012112796@qq.com> * remove unused ver * Update options/locale/locale_en-US.ini Co-authored-by: Cirno the Strongest <1447794+CirnoT@users.noreply.github.com> * Add TrN * Apply suggestions from code review * fix lint * Update options/locale/locale_en-US.ini Co-authored-by: zeripath <art27@cantab.net> * Apply suggestions from code review * move pr proteced files check to TestPatch * Call TestPatch when protected branches settings changed * Apply review suggestion @CirnoT * move to service @lunny * slightly restructure routers/private/hook.go Adds a lot of comments and simplifies the logic Signed-off-by: Andrew Thornton <art27@cantab.net> * placate lint Signed-off-by: Andrew Thornton <art27@cantab.net> * skip duplicate protected files check * fix check logic * slight refactor of TestPatch Signed-off-by: Andrew Thornton <art27@cantab.net> * When checking for protected files changes in TestPatch use the temporary repository Signed-off-by: Andrew Thornton <art27@cantab.net> * fix introduced issue with hook Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove the check on PR index being greater than 0 as it unnecessary Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: Cirno the Strongest <1447794+CirnoT@users.noreply.github.com> Co-authored-by: zeripath <art27@cantab.net> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "fmt"
  8. "net/http"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/convert"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/log"
  14. repo_module "code.gitea.io/gitea/modules/repository"
  15. api "code.gitea.io/gitea/modules/structs"
  16. "code.gitea.io/gitea/modules/web"
  17. "code.gitea.io/gitea/routers/api/v1/utils"
  18. pull_service "code.gitea.io/gitea/services/pull"
  19. repo_service "code.gitea.io/gitea/services/repository"
  20. )
  21. // GetBranch get a branch of a repository
  22. func GetBranch(ctx *context.APIContext) {
  23. // swagger:operation GET /repos/{owner}/{repo}/branches/{branch} repository repoGetBranch
  24. // ---
  25. // summary: Retrieve a specific branch from a repository, including its effective branch protection
  26. // produces:
  27. // - application/json
  28. // parameters:
  29. // - name: owner
  30. // in: path
  31. // description: owner of the repo
  32. // type: string
  33. // required: true
  34. // - name: repo
  35. // in: path
  36. // description: name of the repo
  37. // type: string
  38. // required: true
  39. // - name: branch
  40. // in: path
  41. // description: branch to get
  42. // type: string
  43. // required: true
  44. // responses:
  45. // "200":
  46. // "$ref": "#/responses/Branch"
  47. // "404":
  48. // "$ref": "#/responses/notFound"
  49. branchName := ctx.Params("*")
  50. branch, err := repo_module.GetBranch(ctx.Repo.Repository, branchName)
  51. if err != nil {
  52. if git.IsErrBranchNotExist(err) {
  53. ctx.NotFound(err)
  54. } else {
  55. ctx.Error(http.StatusInternalServerError, "GetBranch", err)
  56. }
  57. return
  58. }
  59. c, err := branch.GetCommit()
  60. if err != nil {
  61. ctx.Error(http.StatusInternalServerError, "GetCommit", err)
  62. return
  63. }
  64. branchProtection, err := ctx.Repo.Repository.GetBranchProtection(branchName)
  65. if err != nil {
  66. ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
  67. return
  68. }
  69. br, err := convert.ToBranch(ctx.Repo.Repository, branch, c, branchProtection, ctx.User, ctx.Repo.IsAdmin())
  70. if err != nil {
  71. ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
  72. return
  73. }
  74. ctx.JSON(http.StatusOK, br)
  75. }
  76. // DeleteBranch get a branch of a repository
  77. func DeleteBranch(ctx *context.APIContext) {
  78. // swagger:operation DELETE /repos/{owner}/{repo}/branches/{branch} repository repoDeleteBranch
  79. // ---
  80. // summary: Delete a specific branch from a repository
  81. // produces:
  82. // - application/json
  83. // parameters:
  84. // - name: owner
  85. // in: path
  86. // description: owner of the repo
  87. // type: string
  88. // required: true
  89. // - name: repo
  90. // in: path
  91. // description: name of the repo
  92. // type: string
  93. // required: true
  94. // - name: branch
  95. // in: path
  96. // description: branch to delete
  97. // type: string
  98. // required: true
  99. // responses:
  100. // "204":
  101. // "$ref": "#/responses/empty"
  102. // "403":
  103. // "$ref": "#/responses/error"
  104. // "404":
  105. // "$ref": "#/responses/notFound"
  106. branchName := ctx.Params("*")
  107. if ctx.Repo.Repository.DefaultBranch == branchName {
  108. ctx.Error(http.StatusForbidden, "DefaultBranch", fmt.Errorf("can not delete default branch"))
  109. return
  110. }
  111. isProtected, err := ctx.Repo.Repository.IsProtectedBranch(branchName, ctx.User)
  112. if err != nil {
  113. ctx.InternalServerError(err)
  114. return
  115. }
  116. if isProtected {
  117. ctx.Error(http.StatusForbidden, "IsProtectedBranch", fmt.Errorf("branch protected"))
  118. return
  119. }
  120. branch, err := repo_module.GetBranch(ctx.Repo.Repository, branchName)
  121. if err != nil {
  122. if git.IsErrBranchNotExist(err) {
  123. ctx.NotFound(err)
  124. } else {
  125. ctx.Error(http.StatusInternalServerError, "GetBranch", err)
  126. }
  127. return
  128. }
  129. c, err := branch.GetCommit()
  130. if err != nil {
  131. ctx.Error(http.StatusInternalServerError, "GetCommit", err)
  132. return
  133. }
  134. if err := ctx.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
  135. Force: true,
  136. }); err != nil {
  137. ctx.Error(http.StatusInternalServerError, "DeleteBranch", err)
  138. return
  139. }
  140. // Don't return error below this
  141. if err := repo_service.PushUpdate(
  142. &repo_module.PushUpdateOptions{
  143. RefFullName: git.BranchPrefix + branchName,
  144. OldCommitID: c.ID.String(),
  145. NewCommitID: git.EmptySHA,
  146. PusherID: ctx.User.ID,
  147. PusherName: ctx.User.Name,
  148. RepoUserName: ctx.Repo.Owner.Name,
  149. RepoName: ctx.Repo.Repository.Name,
  150. }); err != nil {
  151. log.Error("Update: %v", err)
  152. }
  153. if err := ctx.Repo.Repository.AddDeletedBranch(branchName, c.ID.String(), ctx.User.ID); err != nil {
  154. log.Warn("AddDeletedBranch: %v", err)
  155. }
  156. ctx.Status(http.StatusNoContent)
  157. }
  158. // CreateBranch creates a branch for a user's repository
  159. func CreateBranch(ctx *context.APIContext) {
  160. // swagger:operation POST /repos/{owner}/{repo}/branches repository repoCreateBranch
  161. // ---
  162. // summary: Create a branch
  163. // consumes:
  164. // - application/json
  165. // produces:
  166. // - application/json
  167. // parameters:
  168. // - name: owner
  169. // in: path
  170. // description: owner of the repo
  171. // type: string
  172. // required: true
  173. // - name: repo
  174. // in: path
  175. // description: name of the repo
  176. // type: string
  177. // required: true
  178. // - name: body
  179. // in: body
  180. // schema:
  181. // "$ref": "#/definitions/CreateBranchRepoOption"
  182. // responses:
  183. // "201":
  184. // "$ref": "#/responses/Branch"
  185. // "404":
  186. // description: The old branch does not exist.
  187. // "409":
  188. // description: The branch with the same name already exists.
  189. opt := web.GetForm(ctx).(*api.CreateBranchRepoOption)
  190. if ctx.Repo.Repository.IsEmpty {
  191. ctx.Error(http.StatusNotFound, "", "Git Repository is empty.")
  192. return
  193. }
  194. if len(opt.OldBranchName) == 0 {
  195. opt.OldBranchName = ctx.Repo.Repository.DefaultBranch
  196. }
  197. err := repo_module.CreateNewBranch(ctx.User, ctx.Repo.Repository, opt.OldBranchName, opt.BranchName)
  198. if err != nil {
  199. if models.IsErrBranchDoesNotExist(err) {
  200. ctx.Error(http.StatusNotFound, "", "The old branch does not exist")
  201. }
  202. if models.IsErrTagAlreadyExists(err) {
  203. ctx.Error(http.StatusConflict, "", "The branch with the same tag already exists.")
  204. } else if models.IsErrBranchAlreadyExists(err) || git.IsErrPushOutOfDate(err) {
  205. ctx.Error(http.StatusConflict, "", "The branch already exists.")
  206. } else if models.IsErrBranchNameConflict(err) {
  207. ctx.Error(http.StatusConflict, "", "The branch with the same name already exists.")
  208. } else {
  209. ctx.Error(http.StatusInternalServerError, "CreateRepoBranch", err)
  210. }
  211. return
  212. }
  213. branch, err := repo_module.GetBranch(ctx.Repo.Repository, opt.BranchName)
  214. if err != nil {
  215. ctx.Error(http.StatusInternalServerError, "GetBranch", err)
  216. return
  217. }
  218. commit, err := branch.GetCommit()
  219. if err != nil {
  220. ctx.Error(http.StatusInternalServerError, "GetCommit", err)
  221. return
  222. }
  223. branchProtection, err := ctx.Repo.Repository.GetBranchProtection(branch.Name)
  224. if err != nil {
  225. ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
  226. return
  227. }
  228. br, err := convert.ToBranch(ctx.Repo.Repository, branch, commit, branchProtection, ctx.User, ctx.Repo.IsAdmin())
  229. if err != nil {
  230. ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
  231. return
  232. }
  233. ctx.JSON(http.StatusCreated, br)
  234. }
  235. // ListBranches list all the branches of a repository
  236. func ListBranches(ctx *context.APIContext) {
  237. // swagger:operation GET /repos/{owner}/{repo}/branches repository repoListBranches
  238. // ---
  239. // summary: List a repository's branches
  240. // produces:
  241. // - application/json
  242. // parameters:
  243. // - name: owner
  244. // in: path
  245. // description: owner of the repo
  246. // type: string
  247. // required: true
  248. // - name: repo
  249. // in: path
  250. // description: name of the repo
  251. // type: string
  252. // required: true
  253. // - name: page
  254. // in: query
  255. // description: page number of results to return (1-based)
  256. // type: integer
  257. // - name: limit
  258. // in: query
  259. // description: page size of results
  260. // type: integer
  261. // responses:
  262. // "200":
  263. // "$ref": "#/responses/BranchList"
  264. listOptions := utils.GetListOptions(ctx)
  265. skip, _ := listOptions.GetStartEnd()
  266. branches, totalNumOfBranches, err := repo_module.GetBranches(ctx.Repo.Repository, skip, listOptions.PageSize)
  267. if err != nil {
  268. ctx.Error(http.StatusInternalServerError, "GetBranches", err)
  269. return
  270. }
  271. apiBranches := make([]*api.Branch, len(branches))
  272. for i := range branches {
  273. c, err := branches[i].GetCommit()
  274. if err != nil {
  275. ctx.Error(http.StatusInternalServerError, "GetCommit", err)
  276. return
  277. }
  278. branchProtection, err := ctx.Repo.Repository.GetBranchProtection(branches[i].Name)
  279. if err != nil {
  280. ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
  281. return
  282. }
  283. apiBranches[i], err = convert.ToBranch(ctx.Repo.Repository, branches[i], c, branchProtection, ctx.User, ctx.Repo.IsAdmin())
  284. if err != nil {
  285. ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
  286. return
  287. }
  288. }
  289. ctx.SetLinkHeader(int(totalNumOfBranches), listOptions.PageSize)
  290. ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", totalNumOfBranches))
  291. ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link")
  292. ctx.JSON(http.StatusOK, &apiBranches)
  293. }
  294. // GetBranchProtection gets a branch protection
  295. func GetBranchProtection(ctx *context.APIContext) {
  296. // swagger:operation GET /repos/{owner}/{repo}/branch_protections/{name} repository repoGetBranchProtection
  297. // ---
  298. // summary: Get a specific branch protection for the repository
  299. // produces:
  300. // - application/json
  301. // parameters:
  302. // - name: owner
  303. // in: path
  304. // description: owner of the repo
  305. // type: string
  306. // required: true
  307. // - name: repo
  308. // in: path
  309. // description: name of the repo
  310. // type: string
  311. // required: true
  312. // - name: name
  313. // in: path
  314. // description: name of protected branch
  315. // type: string
  316. // required: true
  317. // responses:
  318. // "200":
  319. // "$ref": "#/responses/BranchProtection"
  320. // "404":
  321. // "$ref": "#/responses/notFound"
  322. repo := ctx.Repo.Repository
  323. bpName := ctx.Params(":name")
  324. bp, err := models.GetProtectedBranchBy(repo.ID, bpName)
  325. if err != nil {
  326. ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
  327. return
  328. }
  329. if bp == nil || bp.RepoID != repo.ID {
  330. ctx.NotFound()
  331. return
  332. }
  333. ctx.JSON(http.StatusOK, convert.ToBranchProtection(bp))
  334. }
  335. // ListBranchProtections list branch protections for a repo
  336. func ListBranchProtections(ctx *context.APIContext) {
  337. // swagger:operation GET /repos/{owner}/{repo}/branch_protections repository repoListBranchProtection
  338. // ---
  339. // summary: List branch protections for a repository
  340. // produces:
  341. // - application/json
  342. // parameters:
  343. // - name: owner
  344. // in: path
  345. // description: owner of the repo
  346. // type: string
  347. // required: true
  348. // - name: repo
  349. // in: path
  350. // description: name of the repo
  351. // type: string
  352. // required: true
  353. // responses:
  354. // "200":
  355. // "$ref": "#/responses/BranchProtectionList"
  356. repo := ctx.Repo.Repository
  357. bps, err := repo.GetProtectedBranches()
  358. if err != nil {
  359. ctx.Error(http.StatusInternalServerError, "GetProtectedBranches", err)
  360. return
  361. }
  362. apiBps := make([]*api.BranchProtection, len(bps))
  363. for i := range bps {
  364. apiBps[i] = convert.ToBranchProtection(bps[i])
  365. }
  366. ctx.JSON(http.StatusOK, apiBps)
  367. }
  368. // CreateBranchProtection creates a branch protection for a repo
  369. func CreateBranchProtection(ctx *context.APIContext) {
  370. // swagger:operation POST /repos/{owner}/{repo}/branch_protections repository repoCreateBranchProtection
  371. // ---
  372. // summary: Create a branch protections for a repository
  373. // consumes:
  374. // - application/json
  375. // produces:
  376. // - application/json
  377. // parameters:
  378. // - name: owner
  379. // in: path
  380. // description: owner of the repo
  381. // type: string
  382. // required: true
  383. // - name: repo
  384. // in: path
  385. // description: name of the repo
  386. // type: string
  387. // required: true
  388. // - name: body
  389. // in: body
  390. // schema:
  391. // "$ref": "#/definitions/CreateBranchProtectionOption"
  392. // responses:
  393. // "201":
  394. // "$ref": "#/responses/BranchProtection"
  395. // "403":
  396. // "$ref": "#/responses/forbidden"
  397. // "404":
  398. // "$ref": "#/responses/notFound"
  399. // "422":
  400. // "$ref": "#/responses/validationError"
  401. form := web.GetForm(ctx).(*api.CreateBranchProtectionOption)
  402. repo := ctx.Repo.Repository
  403. // Currently protection must match an actual branch
  404. if !git.IsBranchExist(ctx.Repo.Repository.RepoPath(), form.BranchName) {
  405. ctx.NotFound()
  406. return
  407. }
  408. protectBranch, err := models.GetProtectedBranchBy(repo.ID, form.BranchName)
  409. if err != nil {
  410. ctx.Error(http.StatusInternalServerError, "GetProtectBranchOfRepoByName", err)
  411. return
  412. } else if protectBranch != nil {
  413. ctx.Error(http.StatusForbidden, "Create branch protection", "Branch protection already exist")
  414. return
  415. }
  416. var requiredApprovals int64
  417. if form.RequiredApprovals > 0 {
  418. requiredApprovals = form.RequiredApprovals
  419. }
  420. whitelistUsers, err := models.GetUserIDsByNames(form.PushWhitelistUsernames, false)
  421. if err != nil {
  422. if models.IsErrUserNotExist(err) {
  423. ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
  424. return
  425. }
  426. ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
  427. return
  428. }
  429. mergeWhitelistUsers, err := models.GetUserIDsByNames(form.MergeWhitelistUsernames, false)
  430. if err != nil {
  431. if models.IsErrUserNotExist(err) {
  432. ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
  433. return
  434. }
  435. ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
  436. return
  437. }
  438. approvalsWhitelistUsers, err := models.GetUserIDsByNames(form.ApprovalsWhitelistUsernames, false)
  439. if err != nil {
  440. if models.IsErrUserNotExist(err) {
  441. ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
  442. return
  443. }
  444. ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
  445. return
  446. }
  447. var whitelistTeams, mergeWhitelistTeams, approvalsWhitelistTeams []int64
  448. if repo.Owner.IsOrganization() {
  449. whitelistTeams, err = models.GetTeamIDsByNames(repo.OwnerID, form.PushWhitelistTeams, false)
  450. if err != nil {
  451. if models.IsErrTeamNotExist(err) {
  452. ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
  453. return
  454. }
  455. ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
  456. return
  457. }
  458. mergeWhitelistTeams, err = models.GetTeamIDsByNames(repo.OwnerID, form.MergeWhitelistTeams, false)
  459. if err != nil {
  460. if models.IsErrTeamNotExist(err) {
  461. ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
  462. return
  463. }
  464. ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
  465. return
  466. }
  467. approvalsWhitelistTeams, err = models.GetTeamIDsByNames(repo.OwnerID, form.ApprovalsWhitelistTeams, false)
  468. if err != nil {
  469. if models.IsErrTeamNotExist(err) {
  470. ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
  471. return
  472. }
  473. ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
  474. return
  475. }
  476. }
  477. protectBranch = &models.ProtectedBranch{
  478. RepoID: ctx.Repo.Repository.ID,
  479. BranchName: form.BranchName,
  480. CanPush: form.EnablePush,
  481. EnableWhitelist: form.EnablePush && form.EnablePushWhitelist,
  482. EnableMergeWhitelist: form.EnableMergeWhitelist,
  483. WhitelistDeployKeys: form.EnablePush && form.EnablePushWhitelist && form.PushWhitelistDeployKeys,
  484. EnableStatusCheck: form.EnableStatusCheck,
  485. StatusCheckContexts: form.StatusCheckContexts,
  486. EnableApprovalsWhitelist: form.EnableApprovalsWhitelist,
  487. RequiredApprovals: requiredApprovals,
  488. BlockOnRejectedReviews: form.BlockOnRejectedReviews,
  489. BlockOnOfficialReviewRequests: form.BlockOnOfficialReviewRequests,
  490. DismissStaleApprovals: form.DismissStaleApprovals,
  491. RequireSignedCommits: form.RequireSignedCommits,
  492. ProtectedFilePatterns: form.ProtectedFilePatterns,
  493. BlockOnOutdatedBranch: form.BlockOnOutdatedBranch,
  494. }
  495. err = models.UpdateProtectBranch(ctx.Repo.Repository, protectBranch, models.WhitelistOptions{
  496. UserIDs: whitelistUsers,
  497. TeamIDs: whitelistTeams,
  498. MergeUserIDs: mergeWhitelistUsers,
  499. MergeTeamIDs: mergeWhitelistTeams,
  500. ApprovalsUserIDs: approvalsWhitelistUsers,
  501. ApprovalsTeamIDs: approvalsWhitelistTeams,
  502. })
  503. if err != nil {
  504. ctx.Error(http.StatusInternalServerError, "UpdateProtectBranch", err)
  505. return
  506. }
  507. if err = pull_service.CheckPrsForBaseBranch(ctx.Repo.Repository, protectBranch.BranchName); err != nil {
  508. ctx.Error(http.StatusInternalServerError, "CheckPrsForBaseBranch", err)
  509. return
  510. }
  511. // Reload from db to get all whitelists
  512. bp, err := models.GetProtectedBranchBy(ctx.Repo.Repository.ID, form.BranchName)
  513. if err != nil {
  514. ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
  515. return
  516. }
  517. if bp == nil || bp.RepoID != ctx.Repo.Repository.ID {
  518. ctx.Error(http.StatusInternalServerError, "New branch protection not found", err)
  519. return
  520. }
  521. ctx.JSON(http.StatusCreated, convert.ToBranchProtection(bp))
  522. }
  523. // EditBranchProtection edits a branch protection for a repo
  524. func EditBranchProtection(ctx *context.APIContext) {
  525. // swagger:operation PATCH /repos/{owner}/{repo}/branch_protections/{name} repository repoEditBranchProtection
  526. // ---
  527. // summary: Edit a branch protections for a repository. Only fields that are set will be changed
  528. // consumes:
  529. // - application/json
  530. // produces:
  531. // - application/json
  532. // parameters:
  533. // - name: owner
  534. // in: path
  535. // description: owner of the repo
  536. // type: string
  537. // required: true
  538. // - name: repo
  539. // in: path
  540. // description: name of the repo
  541. // type: string
  542. // required: true
  543. // - name: name
  544. // in: path
  545. // description: name of protected branch
  546. // type: string
  547. // required: true
  548. // - name: body
  549. // in: body
  550. // schema:
  551. // "$ref": "#/definitions/EditBranchProtectionOption"
  552. // responses:
  553. // "200":
  554. // "$ref": "#/responses/BranchProtection"
  555. // "404":
  556. // "$ref": "#/responses/notFound"
  557. // "422":
  558. // "$ref": "#/responses/validationError"
  559. form := web.GetForm(ctx).(*api.EditBranchProtectionOption)
  560. repo := ctx.Repo.Repository
  561. bpName := ctx.Params(":name")
  562. protectBranch, err := models.GetProtectedBranchBy(repo.ID, bpName)
  563. if err != nil {
  564. ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
  565. return
  566. }
  567. if protectBranch == nil || protectBranch.RepoID != repo.ID {
  568. ctx.NotFound()
  569. return
  570. }
  571. if form.EnablePush != nil {
  572. if !*form.EnablePush {
  573. protectBranch.CanPush = false
  574. protectBranch.EnableWhitelist = false
  575. protectBranch.WhitelistDeployKeys = false
  576. } else {
  577. protectBranch.CanPush = true
  578. if form.EnablePushWhitelist != nil {
  579. if !*form.EnablePushWhitelist {
  580. protectBranch.EnableWhitelist = false
  581. protectBranch.WhitelistDeployKeys = false
  582. } else {
  583. protectBranch.EnableWhitelist = true
  584. if form.PushWhitelistDeployKeys != nil {
  585. protectBranch.WhitelistDeployKeys = *form.PushWhitelistDeployKeys
  586. }
  587. }
  588. }
  589. }
  590. }
  591. if form.EnableMergeWhitelist != nil {
  592. protectBranch.EnableMergeWhitelist = *form.EnableMergeWhitelist
  593. }
  594. if form.EnableStatusCheck != nil {
  595. protectBranch.EnableStatusCheck = *form.EnableStatusCheck
  596. }
  597. if protectBranch.EnableStatusCheck {
  598. protectBranch.StatusCheckContexts = form.StatusCheckContexts
  599. }
  600. if form.RequiredApprovals != nil && *form.RequiredApprovals >= 0 {
  601. protectBranch.RequiredApprovals = *form.RequiredApprovals
  602. }
  603. if form.EnableApprovalsWhitelist != nil {
  604. protectBranch.EnableApprovalsWhitelist = *form.EnableApprovalsWhitelist
  605. }
  606. if form.BlockOnRejectedReviews != nil {
  607. protectBranch.BlockOnRejectedReviews = *form.BlockOnRejectedReviews
  608. }
  609. if form.BlockOnOfficialReviewRequests != nil {
  610. protectBranch.BlockOnOfficialReviewRequests = *form.BlockOnOfficialReviewRequests
  611. }
  612. if form.DismissStaleApprovals != nil {
  613. protectBranch.DismissStaleApprovals = *form.DismissStaleApprovals
  614. }
  615. if form.RequireSignedCommits != nil {
  616. protectBranch.RequireSignedCommits = *form.RequireSignedCommits
  617. }
  618. if form.ProtectedFilePatterns != nil {
  619. protectBranch.ProtectedFilePatterns = *form.ProtectedFilePatterns
  620. }
  621. if form.BlockOnOutdatedBranch != nil {
  622. protectBranch.BlockOnOutdatedBranch = *form.BlockOnOutdatedBranch
  623. }
  624. var whitelistUsers []int64
  625. if form.PushWhitelistUsernames != nil {
  626. whitelistUsers, err = models.GetUserIDsByNames(form.PushWhitelistUsernames, false)
  627. if err != nil {
  628. if models.IsErrUserNotExist(err) {
  629. ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
  630. return
  631. }
  632. ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
  633. return
  634. }
  635. } else {
  636. whitelistUsers = protectBranch.WhitelistUserIDs
  637. }
  638. var mergeWhitelistUsers []int64
  639. if form.MergeWhitelistUsernames != nil {
  640. mergeWhitelistUsers, err = models.GetUserIDsByNames(form.MergeWhitelistUsernames, false)
  641. if err != nil {
  642. if models.IsErrUserNotExist(err) {
  643. ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
  644. return
  645. }
  646. ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
  647. return
  648. }
  649. } else {
  650. mergeWhitelistUsers = protectBranch.MergeWhitelistUserIDs
  651. }
  652. var approvalsWhitelistUsers []int64
  653. if form.ApprovalsWhitelistUsernames != nil {
  654. approvalsWhitelistUsers, err = models.GetUserIDsByNames(form.ApprovalsWhitelistUsernames, false)
  655. if err != nil {
  656. if models.IsErrUserNotExist(err) {
  657. ctx.Error(http.StatusUnprocessableEntity, "User does not exist", err)
  658. return
  659. }
  660. ctx.Error(http.StatusInternalServerError, "GetUserIDsByNames", err)
  661. return
  662. }
  663. } else {
  664. approvalsWhitelistUsers = protectBranch.ApprovalsWhitelistUserIDs
  665. }
  666. var whitelistTeams, mergeWhitelistTeams, approvalsWhitelistTeams []int64
  667. if repo.Owner.IsOrganization() {
  668. if form.PushWhitelistTeams != nil {
  669. whitelistTeams, err = models.GetTeamIDsByNames(repo.OwnerID, form.PushWhitelistTeams, false)
  670. if err != nil {
  671. if models.IsErrTeamNotExist(err) {
  672. ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
  673. return
  674. }
  675. ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
  676. return
  677. }
  678. } else {
  679. whitelistTeams = protectBranch.WhitelistTeamIDs
  680. }
  681. if form.MergeWhitelistTeams != nil {
  682. mergeWhitelistTeams, err = models.GetTeamIDsByNames(repo.OwnerID, form.MergeWhitelistTeams, false)
  683. if err != nil {
  684. if models.IsErrTeamNotExist(err) {
  685. ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
  686. return
  687. }
  688. ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
  689. return
  690. }
  691. } else {
  692. mergeWhitelistTeams = protectBranch.MergeWhitelistTeamIDs
  693. }
  694. if form.ApprovalsWhitelistTeams != nil {
  695. approvalsWhitelistTeams, err = models.GetTeamIDsByNames(repo.OwnerID, form.ApprovalsWhitelistTeams, false)
  696. if err != nil {
  697. if models.IsErrTeamNotExist(err) {
  698. ctx.Error(http.StatusUnprocessableEntity, "Team does not exist", err)
  699. return
  700. }
  701. ctx.Error(http.StatusInternalServerError, "GetTeamIDsByNames", err)
  702. return
  703. }
  704. } else {
  705. approvalsWhitelistTeams = protectBranch.ApprovalsWhitelistTeamIDs
  706. }
  707. }
  708. err = models.UpdateProtectBranch(ctx.Repo.Repository, protectBranch, models.WhitelistOptions{
  709. UserIDs: whitelistUsers,
  710. TeamIDs: whitelistTeams,
  711. MergeUserIDs: mergeWhitelistUsers,
  712. MergeTeamIDs: mergeWhitelistTeams,
  713. ApprovalsUserIDs: approvalsWhitelistUsers,
  714. ApprovalsTeamIDs: approvalsWhitelistTeams,
  715. })
  716. if err != nil {
  717. ctx.Error(http.StatusInternalServerError, "UpdateProtectBranch", err)
  718. return
  719. }
  720. if err = pull_service.CheckPrsForBaseBranch(ctx.Repo.Repository, protectBranch.BranchName); err != nil {
  721. ctx.Error(http.StatusInternalServerError, "CheckPrsForBaseBranch", err)
  722. return
  723. }
  724. // Reload from db to ensure get all whitelists
  725. bp, err := models.GetProtectedBranchBy(repo.ID, bpName)
  726. if err != nil {
  727. ctx.Error(http.StatusInternalServerError, "GetProtectedBranchBy", err)
  728. return
  729. }
  730. if bp == nil || bp.RepoID != ctx.Repo.Repository.ID {
  731. ctx.Error(http.StatusInternalServerError, "New branch protection not found", err)
  732. return
  733. }
  734. ctx.JSON(http.StatusOK, convert.ToBranchProtection(bp))
  735. }
  736. // DeleteBranchProtection deletes a branch protection for a repo
  737. func DeleteBranchProtection(ctx *context.APIContext) {
  738. // swagger:operation DELETE /repos/{owner}/{repo}/branch_protections/{name} repository repoDeleteBranchProtection
  739. // ---
  740. // summary: Delete a specific branch protection for the repository
  741. // produces:
  742. // - application/json
  743. // parameters:
  744. // - name: owner
  745. // in: path
  746. // description: owner of the repo
  747. // type: string
  748. // required: true
  749. // - name: repo
  750. // in: path
  751. // description: name of the repo
  752. // type: string
  753. // required: true
  754. // - name: name
  755. // in: path
  756. // description: name of protected branch
  757. // type: string
  758. // required: true
  759. // responses:
  760. // "204":
  761. // "$ref": "#/responses/empty"
  762. // "404":
  763. // "$ref": "#/responses/notFound"
  764. repo := ctx.Repo.Repository
  765. bpName := ctx.Params(":name")
  766. bp, err := models.GetProtectedBranchBy(repo.ID, bpName)
  767. if err != nil {
  768. ctx.Error(http.StatusInternalServerError, "GetProtectedBranchByID", err)
  769. return
  770. }
  771. if bp == nil || bp.RepoID != repo.ID {
  772. ctx.NotFound()
  773. return
  774. }
  775. if err := ctx.Repo.Repository.DeleteProtectedBranch(bp.ID); err != nil {
  776. ctx.Error(http.StatusInternalServerError, "DeleteProtectedBranch", err)
  777. return
  778. }
  779. ctx.Status(http.StatusNoContent)
  780. }