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.

hook.go 21 kB

Use native git variants by default with go-git variants as build tag (#13673) * Move last commit cache back into modules/git Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove go-git from the interface for last commit cache Signed-off-by: Andrew Thornton <art27@cantab.net> * move cacheref to last_commit_cache Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove go-git from routers/private/hook Signed-off-by: Andrew Thornton <art27@cantab.net> * Move FindLFSFiles to pipeline Signed-off-by: Andrew Thornton <art27@cantab.net> * Make no-go-git variants Signed-off-by: Andrew Thornton <art27@cantab.net> * Submodule RefID Signed-off-by: Andrew Thornton <art27@cantab.net> * fix issue with GetCommitsInfo Signed-off-by: Andrew Thornton <art27@cantab.net> * fix GetLastCommitForPaths Signed-off-by: Andrew Thornton <art27@cantab.net> * Improve efficiency Signed-off-by: Andrew Thornton <art27@cantab.net> * More efficiency Signed-off-by: Andrew Thornton <art27@cantab.net> * even faster Signed-off-by: Andrew Thornton <art27@cantab.net> * Reduce duplication * As per @lunny Signed-off-by: Andrew Thornton <art27@cantab.net> * attempt to fix drone Signed-off-by: Andrew Thornton <art27@cantab.net> * fix test-tags Signed-off-by: Andrew Thornton <art27@cantab.net> * default to use no-go-git variants and add gogit build tag Signed-off-by: Andrew Thornton <art27@cantab.net> * placate lint Signed-off-by: Andrew Thornton <art27@cantab.net> * as per @6543 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. // Package private includes all internal routes. The package name internal is ideal but Golang is not allowed, so we use private as package name instead.
  5. package private
  6. import (
  7. "bufio"
  8. "context"
  9. "fmt"
  10. "io"
  11. "net/http"
  12. "os"
  13. "strings"
  14. "code.gitea.io/gitea/models"
  15. "code.gitea.io/gitea/modules/git"
  16. "code.gitea.io/gitea/modules/log"
  17. "code.gitea.io/gitea/modules/private"
  18. repo_module "code.gitea.io/gitea/modules/repository"
  19. "code.gitea.io/gitea/modules/setting"
  20. "code.gitea.io/gitea/modules/util"
  21. pull_service "code.gitea.io/gitea/services/pull"
  22. repo_service "code.gitea.io/gitea/services/repository"
  23. "gitea.com/macaron/macaron"
  24. )
  25. func verifyCommits(oldCommitID, newCommitID string, repo *git.Repository, env []string) error {
  26. stdoutReader, stdoutWriter, err := os.Pipe()
  27. if err != nil {
  28. log.Error("Unable to create os.Pipe for %s", repo.Path)
  29. return err
  30. }
  31. defer func() {
  32. _ = stdoutReader.Close()
  33. _ = stdoutWriter.Close()
  34. }()
  35. // This is safe as force pushes are already forbidden
  36. err = git.NewCommand("rev-list", oldCommitID+"..."+newCommitID).
  37. RunInDirTimeoutEnvFullPipelineFunc(env, -1, repo.Path,
  38. stdoutWriter, nil, nil,
  39. func(ctx context.Context, cancel context.CancelFunc) error {
  40. _ = stdoutWriter.Close()
  41. err := readAndVerifyCommitsFromShaReader(stdoutReader, repo, env)
  42. if err != nil {
  43. log.Error("%v", err)
  44. cancel()
  45. }
  46. _ = stdoutReader.Close()
  47. return err
  48. })
  49. if err != nil && !isErrUnverifiedCommit(err) {
  50. log.Error("Unable to check commits from %s to %s in %s: %v", oldCommitID, newCommitID, repo.Path, err)
  51. }
  52. return err
  53. }
  54. func readAndVerifyCommitsFromShaReader(input io.ReadCloser, repo *git.Repository, env []string) error {
  55. scanner := bufio.NewScanner(input)
  56. for scanner.Scan() {
  57. line := scanner.Text()
  58. err := readAndVerifyCommit(line, repo, env)
  59. if err != nil {
  60. log.Error("%v", err)
  61. return err
  62. }
  63. }
  64. return scanner.Err()
  65. }
  66. func readAndVerifyCommit(sha string, repo *git.Repository, env []string) error {
  67. stdoutReader, stdoutWriter, err := os.Pipe()
  68. if err != nil {
  69. log.Error("Unable to create pipe for %s: %v", repo.Path, err)
  70. return err
  71. }
  72. defer func() {
  73. _ = stdoutReader.Close()
  74. _ = stdoutWriter.Close()
  75. }()
  76. hash := git.MustIDFromString(sha)
  77. return git.NewCommand("cat-file", "commit", sha).
  78. RunInDirTimeoutEnvFullPipelineFunc(env, -1, repo.Path,
  79. stdoutWriter, nil, nil,
  80. func(ctx context.Context, cancel context.CancelFunc) error {
  81. _ = stdoutWriter.Close()
  82. commit, err := git.CommitFromReader(repo, hash, stdoutReader)
  83. if err != nil {
  84. return err
  85. }
  86. verification := models.ParseCommitWithSignature(commit)
  87. if !verification.Verified {
  88. cancel()
  89. return &errUnverifiedCommit{
  90. commit.ID.String(),
  91. }
  92. }
  93. return nil
  94. })
  95. }
  96. type errUnverifiedCommit struct {
  97. sha string
  98. }
  99. func (e *errUnverifiedCommit) Error() string {
  100. return fmt.Sprintf("Unverified commit: %s", e.sha)
  101. }
  102. func isErrUnverifiedCommit(err error) bool {
  103. _, ok := err.(*errUnverifiedCommit)
  104. return ok
  105. }
  106. // HookPreReceive checks whether a individual commit is acceptable
  107. func HookPreReceive(ctx *macaron.Context, opts private.HookOptions) {
  108. ownerName := ctx.Params(":owner")
  109. repoName := ctx.Params(":repo")
  110. repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName)
  111. if err != nil {
  112. log.Error("Unable to get repository: %s/%s Error: %v", ownerName, repoName, err)
  113. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  114. "err": err.Error(),
  115. })
  116. return
  117. }
  118. repo.OwnerName = ownerName
  119. gitRepo, err := git.OpenRepository(repo.RepoPath())
  120. if err != nil {
  121. log.Error("Unable to get git repository for: %s/%s Error: %v", ownerName, repoName, err)
  122. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  123. "err": err.Error(),
  124. })
  125. return
  126. }
  127. defer gitRepo.Close()
  128. // Generate git environment for checking commits
  129. env := os.Environ()
  130. if opts.GitAlternativeObjectDirectories != "" {
  131. env = append(env,
  132. private.GitAlternativeObjectDirectories+"="+opts.GitAlternativeObjectDirectories)
  133. }
  134. if opts.GitObjectDirectory != "" {
  135. env = append(env,
  136. private.GitObjectDirectory+"="+opts.GitObjectDirectory)
  137. }
  138. if opts.GitQuarantinePath != "" {
  139. env = append(env,
  140. private.GitQuarantinePath+"="+opts.GitQuarantinePath)
  141. }
  142. // Iterate across the provided old commit IDs
  143. for i := range opts.OldCommitIDs {
  144. oldCommitID := opts.OldCommitIDs[i]
  145. newCommitID := opts.NewCommitIDs[i]
  146. refFullName := opts.RefFullNames[i]
  147. branchName := strings.TrimPrefix(refFullName, git.BranchPrefix)
  148. if branchName == repo.DefaultBranch && newCommitID == git.EmptySHA {
  149. log.Warn("Forbidden: Branch: %s is the default branch in %-v and cannot be deleted", branchName, repo)
  150. ctx.JSON(http.StatusForbidden, map[string]interface{}{
  151. "err": fmt.Sprintf("branch %s is the default branch and cannot be deleted", branchName),
  152. })
  153. return
  154. }
  155. protectBranch, err := models.GetProtectedBranchBy(repo.ID, branchName)
  156. if err != nil {
  157. log.Error("Unable to get protected branch: %s in %-v Error: %v", branchName, repo, err)
  158. ctx.JSON(500, map[string]interface{}{
  159. "err": err.Error(),
  160. })
  161. return
  162. }
  163. // Allow pushes to non-protected branches
  164. if protectBranch == nil || !protectBranch.IsProtected() {
  165. continue
  166. }
  167. // This ref is a protected branch.
  168. //
  169. // First of all we need to enforce absolutely:
  170. //
  171. // 1. Detect and prevent deletion of the branch
  172. if newCommitID == git.EmptySHA {
  173. log.Warn("Forbidden: Branch: %s in %-v is protected from deletion", branchName, repo)
  174. ctx.JSON(http.StatusForbidden, map[string]interface{}{
  175. "err": fmt.Sprintf("branch %s is protected from deletion", branchName),
  176. })
  177. return
  178. }
  179. // 2. Disallow force pushes to protected branches
  180. if git.EmptySHA != oldCommitID {
  181. output, err := git.NewCommand("rev-list", "--max-count=1", oldCommitID, "^"+newCommitID).RunInDirWithEnv(repo.RepoPath(), env)
  182. if err != nil {
  183. log.Error("Unable to detect force push between: %s and %s in %-v Error: %v", oldCommitID, newCommitID, repo, err)
  184. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  185. "err": fmt.Sprintf("Fail to detect force push: %v", err),
  186. })
  187. return
  188. } else if len(output) > 0 {
  189. log.Warn("Forbidden: Branch: %s in %-v is protected from force push", branchName, repo)
  190. ctx.JSON(http.StatusForbidden, map[string]interface{}{
  191. "err": fmt.Sprintf("branch %s is protected from force push", branchName),
  192. })
  193. return
  194. }
  195. }
  196. // 3. Enforce require signed commits
  197. if protectBranch.RequireSignedCommits {
  198. err := verifyCommits(oldCommitID, newCommitID, gitRepo, env)
  199. if err != nil {
  200. if !isErrUnverifiedCommit(err) {
  201. log.Error("Unable to check commits from %s to %s in %-v: %v", oldCommitID, newCommitID, repo, err)
  202. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  203. "err": fmt.Sprintf("Unable to check commits from %s to %s: %v", oldCommitID, newCommitID, err),
  204. })
  205. return
  206. }
  207. unverifiedCommit := err.(*errUnverifiedCommit).sha
  208. log.Warn("Forbidden: Branch: %s in %-v is protected from unverified commit %s", branchName, repo, unverifiedCommit)
  209. ctx.JSON(http.StatusForbidden, map[string]interface{}{
  210. "err": fmt.Sprintf("branch %s is protected from unverified commit %s", branchName, unverifiedCommit),
  211. })
  212. return
  213. }
  214. }
  215. // Now there are several tests which can be overridden:
  216. //
  217. // 4. Check protected file patterns - this is overridable from the UI
  218. changedProtectedfiles := false
  219. protectedFilePath := ""
  220. globs := protectBranch.GetProtectedFilePatterns()
  221. if len(globs) > 0 {
  222. _, err := pull_service.CheckFileProtection(oldCommitID, newCommitID, globs, 1, env, gitRepo)
  223. if err != nil {
  224. if !models.IsErrFilePathProtected(err) {
  225. log.Error("Unable to check file protection for commits from %s to %s in %-v: %v", oldCommitID, newCommitID, repo, err)
  226. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  227. "err": fmt.Sprintf("Unable to check file protection for commits from %s to %s: %v", oldCommitID, newCommitID, err),
  228. })
  229. return
  230. }
  231. changedProtectedfiles = true
  232. protectedFilePath = err.(models.ErrFilePathProtected).Path
  233. }
  234. }
  235. // 5. Check if the doer is allowed to push
  236. canPush := false
  237. if opts.IsDeployKey {
  238. canPush = !changedProtectedfiles && protectBranch.CanPush && (!protectBranch.EnableWhitelist || protectBranch.WhitelistDeployKeys)
  239. } else {
  240. canPush = !changedProtectedfiles && protectBranch.CanUserPush(opts.UserID)
  241. }
  242. // 6. If we're not allowed to push directly
  243. if !canPush {
  244. // Is this is a merge from the UI/API?
  245. if opts.ProtectedBranchID == 0 {
  246. // 6a. If we're not merging from the UI/API then there are two ways we got here:
  247. //
  248. // We are changing a protected file and we're not allowed to do that
  249. if changedProtectedfiles {
  250. log.Warn("Forbidden: Branch: %s in %-v is protected from changing file %s", branchName, repo, protectedFilePath)
  251. ctx.JSON(http.StatusForbidden, map[string]interface{}{
  252. "err": fmt.Sprintf("branch %s is protected from changing file %s", branchName, protectedFilePath),
  253. })
  254. return
  255. }
  256. // Or we're simply not able to push to this protected branch
  257. log.Warn("Forbidden: User %d is not allowed to push to protected branch: %s in %-v", opts.UserID, branchName, repo)
  258. ctx.JSON(http.StatusForbidden, map[string]interface{}{
  259. "err": fmt.Sprintf("Not allowed to push to protected branch %s", branchName),
  260. })
  261. return
  262. }
  263. // 6b. Merge (from UI or API)
  264. // Get the PR, user and permissions for the user in the repository
  265. pr, err := models.GetPullRequestByID(opts.ProtectedBranchID)
  266. if err != nil {
  267. log.Error("Unable to get PullRequest %d Error: %v", opts.ProtectedBranchID, err)
  268. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  269. "err": fmt.Sprintf("Unable to get PullRequest %d Error: %v", opts.ProtectedBranchID, err),
  270. })
  271. return
  272. }
  273. user, err := models.GetUserByID(opts.UserID)
  274. if err != nil {
  275. log.Error("Unable to get User id %d Error: %v", opts.UserID, err)
  276. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  277. "err": fmt.Sprintf("Unable to get User id %d Error: %v", opts.UserID, err),
  278. })
  279. return
  280. }
  281. perm, err := models.GetUserRepoPermission(repo, user)
  282. if err != nil {
  283. log.Error("Unable to get Repo permission of repo %s/%s of User %s", repo.OwnerName, repo.Name, user.Name, err)
  284. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  285. "err": fmt.Sprintf("Unable to get Repo permission of repo %s/%s of User %s: %v", repo.OwnerName, repo.Name, user.Name, err),
  286. })
  287. return
  288. }
  289. // Now check if the user is allowed to merge PRs for this repository
  290. allowedMerge, err := pull_service.IsUserAllowedToMerge(pr, perm, user)
  291. if err != nil {
  292. log.Error("Error calculating if allowed to merge: %v", err)
  293. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  294. "err": fmt.Sprintf("Error calculating if allowed to merge: %v", err),
  295. })
  296. return
  297. }
  298. if !allowedMerge {
  299. log.Warn("Forbidden: User %d is not allowed to push to protected branch: %s in %-v and is not allowed to merge pr #%d", opts.UserID, branchName, repo, pr.Index)
  300. ctx.JSON(http.StatusForbidden, map[string]interface{}{
  301. "err": fmt.Sprintf("Not allowed to push to protected branch %s", branchName),
  302. })
  303. return
  304. }
  305. // If we're an admin for the repository we can ignore status checks, reviews and override protected files
  306. if perm.IsAdmin() {
  307. continue
  308. }
  309. // Now if we're not an admin - we can't overwrite protected files so fail now
  310. if changedProtectedfiles {
  311. log.Warn("Forbidden: Branch: %s in %-v is protected from changing file %s", branchName, repo, protectedFilePath)
  312. ctx.JSON(http.StatusForbidden, map[string]interface{}{
  313. "err": fmt.Sprintf("branch %s is protected from changing file %s", branchName, protectedFilePath),
  314. })
  315. return
  316. }
  317. // Check all status checks and reviews are ok
  318. if err := pull_service.CheckPRReadyToMerge(pr, true); err != nil {
  319. if models.IsErrNotAllowedToMerge(err) {
  320. log.Warn("Forbidden: User %d is not allowed push to protected branch %s in %-v and pr #%d is not ready to be merged: %s", opts.UserID, branchName, repo, pr.Index, err.Error())
  321. ctx.JSON(http.StatusForbidden, map[string]interface{}{
  322. "err": fmt.Sprintf("Not allowed to push to protected branch %s and pr #%d is not ready to be merged: %s", branchName, opts.ProtectedBranchID, err.Error()),
  323. })
  324. return
  325. }
  326. log.Error("Unable to check if mergable: protected branch %s in %-v and pr #%d. Error: %v", opts.UserID, branchName, repo, pr.Index, err)
  327. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  328. "err": fmt.Sprintf("Unable to get status of pull request %d. Error: %v", opts.ProtectedBranchID, err),
  329. })
  330. return
  331. }
  332. }
  333. }
  334. ctx.PlainText(http.StatusOK, []byte("ok"))
  335. }
  336. // HookPostReceive updates services and users
  337. func HookPostReceive(ctx *macaron.Context, opts private.HookOptions) {
  338. ownerName := ctx.Params(":owner")
  339. repoName := ctx.Params(":repo")
  340. var repo *models.Repository
  341. updates := make([]*repo_module.PushUpdateOptions, 0, len(opts.OldCommitIDs))
  342. wasEmpty := false
  343. for i := range opts.OldCommitIDs {
  344. refFullName := opts.RefFullNames[i]
  345. // Only trigger activity updates for changes to branches or
  346. // tags. Updates to other refs (eg, refs/notes, refs/changes,
  347. // or other less-standard refs spaces are ignored since there
  348. // may be a very large number of them).
  349. if strings.HasPrefix(refFullName, git.BranchPrefix) || strings.HasPrefix(refFullName, git.TagPrefix) {
  350. if repo == nil {
  351. var err error
  352. repo, err = models.GetRepositoryByOwnerAndName(ownerName, repoName)
  353. if err != nil {
  354. log.Error("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err)
  355. ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{
  356. Err: fmt.Sprintf("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err),
  357. })
  358. return
  359. }
  360. if repo.OwnerName == "" {
  361. repo.OwnerName = ownerName
  362. }
  363. wasEmpty = repo.IsEmpty
  364. }
  365. option := repo_module.PushUpdateOptions{
  366. RefFullName: refFullName,
  367. OldCommitID: opts.OldCommitIDs[i],
  368. NewCommitID: opts.NewCommitIDs[i],
  369. PusherID: opts.UserID,
  370. PusherName: opts.UserName,
  371. RepoUserName: ownerName,
  372. RepoName: repoName,
  373. }
  374. updates = append(updates, &option)
  375. if repo.IsEmpty && option.IsBranch() && option.BranchName() == "master" {
  376. // put the master branch first
  377. copy(updates[1:], updates)
  378. updates[0] = &option
  379. }
  380. }
  381. }
  382. if repo != nil && len(updates) > 0 {
  383. if err := repo_service.PushUpdates(updates); err != nil {
  384. log.Error("Failed to Update: %s/%s Total Updates: %d", ownerName, repoName, len(updates))
  385. for i, update := range updates {
  386. log.Error("Failed to Update: %s/%s Update: %d/%d: Branch: %s", ownerName, repoName, i, len(updates), update.BranchName())
  387. }
  388. log.Error("Failed to Update: %s/%s Error: %v", ownerName, repoName, err)
  389. ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{
  390. Err: fmt.Sprintf("Failed to Update: %s/%s Error: %v", ownerName, repoName, err),
  391. })
  392. return
  393. }
  394. }
  395. // Push Options
  396. if repo != nil && len(opts.GitPushOptions) > 0 {
  397. repo.IsPrivate = opts.GitPushOptions.Bool(private.GitPushOptionRepoPrivate, repo.IsPrivate)
  398. repo.IsTemplate = opts.GitPushOptions.Bool(private.GitPushOptionRepoTemplate, repo.IsTemplate)
  399. if err := models.UpdateRepositoryCols(repo, "is_private", "is_template"); err != nil {
  400. log.Error("Failed to Update: %s/%s Error: %v", ownerName, repoName, err)
  401. ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{
  402. Err: fmt.Sprintf("Failed to Update: %s/%s Error: %v", ownerName, repoName, err),
  403. })
  404. }
  405. }
  406. results := make([]private.HookPostReceiveBranchResult, 0, len(opts.OldCommitIDs))
  407. // We have to reload the repo in case its state is changed above
  408. repo = nil
  409. var baseRepo *models.Repository
  410. for i := range opts.OldCommitIDs {
  411. refFullName := opts.RefFullNames[i]
  412. newCommitID := opts.NewCommitIDs[i]
  413. branch := git.RefEndName(opts.RefFullNames[i])
  414. if newCommitID != git.EmptySHA && strings.HasPrefix(refFullName, git.BranchPrefix) {
  415. if repo == nil {
  416. var err error
  417. repo, err = models.GetRepositoryByOwnerAndName(ownerName, repoName)
  418. if err != nil {
  419. log.Error("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err)
  420. ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{
  421. Err: fmt.Sprintf("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err),
  422. RepoWasEmpty: wasEmpty,
  423. })
  424. return
  425. }
  426. if repo.OwnerName == "" {
  427. repo.OwnerName = ownerName
  428. }
  429. if !repo.AllowsPulls() {
  430. // We can stop there's no need to go any further
  431. ctx.JSON(http.StatusOK, private.HookPostReceiveResult{
  432. RepoWasEmpty: wasEmpty,
  433. })
  434. return
  435. }
  436. baseRepo = repo
  437. if repo.IsFork {
  438. if err := repo.GetBaseRepo(); err != nil {
  439. log.Error("Failed to get Base Repository of Forked repository: %-v Error: %v", repo, err)
  440. ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{
  441. Err: fmt.Sprintf("Failed to get Base Repository of Forked repository: %-v Error: %v", repo, err),
  442. RepoWasEmpty: wasEmpty,
  443. })
  444. return
  445. }
  446. baseRepo = repo.BaseRepo
  447. }
  448. }
  449. if !repo.IsFork && branch == baseRepo.DefaultBranch {
  450. results = append(results, private.HookPostReceiveBranchResult{})
  451. continue
  452. }
  453. pr, err := models.GetUnmergedPullRequest(repo.ID, baseRepo.ID, branch, baseRepo.DefaultBranch)
  454. if err != nil && !models.IsErrPullRequestNotExist(err) {
  455. log.Error("Failed to get active PR in: %-v Branch: %s to: %-v Branch: %s Error: %v", repo, branch, baseRepo, baseRepo.DefaultBranch, err)
  456. ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{
  457. Err: fmt.Sprintf(
  458. "Failed to get active PR in: %-v Branch: %s to: %-v Branch: %s Error: %v", repo, branch, baseRepo, baseRepo.DefaultBranch, err),
  459. RepoWasEmpty: wasEmpty,
  460. })
  461. return
  462. }
  463. if pr == nil {
  464. if repo.IsFork {
  465. branch = fmt.Sprintf("%s:%s", repo.OwnerName, branch)
  466. }
  467. results = append(results, private.HookPostReceiveBranchResult{
  468. Message: setting.Git.PullRequestPushMessage && repo.AllowsPulls(),
  469. Create: true,
  470. Branch: branch,
  471. URL: fmt.Sprintf("%s/compare/%s...%s", baseRepo.HTMLURL(), util.PathEscapeSegments(baseRepo.DefaultBranch), util.PathEscapeSegments(branch)),
  472. })
  473. } else {
  474. results = append(results, private.HookPostReceiveBranchResult{
  475. Message: setting.Git.PullRequestPushMessage && repo.AllowsPulls(),
  476. Create: false,
  477. Branch: branch,
  478. URL: fmt.Sprintf("%s/pulls/%d", baseRepo.HTMLURL(), pr.Index),
  479. })
  480. }
  481. }
  482. }
  483. ctx.JSON(http.StatusOK, private.HookPostReceiveResult{
  484. Results: results,
  485. RepoWasEmpty: wasEmpty,
  486. })
  487. }
  488. // SetDefaultBranch updates the default branch
  489. func SetDefaultBranch(ctx *macaron.Context) {
  490. ownerName := ctx.Params(":owner")
  491. repoName := ctx.Params(":repo")
  492. branch := ctx.Params(":branch")
  493. repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName)
  494. if err != nil {
  495. log.Error("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err)
  496. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  497. "Err": fmt.Sprintf("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err),
  498. })
  499. return
  500. }
  501. if repo.OwnerName == "" {
  502. repo.OwnerName = ownerName
  503. }
  504. repo.DefaultBranch = branch
  505. gitRepo, err := git.OpenRepository(repo.RepoPath())
  506. if err != nil {
  507. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  508. "Err": fmt.Sprintf("Failed to get git repository: %s/%s Error: %v", ownerName, repoName, err),
  509. })
  510. return
  511. }
  512. if err := gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
  513. if !git.IsErrUnsupportedVersion(err) {
  514. gitRepo.Close()
  515. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  516. "Err": fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
  517. })
  518. return
  519. }
  520. }
  521. gitRepo.Close()
  522. if err := repo.UpdateDefaultBranch(); err != nil {
  523. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  524. "Err": fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
  525. })
  526. return
  527. }
  528. ctx.PlainText(200, []byte("success"))
  529. }