Create new plugin ref-copy

Change-Id: If663f59a0190d37074dc09e430e1e11f9f7a8cd3
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ebbbd5f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+/.classpath
+/.project
+/.settings
diff --git a/BUILD b/BUILD
new file mode 100644
index 0000000..d870fbe
--- /dev/null
+++ b/BUILD
@@ -0,0 +1,20 @@
+load("//tools/bzl:plugin.bzl", "gerrit_plugin")
+load("//tools/bzl:js.bzl", "gerrit_js_bundle")
+
+gerrit_plugin(
+    name = "ref-copy",
+    srcs = glob(["src/main/java/**/*.java"]),
+    manifest_entries = [
+        "Gerrit-PluginName: ref-copy",
+        "Gerrit-Module: com.googlesource.gerrit.plugins.refcopy.Module",
+        "Implementation-Title: Ref Copy Plugin",
+    ],
+    resources = glob(["src/main/**/*"]),
+    resource_jars = [":ref-copy-ui"],
+)
+
+gerrit_js_bundle(
+    name = "ref-copy-ui",
+    srcs = glob(["ref-copy/*.js"]),
+    entry_point = "ref-copy/plugin.js",
+)
diff --git a/ref-copy/plugin.js b/ref-copy/plugin.js
new file mode 100644
index 0000000..6e3adc1
--- /dev/null
+++ b/ref-copy/plugin.js
@@ -0,0 +1,20 @@
+// Copyright (C) 2021 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.
+
+import './ref-copy';
+
+Gerrit.install(plugin => {
+  plugin.registerCustomComponent(
+      'change-metadata-item', 'ref-copy');
+});
diff --git a/ref-copy/ref-copy.js b/ref-copy/ref-copy.js
new file mode 100644
index 0000000..4ced2bd
--- /dev/null
+++ b/ref-copy/ref-copy.js
@@ -0,0 +1,51 @@
+// Copyright (C) 2021 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.
+
+import {htmlTemplate} from './ref-copy_html';
+
+const COPY_TIMEOUT_MS = 1000;
+
+class RefCopy extends Polymer.Element {
+
+  static get is() { return 'ref-copy'; }
+
+  static get template() { return htmlTemplate; }
+
+  static get properties() {
+    return {
+      revision: Object,
+    };
+  }
+
+  _handleInputTap(e) {
+    e.preventDefault();
+    Polymer.dom(e).rootTarget.select();
+  }
+
+  _copyToClipboard(e) {
+    const ref = this.$.input.value;
+    const copyBtn = e.target;
+    if (ref) {
+      navigator.clipboard.writeText(ref).then(() => {
+        copyBtn.innerText = 'done';
+        setTimeout(() => {
+          copyBtn.innerText = 'copy'}, COPY_TIMEOUT_MS);
+        }).catch(err => {
+          console.log('Failed to copy ref to clipboard', err);
+        })
+    }
+  }
+}
+
+customElements.define(RefCopy.is, RefCopy);
diff --git a/ref-copy/ref-copy_html.js b/ref-copy/ref-copy_html.js
new file mode 100644
index 0000000..d39c9e2
--- /dev/null
+++ b/ref-copy/ref-copy_html.js
@@ -0,0 +1,43 @@
+// Copyright (C) 2021 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.
+
+export const htmlTemplate = Polymer.html`
+<style>
+  .text {
+    min-width: 8em;
+    padding: var(--spacing-l);
+    text-transform: none;
+  }
+</style>
+<div class="text">
+  <iron-input
+      bind-value="{{revision.ref}}"
+      readonly>
+    <input
+        id="input"
+        is="iron-input"
+        type="text"
+        on-tap="_handleInputTap"
+        bind-value="{{revision.ref}}"
+        readonly/>
+  </iron-input>
+  <gr-button
+    id="button"
+    link
+    class="copyToClipboard"
+    on-tap="_copyToClipboard">
+    copy
+  </gr-button>
+</div>
+<gr-js-api-interface id="jsAPI"></gr-js-api-interface>`;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/refcopy/Module.java b/src/main/java/com/googlesource/gerrit/plugins/refcopy/Module.java
new file mode 100644
index 0000000..0f73e0e
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/refcopy/Module.java
@@ -0,0 +1,29 @@
+// Copyright (C) 2021 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.refcopy;
+
+import com.google.gerrit.extensions.annotations.Exports;
+import com.google.gerrit.extensions.config.DownloadCommand;
+import com.google.gerrit.extensions.registration.DynamicSet;
+import com.google.gerrit.extensions.webui.JavaScriptPlugin;
+import com.google.gerrit.extensions.webui.WebUiPlugin;
+import com.google.inject.AbstractModule;
+
+public class Module extends AbstractModule {
+  @Override
+  protected void configure() {
+    bind(DownloadCommand.class).annotatedWith(Exports.named("Ref")).to(RefCopyCommand.class);
+    DynamicSet.bind(binder(), WebUiPlugin.class).toInstance(new JavaScriptPlugin("ref-copy-ui.js"));
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/refcopy/RefCopyCommand.java b/src/main/java/com/googlesource/gerrit/plugins/refcopy/RefCopyCommand.java
new file mode 100644
index 0000000..aa0c425
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/refcopy/RefCopyCommand.java
@@ -0,0 +1,24 @@
+// Copyright (C) 2021 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.refcopy;
+
+import com.google.gerrit.extensions.config.DownloadCommand;
+import com.google.gerrit.extensions.config.DownloadScheme;
+
+public class RefCopyCommand extends DownloadCommand {
+  @Override
+  public String getCommand(DownloadScheme scheme, String project, String ref) {
+    return ref;
+  }
+}
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md
new file mode 100644
index 0000000..1016cf7
--- /dev/null
+++ b/src/main/resources/Documentation/about.md
@@ -0,0 +1,11 @@
+This is a convenience plugin that allows users to easily copy the change-ref of a change.
+
+It provides the following Gerrit UI enhancements.
+
+* Adds a ref copy link to the download menu.
+* Adds a copyable ref as a change-metadata-item.
+
+> Note: As this plugin is Polymer 3 compliant,
+it was designed to work for Gerrit v3.3 and newer [1]
+
+[1] https://www.gerritcodereview.com/3.4.html#html-plugin-support-is-removed
diff --git a/src/main/resources/Documentation/build.md b/src/main/resources/Documentation/build.md
new file mode 100644
index 0000000..eeb502d
--- /dev/null
+++ b/src/main/resources/Documentation/build.md
@@ -0,0 +1,37 @@
+Build
+=====
+
+This @PLUGIN@ plugin is built with Bazel.
+
+Clone (or link) this plugin to the `plugins` directory of Gerrit's source tree.
+
+Then issue
+
+```
+  bazel build plugins/@PLUGIN@
+```
+
+in the root of Gerrit's source tree to build
+
+The output is created in
+
+```
+  bazel-genfiles/plugins/@PLUGIN@/@PLUGIN@.jar
+```
+
+This project can be imported into the Eclipse IDE.
+Add the plugin name to the `CUSTOM_PLUGINS` set in
+Gerrit core in `tools/bzl/plugins.bzl`, and execute:
+
+```
+  ./tools/eclipse/project.py
+```
+
+To execute the tests run:
+
+```
+  bazel test plugins/@PLUGIN@:all
+```
+
+How to build the Gerrit Plugin API is described in the [Gerrit
+documentation](../../../Documentation/dev-bazel.html#_extension_and_plugin_api_jar_files).