Java tutorial
package org.jenkinsci.infra.webhook.checkers; import com.atlassian.jira.rest.client.api.JiraRestClient; import com.atlassian.jira.rest.client.api.domain.Issue; import org.apache.commons.lang.StringUtils; import org.apache.maven.model.Model; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.project.MavenProject; import org.jenkinsci.infra.webhook.Checker; import org.jenkinsci.infra.webhook.JiraHelper; import org.jenkinsci.infra.webhook.VerificationMessage; import org.kohsuke.github.GHContent; import org.kohsuke.github.GHRepository; import org.kohsuke.github.GitHub; import javax.xml.bind.DatatypeConverter; import java.io.FileNotFoundException; import java.io.InputStreamReader; import java.util.HashSet; import java.util.regex.Matcher; import static org.jenkinsci.infra.webhook.VerificationMessage.Severity; /** * Created by acearl on 2/17/2017. */ public class MavenChecker extends Checker { @Override public boolean check(Issue issue, HashSet<VerificationMessage> hostingIssues) { try { GitHub gh = GitHub.connect(); String forkFrom = JiraHelper.getFieldValueByName(issue, "Repository URL"); String forkTo = JiraHelper.getFieldValueByName(issue, "New Repository Name"); if (!StringUtils.isBlank(forkFrom)) { if (forkFrom.endsWith(".git")) { forkFrom = forkFrom.substring(0, forkFrom.length() - 4); hostingIssues.add(new VerificationMessage(Severity.REQUIRED, "Your repository URL has '.git' at the end, the GitHub API doesn't support " + "repository names with .git at the end, please remove it.")); } Matcher m = GITHUB_URL_PATTERN.matcher(forkFrom); if (m.matches()) { String owner = m.group(1); String repoName = m.group(2); GHRepository repo = gh.getRepository(owner + "/" + repoName); try { GHContent pomXml = repo.getFileContent("pom.xml"); // the pom.xml file should be text, so we can just use .Content InputStreamReader reader = new InputStreamReader(pomXml.read()); DatatypeConverter.parseBase64Binary(reader.) MavenXpp3Reader xpp3Reader = new MavenXpp3Reader(); Model model = xpp3Reader.read(reader); MavenProject project = new MavenProject(model); if (!StringUtils.isBlank(forkTo)) { checkArtifactId(project, forkTo, hostingIssues); } checkParentInfo(project, hostingIssues); checkName(project, hostingIssues); checkLicenses(project, hostingIssues); } catch (Exception e) { e.printStackTrace(); } } } else { hostingIssues.add(new VerificationMessage(Severity.REQUIRED, INVALID_FORK_FROM, forkFrom)); } } catch (Exception e) { return false; } return true; } private void checkArtifactId(MavenProject project, String forkTo, HashSet<VerificationMessage> hostingIssues) { String artifactId = project.getArtifactId(); if (!StringUtils.isBlank(artifactId)) { if (!artifactId.equalsIgnoreCase(forkTo.replace("-plugin", ""))) { hostingIssues.add(new VerificationMessage(Severity.REQUIRED, "The <artifactId> from the pom.xml (%s) is incorrect, it should be %s (new repository name with -plugin removed)", artifactId, forkTo.replace("-plugin", ""))); } if (artifactId.toLowerCase().contains("jenkins") || artifactId.toLowerCase().contains("hudson")) { hostingIssues.add(new VerificationMessage(Severity.REQUIRED, "The <artifactId> from the pom.xml (%s) MUST not contain \"Jenkins\" or \"Hudson\"", artifactId)); } } else { hostingIssues.add(new VerificationMessage(Severity.REQUIRED, "The pom.xml file does not contain a valid <artifactId> for the project")); } } private void checkParentInfo(MavenProject project, HashSet<VerificationMessage> hostingIssues) { /* XNamespace ns = "http://maven.apache.org/POM/4.0.0"; try { var nameNode = doc.Element(ns + "project").Element(ns + "name"); if(nameNode != null && nameNode.Value != null) { var name = nameNode.Value; if(string.IsNullOrWhiteSpace(name)) { hostingIssues.Add(new VerificationMessage(VerificationMessage.Severity.Required, "The <name> from the pom.xml is blank or missing")); } if(name.ToLower().Contains("jenkins")) { hostingIssues.Add(new VerificationMessage(VerificationMessage.Severity.Required, "The <name> should not contain \"Jenkins\"")); } } else { hostingIssues.Add(new VerificationMessage(VerificationMessage.Severity.Required, "The pom.xml file does not contain a valid <name> for the project")); } } catch(Exception ex) { log.Info($"Error trying to access <name>: {ex.Message}"); hostingIssues.Add(new VerificationMessage(VerificationMessage.Severity.Required, INVALID_POM)); } */ } private void checkName(MavenProject project, HashSet<VerificationMessage> hostingIssues) { /* XNamespace ns = "http://maven.apache.org/POM/4.0.0"; try { var parentNode = doc.Element(ns + "project").Element(ns + "parent"); if(parentNode != null) { var groupIdNode = parentNode.Element("groupId"); if(groupIdNode != null && groupIdNode.Value != null && groupIdNode.Value != "org.jenkins-ci.plugins") { hostingIssues.Add(new VerificationMessage(VerificationMessage.Severity.Required, "The groupId for your parent pom is not \"org.jenkins-ci.plugins\".")); } } } catch(Exception ex) { log.Info($"Error trying to access the <parent> information: {ex.Message}"); } */ } private void checkLicenses(MavenProject project, HashSet<VerificationMessage> hostingIssues) { /* XNamespace ns = "http://maven.apache.org/POM/4.0.0"; var SPECIFY_LICENSE = "Specify an open source license for your code (most plugins use MIT)."; try { var licensesNode = doc.Element(ns + "project").Element(ns + "licenses"); if(licensesNode != null) { if(licensesNode.Elements(ns + "license").Count() == 0) { hostingIssues.Add(new VerificationMessage(VerificationMessage.Severity.Required, SPECIFY_LICENSE)); } } else { hostingIssues.Add(new VerificationMessage(VerificationMessage.Severity.Required, SPECIFY_LICENSE)); } } catch(Exception ex) { log.Info($"Error trying to access the <licenses> information: {ex.Message}"); } */ } }