Search in sources :

Example 1 with CommitDescriptor

use of com.teamscale.client.CommitDescriptor in project teamscale-jacoco-agent by cqse.

the class ArtifactoryConfig method parseGitProperties.

/**
 * Parses the commit information form a git.properties file.
 */
public static CommitInfo parseGitProperties(File jarFile, DateTimeFormatter gitPropertiesCommitTimeFormat) throws IOException, InvalidGitPropertiesException {
    Pair<String, Properties> entryWithProperties = GitPropertiesLocatorUtils.findGitPropertiesInFile(jarFile, true);
    if (entryWithProperties == null) {
        return null;
    }
    String entry = entryWithProperties.getFirst();
    Properties properties = entryWithProperties.getSecond();
    String revision = GitPropertiesLocatorUtils.getGitPropertiesValue(properties, GitPropertiesLocatorUtils.GIT_PROPERTIES_GIT_COMMIT_ID, entry, jarFile);
    String branchName = GitPropertiesLocatorUtils.getGitPropertiesValue(properties, GitPropertiesLocator.GIT_PROPERTIES_GIT_BRANCH, entry, jarFile);
    long timestamp = ZonedDateTime.parse(GitPropertiesLocatorUtils.getGitPropertiesValue(properties, GitPropertiesLocator.GIT_PROPERTIES_GIT_COMMIT_TIME, entry, jarFile), gitPropertiesCommitTimeFormat).toInstant().toEpochMilli();
    return new CommitInfo(revision, new CommitDescriptor(branchName, timestamp));
}
Also used : CommitDescriptor(com.teamscale.client.CommitDescriptor) Properties(java.util.Properties)

Example 2 with CommitDescriptor

use of com.teamscale.client.CommitDescriptor in project teamscale-jacoco-agent by cqse.

the class TeamscaleConfig method getCommitFromManifest.

/**
 * Reads `Branch` and `Timestamp` entries from the given jar/war file's manifest and builds a commit descriptor out
 * of it.
 */
private CommitDescriptor getCommitFromManifest(File jarFile) throws AgentOptionParseException {
    Manifest manifest = getManifestFromJarFile(jarFile);
    String branch = manifest.getMainAttributes().getValue("Branch");
    String timestamp = manifest.getMainAttributes().getValue("Timestamp");
    if (StringUtils.isEmpty(branch)) {
        throw new AgentOptionParseException("No entry 'Branch' in MANIFEST");
    } else if (StringUtils.isEmpty(timestamp)) {
        throw new AgentOptionParseException("No entry 'Timestamp' in MANIFEST");
    }
    logger.debug("Found commit " + branch + ":" + timestamp + " in file " + jarFile);
    return new CommitDescriptor(branch, timestamp);
}
Also used : AgentOptionParseException(com.teamscale.jacoco.agent.options.AgentOptionParseException) CommitDescriptor(com.teamscale.client.CommitDescriptor) Manifest(java.util.jar.Manifest)

Example 3 with CommitDescriptor

use of com.teamscale.client.CommitDescriptor in project teamscale-jacoco-agent by cqse.

the class CoverageToTeamscaleStrategyTest method mockOptions.

private AgentOptions mockOptions() throws IOException {
    AgentOptions options = mock(AgentOptions.class);
    when(options.createTeamscaleClient()).thenReturn(client);
    when(options.createTempFile(any(), any())).thenReturn(new File(tempDir, "test"));
    TeamscaleServer server = new TeamscaleServer();
    server.commit = new CommitDescriptor("branch", "12345");
    server.url = HttpUrl.get("http://doesnt-exist.io");
    server.userName = "build";
    server.userAccessToken = "token";
    server.partition = "partition";
    when(options.getTeamscaleServerOptions()).thenReturn(server);
    when(options.createTeamscaleClient()).thenReturn(client);
    return options;
}
Also used : TeamscaleServer(com.teamscale.client.TeamscaleServer) CommitDescriptor(com.teamscale.client.CommitDescriptor) File(java.io.File) AgentOptions(com.teamscale.jacoco.agent.options.AgentOptions)

