{"record":{"id":"82a2978450c03697","repo":"theonedev/onedev","slug":"errormessage-from-checkrestoresourcebranchconditi","errorCode":null,"errorMessage":"errorMessage (from checkRestoreSourceBranchCondition)","messagePattern":"errorMessage \\(from checkRestoreSourceBranchCondition\\)","errorType":"validation","errorClass":"NotAcceptableException","httpStatus":400,"severity":"error","filePath":"server-core/src/main/java/io/onedev/server/service/impl/DefaultPullRequestService.java","lineNumber":278,"sourceCode":"    \tquery = getSession().createQuery(String.format(\"update CodeCommentStatusChange set %s=null where %s=:request\",\n    \t\t\tCodeCommentStatusChange.PROP_COMPARE_CONTEXT + \".\" + CompareContext.PROP_PULL_REQUEST,\n    \t\t\tCodeCommentStatusChange.PROP_COMPARE_CONTEXT + \".\" + CompareContext.PROP_PULL_REQUEST));\n    \tquery.setParameter(\"request\", request);\n    \tquery.executeUpdate();\n\n\t\tdao.remove(request);\n\t}\n\n\tprivate GitService getGitService() {\n\t\treturn OneDev.getInstance(GitService.class);\n\t}\n\n\t@Transactional\n\t@Override\n\tpublic void restoreSourceBranch(User user, PullRequest request, String note) {\n        var errorMessage = request.checkRestoreSourceBranchCondition();\n        if (errorMessage != null)\n            throw new NotAcceptableException(errorMessage);\n\n\t\tif (request.getSource().getObjectName(false) == null) {\n\t\t\tgetGitService().createBranch(request.getSourceProject(), request.getSourceBranch(),\n\t\t\t\t\trequest.getLatestUpdate().getHeadCommitHash());\n\n\t\t\tPullRequestChange change = new PullRequestChange();\n\t\t\tchange.setDate(new Date());\n\t\t\tchange.setData(new PullRequestSourceBranchRestoreData());\n\t\t\tchange.setRequest(request);\n\t\t\tchange.setUser(user);\n\t\t\tchangeService.create(change, note);\n\t\t}\n\t}\n\n\t@Transactional\n\t@Override\n\tpublic void deleteSourceBranch(User user, PullRequest request, String note) {\n        var errorMessage = request.checkDeleteSourceBranchCondition();","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/theonedev/onedev/blob/d44925c47c37992c828ea673a5f9620539bc3ff2/server-core/src/main/java/io/onedev/server/service/impl/DefaultPullRequestService.java#L260-L296","documentation":"restoreSourceBranch throws the message returned by PullRequest.checkRestoreSourceBranchCondition() as a NotAcceptableException when the source branch cannot be restored. The condition fails when the source project no longer exists or the source branch already exists (nothing to restore). The thrown message equals whatever checkRestoreSourceBranchCondition returned.","triggerScenarios":"Calling DefaultPullRequestService.restoreSourceBranch(user, request, note) when checkRestoreSourceBranchCondition() returns non-null: getSourceProject() == null (\"Source project no longer exists\") or getSource().getObjectName(false) != null (\"Source branch already exists\").","commonSituations":"Clicking 'restore source branch' on a PR whose branch was recreated manually; source fork/repository was deleted so restoration target is gone; scripting branch restores without checking current branch state.","solutions":["If the branch already exists, no restore is needed — skip the call","If the source project was deleted, restore/recreate the project first (or abandon the restore)","Call request.checkRestoreSourceBranchCondition() yourself and only restore when it returns null"],"exampleFix":"// before\nprService.restoreSourceBranch(user, request, \"restore\"); // 400 \"Source branch already exists\"\n// after\nif (request.checkRestoreSourceBranchCondition() == null)\n    prService.restoreSourceBranch(user, request, \"restore\");","handlingStrategy":"validation","validationCode":"String err = request.checkRestoreSourceBranchCondition();\nboolean canRestore = err == null;","typeGuard":null,"tryCatchPattern":"try {\n    prService.restoreSourceBranch(user, request, note);\n} catch (NotAcceptableException e) {\n    // branch already exists or source project gone\n}","preventionTips":["Only offer 'restore branch' when the branch is absent","Check source project existence first","Treat an existing branch as already-restored"],"tags":["pull-request","branch","restore","precondition"],"backgroundTag":"invalid-state-transition","analyzedSha":"d44925c47c37992c828ea673a5f9620539bc3ff2","analyzedAt":"2026-09-06T07:18:27.995Z","contentChangedAt":"2026-09-06T07:18:27.995Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}