forked from go-gitea/gitea
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API for manipulating Git hooks (go-gitea#6436)
* Add API for manipulating Git hooks Signed-off-by: Segev Finer <[email protected]> * Replace code.gitea.io/sdk with PR branch temporarily for CI * Switch back to code.gitea.io/sdk@master * Return 403 instead of 404 on no permission to edit hooks in API * Add tests for Git hooks API * Update models/repo_list_test.go Co-Authored-By: segevfiner <[email protected]> * Update models/repo_list_test.go Co-Authored-By: segevfiner <[email protected]> * empty line
- Loading branch information
1 parent
827ab6b
commit 3454836
Showing
48 changed files
with
1,306 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package integrations | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models" | ||
api "code.gitea.io/sdk/gitea" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const testHookContent = `#!/bin/bash | ||
echo Hello, World! | ||
` | ||
|
||
func TestAPIListGitHooks(t *testing.T) { | ||
prepareTestEnv(t) | ||
|
||
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 37}).(*models.Repository) | ||
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) | ||
|
||
// user1 is an admin user | ||
session := loginUser(t, "user1") | ||
token := getTokenForLoggedInUser(t, session) | ||
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/hooks/git?token=%s", | ||
owner.Name, repo.Name, token) | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
var apiGitHooks []*api.GitHook | ||
DecodeJSON(t, resp, &apiGitHooks) | ||
assert.Len(t, apiGitHooks, 3) | ||
for _, apiGitHook := range apiGitHooks { | ||
if apiGitHook.Name == "pre-receive" { | ||
assert.True(t, apiGitHook.IsActive) | ||
assert.Equal(t, testHookContent, apiGitHook.Content) | ||
} else { | ||
assert.False(t, apiGitHook.IsActive) | ||
assert.Empty(t, apiGitHook.Content) | ||
} | ||
} | ||
} | ||
|
||
func TestAPIListGitHooksNoHooks(t *testing.T) { | ||
prepareTestEnv(t) | ||
|
||
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) | ||
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) | ||
|
||
// user1 is an admin user | ||
session := loginUser(t, "user1") | ||
token := getTokenForLoggedInUser(t, session) | ||
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/hooks/git?token=%s", | ||
owner.Name, repo.Name, token) | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
var apiGitHooks []*api.GitHook | ||
DecodeJSON(t, resp, &apiGitHooks) | ||
assert.Len(t, apiGitHooks, 3) | ||
for _, apiGitHook := range apiGitHooks { | ||
assert.False(t, apiGitHook.IsActive) | ||
assert.Empty(t, apiGitHook.Content) | ||
} | ||
} | ||
|
||
func TestAPIListGitHooksNoAccess(t *testing.T) { | ||
prepareTestEnv(t) | ||
|
||
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) | ||
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) | ||
|
||
session := loginUser(t, owner.Name) | ||
token := getTokenForLoggedInUser(t, session) | ||
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/hooks/git?token=%s", | ||
owner.Name, repo.Name, token) | ||
MakeRequest(t, req, http.StatusForbidden) | ||
} | ||
|
||
func TestAPIGetGitHook(t *testing.T) { | ||
prepareTestEnv(t) | ||
|
||
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 37}).(*models.Repository) | ||
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) | ||
|
||
// user1 is an admin user | ||
session := loginUser(t, "user1") | ||
token := getTokenForLoggedInUser(t, session) | ||
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/hooks/git/pre-receive?token=%s", | ||
owner.Name, repo.Name, token) | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
var apiGitHook *api.GitHook | ||
DecodeJSON(t, resp, &apiGitHook) | ||
assert.True(t, apiGitHook.IsActive) | ||
assert.Equal(t, testHookContent, apiGitHook.Content) | ||
} | ||
|
||
func TestAPIGetGitHookNoAccess(t *testing.T) { | ||
prepareTestEnv(t) | ||
|
||
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) | ||
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) | ||
|
||
session := loginUser(t, owner.Name) | ||
token := getTokenForLoggedInUser(t, session) | ||
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/hooks/git/pre-receive?token=%s", | ||
owner.Name, repo.Name, token) | ||
MakeRequest(t, req, http.StatusForbidden) | ||
} | ||
|
||
func TestAPIEditGitHook(t *testing.T) { | ||
prepareTestEnv(t) | ||
|
||
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) | ||
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) | ||
|
||
// user1 is an admin user | ||
session := loginUser(t, "user1") | ||
token := getTokenForLoggedInUser(t, session) | ||
|
||
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/hooks/git/pre-receive?token=%s", | ||
owner.Name, repo.Name, token) | ||
req := NewRequestWithJSON(t, "PATCH", urlStr, &api.EditGitHookOption{ | ||
Content: testHookContent, | ||
}) | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
var apiGitHook *api.GitHook | ||
DecodeJSON(t, resp, &apiGitHook) | ||
assert.True(t, apiGitHook.IsActive) | ||
assert.Equal(t, testHookContent, apiGitHook.Content) | ||
|
||
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/hooks/git/pre-receive?token=%s", | ||
owner.Name, repo.Name, token) | ||
resp = MakeRequest(t, req, http.StatusOK) | ||
var apiGitHook2 *api.GitHook | ||
DecodeJSON(t, resp, &apiGitHook2) | ||
assert.True(t, apiGitHook2.IsActive) | ||
assert.Equal(t, testHookContent, apiGitHook2.Content) | ||
} | ||
|
||
func TestAPIEditGitHookNoAccess(t *testing.T) { | ||
prepareTestEnv(t) | ||
|
||
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) | ||
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) | ||
|
||
session := loginUser(t, owner.Name) | ||
token := getTokenForLoggedInUser(t, session) | ||
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/hooks/git/pre-receive?token=%s", | ||
owner.Name, repo.Name, token) | ||
req := NewRequestWithJSON(t, "PATCH", urlStr, &api.EditGitHookOption{ | ||
Content: testHookContent, | ||
}) | ||
MakeRequest(t, req, http.StatusForbidden) | ||
} | ||
|
||
func TestAPIDeleteGitHook(t *testing.T) { | ||
prepareTestEnv(t) | ||
|
||
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 37}).(*models.Repository) | ||
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) | ||
|
||
// user1 is an admin user | ||
session := loginUser(t, "user1") | ||
token := getTokenForLoggedInUser(t, session) | ||
|
||
req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/hooks/git/pre-receive?token=%s", | ||
owner.Name, repo.Name, token) | ||
MakeRequest(t, req, http.StatusNoContent) | ||
|
||
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/hooks/git/pre-receive?token=%s", | ||
owner.Name, repo.Name, token) | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
var apiGitHook2 *api.GitHook | ||
DecodeJSON(t, resp, &apiGitHook2) | ||
assert.False(t, apiGitHook2.IsActive) | ||
assert.Empty(t, apiGitHook2.Content) | ||
} | ||
|
||
func TestAPIDeleteGitHookNoAccess(t *testing.T) { | ||
prepareTestEnv(t) | ||
|
||
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) | ||
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User) | ||
|
||
session := loginUser(t, owner.Name) | ||
token := getTokenForLoggedInUser(t, session) | ||
req := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/hooks/git/pre-receive?token=%s", | ||
owner.Name, repo.Name, token) | ||
MakeRequest(t, req, http.StatusForbidden) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
integrations/gitea-repositories-meta/user2/git_hooks_test.git/HEAD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ref: refs/heads/master |
4 changes: 4 additions & 0 deletions
4
integrations/gitea-repositories-meta/user2/git_hooks_test.git/config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[core] | ||
repositoryformatversion = 0 | ||
filemode = true | ||
bare = true |
1 change: 1 addition & 0 deletions
1
integrations/gitea-repositories-meta/user2/git_hooks_test.git/description
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Unnamed repository; edit this file 'description' to name the repository. |
15 changes: 15 additions & 0 deletions
15
integrations/gitea-repositories-meta/user2/git_hooks_test.git/hooks/applypatch-msg.sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/sh | ||
# | ||
# An example hook script to check the commit log message taken by | ||
# applypatch from an e-mail message. | ||
# | ||
# The hook should exit with non-zero status after issuing an | ||
# appropriate message if it wants to stop the commit. The hook is | ||
# allowed to edit the commit message file. | ||
# | ||
# To enable this hook, rename this file to "applypatch-msg". | ||
|
||
. git-sh-setup | ||
commitmsg="$(git rev-parse --git-path hooks/commit-msg)" | ||
test -x "$commitmsg" && exec "$commitmsg" ${1+"$@"} | ||
: |
24 changes: 24 additions & 0 deletions
24
integrations/gitea-repositories-meta/user2/git_hooks_test.git/hooks/commit-msg.sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/bin/sh | ||
# | ||
# An example hook script to check the commit log message. | ||
# Called by "git commit" with one argument, the name of the file | ||
# that has the commit message. The hook should exit with non-zero | ||
# status after issuing an appropriate message if it wants to stop the | ||
# commit. The hook is allowed to edit the commit message file. | ||
# | ||
# To enable this hook, rename this file to "commit-msg". | ||
|
||
# Uncomment the below to add a Signed-off-by line to the message. | ||
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg | ||
# hook is more suited to it. | ||
# | ||
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') | ||
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1" | ||
|
||
# This example catches duplicate Signed-off-by lines. | ||
|
||
test "" = "$(grep '^Signed-off-by: ' "$1" | | ||
sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || { | ||
echo >&2 Duplicate Signed-off-by lines. | ||
exit 1 | ||
} |
7 changes: 7 additions & 0 deletions
7
integrations/gitea-repositories-meta/user2/git_hooks_test.git/hooks/post-receive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env bash | ||
ORI_DIR=`pwd` | ||
SHELL_FOLDER=$(cd "$(dirname "$0")";pwd) | ||
cd "$ORI_DIR" | ||
for i in `ls "$SHELL_FOLDER/post-receive.d"`; do | ||
sh "$SHELL_FOLDER/post-receive.d/$i" | ||
done |
2 changes: 2 additions & 0 deletions
2
integrations/gitea-repositories-meta/user2/git_hooks_test.git/hooks/post-receive.d/gitea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env bash | ||
"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" post-receive |
8 changes: 8 additions & 0 deletions
8
integrations/gitea-repositories-meta/user2/git_hooks_test.git/hooks/post-update.sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/sh | ||
# | ||
# An example hook script to prepare a packed repository for use over | ||
# dumb transports. | ||
# | ||
# To enable this hook, rename this file to "post-update". | ||
|
||
exec git update-server-info |
14 changes: 14 additions & 0 deletions
14
integrations/gitea-repositories-meta/user2/git_hooks_test.git/hooks/pre-applypatch.sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/sh | ||
# | ||
# An example hook script to verify what is about to be committed | ||
# by applypatch from an e-mail message. | ||
# | ||
# The hook should exit with non-zero status after issuing an | ||
# appropriate message if it wants to stop the commit. | ||
# | ||
# To enable this hook, rename this file to "pre-applypatch". | ||
|
||
. git-sh-setup | ||
precommit="$(git rev-parse --git-path hooks/pre-commit)" | ||
test -x "$precommit" && exec "$precommit" ${1+"$@"} | ||
: |
49 changes: 49 additions & 0 deletions
49
integrations/gitea-repositories-meta/user2/git_hooks_test.git/hooks/pre-commit.sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/bin/sh | ||
# | ||
# An example hook script to verify what is about to be committed. | ||
# Called by "git commit" with no arguments. The hook should | ||
# exit with non-zero status after issuing an appropriate message if | ||
# it wants to stop the commit. | ||
# | ||
# To enable this hook, rename this file to "pre-commit". | ||
|
||
if git rev-parse --verify HEAD >/dev/null 2>&1 | ||
then | ||
against=HEAD | ||
else | ||
# Initial commit: diff against an empty tree object | ||
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 | ||
fi | ||
|
||
# If you want to allow non-ASCII filenames set this variable to true. | ||
allownonascii=$(git config --bool hooks.allownonascii) | ||
|
||
# Redirect output to stderr. | ||
exec 1>&2 | ||
|
||
# Cross platform projects tend to avoid non-ASCII filenames; prevent | ||
# them from being added to the repository. We exploit the fact that the | ||
# printable range starts at the space character and ends with tilde. | ||
if [ "$allownonascii" != "true" ] && | ||
# Note that the use of brackets around a tr range is ok here, (it's | ||
# even required, for portability to Solaris 10's /usr/bin/tr), since | ||
# the square bracket bytes happen to fall in the designated range. | ||
test $(git diff --cached --name-only --diff-filter=A -z $against | | ||
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 | ||
then | ||
cat <<\EOF | ||
Error: Attempt to add a non-ASCII file name. | ||
This can cause problems if you want to work with people on other platforms. | ||
To be portable it is advisable to rename the file. | ||
If you know what you are doing you can disable this check using: | ||
git config hooks.allownonascii true | ||
EOF | ||
exit 1 | ||
fi | ||
|
||
# If there are whitespace errors, print the offending file names and fail. | ||
exec git diff-index --check --cached $against -- |
Oops, something went wrong.