Search in sources :

Example 6 with ReflogEntry

use of org.eclipse.jgit.lib.ReflogEntry in project gerrit by GerritCodeReview.

the class GetReflog method apply.

// TODO(issue-15517): Fix the JdkObsolete issue with Date once JGit's PersonIdent class supports
// Instants
@SuppressWarnings("JdkObsolete")
@Override
public Response<List<ReflogEntryInfo>> apply(BranchResource rsrc) throws RestApiException, IOException, PermissionBackendException {
    permissionBackend.user(rsrc.getUser()).project(rsrc.getNameKey()).check(ProjectPermission.READ_REFLOG);
    try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) {
        ReflogReader r;
        try {
            r = repo.getReflogReader(rsrc.getRef());
        } catch (UnsupportedOperationException e) {
            String msg = "reflog not supported on repo " + rsrc.getNameKey().get();
            logger.atSevere().log("%s", msg);
            throw new MethodNotAllowedException(msg, e);
        }
        if (r == null) {
            throw new ResourceNotFoundException(rsrc.getRef());
        }
        List<ReflogEntry> entries;
        if (from == null && to == null) {
            entries = limit > 0 ? r.getReverseEntries(limit) : r.getReverseEntries();
        } else {
            entries = limit > 0 ? new ArrayList<>(limit) : new ArrayList<>();
            for (ReflogEntry e : r.getReverseEntries()) {
                Instant timestamp = e.getWho().getWhen().toInstant();
                if ((from == null || from.isBefore(timestamp)) && (to == null || to.isAfter(timestamp))) {
                    entries.add(e);
                }
                if (limit > 0 && entries.size() >= limit) {
                    break;
                }
            }
        }
        return Response.ok(Lists.transform(entries, this::newReflogEntryInfo));
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) ReflogReader(org.eclipse.jgit.lib.ReflogReader) ReflogEntry(org.eclipse.jgit.lib.ReflogEntry) Instant(java.time.Instant) ArrayList(java.util.ArrayList) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 7 with ReflogEntry

use of org.eclipse.jgit.lib.ReflogEntry in project gerrit by GerritCodeReview.

the class ReflogIT method peerIPIncludedInReflogRecord.

@Test
@GerritConfig(name = "gerrit.enablePeerIPInReflogRecord", value = "true")
public void peerIPIncludedInReflogRecord() throws Exception {
    PushOneCommit.Result r = createChange();
    Change.Id id = r.getChange().getId();
    try (Repository repo = repoManager.openRepository(r.getChange().project())) {
        File log = new File(repo.getDirectory(), "logs/" + changeMetaRef(id));
        if (!log.exists()) {
            log.getParentFile().mkdirs();
            assertThat(log.createNewFile()).isTrue();
        }
        gApi.changes().id(id.get()).topic("foo");
        ReflogEntry last = repo.getReflogReader(changeMetaRef(id)).getLastEntry();
        assertThat(last.getWho().getEmailAddress()).isEqualTo(admin.username() + "|account-" + admin.id() + "@unknown");
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ReflogEntry(org.eclipse.jgit.lib.ReflogEntry) Change(com.google.gerrit.entities.Change) File(java.io.File) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 8 with ReflogEntry

use of org.eclipse.jgit.lib.ReflogEntry in project gerrit by GerritCodeReview.

the class ReflogIT method emaiIncludedInReflogRecord.

@Test
public void emaiIncludedInReflogRecord() throws Exception {
    PushOneCommit.Result r = createChange();
    Change.Id id = r.getChange().getId();
    try (Repository repo = repoManager.openRepository(r.getChange().project())) {
        File log = new File(repo.getDirectory(), "logs/" + changeMetaRef(id));
        if (!log.exists()) {
            log.getParentFile().mkdirs();
            assertThat(log.createNewFile()).isTrue();
        }
        gApi.changes().id(id.get()).topic("foo");
        ReflogEntry last = repo.getReflogReader(changeMetaRef(id)).getLastEntry();
        assertThat(last.getWho().getEmailAddress()).isEqualTo(admin.email());
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ReflogEntry(org.eclipse.jgit.lib.ReflogEntry) Change(com.google.gerrit.entities.Change) File(java.io.File) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 9 with ReflogEntry

use of org.eclipse.jgit.lib.ReflogEntry in project gerrit by GerritCodeReview.

the class ReflogIT method guessRestApiInReflog.

@Test
public void guessRestApiInReflog() throws Exception {
    PushOneCommit.Result r = createChange();
    Change.Id id = r.getChange().getId();
    try (Repository repo = repoManager.openRepository(r.getChange().project())) {
        File log = new File(repo.getDirectory(), "logs/" + changeMetaRef(id));
        if (!log.exists()) {
            log.getParentFile().mkdirs();
            assertThat(log.createNewFile()).isTrue();
        }
        gApi.changes().id(id.get()).topic("foo");
        ReflogEntry last = repo.getReflogReader(changeMetaRef(id)).getLastEntry();
        assertWithMessage("last RefLogEntry").that(last).isNotNull();
        assertThat(last.getComment()).isEqualTo("restapi.change.PutTopic");
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ReflogEntry(org.eclipse.jgit.lib.ReflogEntry) Change(com.google.gerrit.entities.Change) File(java.io.File) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Aggregations

ReflogEntry (org.eclipse.jgit.lib.ReflogEntry)9 Repository (org.eclipse.jgit.lib.Repository)6 IOException (java.io.IOException)4 ReflogReader (org.eclipse.jgit.lib.ReflogReader)4 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)3 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)3 Change (com.google.gerrit.entities.Change)3 File (java.io.File)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 RevWalk (org.eclipse.jgit.revwalk.RevWalk)3 Test (org.junit.Test)3 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 ArrayList (java.util.ArrayList)2 ObjectId (org.eclipse.jgit.lib.ObjectId)2 GerritConfig (com.google.gerrit.acceptance.config.GerritConfig)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)1 Timestamp (java.sql.Timestamp)1 Instant (java.time.Instant)1 Date (java.util.Date)1