Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bots: add census bot #796

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter
Filter file types
Beta Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.

Always

Just for now

@@ -0,0 +1,47 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

module {
name = 'org.openjdk.skara.bots.censussync'
test {
requires 'org.junit.jupiter.api'
requires 'org.openjdk.skara.test'
opens 'org.openjdk.skara.bots.censussync' to 'org.junit.platform.commons'
}
}

dependencies {
implementation project(':bot')
implementation project(':ci')
implementation project(':vcs')
implementation project(':host')
implementation project(':forge')
implementation project(':issuetracker')
implementation project(':census')
implementation project(':process')
implementation project(':json')
implementation project(':network')
implementation project(':storage')

testImplementation project(':test')
}
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
module org.openjdk.skara.bots.censusssync {
requires org.openjdk.skara.vcs;
requires org.openjdk.skara.host;
requires org.openjdk.skara.network;
requires org.openjdk.skara.bot;
requires org.openjdk.skara.process;
requires org.openjdk.skara.storage;
requires java.logging;

provides org.openjdk.skara.bot.BotFactory with org.openjdk.skara.bots.censussync.CensusSyncBotFactory;
}
@@ -0,0 +1,141 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package org.openjdk.skara.bots.censussync;

import org.openjdk.skara.bot.*;
import org.openjdk.skara.census.Census;
import org.openjdk.skara.forge.*;
import org.openjdk.skara.vcs.*;
import org.openjdk.skara.vcs.openjdk.convert.*;
import org.openjdk.skara.storage.StorageBuilder;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
import java.net.URI;
import java.net.URLEncoder;
import java.util.logging.Logger;
import java.time.*;
import java.time.format.*;

public class CensusSyncBot implements Bot, WorkItem {
private static final Logger log = Logger.getLogger("org.openjdk.skara.bots");;
private final HostedRepository from;
private final HostedRepository to;
private final int version;
private Hash last;

CensusSyncBot(HostedRepository from, HostedRepository to, int version) {
this.from = from;
this.to = to;
this.version = version;
this.last = null;
}

@Override
public boolean concurrentWith(WorkItem other) {
if (!(other instanceof CensusSyncBot)) {
return true;
}
var o = (CensusSyncBot) other;
return !o.to.equals(to);
}

@Override
public String toString() {
return "CensusSyncBot(" + from.name() + "->" + to.name() + "@" + version + ")";
}

@Override
public List<WorkItem> getPeriodicItems() {
return List.of(this);
}

@Override
public Collection<WorkItem> run(Path scratch) {
try {
var fromDir = scratch.resolve("from.git");
var fromRepo = Repository.materialize(fromDir, from.url(), "master");
if (last != null && last.equals(fromRepo.head())) {
// Nothing to do
return List.of();
}

var census = Census.parse(fromDir);

var toDir = scratch.resolve("to.git");
var toRepo = Repository.materialize(toDir, to.url(), "master");

var censusXML = toRepo.root().resolve("census.xml");
if (!Files.exists(censusXML)) {
Files.createFile(censusXML);
}
try (var file = new PrintWriter(Files.newBufferedWriter(censusXML))) {
file.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
var formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'H:m:sZ");
file.println("<census time=\"" + ZonedDateTime.now().format(formatter) + "\">");
for (var contributor : census.contributors()) {
file.println("<person name=\"" + contributor.username() + "\">");
file.println(" <full-name>" + contributor.fullName().orElse("") + "</full-name>");
file.println("</person>");
}
for (var group : census.groups()) {
file.println("<group name=\"" + group.name() + "\">");
file.println(" <full-name>" + group.fullName() + "</full-name>");
file.println(" <person ref=\"" + group.lead().username() + "\" role=\"lead\" />");
for (var member : group.members()) {
if (!member.username().equals(group.lead().username())) {
file.println(" <person ref=\"" + member.username() + "\" />");
}
}
file.println("</group>");
}
for (var project : census.projects()) {
file.println("<project name=\"" + project.name() + "\">");
file.println(" <full-name>" + project.fullName() + "</full-name>");
file.println(" <sponsor ref=\"" + project.sponsor().name() + "\" />");

var roles = project.roles(version);
for (var role : roles.keySet()) {
for (var member : roles.get(role)) {
file.println(" <person role=\"" + role + "\" ref=\"" + member.username() + "\" />");
}
}

file.println("</project>");
}
file.println("</census>");
}
toRepo.add(censusXML);
var head = toRepo.commit("Updated census.xml", "duke", "duke@openjdk.org");
toRepo.push(head, to.url(), "master", false);
last = fromRepo.head();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return List.of();
}
}
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.skara.bots.censussync;

import org.openjdk.skara.bot.*;
import org.openjdk.skara.vcs.*;
import org.openjdk.skara.vcs.openjdk.convert.Mark;

import java.util.*;
import java.net.URI;
import java.nio.file.Path;
import java.util.logging.Logger;

public class CensusSyncBotFactory implements BotFactory {
private static final Logger log = Logger.getLogger("org.openjdk.skara.bots");

@Override
public String name() {
return "censussync";
}

@Override
public List<Bot> create(BotConfiguration configuration) {
var bots = new ArrayList<Bot>();
var specific = configuration.specific();
for (var sync : specific.get("sync").asArray()) {
var from = configuration.repository(sync.get("from").asString());
var to = configuration.repository(sync.get("to").asString());
var version = sync.get("version").asInt();
bots.add(new CensusSyncBot(from, to, version));
}
return bots;
}
}
@@ -50,6 +50,7 @@ dependencies {
implementation project(':bots:forward')
implementation project(':bots:bridgekeeper')
implementation project(':bots:checkout')
implementation project(':bots:censussync')
implementation project(':ci')
implementation project(':vcs')
implementation project(':jcheck')
@@ -110,6 +110,16 @@ public boolean isAuthor(String username, int version) {
return result;
}

public Map<String, Set<Contributor>> roles(int version) {
var res = new HashMap<String, Set<Contributor>>();
var lead = lead(version);
res.put("lead", lead == null ? Set.of() : Set.of(lead));
res.put("reviewer", members(reviewers, version));
res.put("comitter", members(committers, version));
res.put("author", members(authors, version));
return res;
}

public Contributor lead(int version) {
var leadersAtVersion = members(leaders, version);
if (leadersAtVersion.size() != 1) {
@@ -47,6 +47,7 @@ include 'issuetracker'
include 'version'

include 'bots:bridgekeeper'
include 'bots:censussync'
include 'bots:checkout'
include 'bots:cli'
include 'bots:csr'
ProTip! Use n and p to navigate between commits in a pull request.