Skip to content
GitLab
菜单
为什么选择 GitLab
定价
联系销售
探索
为什么选择 GitLab
定价
联系销售
探索
登录
获取免费试用
主导航
搜索或转到…
项目
GitLab
管理
动态
成员
标记
计划
议题
议题看板
里程碑
迭代
需求
代码
合并请求
仓库
分支
提交
标签
仓库图
比较修订版本
代码片段
锁定的文件
构建
流水线
作业
流水线计划
测试用例
产物
部署
发布
Package registry
Container registry
模型注册表
运维
环境
Terraform 模块
监控
事件
服务台
分析
价值流分析
贡献者分析
CI/CD 分析
仓库分析
代码评审分析
议题分析
洞察
模型实验
效能分析
帮助
帮助
支持
GitLab 文档
比较 GitLab 各版本
社区论坛
为极狐GitLab 提交贡献
提交反馈
隐私声明
快捷键
?
新增功能
4
代码片段
群组
项目
显示更多面包屑
gitlab-cn
GitLab
提交
1fc20a6b
未验证
提交
1fc20a6b
编辑于
9 years ago
作者:
Dmitriy Zaporozhets
浏览文件
操作
下载
补丁
差异文件
Refactor complex methods to make CI green
Signed-off-by:
Dmitriy Zaporozhets
<
dmitriy.zaporozhets@gmail.com
>
上级
a5f7374a
No related branches found
分支 包含提交
No related tags found
标签 包含提交
无相关合并请求
变更
2
隐藏空白变更内容
行内
左右并排
显示
2 个更改的文件
app/models/git_hook.rb
+21
-15
21 个添加, 15 个删除
app/models/git_hook.rb
lib/gitlab/git_access.rb
+61
-47
61 个添加, 47 个删除
lib/gitlab/git_access.rb
有
82 个添加
和
62 个删除
app/models/git_hook.rb
+
21
−
15
浏览文件 @
1fc20a6b
...
@@ -2,24 +2,30 @@ class GitHook < ActiveRecord::Base
...
@@ -2,24 +2,30 @@ class GitHook < ActiveRecord::Base
belongs_to
:project
belongs_to
:project
validates
:project
,
presence:
true
,
unless:
"is_sample?"
validates
:project
,
presence:
true
,
unless:
"is_sample?"
def
commit_validation?
commit_message_regex
.
present?
||
author_email_regex
.
present?
||
member_check
||
file_name_regex
.
present?
||
max_file_size
>
0
end
def
commit_message_allowed?
(
message
)
def
commit_message_allowed?
(
message
)
if
commit_message_regex
.
present?
data_valid?
(
message
,
commit_message_regex
)
if
message
=~
Regexp
.
new
(
commit_message_regex
)
end
true
else
def
author_email_allowed?
(
email
)
false
data_valid?
(
email
,
author_email_regex
)
end
end
private
def
data_valid?
(
data
,
regex
)
if
regex
.
present?
!!
(
data
=~
Regexp
.
new
(
regex
))
else
else
true
true
end
end
end
end
def
commit_validation?
commit_message_regex
.
present?
||
author_email_regex
.
present?
||
member_check
||
file_name_regex
.
present?
||
max_file_size
>
0
end
end
end
此差异已折叠。
点击以展开。
lib/gitlab/git_access.rb
+
61
−
47
浏览文件 @
1fc20a6b
...
@@ -170,9 +170,9 @@ def forced_push?(oldrev, newrev)
...
@@ -170,9 +170,9 @@ def forced_push?(oldrev, newrev)
end
end
def
git_hook_check
(
user
,
project
,
ref
,
oldrev
,
newrev
)
def
git_hook_check
(
user
,
project
,
ref
,
oldrev
,
newrev
)
return
build_status_object
(
true
)
unless
project
.
git_hook
unless
project
.
git_hook
&&
newrev
&&
oldrev
return
build_status_object
(
true
)
return
build_status_object
(
true
)
unless
newrev
&&
oldrev
end
git_hook
=
project
.
git_hook
git_hook
=
project
.
git_hook
...
@@ -182,8 +182,9 @@ def git_hook_check(user, project, ref, oldrev, newrev)
...
@@ -182,8 +182,9 @@ def git_hook_check(user, project, ref, oldrev, newrev)
return
build_status_object
(
false
,
"You can not delete tag"
)
return
build_status_object
(
false
,
"You can not delete tag"
)
end
end
else
else
return
build_status_object
(
true
)
unless
git_hook
.
commit_validation?
if
Gitlab
::
Git
.
blank_ref?
(
newrev
)
||
!
git_hook
.
commit_validation?
return
build_status_object
(
true
)
if
Gitlab
::
Git
.
blank_ref?
(
newrev
)
return
build_status_object
(
true
)
end
oldrev
=
project
.
default_branch
if
Gitlab
::
Git
.
blank_ref?
(
oldrev
)
oldrev
=
project
.
default_branch
if
Gitlab
::
Git
.
blank_ref?
(
oldrev
)
...
@@ -195,61 +196,76 @@ def git_hook_check(user, project, ref, oldrev, newrev)
...
@@ -195,61 +196,76 @@ def git_hook_check(user, project, ref, oldrev, newrev)
end
end
commits
.
each
do
|
commit
|
commits
.
each
do
|
commit
|
if
git_hook
.
commit_message_regex
.
present?
if
status_object
=
check_commit
(
commit
,
git_hook
)
unless
commit
.
safe_message
=~
Regexp
.
new
(
git_hook
.
commit_message_regex
)
return
status_object
return
build_status_object
(
false
,
"Commit message does not follow the pattern '
#{
git_hook
.
commit_message_regex
}
'"
)
end
end
end
end
end
if
git_hook
.
author_email_regex
.
present?
build_status_object
(
true
)
unless
commit
.
committer_email
=~
Regexp
.
new
(
git_hook
.
author_email_regex
)
end
return
build_status_object
(
false
,
"Committer's email '
#{
commit
.
committer_email
}
' does not follow the pattern '
#{
git_hook
.
author_email_regex
}
'"
)
end
unless
commit
.
author_email
=~
Regexp
.
new
(
git_hook
.
author_email_regex
)
private
return
build_status_object
(
false
,
"Author's email '
#{
commit
.
author_email
}
' does not follow the pattern '
#{
git_hook
.
author_email_regex
}
'"
)
end
# If commit does not pass git hook validation the whole push should be rejected.
end
# This method should return nil if no error found or status object if there are some errors.
# In case of errors - all other checks will be canceled and push will be rejected.
def
check_commit
(
commit
,
git_hook
)
unless
git_hook
.
commit_message_allowed?
(
commit
.
safe_message
)
return
build_status_object
(
false
,
"Commit message does not follow the pattern '
#{
git_hook
.
commit_message_regex
}
'"
)
end
unless
git_hook
.
author_email_allowed?
(
commit
.
committer_email
)
return
build_status_object
(
false
,
"Committer's email '
#{
commit
.
committer_email
}
' does not follow the pattern '
#{
git_hook
.
author_email_regex
}
'"
)
end
unless
git_hook
.
author_email_allowed?
(
commit
.
author_email
)
return
build_status_object
(
false
,
"Author's email '
#{
commit
.
author_email
}
' does not follow the pattern '
#{
git_hook
.
author_email_regex
}
'"
)
end
# Check whether author is a GitLab member
if
git_hook
.
member_check
unless
User
.
existing_member?
(
commit
.
author_email
.
downcase
)
return
build_status_object
(
false
,
"Author '
#{
commit
.
author_email
}
' is not a member of team"
)
end
# Check whether author is a GitLab member
if
commit
.
author_email
.
downcase
!=
commit
.
committer_email
.
downcase
if
git_hook
.
member_check
unless
User
.
existing_member?
(
commit
.
committer_email
.
downcase
)
unless
User
.
existing_member?
(
commit
.
author_email
.
downcase
)
return
build_status_object
(
false
,
"Committer '
#{
commit
.
committer_email
}
' is not a member of team"
)
return
build_status_object
(
false
,
"Author '
#{
commit
.
author_email
}
' is not a member of team"
)
end
if
commit
.
author_email
.
downcase
!=
commit
.
committer_email
.
downcase
unless
User
.
existing_member?
(
commit
.
committer_email
.
downcase
)
return
build_status_object
(
false
,
"Committer '
#{
commit
.
committer_email
}
' is not a member of team"
)
end
end
end
end
end
end
if
status_object
=
check_commit_diff
(
commit
,
git_hook
)
return
status_object
end
if
git_hook
.
file_name_regex
.
present?
nil
commit
.
diffs
.
each
do
|
diff
|
end
if
(
diff
.
renamed_file
||
diff
.
new_file
)
&&
diff
.
new_path
=~
Regexp
.
new
(
git_hook
.
file_name_regex
)
return
build_status_object
(
false
,
"File name
#{
diff
.
new_path
.
inspect
}
does not follow the pattern '
#{
git_hook
.
file_name_regex
}
'"
)
def
check_commit_diff
(
commit
,
git_hook
)
end
if
git_hook
.
file_name_regex
.
present?
end
commit
.
diffs
.
each
do
|
diff
|
if
(
diff
.
renamed_file
||
diff
.
new_file
)
&&
diff
.
new_path
=~
Regexp
.
new
(
git_hook
.
file_name_regex
)
return
build_status_object
(
false
,
"File name
#{
diff
.
new_path
.
inspect
}
does not follow the pattern '
#{
git_hook
.
file_name_regex
}
'"
)
end
end
end
end
if
git_hook
.
max_file_size
>
0
if
git_hook
.
max_file_size
>
0
commit
.
diffs
.
each
do
|
diff
|
commit
.
diffs
.
each
do
|
diff
|
next
if
diff
.
deleted_file
next
if
diff
.
deleted_file
blob
=
project
.
repository
.
blob_at
(
commit
.
id
,
diff
.
new_path
)
blob
=
project
.
repository
.
blob_at
(
commit
.
id
,
diff
.
new_path
)
if
blob
.
size
>
git_hook
.
max_file_size
.
megabytes
if
blob
.
size
>
git_hook
.
max_file_size
.
megabytes
return
build_status_object
(
false
,
"File
#{
diff
.
new_path
.
inspect
}
is larger than the allowed size of
#{
git_hook
.
max_file_size
}
MB"
)
return
build_status_object
(
false
,
"File
#{
diff
.
new_path
.
inspect
}
is larger than the allowed size of
#{
git_hook
.
max_file_size
}
MB"
)
end
end
end
end
end
end
end
end
build_status_object
(
true
)
nil
end
end
private
def
protected_branch_action
(
oldrev
,
newrev
,
branch_name
)
def
protected_branch_action
(
oldrev
,
newrev
,
branch_name
)
# we dont allow force push to protected branch
# we dont allow force push to protected branch
if
forced_push?
(
oldrev
,
newrev
)
if
forced_push?
(
oldrev
,
newrev
)
...
@@ -290,8 +306,6 @@ def tag_name(ref)
...
@@ -290,8 +306,6 @@ def tag_name(ref)
end
end
end
end
protected
def
build_status_object
(
status
,
message
=
''
)
def
build_status_object
(
status
,
message
=
''
)
GitAccessStatus
.
new
(
status
,
message
)
GitAccessStatus
.
new
(
status
,
message
)
end
end
...
...
此差异已折叠。
点击以展开。
预览
0%
加载中
请重试
或
添加新附件
.
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
保存评论
取消
想要评论请
注册
或
登录