Search in sources :

Example 71 with ReceiveCommand

use of org.eclipse.jgit.transport.ReceiveCommand in project gitblit by gitblit.

the class MirrorService method run.

@Override
public void run() {
    if (!isReady()) {
        return;
    }
    running.set(true);
    for (String repositoryName : repositoryManager.getRepositoryList()) {
        if (forceClose.get()) {
            break;
        }
        if (repositoryManager.isCollectingGarbage(repositoryName)) {
            logger.debug("mirror is skipping {} garbagecollection", repositoryName);
            continue;
        }
        RepositoryModel model = null;
        Repository repository = null;
        try {
            model = repositoryManager.getRepositoryModel(repositoryName);
            if (!model.isMirror && !model.isBare) {
                // repository must be a valid bare git mirror
                logger.debug("mirror is skipping {} !mirror !bare", repositoryName);
                continue;
            }
            repository = repositoryManager.getRepository(repositoryName);
            if (repository == null) {
                logger.warn(MessageFormat.format("MirrorExecutor is missing repository {0}?!?", repositoryName));
                continue;
            }
            // automatically repair (some) invalid fetch ref specs
            if (!repairAttempted.contains(repositoryName)) {
                repairAttempted.add(repositoryName);
                JGitUtils.repairFetchSpecs(repository);
            }
            // find the first mirror remote - there should only be one
            StoredConfig rc = repository.getConfig();
            RemoteConfig mirror = null;
            List<RemoteConfig> configs = RemoteConfig.getAllRemoteConfigs(rc);
            for (RemoteConfig config : configs) {
                if (config.isMirror()) {
                    mirror = config;
                    break;
                }
            }
            if (mirror == null) {
                // repository does not have a mirror remote
                logger.debug("mirror is skipping {} no mirror remote found", repositoryName);
                continue;
            }
            logger.debug("checking {} remote {} for ref updates", repositoryName, mirror.getName());
            final boolean testing = false;
            Git git = new Git(repository);
            CredentialsProvider creds = null;
            URIish fetchUri = mirror.getURIs().get(0);
            if (fetchUri.getUser() != null && fetchUri.getPass() != null) {
                creds = new UsernamePasswordCredentialsProvider(fetchUri.getUser(), fetchUri.getPass());
            }
            FetchResult result = git.fetch().setCredentialsProvider(creds).setRemote(mirror.getName()).setDryRun(testing).call();
            Collection<TrackingRefUpdate> refUpdates = result.getTrackingRefUpdates();
            if (refUpdates.size() > 0) {
                ReceiveCommand ticketBranchCmd = null;
                for (TrackingRefUpdate ru : refUpdates) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("updated mirror ");
                    sb.append(repositoryName);
                    sb.append(" ");
                    sb.append(ru.getRemoteName());
                    sb.append(" -> ");
                    sb.append(ru.getLocalName());
                    if (ru.getResult() == Result.FORCED) {
                        sb.append(" (forced)");
                    }
                    sb.append(" ");
                    sb.append(ru.getOldObjectId() == null ? "" : ru.getOldObjectId().abbreviate(7).name());
                    sb.append("..");
                    sb.append(ru.getNewObjectId() == null ? "" : ru.getNewObjectId().abbreviate(7).name());
                    logger.info(sb.toString());
                    if (BranchTicketService.BRANCH.equals(ru.getLocalName())) {
                        ReceiveCommand.Type type = null;
                        switch(ru.getResult()) {
                            case NEW:
                                type = Type.CREATE;
                                break;
                            case FAST_FORWARD:
                                type = Type.UPDATE;
                                break;
                            case FORCED:
                                type = Type.UPDATE_NONFASTFORWARD;
                                break;
                            default:
                                type = null;
                                break;
                        }
                        if (type != null) {
                            ticketBranchCmd = new ReceiveCommand(ru.getOldObjectId(), ru.getNewObjectId(), ru.getLocalName(), type);
                        }
                    }
                }
                if (ticketBranchCmd != null) {
                    repository.fireEvent(new ReceiveCommandEvent(model, ticketBranchCmd));
                }
            }
        } catch (Exception e) {
            logger.error("Error updating mirror " + repositoryName, e);
        } finally {
            // cleanup
            if (repository != null) {
                repository.close();
            }
        }
    }
    running.set(false);
}
Also used : URIish(org.eclipse.jgit.transport.URIish) ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) FetchResult(org.eclipse.jgit.transport.FetchResult) RepositoryModel(com.gitblit.models.RepositoryModel) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) TrackingRefUpdate(org.eclipse.jgit.transport.TrackingRefUpdate) Type(org.eclipse.jgit.transport.ReceiveCommand.Type) StoredConfig(org.eclipse.jgit.lib.StoredConfig) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) ReceiveCommandEvent(com.gitblit.git.ReceiveCommandEvent) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig)

