The Unified and Autonomous Development Platform
feat(tod): support API operations without current project context
Robin Shenfd0a5d2315ba
diff --git a/server-core/src/main/java/io/onedev/server/ai/BuildHelper.java b/server-core/src/main/java/io/onedev/server/ai/BuildHelper.javaindex 4527296ae0..f146684807 100644--- a/server-core/src/main/java/io/onedev/server/ai/BuildHelper.java+++ b/server-core/src/main/java/io/onedev/server/ai/BuildHelper.java@@ -11,6 +11,7 @@ import io.onedev.server.OneDev;import io.onedev.server.model.Build;import io.onedev.server.model.Project;import io.onedev.server.service.UrlService;+import org.jspecify.annotations.Nullable;public class BuildHelper {@@ -22,7 +23,7 @@ public class BuildHelper {return OneDev.getInstance(UrlService.class);}- public static Map<String, Object> getSummary(Project currentProject, Build build) {+ public static Map<String, Object> getSummary(@Nullable Project currentProject, Build build) {var typeReference = new TypeReference<LinkedHashMap<String, Object>>() {};var summary = getObjectMapper().convertValue(build, typeReference);summary.remove("id");@@ -52,7 +53,7 @@ public class BuildHelper {return summary;}- public static Map<String, Object> getDetail(Project currentProject, Build build) {+ public static Map<String, Object> getDetail(@Nullable Project currentProject, Build build) {var data = getSummary(currentProject, build);data.put("params", build.getParamMap());data.put("labels", build.getLabels().stream().map(it->it.getSpec().getName()).collect(Collectors.toList()));diff --git a/server-core/src/main/java/io/onedev/server/ai/IssueHelper.java b/server-core/src/main/java/io/onedev/server/ai/IssueHelper.javaindex e99b01aa2a..a52fdcee7f 100644--- a/server-core/src/main/java/io/onedev/server/ai/IssueHelper.java+++ b/server-core/src/main/java/io/onedev/server/ai/IssueHelper.java@@ -46,6 +46,7 @@ import io.onedev.server.service.IssueService;import io.onedev.server.service.IterationService;import io.onedev.server.service.SettingService;import io.onedev.server.service.UrlService;+import org.jspecify.annotations.Nullable;public class IssueHelper {@@ -57,7 +58,7 @@ public class IssueHelper {return OneDev.getInstance(UrlService.class);}- public static Map<String, Object> getSummary(Project currentProject, Issue issue) {+ public static Map<String, Object> getSummary(@Nullable Project currentProject, Issue issue) {var typeReference = new TypeReference<LinkedHashMap<String, Object>>() {};var summary = getObjectMapper().convertValue(issue, typeReference);summary.remove("id");@@ -78,11 +79,7 @@ public class IssueHelper {summary.put("Project", issue.getProject().getPath());summary.remove("lastActivity");summary.put("lastActivityDate", issue.getLastActivity().getDate());- for (var it = summary.entrySet().iterator(); it.hasNext();) {- var entry = it.next();- if (entry.getKey().endsWith("Count"))- it.remove();- }+ summary.entrySet().removeIf(entry -> entry.getKey().endsWith("Count"));return summary;}@@ -99,7 +96,7 @@ public class IssueHelper {return comments;}- public static Map<String, Object> getDetail(Project currentProject, Issue issue) {+ public static Map<String, Object> getDetail(@Nullable Project currentProject, Issue issue) {var data = getSummary(currentProject, issue);data.put("branch", issue.getBranch());@@ -160,8 +157,7 @@ public class IssueHelper {public static Map<String, Object> getFieldProperties(FieldSpec field) {var description = field.getDescription() != null ? field.getDescription().replace('\n', ' ') : "";- if (field instanceof ChoiceField) {- var choiceField = (ChoiceField) field;+ if (field instanceof ChoiceField choiceField) {if (field.isAllowMultiple())description = appendDescription(description, "Expects one or more of: " + String.join(", ", choiceField.getPossibleValues()));else@@ -225,9 +221,7 @@ public class IssueHelper {public static void setFieldValues(Map<String, Serializable> data, JsonNode fieldsNode) {if (fieldsNode == null || !fieldsNode.isObject())return;- var fields = fieldsNode.properties().iterator();- while (fields.hasNext()) {- var entry = fields.next();+ for (Entry<String, JsonNode> entry : fieldsNode.properties()) {setFieldValue(data, entry.getKey(), entry.getValue());}}diff --git a/server-core/src/main/java/io/onedev/server/ai/PullRequestHelper.java b/server-core/src/main/java/io/onedev/server/ai/PullRequestHelper.javaindex 5584ba00f1..086bd9579a 100644--- a/server-core/src/main/java/io/onedev/server/ai/PullRequestHelper.java+++ b/server-core/src/main/java/io/onedev/server/ai/PullRequestHelper.java@@ -8,6 +8,7 @@ import java.util.List;import java.util.Map;import java.util.stream.Collectors;+import io.onedev.server.model.*;import org.apache.commons.lang3.tuple.ImmutablePair;import org.apache.commons.lang3.tuple.Pair;import org.eclipse.jgit.diff.DiffEntry.ChangeType;@@ -26,13 +27,6 @@ import io.onedev.server.git.BlobIdent;import io.onedev.server.git.GitUtils;import io.onedev.server.git.service.DiffEntryFacade;import io.onedev.server.git.service.GitService;-import io.onedev.server.model.CodeComment;-import io.onedev.server.model.CodeCommentReply;-import io.onedev.server.model.Project;-import io.onedev.server.model.PullRequest;-import io.onedev.server.model.PullRequestComment;-import io.onedev.server.model.PullRequestReview;-import io.onedev.server.model.User;import io.onedev.server.model.support.CompareContext;import io.onedev.server.model.support.Mark;import io.onedev.server.service.CodeCommentService;@@ -41,6 +35,7 @@ import io.onedev.server.service.UrlService;import io.onedev.server.util.diff.DiffUtils;import io.onedev.server.util.diff.WhitespaceOption;import io.onedev.server.web.util.DiffPlanarRange;+import org.jspecify.annotations.Nullable;public class PullRequestHelper {@@ -52,7 +47,7 @@ public class PullRequestHelper {return OneDev.getInstance(UrlService.class);}- public static Map<String, Object> getSummary(Project currentProject,+ public static Map<String, Object> getSummary(@Nullable Project currentProject,PullRequest pullRequest, boolean checkMergeConditionIfOpen) {var typeReference = new TypeReference<LinkedHashMap<String, Object>>() {};var summary = getObjectMapper().convertValue(pullRequest, typeReference);@@ -79,18 +74,14 @@ public class PullRequestHelper {summary.remove("codeCommentsUpdateDate");summary.remove("lastActivity");summary.put("lastActivityDate", pullRequest.getLastActivity().getDate());- for (var it = summary.entrySet().iterator(); it.hasNext();) {- var entry = it.next();- if (entry.getKey().endsWith("Count"))- it.remove();- }+ summary.entrySet().removeIf(entry -> entry.getKey().endsWith("Count"));return summary;}- public static Map<String, Object> getDetail(Project currentProject, PullRequest pullRequest) {+ public static Map<String, Object> getDetail(@Nullable Project currentProject, PullRequest pullRequest) {var data = getSummary(currentProject, pullRequest, true);data.put("headCommitHash", pullRequest.getLatestUpdate().getHeadCommitHash());- data.put("assignees", pullRequest.getAssignees().stream().map(it->it.getName()).collect(Collectors.toList()));+ data.put("assignees", pullRequest.getAssignees().stream().map(User::getName).collect(Collectors.toList()));var reviews = new ArrayList<Map<String, Object>>();for (var review : pullRequest.getReviews()) {if (review.getStatus() == PullRequestReview.Status.EXCLUDED)@@ -112,14 +103,13 @@ public class PullRequestHelper {return data;}- public static List<Map<String, Object>> getBuilds(Project currentProject, PullRequest pullRequest) {+ public static List<Map<String, Object>> getBuilds(@Nullable Project currentProject, PullRequest pullRequest) {var builds = new ArrayList<Map<String, Object>>();- pullRequest.getCurrentBuilds().stream()- .forEach(it -> {- var summary = BuildHelper.getSummary(currentProject, it);- summary.put("link", getUrlService().urlFor(it, true));- builds.add(summary);- });+ for (Build build : pullRequest.getCurrentBuilds()) {+ var summary = BuildHelper.getSummary(currentProject, build);+ summary.put("link", getUrlService().urlFor(build, true));+ builds.add(summary);+ }return builds;}diff --git a/server-core/src/main/java/io/onedev/server/ai/TodResource.java b/server-core/src/main/java/io/onedev/server/ai/TodResource.javaindex 3d900aedad..ef9ba91f03 100644--- a/server-core/src/main/java/io/onedev/server/ai/TodResource.java+++ b/server-core/src/main/java/io/onedev/server/ai/TodResource.java@@ -207,7 +207,7 @@ public class TodResource {public VersionInfo checkVersion() {var versionInfo = new VersionInfo();versionInfo.serverVersion = OneDev.getInstance().getVersion();- versionInfo.minRequiredTodVersion = "4.1.0";+ versionInfo.minRequiredTodVersion = "4.4.0";return versionInfo;}@@ -218,17 +218,6 @@ public class TodResource {return project;}- private ProjectContext getProjectContext(String projectPath, String currentProjectPath) {- if (projectPath == null)- projectPath = currentProjectPath;-- var projectContext = new ProjectContext();- projectContext.project = getProject(projectPath);- projectContext.currentProject = getProject(currentProjectPath);-- return projectContext;- }-private Map<String, Object> getFieldProperties(FieldSpec field) {var fieldProperties = new HashMap<>(IssueHelper.getFieldProperties(field));fieldProperties.put("description", escapeHtml5((String) fieldProperties.get("description")));@@ -423,7 +412,7 @@ public class TodResource {@Path("/query-issues")@GETpublic List<Map<String, Object>> queryIssues(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("project") String projectPath,@QueryParam("query") String query,@QueryParam("offset") int offset,@@ -432,7 +421,11 @@ public class TodResource {if (SecurityUtils.getUser(subject) == null)throw new UnauthenticatedException();- var projectContext = getProjectContext(projectPath, currentProjectPath);+ if (projectPath == null)+ projectPath = currentProjectPath;++ var project = projectPath != null? getProject(projectPath): null;+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;if (count > RestConstants.MAX_PAGE_SIZE)throw new NotAcceptableException("Count should not be greater than " + RestConstants.MAX_PAGE_SIZE);@@ -441,14 +434,15 @@ public class TodResource {if (query != null) {var option = new IssueQueryParseOption();option.withCurrentUserCriteria(true);- parsedQuery = IssueQuery.parse(projectContext.project, query, option, true);+ parsedQuery = IssueQuery.parse(project, query, option, true);} else {parsedQuery = new IssueQuery(null, new ArrayList<>());}var summaries = new ArrayList<Map<String, Object>>();- for (var issue : issueService.query(subject, new ProjectScope(projectContext.project, true, false), parsedQuery, true, offset, count)) {- var summary = IssueHelper.getSummary(projectContext.currentProject, issue);+ var projectScope = project != null? new ProjectScope(project, true, false): null;+ for (var issue : issueService.query(subject, projectScope, parsedQuery, true, offset, count)) {+ var summary = IssueHelper.getSummary(currentProject, issue);for (var entry: issue.getFieldInputs().entrySet()) {summary.put(entry.getKey(), entry.getValue().getValues());}@@ -458,7 +452,7 @@ public class TodResource {return summaries;}- private Issue getIssue(Project currentProject, String referenceString) {+ private Issue getIssue(@Nullable Project currentProject, String referenceString) {var issueReference = IssueReference.of(referenceString, currentProject);var issue = issueService.find(issueReference.getProject(), issueReference.getNumber());if (issue != null) {@@ -473,14 +467,14 @@ public class TodResource {@Path("/get-issue")@GETpublic Map<String, Object> getIssueDetail(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String issueReference,@QueryParam("forWrite") Boolean forWrite) {var subject = SecurityUtils.getSubject();if (SecurityUtils.getUser(subject) == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var issue = getIssue(currentProject, issueReference);if (forWrite != null && forWrite &&!SecurityUtils.canWriteCode(issue.getProject()))@@ -523,11 +517,11 @@ public class TodResource {@Path("/get-issue-comments")@GETpublic List<Map<String, Object>> getIssueComments(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String issueReference) {if (SecurityUtils.getUser() == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var issue = getIssue(currentProject, issueReference);return IssueHelper.getComments(issue);}@@ -536,13 +530,13 @@ public class TodResource {@Consumes(MediaType.TEXT_PLAIN)@POSTpublic Map<String, Object> addIssueComment(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String issueReference,@NotNull String commentContent) {if (SecurityUtils.getUser() == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var issue = getIssue(currentProject, issueReference);var comment = new IssueComment();comment.setIssue(issue);@@ -561,29 +555,36 @@ public class TodResource {@Path("/create-issue")@POSTpublic Map<String, Object> createIssue(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("project") String projectPath,@NotNull Map<String, @Valid Serializable> data) {var subject = SecurityUtils.getSubject();if (SecurityUtils.getUser(subject) == null)throw new UnauthenticatedException();- var projectContext = getProjectContext(projectPath, currentProjectPath);+ if (projectPath == null)+ projectPath = currentProjectPath;++ if (projectPath == null)+ throw new NotAcceptableException("Either project or current project should be specified");++ var project = getProject(projectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;Issue issue;try {- issue = IssueHelper.createIssue(subject, projectContext.project, data);+ issue = IssueHelper.createIssue(subject, project, data);} catch (ExplicitException e) {throw new NotAcceptableException(e.getMessage());}- return IssueHelper.getDetail(projectContext.currentProject, issue);+ return IssueHelper.getDetail(currentProject, issue);}@Path("/edit-issue")@POSTpublic Map<String, Object> editIssue(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String issueReference,@NotNull Map<String, Serializable> data) {var subject = SecurityUtils.getSubject();@@ -592,7 +593,7 @@ public class TodResource {if (user == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var issue = getIssue(currentProject, issueReference);@@ -657,7 +658,7 @@ public class TodResource {@Path("/change-issue-state")@POSTpublic Map<String, Object> changeIssueState(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String issueReference,@NotNull Map<String, Serializable> data) {var subject = SecurityUtils.getSubject();@@ -665,7 +666,7 @@ public class TodResource {if (user == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var issue = getIssue(currentProject, issueReference);IssueHelper.normalizeData(data);@@ -690,30 +691,27 @@ public class TodResource {@Path("/ensure-issue-branch")@POSTpublic String ensureIssueBranch(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String issueReference) {var subject = SecurityUtils.getSubject();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var issue = getIssue(currentProject, issueReference);- if (!issue.getProject().equals(currentProject))- throw new NotAcceptableException("Issue " + issueReference + " is not in current project");-return issueService.ensureBranch(subject, issue);}@Path("/link-issues")@GETpublic Map<String, Object> linkIssues(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("sourceReference") @NotNull String sourceReference,@QueryParam("linkName") @Nullable String linkName,@QueryParam("targetReference") @NotNull String targetReference) {if (SecurityUtils.getUser() == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var sourceIssue = getIssue(currentProject, sourceReference);var targetIssue = getIssue(currentProject, targetReference);@@ -750,13 +748,13 @@ public class TodResource {@Consumes(MediaType.TEXT_PLAIN)@POSTpublic Map<String, Object> logWork(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String issueReference,@QueryParam("spentHours") int spentHours, String comment) {if (SecurityUtils.getUser() == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var issue = getIssue(currentProject, issueReference);@@ -785,7 +783,7 @@ public class TodResource {@Path("/query-pull-requests")@GETpublic List<Map<String, Object>> queryPullRequests(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("project") String projectPath,@QueryParam("query") String query,@QueryParam("offset") int offset,@@ -794,9 +792,13 @@ public class TodResource {if (SecurityUtils.getUser(subject) == null)throw new UnauthenticatedException();- var projectContext = getProjectContext(projectPath, currentProjectPath);+ if (projectPath == null)+ projectPath = currentProjectPath;++ var project = projectPath != null? getProject(projectPath): null;+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;- if (!SecurityUtils.canReadCode(projectContext.project))+ if (project != null && !SecurityUtils.canReadCode(subject, project))throw new UnauthorizedException("Code read permission required to query pull requests");if (count > RestConstants.MAX_PAGE_SIZE)@@ -804,13 +806,13 @@ public class TodResource {EntityQuery<PullRequest> parsedQuery;if (query != null)- parsedQuery = PullRequestQuery.parse(projectContext.project, query, true);+ parsedQuery = PullRequestQuery.parse(project, query, true);elseparsedQuery = new PullRequestQuery();var summaries = new ArrayList<Map<String, Object>>();- for (var pullRequest : pullRequestService.query(subject, projectContext.project, parsedQuery, false, offset, count)) {- var summary = PullRequestHelper.getSummary(projectContext.currentProject, pullRequest, false);+ for (var pullRequest : pullRequestService.query(subject, project, parsedQuery, false, offset, count)) {+ var summary = PullRequestHelper.getSummary(currentProject, pullRequest, false);summary.put("link", urlService.urlFor(pullRequest, true));summaries.add(summary);}@@ -820,7 +822,7 @@ public class TodResource {@Path("/query-builds")@GETpublic List<Map<String, Object>> queryBuilds(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("project") String projectPath,@QueryParam("query") String query,@QueryParam("offset") int offset,@@ -829,20 +831,24 @@ public class TodResource {if (SecurityUtils.getUser(subject) == null)throw new UnauthenticatedException();- var projectContext = getProjectContext(projectPath, currentProjectPath);+ if (projectPath == null)+ projectPath = currentProjectPath;++ var project = projectPath != null? getProject(projectPath): null;+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;if (count > RestConstants.MAX_PAGE_SIZE)throw new NotAcceptableException("Count should not be greater than " + RestConstants.MAX_PAGE_SIZE);EntityQuery<Build> parsedQuery;if (query != null)- parsedQuery = BuildQuery.parse(projectContext.project, query, true, true);+ parsedQuery = BuildQuery.parse(project, query, true, true);elseparsedQuery = new BuildQuery();var summaries = new ArrayList<Map<String, Object>>();- for (var build : buildService.query(subject, projectContext.project, parsedQuery, false, offset, count)) {- var summary = BuildHelper.getSummary(projectContext.currentProject, build);+ for (var build : buildService.query(subject, project, parsedQuery, false, offset, count)) {+ var summary = BuildHelper.getSummary(currentProject, build);summary.put("link", urlService.urlFor(build, true));summaries.add(summary);}@@ -852,12 +858,12 @@ public class TodResource {@Path("/get-build")@GETpublic Map<String, Object> getBuildDetail(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String buildReference) {if (SecurityUtils.getUser() == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var build = getBuild(currentProject, buildReference);return BuildHelper.getDetail(currentProject, build);}@@ -865,14 +871,14 @@ public class TodResource {@Path("/get-build-code-problems")@GETpublic List<Map<String, Object>> getBuildCodeProblems(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String buildReference,@QueryParam("reportName") @NotNull String reportName,@QueryParam("severityLevel") @NotNull CodeProblem.Severity severityLevel) {if (SecurityUtils.getUser() == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var build = getBuild(currentProject, buildReference);if (!SecurityUtils.canAccessReport(build, reportName))@@ -911,14 +917,14 @@ public class TodResource {@Path("/get-build-unit-test-report")@GETpublic Response getBuildUnitTestReport(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String buildReference,@QueryParam("reportName") @NotNull String reportName,@QueryParam("artifactPath") String artifactPath) {if (SecurityUtils.getUser() == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var build = getBuild(currentProject, buildReference);if (!SecurityUtils.canAccessReport(build, reportName))@@ -981,12 +987,12 @@ public class TodResource {@Path("/get-previous-successful-similar-build")@GETpublic Map<String, Object> getPreviousSuccessfulSimilarBuild(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String buildReference) {if (SecurityUtils.getUser() == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var build = getBuild(currentProject, buildReference);@@ -1000,13 +1006,13 @@ public class TodResource {@Path("/get-pull-request")@GETpublic Map<String, Object> getPullRequestDetail(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String pullRequestReference,@QueryParam("forWrite") Boolean forWrite) {if (SecurityUtils.getUser() == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var pullRequest = getPullRequest(currentProject, pullRequestReference);if (forWrite != null && forWrite) {@@ -1023,12 +1029,12 @@ public class TodResource {@Path("/get-pull-request-comments")@GETpublic List<Map<String, Object>> getPullRequestComments(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String pullRequestReference) {if (SecurityUtils.getUser() == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var pullRequest = getPullRequest(currentProject, pullRequestReference);return PullRequestHelper.getComments(pullRequest);@@ -1037,12 +1043,12 @@ public class TodResource {@Path("/get-pull-request-code-comments")@GETpublic List<Map<String, Object>> getPullRequestCodeComments(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String pullRequestReference) {if (SecurityUtils.getUser() == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var pullRequest = getPullRequest(currentProject, pullRequestReference);@@ -1052,12 +1058,13 @@ public class TodResource {@Path("/get-pull-request-builds")@GETpublic List<Map<String, Object>> getPullRequestBuilds(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String pullRequestReference) {if (SecurityUtils.getUser() == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;+var pullRequest = getPullRequest(currentProject, pullRequestReference);return PullRequestHelper.getBuilds(currentProject, pullRequest);}@@ -1065,13 +1072,13 @@ public class TodResource {@Path("/get-pull-request-patch-info")@GETpublic Map<String, String> getPullRequestPatchInfo(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String pullRequestReference) {var user = SecurityUtils.getUser();if (user == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var pullRequest = getPullRequest(currentProject, pullRequestReference);@@ -1101,7 +1108,7 @@ public class TodResource {@Path("/create-pull-request")@POSTpublic Map<String, Object> createPullRequest(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("targetProject") String targetProjectPath,@QueryParam("sourceProject") String sourceProjectPath,@NotNull Map<String, Serializable> data) {@@ -1203,7 +1210,7 @@ public class TodResource {@GET@Nullablepublic String getPullRequestTitleAndDescriptionRequirement(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("targetProject") String targetProjectPath,@QueryParam("sourceProject") String sourceProjectPath,@QueryParam("targetBranch") String targetBranch,@@ -1239,7 +1246,7 @@ public class TodResource {@GET@Nullablepublic String getPullRequestCommitMessageRequirement(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("targetProject") String targetProjectPath,@QueryParam("sourceProject") String sourceProjectPath,@QueryParam("targetBranch") String targetBranch,@@ -1254,7 +1261,7 @@ public class TodResource {}private CreatePullRequestEssentialInfo getCreatePullRequestEssentialInfo(- String currentProjectPath,+ @Nullable String currentProjectPath,@Nullable String targetProjectPath,@Nullable String sourceProjectPath,@Nullable String targetBranch,@@ -1263,7 +1270,7 @@ public class TodResource {if (user == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;Project sourceProject;if (sourceProjectPath == null)@@ -1271,6 +1278,9 @@ public class TodResource {elsesourceProject = getProject(sourceProjectPath);+ if (sourceProject == null)+ throw new NotAcceptableException("Either source project or current project should be specified");+if (!SecurityUtils.canReadCode(sourceProject))throw new UnauthorizedException("No permission to read code of source project: " + sourceProjectPath);@@ -1282,6 +1292,7 @@ public class TodResource {if (targetProject == null)targetProject = sourceProject;}+if (!SecurityUtils.canReadCode(targetProject))throw new UnauthorizedException("No permission to read code of target project: " + targetProjectPath);@@ -1302,7 +1313,7 @@ public class TodResource {return info;}- private PullRequest getPullRequest(Project currentProject, String referenceString) {+ private PullRequest getPullRequest(@Nullable Project currentProject, String referenceString) {var requestReference = PullRequestReference.of(referenceString, currentProject);var request = pullRequestService.find(requestReference.getProject(), requestReference.getNumber());if (request != null) {@@ -1318,13 +1329,14 @@ public class TodResource {@Path("/edit-pull-request")@POSTpublic Map<String, Object> editPullRequest(- @QueryParam("currentProject") @NotNull String currentProjectPath,- @QueryParam("reference") @NotNull String pullRequestReference, @NotNull Map<String, Serializable> data) {+ @QueryParam("currentProject") String currentProjectPath,+ @QueryParam("reference") @NotNull String pullRequestReference,+ @NotNull Map<String, Serializable> data) {var user = SecurityUtils.getUser();if (user == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var request = getPullRequest(currentProject, pullRequestReference);@@ -1453,14 +1465,14 @@ public class TodResource {@Consumes(MediaType.TEXT_PLAIN)@POSTpublic Map<String, Object> approvePullRequest(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String pullRequestReference,String comment) {var user = SecurityUtils.getUser();if (user == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var pullRequest = getPullRequest(currentProject, pullRequestReference);pullRequestReviewService.review(user, pullRequest, true, trimToNull(comment));@@ -1471,14 +1483,14 @@ public class TodResource {@Consumes(MediaType.TEXT_PLAIN)@POSTpublic Map<String, Object> requestChangesOnPullRequest(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String pullRequestReference,String comment) {var user = SecurityUtils.getUser();if (user == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var pullRequest = getPullRequest(currentProject, pullRequestReference);pullRequestReviewService.review(user, pullRequest, false, trimToNull(comment));@@ -1489,14 +1501,14 @@ public class TodResource {@Consumes(MediaType.TEXT_PLAIN)@POSTpublic Map<String, Object> mergePullRequest(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String pullRequestReference,String commitMessage) {var user = SecurityUtils.getUser();if (user == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var pullRequest = getPullRequest(currentProject, pullRequestReference);if (!SecurityUtils.canWriteCode(user.asSubject(), pullRequest.getProject()))@@ -1513,14 +1525,14 @@ public class TodResource {@Consumes(MediaType.TEXT_PLAIN)@POSTpublic Map<String, Object> discardPullRequest(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String pullRequestReference,String comment) {var user = SecurityUtils.getUser();if (user == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var pullRequest = getPullRequest(currentProject, pullRequestReference);if (!SecurityUtils.canModifyPullRequest(pullRequest))@@ -1534,14 +1546,14 @@ public class TodResource {@Consumes(MediaType.TEXT_PLAIN)@POSTpublic Map<String, Object> reopenPullRequest(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String pullRequestReference,String comment) {var user = SecurityUtils.getUser();if (user == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var pullRequest = getPullRequest(currentProject, pullRequestReference);if (!SecurityUtils.canModifyPullRequest(pullRequest))@@ -1555,14 +1567,14 @@ public class TodResource {@Consumes(MediaType.TEXT_PLAIN)@POSTpublic Map<String, Object> deletePullRequestSourceBranch(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String pullRequestReference,String comment) {var user = SecurityUtils.getUser();if (user == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var pullRequest = getPullRequest(currentProject, pullRequestReference);if (!SecurityUtils.canModifyPullRequest(pullRequest)@@ -1578,14 +1590,14 @@ public class TodResource {@Consumes(MediaType.TEXT_PLAIN)@POSTpublic Map<String, Object> restorePullRequestSourceBranch(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String pullRequestReference,String comment) {var user = SecurityUtils.getUser();if (user == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var pullRequest = getPullRequest(currentProject, pullRequestReference);if (!SecurityUtils.canModifyPullRequest(pullRequest)@@ -1601,13 +1613,13 @@ public class TodResource {@Consumes(MediaType.TEXT_PLAIN)@POSTpublic Map<String, Object> addPullRequestComment(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String pullRequestReference,@NotNull String commentContent) {if (SecurityUtils.getUser() == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var pullRequest = getPullRequest(currentProject, pullRequestReference);var comment = new PullRequestComment();@@ -1628,7 +1640,7 @@ public class TodResource {@Consumes(MediaType.TEXT_PLAIN)@POSTpublic Map<String, Object> addPullRequestCodeComment(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,@QueryParam("reference") @NotNull String pullRequestReference,@QueryParam("filePath") @NotNull String filePath,@QueryParam("fromLineNumber") int fromLineNumber, // index starts from 1@@ -1638,7 +1650,7 @@ public class TodResource {if (user == null)throw new UnauthenticatedException();- var currentProject = getProject(currentProjectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var pullRequest = getPullRequest(currentProject, pullRequestReference);var comment = PullRequestHelper.addCodeComment(pullRequest, user, filePath, fromLineNumber, toLineNumber, commentContent);@@ -1675,7 +1687,8 @@ public class TodResource {@Path("/run-job")@POSTpublic Map<String, Object> runJob(- @QueryParam("currentProject") @NotNull String currentProjectPath,+ @QueryParam("currentProject") String currentProjectPath,+ @QueryParam("project") String projectPath,@NotNull Map<String, @Valid Serializable> data) {var subject = SecurityUtils.getSubject();var user = SecurityUtils.getUser(subject);@@ -1683,13 +1696,20 @@ public class TodResource {if (user == null)throw new UnauthenticatedException();- var project = getProject(currentProjectPath);+ if (projectPath == null)+ projectPath = currentProjectPath;++ if (projectPath == null)+ throw new NotAcceptableException("Either project or current project should be specified");++ var project = getProject(projectPath);+ var currentProject = currentProjectPath != null? getProject(currentProjectPath): null;var jobName = trimToNull((String)data.get("jobName"));if (jobName == null)throw new NotAcceptableException("Job name is required");- if (!SecurityUtils.canRunJob(subject, project, jobName))+ if (!SecurityUtils.canRunJob(subject, project, jobName))throw new UnauthorizedException();String refName;@@ -1716,13 +1736,11 @@ public class TodResource {if (paramData instanceof List) {params = new HashMap<String, List<String>>();List<String> paramPairs = (List<String>) paramData;- if (paramPairs != null) {- for (var paramPair: paramPairs) {- var paramName = trimToNull(StringUtils.substringBefore(paramPair, "="));- var paramValue = trimToNull(StringUtils.substringAfter(paramPair, "="));- if (paramName != null && paramValue != null)- params.computeIfAbsent(paramName, k -> new ArrayList<>()).add(paramValue);- }+ for (var paramPair : paramPairs) {+ var paramName = trimToNull(StringUtils.substringBefore(paramPair, "="));+ var paramValue = trimToNull(StringUtils.substringAfter(paramPair, "="));+ if (paramName != null && paramValue != null)+ params.computeIfAbsent(paramName, k -> new ArrayList<>()).add(paramValue);}} else if (paramData instanceof Map) {params = (Map<String, List<String>>) paramData;@@ -1734,12 +1752,12 @@ public class TodResource {if (reason == null)throw new NotAcceptableException("Reason is required");- var build = jobService.submit(user, project, ObjectId.fromString(commitHash), jobName,+ var build = jobService.submit(user, project, ObjectId.fromString(commitHash), jobName,params, refName, null, null, reason);if (build.isFinished())jobService.resubmit(user, build, reason);- var summary = BuildHelper.getSummary(project, build);+ var summary = BuildHelper.getSummary(currentProject, build);summary.put("id", build.getId());return summary;}@@ -1796,7 +1814,7 @@ public class TodResource {}}- private Build getBuild(Project currentProject, String referenceString) {+ private Build getBuild(@Nullable Project currentProject, String referenceString) {var buildReference = BuildReference.of(referenceString, currentProject);var build = buildService.find(buildReference.getProject(), buildReference.getNumber());if (build != null) {@@ -1815,13 +1833,6 @@ public class TodResource {}}- private static class ProjectContext {-- Project project;-- Project currentProject;- }-private static class VersionInfo {@SuppressWarnings("unused")@@ -1834,6 +1845,7 @@ public class TodResource {private static class CreatePullRequestEssentialInfo {+ @NullableProject currentProject;ProjectAndBranch target;