<commit_before>/*
 * Copyright 2017 <NAME>.
 *
 * Licensed 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 com.github.rodm.teamcity.gradle.scripts.server;

import jetbrains.buildServer.controllers.admin.projects.EditProjectTab;
import jetbrains.buildServer.serverSide.BuildTypeTemplate;
import jetbrains.buildServer.serverSide.SBuildFeatureDescriptor;
import jetbrains.buildServer.serverSide.SBuildType;
import jetbrains.buildServer.serverSide.SProject;
import jetbrains.buildServer.web.openapi.PagePlaces;
import jetbrains.buildServer.web.openapi.PluginDescriptor;
import org.jetbrains.annotations.NotNull;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static com.github.rodm.teamcity.gradle.scripts.GradleInitScriptsPlugin.FEATURE_TYPE;
import static com.github.rodm.teamcity.gradle.scripts.GradleInitScriptsPlugin.INIT_SCRIPT_NAME;
import static com.github.rodm.teamcity.gradle.scripts.GradleInitScriptsPlugin.PLUGIN_NAME;

public class GradleInitScriptsPage extends EditProjectTab {

    private static String TITLE = "Gradle Init Scripts";

    private GradleScriptsManager scriptsManager;

    GradleInitScriptsPage(@NotNull final PagePlaces pagePlaces,
                          @NotNull final PluginDescriptor descriptor,
                          @NotNull final GradleScriptsManager scriptsManager)
    {
        super(pagePlaces, PLUGIN_NAME, descriptor.getPluginResourcesPath("projectPage.jsp"), TITLE);
        this.scriptsManager = scriptsManager;
        addCssFile("/css/admin/buildTypeForm.css");
        addJsFile(descriptor.getPluginResourcesPath("initScripts.js"));
    }

    @Override
    public void fillModel(@NotNull Map<String, Object> model, @NotNull HttpServletRequest request) {
        super.fillModel(model, request);
        final SProject project = getProject(request);
        if (project != null) {
            String fileName = request.getParameter("file");
            if (fileName != null) {
                String fileContent = scriptsManager.findScript(project, fileName);
                if (fileContent != null) {
                    model.put("fileName", fileName);
                    model.put("fileContent", fileContent);
                }
            }
            Map<SProject, List<String>> scripts = scriptsManager.getScriptNames(project);
            model.put("scripts", scripts);
            Map<String, ScriptUsage> usage = getScriptsUsage(scripts);
            model.put("usage", usage);
        }
    }

    @NotNull
    @Override
    public String getTabTitle(@NotNull final HttpServletRequest request) {
        final SProject project = getProject(request);
        String result = TITLE;
        if (project != null) {
            int count =  scriptsManager.getScriptsCount(project);
            if (count > 0) {
                result = TITLE + " (" + count + ")";
            }
        }
        return result;
    }

    private Map<String, ScriptUsage> getScriptsUsage(Map<SProject, List<String>> scripts) {
        Map<String, ScriptUsage> usage = new LinkedHashMap<>();
        for (Map.Entry<SProject, List<String>> entry : scripts.entrySet()) {
            for (String script : entry.getValue()) {
                usage.put(script, new ScriptUsage());
            }
        }

        for (Map.Entry<SProject, List<String>> entry : scripts.entrySet()) {
            SProject project = entry.getKey();
            addScriptUsageForSubProjects(project, usage, Collections.emptyList());
            addScriptUsageForProject(project, usage, Collections.emptyList());
        }
        return usage;
    }

    private void addScriptUsageForSubProjects(SProject parent, Map<String, ScriptUsage> usage, List<String> subProjectScripts) {
        for (SProject project : parent.getOwnProjects()) {
            List<String> scripts = getProjectScripts(project);
            scripts.addAll(subProjectScripts);
            addScriptUsageForSubProjects(project, usage, scripts);
            addScriptUsageForProject(project, usage, scripts);
        }
    }

    private void addScriptUsageForProject(SProject project, Map<String, ScriptUsage> usage, List<String> subProjectScripts) {
        for (SBuildType buildType : project.getOwnBuildTypes()) {
            for (SBuildFeatureDescriptor feature : buildType.getBuildFeaturesOfType(FEATURE_TYPE)) {
                Map<String, String> parameters = feature.getParameters();
                String scriptName = parameters.get(INIT_SCRIPT_NAME);
                if (!subProjectScripts.contains(scriptName) && usage.get(scriptName) != null) {
                    usage.get(scriptName).addBuildType(buildType);
                }
            }
        }
        for (BuildTypeTemplate buildTemplate : project.getOwnBuildTypeTemplates()) {
            for (SBuildFeatureDescriptor feature : buildTemplate.getBuildFeaturesOfType(FEATURE_TYPE)) {
                Map<String, String> parameters = feature.getParameters();
                String scriptName = parameters.get(INIT_SCRIPT_NAME);
                if (!subProjectScripts.contains(scriptName) && usage.get(scriptName) != null) {
                    usage.get(scriptName).addBuildTemplate(buildTemplate);
                }
            }
        }
    }

    private List<String> getProjectScripts(SProject project) {
        return scriptsManager.getScriptNames(project).getOrDefault(project, new ArrayList<>());
    }
}<commit_msg>Correct usage to not show build types or templates from sibling projects<commit_after>/*
 * Copyright 2017 <NAME>.
 *
 * Licensed 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 com.github.rodm.teamcity.gradle.scripts.server;

import jetbrains.buildServer.controllers.admin.projects.EditProjectTab;
import jetbrains.buildServer.serverSide.BuildTypeTemplate;
import jetbrains.buildServer.serverSide.SBuildFeatureDescriptor;
import jetbrains.buildServer.serverSide.SBuildType;
import jetbrains.buildServer.serverSide.SProject;
import jetbrains.buildServer.web.openapi.PagePlaces;
import jetbrains.buildServer.web.openapi.PluginDescriptor;
import org.jetbrains.annotations.NotNull;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static com.github.rodm.teamcity.gradle.scripts.GradleInitScriptsPlugin.FEATURE_TYPE;
import static com.github.rodm.teamcity.gradle.scripts.GradleInitScriptsPlugin.INIT_SCRIPT_NAME;
import static com.github.rodm.teamcity.gradle.scripts.GradleInitScriptsPlugin.PLUGIN_NAME;

public class GradleInitScriptsPage extends EditProjectTab {

    private static String TITLE = "Gradle Init Scripts";

    private GradleScriptsManager scriptsManager;

    GradleInitScriptsPage(@NotNull final PagePlaces pagePlaces,
                          @NotNull final PluginDescriptor descriptor,
                          @NotNull final GradleScriptsManager scriptsManager)
    {
        super(pagePlaces, PLUGIN_NAME, descriptor.getPluginResourcesPath("projectPage.jsp"), TITLE);
        this.scriptsManager = scriptsManager;
        addCssFile("/css/admin/buildTypeForm.css");
        addJsFile(descriptor.getPluginResourcesPath("initScripts.js"));
    }

    @Override
    public void fillModel(@NotNull Map<String, Object> model, @NotNull HttpServletRequest request) {
        super.fillModel(model, request);
        final SProject project = getProject(request);
        if (project != null) {
            String fileName = request.getParameter("file");
            if (fileName != null) {
                String fileContent = scriptsManager.findScript(project, fileName);
                if (fileContent != null) {
                    model.put("fileName", fileName);
                    model.put("fileContent", fileContent);
                }
            }
            Map<SProject, List<String>> scripts = scriptsManager.getScriptNames(project);
            model.put("scripts", scripts);
            Map<String, ScriptUsage> usage = getScriptsUsage(scripts);
            model.put("usage", usage);
        }
    }

    @NotNull
    @Override
    public String getTabTitle(@NotNull final HttpServletRequest request) {
        final SProject project = getProject(request);
        String result = TITLE;
        if (project != null) {
            int count =  scriptsManager.getScriptsCount(project);
            if (count > 0) {
                result = TITLE + " (" + count + ")";
            }
        }
        return result;
    }

    private Map<String, ScriptUsage> getScriptsUsage(Map<SProject, List<String>> scripts) {
        Map<String, ScriptUsage> usage = new LinkedHashMap<>();
        for (Map.Entry<SProject, List<String>> entry : scripts.entrySet()) {
            SProject project = entry.getKey();
            for (String script : entry.getValue()) {
                usage.put(script, new ScriptUsage());
                addScriptUsageForSubProjects(script, project, usage);
            }
        }
        return usage;
    }

    private void addScriptUsageForSubProjects(String name, SProject parent, Map<String, ScriptUsage> usage) {
        for (SProject project : parent.getOwnProjects()) {
            List<String> scripts = getProjectScripts(project);
            if (!scripts.contains(name)) {
                addScriptUsageForSubProjects(name, project, usage);
            }
        }
        addScriptUsageForProject(name, parent, usage);
    }

    private void addScriptUsageForProject(String name, SProject project, Map<String, ScriptUsage> usage) {
        for (SBuildType buildType : project.getOwnBuildTypes()) {
            for (SBuildFeatureDescriptor feature : buildType.getBuildFeaturesOfType(FEATURE_TYPE)) {
                Map<String, String> parameters = feature.getParameters();
                String scriptName = parameters.get(INIT_SCRIPT_NAME);
                if (scriptName.equals(name)) {
                    usage.get(scriptName).addBuildType(buildType);
                }
            }
        }
        for (BuildTypeTemplate buildTemplate : project.getOwnBuildTypeTemplates()) {
            for (SBuildFeatureDescriptor feature : buildTemplate.getBuildFeaturesOfType(FEATURE_TYPE)) {
                Map<String, String> parameters = feature.getParameters();
                String scriptName = parameters.get(INIT_SCRIPT_NAME);
                if (scriptName.equals(name)) {
                    usage.get(scriptName).addBuildTemplate(buildTemplate);
                }
            }
        }
    }

    private List<String> getProjectScripts(SProject project) {
        return scriptsManager.getScriptNames(project).getOrDefault(project, new ArrayList<>());
    }
}
{'fraction_non_alphanumeric': 0.09376059681247881, 'fraction_numerical': 0.001525940996948118, 'mean_word_length': 2.9153667441088618, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '264464', 'n_tokens_mistral': 3083, 'n_tokens_neox': 2996, 'n_words': 702}