Search in sources :

Example 16 with VcsRootInstance

use of jetbrains.buildServer.vcs.VcsRootInstance in project teamcity-git by JetBrains.

the class GitBuildParametersProvider method getParameters.

@NotNull
@Override
public Map<String, String> getParameters(@NotNull SBuild build, boolean emulationMode) {
    Map<String, String> params = new HashMap<String, String>();
    if (emulationMode)
        return params;
    for (BuildRevision revision : build.getRevisions()) {
        RepositoryVersion repositoryVersion = revision.getRepositoryVersion();
        VcsRootInstance root = revision.getRoot();
        if (!Constants.VCS_NAME.equals(root.getVcsName()))
            continue;
        String vcsBranch = repositoryVersion.getVcsBranch();
        if (vcsBranch != null)
            params.put(GitUtils.getGitRootBranchParamName(root), vcsBranch);
    }
    return params;
}
Also used : BuildRevision(jetbrains.buildServer.serverSide.BuildRevision) HashMap(java.util.HashMap) RepositoryVersion(jetbrains.buildServer.serverSide.RepositoryVersion) VcsRootInstance(jetbrains.buildServer.vcs.VcsRootInstance) NotNull(org.jetbrains.annotations.NotNull)

Example 17 with VcsRootInstance

use of jetbrains.buildServer.vcs.VcsRootInstance in project teamcity-git by JetBrains.

the class GitDiagnosticsTab method runTestConnectionForAllProjectRoots.

