package nu.studer.teamcity.buildscan.agent.servicemessage;

import com.gradle.maven.extension.api.GradleEnterpriseApi;
import com.gradle.maven.extension.api.GradleEnterpriseListener;
import org.apache.maven.execution.MavenSession;
import org.codehaus.plexus.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

@SuppressWarnings("unused")
@Component(
    role = GradleEnterpriseListener.class,
    hint = "build-scan-service-message",
    description = "Interacts with Maven builds invoked from TeamCity"
)
public final class BuildScanServiceMessageMavenExtension implements GradleEnterpriseListener {

    private final Logger logger = LoggerFactory.getLogger(BuildScanServiceMessageMavenExtension.class);

    @Override
    public void configure(GradleEnterpriseApi api, MavenSession session) {
        logger.debug("Executing extension: " + getClass().getSimpleName());

        // When a maven build step has an explicitly defined pom file location, TeamCity will run an info goal on it.
        // This logic prevents that scan from being published.
        if (isJetbrainsInfoGoal(api, session)) {
            String goal = session.getGoals().get(0); // `isJetbrainsInfoGoal` guarantees exactly 1 element
            logger.debug("Skipping Build Scan publish for goal " + goal);
            api.getBuildScan().publishOnDemand();
        } else {
            logger.debug("Registering listener capturing build scan link");
            BuildScanServiceMessageSender.register(api.getBuildScan());
            logger.debug("Finished registering listener capturing build scan link");
        }
    }

    private boolean isJetbrainsInfoGoal(GradleEnterpriseApi api, MavenSession session) {
        List<String> goals = session.getGoals();
        if (goals.size() == 1) {
            String goal = goals.get(0);
            return goal.startsWith("org.jetbrains.maven:info-maven3-plugin:") && goal.endsWith(":info");
        } else {
            return false;
        }
    }

}

@@ -947,17 +947,17 @@
  When a 
-m
+M
 aven bui
@@ -993,16 +993,20 @@
 ined pom
+.xml
  file lo
@@ -1054,70 +1054,51 @@
  it.
-%0A        // This logic prevents that scan from being published
+ Ignore build scan publication in this case
 .%0A  
@@ -1108,21 +1108,26 @@
    if (i
+nvoke
 sJet
-b
+B
 rainsInf
@@ -1132,21 +1132,16 @@
 nfoGoal(
-api, 
 session)
@@ -1174,45 +1174,15 @@
 l = 
-session.getGoals().get(0); // %60isJetb
+getJetB
 rain
@@ -1194,38 +1194,18 @@
 Goal
-%60 guarantees exactly 1 element
+(session);
 %0A   
@@ -1240,15 +1240,15 @@
 ing 
-B
+b
 uild 
-S
+s
 can 
@@ -1254,16 +1254,19 @@
  publish
+ing
  for goa
@@ -1607,16 +1607,23 @@
 private 
+static 
 boolean 
@@ -1627,13 +1627,18 @@
 an i
+nvoke
 sJet
-b
+B
 rain
@@ -1651,33 +1651,140 @@
 oal(
-GradleEnterpriseApi api, 
+MavenSession session) %7B%0A        return getJetBrainsInfoGoal(session) != null;%0A    %7D%0A%0A    private static String getJetBrainsInfoGoal(
 Mave
@@ -1860,20 +1860,23 @@
         
-if (
+return 
 goals.si
@@ -1888,75 +1888,24 @@
 == 1
-) %7B%0A            String goal = goals.get(0);%0A            return goal
+ && goals.get(0)
 .sta
@@ -1962,16 +1962,24 @@
  && goal
+s.get(0)
 .endsWit
@@ -1992,62 +1992,31 @@
 fo%22)
-;%0A        %7D else %7B%0A            return false;%0A        %7D
+ ? goals.get(0) : null;
 %0A   

Refactor slightly