You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2021/05/22 04:08:25 UTC

[tomee-site-generator] 06/08: Create a unique list of contributors from a list of duplicates

This is an automated email from the ASF dual-hosted git repository.

dblevins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomee-site-generator.git

commit 1e0eed4c9a0f95ec26ba7c53e30716342a54d97c
Author: David Blevins <da...@gmail.com>
AuthorDate: Fri May 21 20:01:07 2021 -0700

    Create a unique list of contributors from a list of duplicates
---
 .../org/apache/tomee/website/Configuration.java    |  2 +-
 .../tomee/website/contributors/Contributor.java    | 31 ++++++++
 .../website/contributors/ContributorData.java      | 15 ++++
 .../website/contributors/ContributorTest.java      | 86 ++++++++++++++++++++++
 4 files changed, 133 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/tomee/website/Configuration.java b/src/main/java/org/apache/tomee/website/Configuration.java
index d137778..9323bfe 100644
--- a/src/main/java/org/apache/tomee/website/Configuration.java
+++ b/src/main/java/org/apache/tomee/website/Configuration.java
@@ -89,7 +89,7 @@ public class Configuration {
                 new Source("https://github.com/eclipse-ee4j/websocket-api.git", "master", "websocket-api-ee9")
         };
 
-//        if (1 == 1) return new Source[0];
+        if (1 == 1) return new Source[0];
         return new Source[]{
 //                new Source("https://github.com/apache/tomee.git", "master", "tomee-8.0"),
                 new Source("https://github.com/apache/tomee.git", "master", "tomee-9.0").label("milestone").related(microProfile2).related(jakartaEE9).javadoc("^org.apache.(openejb|tomee).*"),
diff --git a/src/main/java/org/apache/tomee/website/contributors/Contributor.java b/src/main/java/org/apache/tomee/website/contributors/Contributor.java
index bdfa002..0ff60aa 100644
--- a/src/main/java/org/apache/tomee/website/contributors/Contributor.java
+++ b/src/main/java/org/apache/tomee/website/contributors/Contributor.java
@@ -19,6 +19,10 @@ package org.apache.tomee.website.contributors;
 import lombok.Builder;
 import lombok.Data;
 
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
 @Data
 @Builder
 public class Contributor {
@@ -27,4 +31,31 @@ public class Contributor {
     private String name;
     private String github;
     private String avatar;
+    private Stats stats;
+
+    public Contributor add(final Contributor that) {
+        return new Contributor(id, committer, name, github, avatar, stats.add(that.stats));
+    }
+
+    /**
+     * Take the specified list of Contributors, which may have duplicates, and
+     * combine each duplicate down to one Contributor instance while keeping a
+     * total of all their stats.
+     */
+    public static List<Contributor> unique(final List<Contributor> listWithDuplicates) {
+        final Map<String, List<Contributor>> map = listWithDuplicates.stream()
+                .collect(Collectors.groupingBy(Contributor::getName));
+
+        return map.values().stream()
+                .map(Contributor::reduce)
+                .collect(Collectors.toList());
+    }
+
+    private static Contributor reduce(final List<Contributor> instances) {
+        return instances.stream()
+                .reduce(Contributor::add)
+                .orElseThrow(IllegalStateException::new);
+    }
+
+
 }
diff --git a/src/main/java/org/apache/tomee/website/contributors/ContributorData.java b/src/main/java/org/apache/tomee/website/contributors/ContributorData.java
index 3ace899..4a6bfa3 100644
--- a/src/main/java/org/apache/tomee/website/contributors/ContributorData.java
+++ b/src/main/java/org/apache/tomee/website/contributors/ContributorData.java
@@ -48,6 +48,21 @@ public class ContributorData {
                 .orElse(new Stats(0, 0, 0));
     }
 
+    /**
+     * Convert the ContributorData to a Contributor instance
+     */
+    public Contributor asContributor() {
+        final ContributorData.Author author = this.getAuthor();
+
+        return Contributor.builder()
+                .id(author.getId() + "")
+                .name(author.getLogin())
+                .github("https://github.com/" + author.getLogin())
+                .avatar(author.getAvatar())
+                .stats(this.getStats())
+                .build();
+    }
+
     @Data
     @NoArgsConstructor
     @AllArgsConstructor
diff --git a/src/test/java/org/apache/tomee/website/contributors/ContributorTest.java b/src/test/java/org/apache/tomee/website/contributors/ContributorTest.java
new file mode 100644
index 0000000..51da5d1
--- /dev/null
+++ b/src/test/java/org/apache/tomee/website/contributors/ContributorTest.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.tomee.website.contributors;
+
+import org.apache.openejb.loader.IO;
+import org.junit.Test;
+
+import javax.json.bind.Jsonb;
+import javax.json.bind.JsonbBuilder;
+import java.io.IOException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import static org.junit.Assert.assertEquals;
+
+public class ContributorTest {
+
+    @Test
+    public void unique() throws IOException {
+        final URL resource = this.getClass().getClassLoader().getResource("contributor-data.json");
+        final String json = IO.slurp(resource);
+        final Jsonb jsonb = JsonbBuilder.create();
+        final ContributorData[] data = jsonb.fromJson(json, ContributorData[].class);
+
+        final List<ContributorData> list = new ArrayList<>(Arrays.asList(data));
+        list.add(data[3]); // djencks
+        list.add(data[5]); // rzo1
+        list.add(data[5]); // rzo1
+        list.add(data[5]); // rzo1
+        list.add(data[10]); // dblevins
+
+        final List<Contributor> duplicates = list.stream()
+                .map(ContributorData::asContributor)
+                .collect(Collectors.toList());
+
+        final List<Contributor> unique = Contributor.unique(duplicates);
+        final Map<String, Contributor> map = unique.stream().collect(Collectors.toMap(Contributor::getName, Function.identity()));
+        
+        assertEquals(17, duplicates.size());
+        assertEquals(12, unique.size());
+        assertEquals(12, map.size());
+
+        final Contributor djencks = map.get("djencks");
+        assertEquals("djencks", djencks.getName());
+        assertEquals(72, djencks.getStats().getCommits());
+        assertEquals(70486, djencks.getStats().getLinesAdded());
+        assertEquals(50040, djencks.getStats().getLinesRemoved());
+
+        final Contributor rzo1 = map.get("rzo1");
+        assertEquals("rzo1", rzo1.getName());
+        assertEquals(48, rzo1.getStats().getCommits());
+        assertEquals(5952, rzo1.getStats().getLinesAdded());
+        assertEquals(4164, rzo1.getStats().getLinesRemoved());
+
+        final Contributor dblevins = map.get("dblevins");
+        assertEquals("dblevins", dblevins.getName());
+        assertEquals(158, dblevins.getStats().getCommits());
+        assertEquals(88898, dblevins.getStats().getLinesAdded());
+        assertEquals(139256, dblevins.getStats().getLinesRemoved());
+
+        final Contributor cesarhernandezgt = map.get("cesarhernandezgt");
+        assertEquals("cesarhernandezgt", cesarhernandezgt.getName());
+        assertEquals(14, cesarhernandezgt.getStats().getCommits());
+        assertEquals(1073, cesarhernandezgt.getStats().getLinesAdded());
+        assertEquals(356, cesarhernandezgt.getStats().getLinesRemoved());
+    }
+}