forgeplus-react/src/forge/Settings/BranchRule.jsx

305 lines
11 KiB
React
Raw Normal View History

2020-11-26 15:45:54 +08:00
import React, { forwardRef, useCallback, useState, useEffect } from "react";
2020-12-04 19:26:14 +08:00
import { Form, InputNumber , Select, Button , Checkbox , Radio } from "antd";
2020-11-26 15:45:54 +08:00
import Title from "../Component/Title";
2020-12-04 19:26:14 +08:00
import { Cancel } from "../Component/layout";
2020-11-26 15:45:54 +08:00
import axios from "axios";
2020-06-19 14:08:05 +08:00
export default Form.create()(
2020-12-04 19:26:14 +08:00
forwardRef(({ form, match, history , showNotification }) => {
const [ protect , setProtect ] = useState(false);
const [ protects , setProtects ] = useState(false);
const [ mergeOptions , setMergeOptions ] = useState(undefined);
const [ approveOptions , setApproveOptions ] = useState(undefined);
const [ options , setOptions ] = useState(undefined);
2020-12-01 16:49:24 +08:00
const [ list, setList ] = useState(undefined);
2020-12-04 19:26:14 +08:00
const { projectsId, owner , branch } = match.params;
2020-12-04 20:30:23 +08:00
const { getFieldDecorator, validateFields , setFieldsValue , getFieldsValue } = form;
2020-11-26 15:45:54 +08:00
useEffect(() => {
const url = `/${owner}/${projectsId}/collaborators.json`;
axios.get(url).then((result) => {
2020-12-01 16:49:24 +08:00
setList(result.data.members);
2020-11-26 15:45:54 +08:00
})
.catch((error) => {});
}, []);
2020-12-04 19:26:14 +08:00
useEffect(()=>{
if(branch){
// 需要设置分支保护的分支名,获取分支保护详情
getGuardDetail(owner,projectsId,branch);
}
2020-12-04 20:30:23 +08:00
},[]);
2020-12-04 19:26:14 +08:00
function getGuardDetail(owner,projectsId,branch){
const url = `/${owner}/${projectsId}/protected_branches/${branch}/edit.json`;
axios.get(url).then((result)=>{
if(result){
setProtect(result.data.protected);
setProtects(result.data.protected);
let protected_branch = result.data.protected_branch;
if(protected_branch){
let enable_push = protected_branch.enable_push_whitelist ? 2 : protected_branch.enable_push ? 1 : 0;
2020-12-04 20:30:23 +08:00
setFieldsValue({
...protected_branch,
enable_push
})
2020-12-04 19:26:14 +08:00
setOptions(enable_push);
setMergeOptions(protected_branch.enable_merge_whitelist);
setApproveOptions(protected_branch.enable_approvals_whitelist);
}
}
}).catch(error=>{});
2020-11-26 15:45:54 +08:00
}
2020-11-30 16:14:55 +08:00
2020-12-04 19:26:14 +08:00
2020-11-26 15:45:54 +08:00
function saveBranchRule() {
validateFields((error, values) => {
if (!error) {
2020-12-04 19:26:14 +08:00
let url = "";
if(protects){
// 为true则修改
url = `/${owner}/${projectsId}/protected_branches/${branch}.json`;
}else{
// 否则是保存
url = `/${owner}/${projectsId}/protected_branches.json`;
}
axios({
method:!protects ? "post" : (protect ? "patch" : "delete"),
url,
params:
{
branch_name:branch,
2020-12-04 20:30:23 +08:00
enable_push:values.enable_push !== 0 ?false : true,
2020-12-04 19:26:14 +08:00
enable_push_whitelist:values.enable_push === 2 ? true : false,
...values
}
}).then(result=>{
if(result){
showNotification("保存成功!");
}
}).catch(error=>{})
2020-11-26 15:45:54 +08:00
}
});
}
2020-06-19 14:08:05 +08:00
const helper = useCallback(
2020-12-04 20:30:23 +08:00
(label, name, rules, widget, className, isRequired,flag) => (
2020-06-19 14:08:05 +08:00
<div className={className}>
2020-11-26 15:45:54 +08:00
<span className={isRequired ? "required" : ""}>{label}</span>
2020-12-04 20:30:23 +08:00
<Form.Item>
{getFieldDecorator(name, { rules, validateFirst: true , valuePropName:flag ? "checked":"value" })(widget)}
2020-06-19 14:08:05 +08:00
</Form.Item>
</div>
),
[]
);
2020-12-04 19:26:14 +08:00
// 启用分支保护
function changeProtect(e){
setProtect(e.target.checked);
}
// 切换保护选线
function changeProtectOption(e){
setOptions(e.target.value);
}
// 切换是否启用合并白名单
function changeMergeOption(e){
setMergeOptions(e.target.checked);
}
// 是否启用批准仅限列入白名单的用户或团队
function changeWhitelistUsernameOption(e){
setApproveOptions(e.target.checked);
}
2020-11-26 15:45:54 +08:00
return (
2020-12-04 19:26:14 +08:00
<div style={{backgroundColor:"#fff"}}>
2020-12-08 17:16:07 +08:00
<Title>新建'{branch}'保护分支规则</Title>
2020-12-04 19:26:14 +08:00
<Form>
<div style={{padding:"20px 30px"}}>
2020-06-19 14:08:05 +08:00
{helper(
2020-12-04 19:26:14 +08:00
"",
"branchProtect",
[],
<Checkbox checked={protect} onChange={changeProtect}>
启用分支保护<span className="color-grey-9 ml5 font-12">组织删除并限制Git推送和合并到分支</span>
</Checkbox>,
2021-02-26 15:40:44 +08:00
"setHeight"
2020-06-19 14:08:05 +08:00
)}
2020-12-04 19:26:14 +08:00
<div className="pl25 shortStyle">
{helper(
"",
"enable_push",
[],
<Radio.Group disabled={!protect} onChange={changeProtectOption}>
<Radio className="columsRadio" value={0}>
禁用推送<span className="color-grey-9 ml5 font-12">此分支不允许推送</span>
</Radio>
<Radio className="columsRadio" value={1}>
启用推送<span className="color-grey-9 ml5 font-12">任何拥有写访问权限的人将被允许推送到此分支(但不能强行推送)</span>
</Radio>
<Radio className="columsRadio" value={2}>
启用推送白名单<span className="color-grey-9 ml5 font-12">只有列入白名单的用户或团队才能被允许推送到此分支(但不能强行推送)</span>
</Radio>
</Radio.Group>,
""
)}
2021-02-26 15:40:44 +08:00
<div className="pl25 mb15">
2020-12-04 19:26:14 +08:00
{helper(
"",
"push_whitelist_usernames",
[],
<Select
mode="multiple"
placeholder="搜索用户"
style={{ width: '100%' }}
disabled={!protect || (options ===undefined || options !== 2)}
>
{list && list.map(item => (
<Select.Option key={item.id} value={item.login}>
{item.name}
</Select.Option>
))}
</Select>,
"setStyleRule"
)}
</div>
{helper(
"",
"enable_merge_whitelist",
[],
<Checkbox disabled={!protect} checked={mergeOptions} onChange={changeMergeOption}>
启用合并白名单<span className="color-grey-9 ml5 font-12">仅允许白名单用户或团队合并合并请求到此分支</span>
</Checkbox>,
2021-02-26 15:40:44 +08:00
"setHeight"
2020-12-04 19:26:14 +08:00
)}
<div className="pl25 pt5 pb5">
{helper(
"",
"merge_whitelist_usernames",
[],
<Select
mode="multiple"
placeholder="搜索用户"
style={{ width: '100%' }}
disabled={!protect || !mergeOptions}
>
{list && list.map(item => (
<Select.Option key={item.id} value={item.login}>
{item.name}
</Select.Option>
))}
</Select>,
"setStyleRule"
)}
</div>
{helper(
"",
"enable_status_check",
[],
<Checkbox disabled={!protect}>
启用状态检查
</Checkbox>,
2021-02-26 15:40:44 +08:00
"setHeight",false,true
2020-12-04 19:26:14 +08:00
)}
2021-02-26 15:40:44 +08:00
<div style={{display:"flex",alignItems:"center",padding:"10px 0px"}}>
2020-12-04 19:26:14 +08:00
{helper(
"所需的批准数",
"required_approvals",
[],
<InputNumber min={0} style={{width:"140px"}}/>,
"inlineFlex"
)}
<span className="color-grey-9 ml5 font-12">只允许合并有足够审核人数的拉取请求</span>
</div>
{helper(
"",
"enable_approvals_whitelist",
[],
2020-12-04 20:30:23 +08:00
<Checkbox name="enable_approvals_whitelist" disabled={!protect} checked={approveOptions} onChange={changeWhitelistUsernameOption}>
2020-12-04 19:26:14 +08:00
批准仅限列入白名单的用户或团队<span className="color-grey-9 ml5 font-12">只有白名单用户或团队的审核才能计数 没有批准的白名单,任何有写访问权限的人的审核都将计数</span>
</Checkbox>,
2021-02-26 15:40:44 +08:00
"setHeight mb5"
2020-12-04 19:26:14 +08:00
)}
2021-02-26 15:40:44 +08:00
<div className="pl25 mb15">
2020-12-04 19:26:14 +08:00
{helper(
"",
"approvals_whitelist_usernames",
[],
<Select
mode="multiple"
placeholder="搜索用户"
style={{ width: '100%' }}
disabled={!protect || !approveOptions}
>
{list && list.map(item => (
<Select.Option key={item.id} value={item.login}>
{item.name}
</Select.Option>
))}
</Select>,
"setStyleRule"
)}
</div>
{helper(
"",
"block_on_rejected_reviews",
[],
2020-12-04 20:30:23 +08:00
<Checkbox disabled={!protect} name="block_on_rejected_reviews">
2020-12-04 19:26:14 +08:00
拒绝审核阻止了合并<span className="color-grey-9 ml5 font-12">如果官方审查人员要求作出改动即使有足够的批准合并也不允许</span>
</Checkbox>,
2021-02-26 15:40:44 +08:00
"setHeight mb5",false,true
2020-12-04 19:26:14 +08:00
)}
{helper(
"",
"dismiss_stale_approvals",
[],
2020-12-04 20:30:23 +08:00
<Checkbox disabled={!protect} name="dismiss_stale_approvals">
2020-12-04 19:26:14 +08:00
取消过时的批准<span className="color-grey-9 ml5 font-12">当新的提交更改合并请求内容被推送到分支时旧的批准将被撤销</span>
</Checkbox>,
2021-02-26 15:40:44 +08:00
"setHeight mb5",false,true
2020-12-04 19:26:14 +08:00
)}
{helper(
"",
"require_signed_commits",
[],
2020-12-04 20:30:23 +08:00
<Checkbox disabled={!protect} name="require_signed_commits">
2020-12-04 19:26:14 +08:00
需要签名提交
</Checkbox>,
2021-02-26 15:40:44 +08:00
"setHeight mb5",false,true
2020-12-04 19:26:14 +08:00
)}
{helper(
"",
"block_on_outdated_branch",
[],
2020-12-04 20:30:23 +08:00
<Checkbox disabled={!protect} name="block_on_outdated_branch">
2020-12-04 19:26:14 +08:00
如果拉取请求已经过时阻止合并<span className="color-grey-9 ml5 font-12">当头部分支落后基础分支时不能合并</span>
</Checkbox>,
2021-02-26 15:40:44 +08:00
"setHeight",false,true
2020-12-04 19:26:14 +08:00
)}
</div>
<div className="df pb30 pt20">
2020-11-26 15:45:54 +08:00
<Button type="primary" onClick={saveBranchRule}>
保存
</Button>
<Cancel
className="ml30"
onClick={() => {
2021-09-03 14:39:21 +08:00
history.push(`/${owner}/${projectsId}/settings/branches`);
2020-11-26 15:45:54 +08:00
}}
>
取消
</Cancel>
2020-06-19 14:08:05 +08:00
</div>
2020-12-04 19:26:14 +08:00
</div>
</Form>
</div>
2020-11-26 15:45:54 +08:00
);
2020-06-19 14:08:05 +08:00
})
2020-11-26 15:45:54 +08:00
);