// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.googlesource.gerrit.plugins.hooks.jira;

import java.io.IOException;
import java.rmi.RemoteException;

import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gerrit.pgm.init.AllProjectsConfig;
import com.google.gerrit.pgm.init.AllProjectsNameOnInitProvider;
import com.google.gerrit.pgm.init.Section;
import com.google.gerrit.pgm.util.ConsoleUI;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.googlesource.gerrit.plugins.hooks.its.InitIts;
import com.googlesource.gerrit.plugins.hooks.validation.ItsAssociationPolicy;

import org.eclipse.jgit.errors.ConfigInvalidException;

/** Initialize the GitRepositoryManager configuration section. */
@Singleton
class InitJira extends InitIts {
  private static final String COMMENT_LINK_SECTION = "commentLink";
  private final String pluginName;
  private final Section.Factory sections;
  private Section jira;
  private Section jiraComment;
  private String jiraUrl;
  private String jiraUsername;
  private String jiraPassword;

  @Inject
  InitJira(@PluginName String pluginName, ConsoleUI ui,
      Section.Factory sections, AllProjectsConfig allProjectsConfig,
      AllProjectsNameOnInitProvider allProjects) {
    super(pluginName, "Jira", ui, allProjectsConfig, allProjects);
    this.pluginName = pluginName;
    this.sections = sections;
  }

  @Override
  public void run() throws IOException, ConfigInvalidException {
    super.run();

    this.jira = sections.get(pluginName, null);
    this.jiraComment = sections.get(COMMENT_LINK_SECTION, pluginName);

    ui.message("\n");
    ui.header("Jira connectivity");

    do {
      enterJiraConnectivity();
    } while (jiraUrl != null
        && (isConnectivityRequested(jiraUrl) && !isJiraConnectSuccessful()));

    if (jiraUrl == null) {
      return;
    }

    ui.header("Jira issue-tracking association");
    jiraComment.string("Jira issue-Id regex", "match", "([A-Z]+-[0-9]+)");
    jiraComment.set("html",
        String.format("<a href=\"%s/browse/$1\">$1</a>", jiraUrl));
    jiraComment.select("Issue-id enforced in commit message", "association",
        ItsAssociationPolicy.SUGGESTED);
  }

  public void enterJiraConnectivity() {
    jiraUrl = jira.string("Jira URL (empty to skip)", "url", null);
    if (jiraUrl != null) {
      jiraUsername = jira.string("Jira username", "username", "");
      jiraPassword = jira.password("username", "password");
    }
  }

  private boolean isJiraConnectSuccessful() {
    ui.message("Checking Jira connectivity ... ");
    try {
      JiraClient jiraClient = new JiraClient(jiraUrl);
      JiraSession jiraToken =
          jiraClient.login(jiraUsername, jiraPassword);
      jiraClient.logout(jiraToken);
      ui.message("[OK]\n");
      return true;
    } catch (RemoteException e) {
      ui.message("*FAILED* (%s)\n", e.toString());
      return false;
    }
  }
}

@@ -715,16 +715,41 @@
 ception;
+%0Aimport java.util.Arrays;
 %0A%0Aimport
@@ -920,16 +920,61 @@
 ovider;%0A
+import com.google.gerrit.pgm.init.InitFlags;%0A
 import c
@@ -1571,16 +1571,49 @@
 ctions;%0A
+  private final InitFlags flags;%0A
   privat
@@ -1930,16 +1930,33 @@
 Projects
+, InitFlags flags
 ) %7B%0A    
@@ -2082,16 +2082,40 @@
 ctions;%0A
+    this.flags = flags;%0A
   %7D%0A%0A  @
@@ -2202,24 +2202,1135 @@
 per.run();%0A%0A
+    ui.message(%22%5Cn%22);%0A    ui.header(%22Jira connectivity%22);%0A%0A    if (!pluginName.equalsIgnoreCase(%22jira%22)%0A        && !flags.cfg.getSections().contains(pluginName)%0A        && flags.cfg.getSections().contains(%22jira%22)) %7B%0A      ui.message(%22A Jira configuration for the 'hooks-jira' plugin was found.%5Cn%22);%0A      if (ui.yesno(true, %22Copy it for the '%25s' plugin?%22, pluginName)) %7B%0A        for (String n : flags.cfg.getNames(%22jira%22)) %7B%0A          flags.cfg.setStringList(pluginName, null, n,%0A              Arrays.asList(flags.cfg.getStringList(%22jira%22, null, n)));%0A        %7D%0A        for (String n : flags.cfg.getNames(COMMENT_LINK_SECTION, %22jira%22)) %7B%0A          flags.cfg.setStringList(COMMENT_LINK_SECTION, pluginName, n,%0A              Arrays.asList(flags.cfg.getStringList(COMMENT_LINK_SECTION, %22jira%22, n)));%0A        %7D%0A%0A        if (ui.yesno(false, %22Remove configuration for 'hooks-jira' plugin?%22)) %7B%0A          flags.cfg.unsetSection(%22jira%22, null);%0A          flags.cfg.unsetSection(COMMENT_LINK_SECTION, %22jira%22);%0A        %7D%0A      %7D else %7B%0A        init();%0A      %7D%0A    %7D else %7B%0A      init();%0A    %7D%0A  %7D%0A%0A  private void init() %7B%0A
     this.jir
@@ -3441,67 +3441,8 @@
 );%0A%0A
-    ui.message(%22%5Cn%22);%0A    ui.header(%22Jira connectivity%22);%0A%0A
     

Add init step to copy configuration from the 'hooks-jira' plugin