private void runTestConnectionForAllProjectRoots(@NotNull SProject project, @NotNull List<VcsRootInstance> vcsRootInstances, @NotNull Date timestamp) {
    final String externalId = project.getExternalId();
    boolean canceled = false;
    try {
        final Map<Integer, String> cachedResults = new HashMap<>();
        final long start = System.currentTimeMillis();
        int processed = 0;
        int cacheHits = 0;
        for (VcsRootInstance ri : vcsRootInstances) {
            ++processed;
            if (!isGitRoot(ri) || ri.getUsages().isEmpty()) {
                continue;
            }
            String error = null;
            final Integer key = getKey(ri);
            if (cachedResults.containsKey(key)) {
                ++cacheHits;
                error = cachedResults.get(key);
                // cached success
                if (error == null)
                    continue;
            } else {
                boolean jGitSucceeded = true;
                try {
                    IOGuard.allowNetworkAndCommandLine(() -> myVcsSupport.getRemoteRefs(ri, false));
                } catch (VcsException e) {
                    jGitSucceeded = false;
                    cachedResults.put(key, null);
                }
                // if jgit fails, no need to check native git
                if (jGitSucceeded) {
                    try {
                        IOGuard.allowNetworkAndCommandLine(() -> myVcsSupport.getRemoteRefs(ri, true));
                        cachedResults.put(key, null);
                    } catch (Throwable e) {
                        error = e.getMessage();
                        cachedResults.put(key, error);
                    }
                }
            }
            final Lock lock = myLocks.get(externalId);
            lock.lock();
            try {
                final VcsRootLink vcsRootLink = new VcsRootLink(ri.getParent());
                final TestConnectionTask task = myTestConnectionsInProgress.get(externalId);
                if (task == null) {
                    canceled = true;
                    break;
                }
                task.setRootsProcessed(processed);
                if (error == null)
                    continue;
                final List<TestConnectionError> rootErrors = task.getRootErrors(vcsRootLink);
                rootErrors.add(new TestConnectionError(error, ri.getUsages().keySet().stream().map(bt -> new BuildTypeLink(bt)).collect(Collectors.toSet())));
            } finally {
                lock.unlock();
            }
        }
        final long duration = System.currentTimeMillis() - start;
        LOG.info("Native git TestConnection for " + LogUtil.describe(project) + (canceled ? " was canceled," : " finished") + " took " + TimeUnit.MILLISECONDS.toSeconds(duration) + "sec, cache size: " + cachedResults.size() + ", cache hits: " + cacheHits);
        if (canceled)
            return;
        if (project.isRootProject()) {
            FileUtil.delete(getTestConnectionResultsFolder());
            final File storedFile = getStoredTestConnectionErrorsFile(timestamp);
            deleteFile(storedFile);
            FileUtil.createParentDirs(storedFile);
            FileWriter writer = null;
            try {
                final Map<VcsRootLink, List<TestConnectionError>> errors;
                final Lock lock = myLocks.get(externalId);
                lock.lock();
                try {
                    errors = myTestConnectionsInProgress.get(externalId).getErrors();
                } finally {
                    lock.unlock();
                }
                writer = new FileWriter(storedFile);
                GSON.toJson(errors, Map.class, writer);
            } catch (Throwable e) {
                LOG.warnAndDebugDetails("Exception while saving native git Test Connection results for " + LogUtil.describe(project) + " to " + storedFile, e);
                deleteFile(storedFile);
            } finally {
                FileUtil.close(writer);
            }
        }
    } finally {
        final Lock lock = myLocks.get(externalId);
        lock.lock();
        try {
            finishTask(externalId);
        } finally {
            lock.unlock();
        }
    }
}
Also used : ProjectHierarchyBean(jetbrains.buildServer.web.util.ProjectHierarchyBean) java.util(java.util) PagePlaces(jetbrains.buildServer.web.openapi.PagePlaces) AjaxRequestProcessor(jetbrains.buildServer.controllers.AjaxRequestProcessor) GsonBuilder(com.google.gson.GsonBuilder) FileUtil(jetbrains.buildServer.util.FileUtil) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) HttpServletRequest(javax.servlet.http.HttpServletRequest) AccessDeniedException(jetbrains.buildServer.serverSide.auth.AccessDeniedException) Gson(com.google.gson.Gson) StringUtil(jetbrains.buildServer.util.StringUtil) VcsRootInstance(jetbrains.buildServer.vcs.VcsRootInstance) Logger(com.intellij.openapi.diagnostic.Logger) XmlResponseUtil(jetbrains.buildServer.controllers.XmlResponseUtil) SVcsRoot(jetbrains.buildServer.vcs.SVcsRoot) Striped(com.google.common.util.concurrent.Striped) ExecutorServices(jetbrains.buildServer.serverSide.executors.ExecutorServices) jetbrains.buildServer.serverSide(jetbrains.buildServer.serverSide) DiagnosticTab(jetbrains.buildServer.diagnostic.web.DiagnosticTab) FileWriter(java.io.FileWriter) HttpServletResponse(javax.servlet.http.HttpServletResponse) SessionUser(jetbrains.buildServer.web.util.SessionUser) BaseController(jetbrains.buildServer.controllers.BaseController) Collectors(java.util.stream.Collectors) File(java.io.File) PluginDescriptor(jetbrains.buildServer.web.openapi.PluginDescriptor) TimeUnit(java.util.concurrent.TimeUnit) Nullable(org.jetbrains.annotations.Nullable) ModelAndView(org.springframework.web.servlet.ModelAndView) Lock(java.util.concurrent.locks.Lock) GitExec(jetbrains.buildServer.buildTriggers.vcs.git.command.GitExec) Permission(jetbrains.buildServer.serverSide.auth.Permission) Dates(jetbrains.buildServer.util.Dates) ActionErrors(jetbrains.buildServer.controllers.ActionErrors) VcsException(jetbrains.buildServer.vcs.VcsException) VcsRootSshKeyManager(jetbrains.buildServer.ssh.VcsRootSshKeyManager) FileReader(java.io.FileReader) CSRFFilter(jetbrains.buildServer.web.CSRFFilter) WebControllerManager(jetbrains.buildServer.web.openapi.WebControllerManager) VcsRoot(jetbrains.buildServer.vcs.VcsRoot) NotNull(org.jetbrains.annotations.NotNull) Element(org.jdom.Element) SUser(jetbrains.buildServer.users.SUser) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) BuildProject(jetbrains.buildServer.BuildProject) LogUtil(jetbrains.buildServer.log.LogUtil) FileWriter(java.io.FileWriter) VcsRootInstance(jetbrains.buildServer.vcs.VcsRootInstance) Lock(java.util.concurrent.locks.Lock) VcsException(jetbrains.buildServer.vcs.VcsException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) File(java.io.File)

