Handle reserved names & patterns on repo creation (#315)

correct var names

working solution

code format ...

first draft

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitnex/GitNex/pulls/315
Reviewed-by: M M Arif <mmarif@swatian.com>
This commit is contained in:
6543 2020-03-29 18:57:19 +00:00 committed by M M Arif
parent 2fd291527f
commit 6e0880e6d8
2 changed files with 19 additions and 6 deletions

View File

@ -25,7 +25,9 @@ import org.mian.gitnex.models.OrganizationRepository;
import org.mian.gitnex.util.AppUtil;
import org.mian.gitnex.util.TinyDB;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import retrofit2.Call;
import retrofit2.Callback;
@ -46,11 +48,15 @@ public class CreateRepoActivity extends BaseActivity {
List<OrgOwner> organizationsList = new ArrayList<>();
//https://github.com/go-gitea/gitea/blob/52cfd2743c0e85b36081cf80a850e6a5901f1865/models/repo.go#L964-L967
final List<String> reservedRepoNames = Arrays.asList(".", "..");
final Pattern reservedRepoPatterns = Pattern.compile("\\.(git|wiki)$");
@Override
protected int getLayoutResourceId(){
return R.layout.activity_new_repo;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -105,7 +111,6 @@ public class CreateRepoActivity extends BaseActivity {
createRepo.setOnClickListener(createRepoListener);
}
}
private View.OnClickListener createRepoListener = new View.OnClickListener() {
@ -153,15 +158,23 @@ public class CreateRepoActivity extends BaseActivity {
Toasty.info(getApplicationContext(), getString(R.string.repoNameErrorInvalid));
}
else if (reservedRepoNames.contains(newRepoName)) {
Toasty.info(getApplicationContext(), getString(R.string.repoNameErrorReservedName));
}
else if (reservedRepoPatterns.matcher(newRepoName).find()) {
Toasty.info(getApplicationContext(), getString(R.string.repoNameErrorReservedPatterns));
}
else {
//Log.i("repoOwner", String.valueOf(repoOwner));
disableProcessButton();
createNewRepository(instanceUrl, Authorization.returnAuthentication(getApplicationContext(), loginUid, instanceToken), loginUid, newRepoName, newRepoDesc, repoOwner, newRepoAccess);
}
}
private void createNewRepository(final String instanceUrl, final String token, String loginUid, String repoName, String repoDesc, String repoOwner, boolean isPrivate) {
@ -229,7 +242,6 @@ public class CreateRepoActivity extends BaseActivity {
enableProcessButton();
}
});
}
private void getOrganizations(String instanceUrl, String instanceToken, final String userLogin) {
@ -304,7 +316,6 @@ public class CreateRepoActivity extends BaseActivity {
enableProcessButton();
}
});
}
private void initCloseListener() {

View File

@ -102,6 +102,8 @@
<string name="repoNameErrorEmpty">Repository name is empty</string>
<string name="repoNameErrorInvalid">Repository name is not valid. [a&#8211;z A&#8211;Z 0&#8211;9 &#8211; _]</string>
<string name="repoNameErrorReservedName">Repository name is reserved</string>
<string name="repoNameErrorReservedPatterns">Repository name contains reserved keywords</string>
<string name="repoDescError">Repository description exceeds the max 255 characters limit</string>
<string name="repoCreated">Repository created successfully</string>
<string name="repoCreatedError">Something went wrong, please try again</string>