Example 4 with CommitDescriptor

use of com.teamscale.client.CommitDescriptor in project teamscale-jacoco-agent by cqse.

the class TestwiseCoverageAgentTest method mockOptions.

private AgentOptions mockOptions(int port) {
    AgentOptions options = mock(AgentOptions.class);
    when(options.createTeamscaleClient()).thenReturn(client);
    TeamscaleServer server = new TeamscaleServer();
    server.commit = new CommitDescriptor("branch", "12345");
    server.url = HttpUrl.get("http://doesnt-exist.io");
    server.userName = "build";
    server.userAccessToken = "token";
    server.partition = "partition";
    when(options.getTeamscaleServerOptions()).thenReturn(server);
    when(options.getHttpServerPort()).thenReturn(port);
    when(options.getTestwiseCoverageMode()).thenReturn(ETestwiseCoverageMode.TEAMSCALE_UPLOAD);
    when(options.createTeamscaleClient()).thenReturn(client);
    return options;
}
Also used : TeamscaleServer(com.teamscale.client.TeamscaleServer) CommitDescriptor(com.teamscale.client.CommitDescriptor) AgentOptions(com.teamscale.jacoco.agent.options.AgentOptions)

Example 5 with CommitDescriptor

use of com.teamscale.client.CommitDescriptor in project teamscale-jacoco-agent by cqse.

the class NwdiMarkerClassLocatingTransformer method transform.

@Override
public byte[] transform(ClassLoader classLoader, String className, Class<?> aClass, ProtectionDomain protectionDomain, byte[] classFileContent) {
    if (protectionDomain == null) {
        // happens for e.g. java.lang. We can ignore these classes
        return null;
    }
    if (StringUtils.isEmpty(className) || !locationIncludeFilter.isIncluded(className)) {
        // only search in jar files of included classes
        return null;
    }
    if (!this.markerClassesToApplications.containsKey(className)) {
        // only kick off search if the marker class was found.
        return null;
    }
    try {
        CodeSource codeSource = protectionDomain.getCodeSource();
        if (codeSource == null) {
            // but there's nothing else we can do here in either case
            return null;
        }
        URL jarOrClassFolderUrl = codeSource.getLocation();
        logger.debug("Found " + className + " in " + jarOrClassFolderUrl);
        if (jarOrClassFolderUrl.getProtocol().toLowerCase().equals("file")) {
            Path file = Paths.get(jarOrClassFolderUrl.toURI());
            BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);
            SapNwdiApplications.SapNwdiApplication application = markerClassesToApplications.get(className);
            CommitDescriptor commitDescriptor = new CommitDescriptor(DTR_BRIDGE_DEFAULT_BRANCH, attr.lastModifiedTime().toMillis());
            store.setCommitForApplication(commitDescriptor, application);
        }
    } catch (Throwable e) {
        // we catch Throwable to be sure that we log all errors as anything thrown from this method is
        // silently discarded by the JVM
        logger.error("Failed to process class {} trying to determine its last modification timestamp.", className, e);
    }
    return null;
}
Also used : Path(java.nio.file.Path) CommitDescriptor(com.teamscale.client.CommitDescriptor) CodeSource(java.security.CodeSource) URL(java.net.URL) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) SapNwdiApplications(com.teamscale.jacoco.agent.options.sapnwdi.SapNwdiApplications)

Aggregations

CommitDescriptor (com.teamscale.client.CommitDescriptor)5 TeamscaleServer (com.teamscale.client.TeamscaleServer)2 AgentOptions (com.teamscale.jacoco.agent.options.AgentOptions)2 AgentOptionParseException (com.teamscale.jacoco.agent.options.AgentOptionParseException)1 SapNwdiApplications (com.teamscale.jacoco.agent.options.sapnwdi.SapNwdiApplications)1 File (java.io.File)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 CodeSource (java.security.CodeSource)1 Properties (java.util.Properties)1 Manifest (java.util.jar.Manifest)1