public Response apply()

in java/com/google/gerrit/plugins/codeowners/restapi/PutCodeOwnerProjectConfig.java [101:273]


  public Response<CodeOwnerProjectConfigInfo> apply(
      ProjectResource projectResource, CodeOwnerProjectConfigInput input)
      throws RestApiException, PermissionBackendException, IOException, ConfigInvalidException {
    // This REST endpoint requires the caller to be a project owner.
    permissionBackend
        .currentUser()
        .project(projectResource.getNameKey())
        .check(ProjectPermission.WRITE_CONFIG);

    try (Repository repo = repoManager.openRepository(projectResource.getNameKey());
        MetaDataUpdate metaDataUpdate =
            metaDataUpdateFactory.get().create(projectResource.getNameKey())) {
      metaDataUpdate.setMessage(String.format("Update %s configuration", pluginName));

      CodeOwnersProjectConfigFile codeOwnersProjectConfigFile = new CodeOwnersProjectConfigFile();
      codeOwnersProjectConfigFile.load(projectResource.getNameKey(), repo);
      Config codeOwnersConfig = codeOwnersProjectConfigFile.getConfig();

      if (input.disabled != null) {
        codeOwnersConfig.setBoolean(
            SECTION_CODE_OWNERS, /* subsection= */ null, KEY_DISABLED, input.disabled);
      }

      if (input.disabledBranches != null) {
        codeOwnersConfig.setStringList(
            SECTION_CODE_OWNERS,
            /* subsection= */ null,
            KEY_DISABLED_BRANCH,
            input.disabledBranches);
      }

      if (input.fileExtension != null) {
        codeOwnersConfig.setString(
            SECTION_CODE_OWNERS, /* subsection= */ null, KEY_FILE_EXTENSION, input.fileExtension);
      }

      if (input.requiredApproval != null) {
        if (input.requiredApproval.isEmpty()) {
          codeOwnersConfig.unset(
              SECTION_CODE_OWNERS, /* subsection= */ null, KEY_REQUIRED_APPROVAL);
        } else {
          codeOwnersConfig.setString(
              SECTION_CODE_OWNERS,
              /* subsection= */ null,
              KEY_REQUIRED_APPROVAL,
              input.requiredApproval);
        }
      }

      if (input.overrideApprovals != null) {
        codeOwnersConfig.setStringList(
            SECTION_CODE_OWNERS,
            /* subsection= */ null,
            KEY_OVERRIDE_APPROVAL,
            input.overrideApprovals);
      }

      if (input.fallbackCodeOwners != null) {
        codeOwnersConfig.setEnum(
            SECTION_CODE_OWNERS,
            /* subsection= */ null,
            KEY_FALLBACK_CODE_OWNERS,
            input.fallbackCodeOwners);
      }

      if (input.globalCodeOwners != null) {
        codeOwnersConfig.setStringList(
            SECTION_CODE_OWNERS,
            /* subsection= */ null,
            KEY_GLOBAL_CODE_OWNER,
            input.globalCodeOwners);
      }

      if (input.exemptedUsers != null) {
        codeOwnersConfig.setStringList(
            SECTION_CODE_OWNERS, /* subsection= */ null, KEY_EXEMPTED_USER, input.exemptedUsers);
      }

      if (input.mergeCommitStrategy != null) {
        codeOwnersConfig.setEnum(
            SECTION_CODE_OWNERS,
            /* subsection= */ null,
            KEY_MERGE_COMMIT_STRATEGY,
            input.mergeCommitStrategy);
      }

      if (input.implicitApprovals != null) {
        codeOwnersConfig.setBoolean(
            SECTION_CODE_OWNERS,
            /* subsection= */ null,
            KEY_ENABLE_IMPLICIT_APPROVALS,
            input.implicitApprovals);
      }

      if (input.overrideInfoUrl != null) {
        codeOwnersConfig.setString(
            SECTION_CODE_OWNERS,
            /* subsection= */ null,
            KEY_OVERRIDE_INFO_URL,
            input.overrideInfoUrl);
      }

      if (input.invalidCodeOwnerConfigInfoUrl != null) {
        codeOwnersConfig.setString(
            SECTION_CODE_OWNERS,
            /* subsection= */ null,
            KEY_INVALID_CODE_OWNER_CONFIG_INFO_URL,
            input.invalidCodeOwnerConfigInfoUrl);
      }

      if (input.readOnly != null) {
        codeOwnersConfig.setBoolean(
            SECTION_CODE_OWNERS, /* subsection= */ null, KEY_READ_ONLY, input.readOnly);
      }

      if (input.exemptPureReverts != null) {
        codeOwnersConfig.setBoolean(
            SECTION_CODE_OWNERS,
            /* subsection= */ null,
            KEY_EXEMPT_PURE_REVERTS,
            input.exemptPureReverts);
      }

      if (input.enableValidationOnCommitReceived != null) {
        codeOwnersConfig.setEnum(
            SECTION_CODE_OWNERS,
            /* subsection= */ null,
            KEY_ENABLE_VALIDATION_ON_COMMIT_RECEIVED,
            input.enableValidationOnCommitReceived);
      }

      if (input.enableValidationOnSubmit != null) {
        codeOwnersConfig.setEnum(
            SECTION_CODE_OWNERS,
            /* subsection= */ null,
            KEY_ENABLE_VALIDATION_ON_SUBMIT,
            input.enableValidationOnSubmit);
      }

      if (input.rejectNonResolvableCodeOwners != null) {
        codeOwnersConfig.setBoolean(
            SECTION_CODE_OWNERS,
            /* subsection= */ null,
            KEY_REJECT_NON_RESOLVABLE_CODE_OWNERS,
            input.rejectNonResolvableCodeOwners);
      }

      if (input.rejectNonResolvableImports != null) {
        codeOwnersConfig.setBoolean(
            SECTION_CODE_OWNERS,
            /* subsection= */ null,
            KEY_REJECT_NON_RESOLVABLE_IMPORTS,
            input.rejectNonResolvableImports);
      }

      if (input.maxPathsInChangeMessages != null) {
        codeOwnersConfig.setInt(
            SECTION_CODE_OWNERS,
            /* subsection= */ null,
            KEY_MAX_PATHS_IN_CHANGE_MESSAGES,
            input.maxPathsInChangeMessages);
      }

      validateConfig(projectResource.getProjectState(), codeOwnersConfig);

      codeOwnersProjectConfigFile.commit(metaDataUpdate);
      projectCache.evictAndReindex(projectResource.getNameKey());
    }

    CodeOwnerProjectConfigInfo updatedCodeOwnerProjectConfigInfo =
        codeOwnerProjectConfigJson.format(projectResource);
    return Response.created(updatedCodeOwnerProjectConfigInfo);
  }