Example 18 with VcsRootInstance

use of jetbrains.buildServer.vcs.VcsRootInstance in project teamcity-git by JetBrains.

the class SpaceExternalChangeViewerExtension method getAvailableProperties.

@Nullable
@Override
public Map<String, String> getAvailableProperties(@NotNull VcsRoot vcs) {
    final VcsRootInstance vcsRoot = (VcsRootInstance) vcs;
    if (!Constants.VCS_NAME.equals(vcsRoot.getVcsName()))
        return null;
    String url = vcsRoot.getProperty(Constants.FETCH_URL);
    if (url == null)
        return null;
    URIish uri;
    try {
        uri = new URIish(url);
    } catch (URISyntaxException e) {
        return null;
    }
    final String gitPath = uri.getPath();
    if (uri.getHost().endsWith(".jetbrains.space")) {
        // Space is on the Jetbrains side
        final String[] strings = gitPath.substring(1).split("/");
        if (strings.length == 3) {
            final String orgName = strings[0];
            final String project = strings[1];
            final String repository = strings[2].replaceFirst("\\.git$", "");
            final String repositoryUrl = String.format("https://%s.jetbrains.space/p/%s/repositories/%s", orgName, project, repository);
            return createResponse(repositoryUrl);
        }
    } else {
        final SVcsRoot vcsRootParent = vcsRoot.getParent();
        final List<OAuthConnectionDescriptor> connections = myOAuthConnectionsManager.getAvailableConnectionsOfType(vcsRootParent.getProject(), SpaceOAuthProvider.TYPE);
        if (connections.isEmpty())
            return null;
        for (OAuthConnectionDescriptor connection : connections) {
            final SpaceConnectDescriber spaceConnectDescriber = new SpaceConnectDescriber(connection);
            final String spaceAddress = spaceConnectDescriber.getAddress();
            if (uri.getHost().contains(spaceAddress)) {
                // Selfhosted Space
                final String[] strings = gitPath.substring(1).split("/");
                if (strings.length == 2) {
                    final String project = strings[0];
                    final String repository = strings[1].replaceFirst("\\.git$", "");
                    final String repositoryUrl = String.format("https://%s/p/%s/repositories/%s", spaceAddress, project, repository);
                    return createResponse(repositoryUrl);
                }
            }
        }
    }
    return null;
}
Also used : URIish(org.eclipse.jgit.transport.URIish) SpaceConnectDescriber(jetbrains.buildServer.serverSide.oauth.space.SpaceConnectDescriber) SVcsRoot(jetbrains.buildServer.vcs.SVcsRoot) OAuthConnectionDescriptor(jetbrains.buildServer.serverSide.oauth.OAuthConnectionDescriptor) URISyntaxException(java.net.URISyntaxException) VcsRootInstance(jetbrains.buildServer.vcs.VcsRootInstance) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with VcsRootInstance

use of jetbrains.buildServer.vcs.VcsRootInstance in project teamcity-git by JetBrains.

the class SGitVcsRoot method getOrRefreshToken.

@Nullable
protected ExpiringAccessToken getOrRefreshToken(@NotNull String tokenId) {
    VcsRoot vcsRoot = getOriginalRoot();
    if (myTokenRefresher == null)
        return null;
    SVcsRoot parentRoot = vcsRoot instanceof SVcsRoot ? (SVcsRoot) vcsRoot : vcsRoot instanceof VcsRootInstance ? ((VcsRootInstance) vcsRoot).getParent() : null;
    if (parentRoot == null) {
        return myTokenRefresher.getRefreshableToken(vcsRoot.getExternalId(), tokenId);
    } else {
        return myTokenRefresher.getRefreshableToken(parentRoot.getProject(), tokenId);
    }
}
Also used : SVcsRoot(jetbrains.buildServer.vcs.SVcsRoot) SVcsRoot(jetbrains.buildServer.vcs.SVcsRoot) VcsRoot(jetbrains.buildServer.vcs.VcsRoot) VcsRootInstance(jetbrains.buildServer.vcs.VcsRootInstance) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with VcsRootInstance