Example 72 with ReceiveCommand

use of org.eclipse.jgit.transport.ReceiveCommand in project gitblit by gitblit.

the class GroovyScriptTest method testProtectRefsDeleteTag.

@Test
public void testProtectRefsDeleteTag() throws Exception {
    MockGitblit gitblit = new MockGitblit();
    MockLogger logger = new MockLogger();
    MockClientLogger clientLogger = new MockClientLogger();
    List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
    ReceiveCommand command = new ReceiveCommand(ObjectId.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), ObjectId.zeroId(), "refs/tags/v1.0");
    commands.add(command);
    RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
    test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
    assertEquals(ReceiveCommand.Result.REJECTED_NODELETE, command.getResult());
    assertEquals(0, logger.messages.size());
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) ArrayList(java.util.ArrayList) RepositoryModel(com.gitblit.models.RepositoryModel) Date(java.util.Date) Test(org.junit.Test)

Example 73 with ReceiveCommand

use of org.eclipse.jgit.transport.ReceiveCommand in project gitblit by gitblit.

the class GroovyScriptTest method testSendMail.

@Test
public void testSendMail() throws Exception {
    MockGitblit gitblit = new MockGitblit();
    MockLogger logger = new MockLogger();
    MockClientLogger clientLogger = new MockClientLogger();
    List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
    commands.add(new ReceiveCommand(ObjectId.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master"));
    commands.add(new ReceiveCommand(ObjectId.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master2"));
    RepositoryModel repository = repositories().getRepositoryModel("helloworld.git");
    repository.mailingLists.add("list@helloworld.git");
    test("sendmail.groovy", gitblit, logger, clientLogger, commands, repository);
    assertEquals(1, logger.messages.size());
    assertEquals(1, gitblit.messages.size());
    MockMail m = gitblit.messages.get(0);
    assertEquals(5, m.toAddresses.size());
    assertTrue(m.message.contains("BIT"));
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) ArrayList(java.util.ArrayList) RepositoryModel(com.gitblit.models.RepositoryModel) Test(org.junit.Test)

Example 74 with ReceiveCommand

use of org.eclipse.jgit.transport.ReceiveCommand in project gitblit by gitblit.

the class GroovyScriptTest method testProtectRefsCreateBranch.

@Test
public void testProtectRefsCreateBranch() throws Exception {
    MockGitblit gitblit = new MockGitblit();
    MockLogger logger = new MockLogger();
    MockClientLogger clientLogger = new MockClientLogger();
    List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
    commands.add(new ReceiveCommand(ObjectId.zeroId(), ObjectId.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master"));
    RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
    test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) ArrayList(java.util.ArrayList) RepositoryModel(com.gitblit.models.RepositoryModel) Date(java.util.Date) Test(org.junit.Test)

Example 75 with ReceiveCommand

use of org.eclipse.jgit.transport.ReceiveCommand in project gitblit by gitblit.

the class GroovyScriptTest method testBlockPush.

@Test
public void testBlockPush() throws Exception {
    MockGitblit gitblit = new MockGitblit();
    MockLogger logger = new MockLogger();
    MockClientLogger clientLogger = new MockClientLogger();
    List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
    commands.add(new ReceiveCommand(ObjectId.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master"));
    RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
    try {
        test("blockpush.groovy", gitblit, logger, clientLogger, commands, repository);
        assertTrue("blockpush should have failed!", false);
    } catch (GitBlitException e) {
        assertTrue(e.getMessage().contains("failed"));
    }
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) ArrayList(java.util.ArrayList) GitBlitException(com.gitblit.GitBlitException) RepositoryModel(com.gitblit.models.RepositoryModel) Date(java.util.Date) Test(org.junit.Test)

Aggregations

ReceiveCommand (org.eclipse.jgit.transport.ReceiveCommand)82 IOException (java.io.IOException)29 ArrayList (java.util.ArrayList)23 ObjectId (org.eclipse.jgit.lib.ObjectId)23 BatchRefUpdate (org.eclipse.jgit.lib.BatchRefUpdate)22 RevWalk (org.eclipse.jgit.revwalk.RevWalk)21 Repository (org.eclipse.jgit.lib.Repository)19 Ref (org.eclipse.jgit.lib.Ref)18 Test (org.junit.Test)16 RepositoryModel (com.gitblit.models.RepositoryModel)13 Map (java.util.Map)13 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)11 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)10 StorageException (com.google.gerrit.exceptions.StorageException)9 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)9 PersonIdent (org.eclipse.jgit.lib.PersonIdent)9 RevCommit (org.eclipse.jgit.revwalk.RevCommit)9 AuthException (com.google.gerrit.extensions.restapi.AuthException)8 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)8 Collection (java.util.Collection)8