use of jetbrains.buildServer.vcs.VcsRootInstance in project teamcity-rest by JetBrains.

the class BuildTypeTest method testBranchesCaseDiff.

// TW-49294
@Test
public void testBranchesCaseDiff() throws ExecutionException, InterruptedException {
    final BuildTypeEx bt = getRootProject().createProject("Project1", "Project test 1").createBuildType("testBT", "My test build type");
    final BuildTypeRequest buildTypeRequest = new BuildTypeRequest();
    buildTypeRequest.setInTests(myBuildTypeFinder, myBranchFinder, myBeanContext);
    MockVcsSupport vcs = vcsSupport().withName("vcs").dagBased(true).register();
    BuildFinderTestBase.MockCollectRepositoryChangesPolicy collectChangesPolicy = new BuildFinderTestBase.MockCollectRepositoryChangesPolicy();
    vcs.setCollectChangesPolicy(collectChangesPolicy);
    final SVcsRoot vcsRoot = bt.getProject().createVcsRoot("vcs", "extId", "name");
    bt.addVcsRoot(vcsRoot);
    final VcsRootInstance vcsRootInstance = bt.getVcsRootInstances().get(0);
    collectChangesPolicy.setCurrentState(vcsRootInstance, createVersionState("master", map("master", "1", "aaa", "2", "bbb", "2", "Aaa", "3")));
    setBranchSpec(vcsRootInstance, "+:*");
    myFixture.getVcsModificationChecker().checkForModifications(bt.getVcsRootInstances(), OperationRequestor.UNKNOWN);
    Branches branches = buildTypeRequest.serveBranches("id:testBT", "policy:ALL_BRANCHES", Fields.ALL_NESTED.getFieldsSpec());
    assertEquals("<default>", branches.branches.get(0).getInternalName());
    assertEquals("Aaa", branches.branches.get(1).getInternalName());
    assertEquals("aaa", branches.branches.get(2).getInternalName());
    assertEquals("bbb", branches.branches.get(3).getInternalName());
}
Also used : SVcsRoot(jetbrains.buildServer.vcs.SVcsRoot) BuildTypeRequest(jetbrains.buildServer.server.rest.request.BuildTypeRequest) Branches(jetbrains.buildServer.server.rest.model.build.Branches) MockVcsSupport(jetbrains.buildServer.serverSide.impl.MockVcsSupport) BuildFinderTestBase(jetbrains.buildServer.server.rest.data.BuildFinderTestBase) VcsRootInstance(jetbrains.buildServer.vcs.VcsRootInstance) Test(org.testng.annotations.Test) ProjectRequestTest(jetbrains.buildServer.server.rest.request.ProjectRequestTest) BaseFinderTest(jetbrains.buildServer.server.rest.data.BaseFinderTest)

Aggregations

VcsRootInstance (jetbrains.buildServer.vcs.VcsRootInstance)31 Test (org.testng.annotations.Test)15 SVcsModification (jetbrains.buildServer.vcs.SVcsModification)14 ChangeStatus (jetbrains.buildServer.server.rest.model.change.ChangeStatus)12 BuildTypeImpl (jetbrains.buildServer.serverSide.impl.BuildTypeImpl)12 SVcsRoot (jetbrains.buildServer.vcs.SVcsRoot)9 BaseFinderTest (jetbrains.buildServer.server.rest.data.BaseFinderTest)6 RunningBuildEx (jetbrains.buildServer.serverSide.RunningBuildEx)5 BuildFinderTestBase (jetbrains.buildServer.server.rest.data.BuildFinderTestBase)4 Nullable (org.jetbrains.annotations.Nullable)4 SUser (jetbrains.buildServer.users.SUser)3 NotNull (org.jetbrains.annotations.NotNull)3 Logger (com.intellij.openapi.diagnostic.Logger)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ActionErrors (jetbrains.buildServer.controllers.ActionErrors)2 Branches (jetbrains.buildServer.server.rest.model.build.Branches)2 jetbrains.buildServer.serverSide (jetbrains.buildServer.serverSide)2