Search in sources :

Example 16 with RemoteRefUpdate

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

the class GitServletTest method testCommitterVerification.

private void testCommitterVerification(UserModel user, String displayName, String emailAddress, boolean expectedSuccess) throws Exception {
    delete(user);
    CredentialsProvider cp = new UsernamePasswordCredentialsProvider(user.username, user.password);
    // fork from original to a temporary bare repo
    File verification = new File(GitBlitSuite.REPOSITORIES, "refchecks/verify-committer.git");
    if (verification.exists()) {
        FileUtils.delete(verification, FileUtils.RECURSIVE);
    }
    CloneCommand clone = Git.cloneRepository();
    clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
    clone.setDirectory(verification);
    clone.setBare(true);
    clone.setCloneAllBranches(true);
    clone.setCredentialsProvider(cp);
    GitBlitSuite.close(clone.call());
    // require push permissions and committer verification
    RepositoryModel model = repositories().getRepositoryModel("refchecks/verify-committer.git");
    model.authorizationControl = AuthorizationControl.NAMED;
    model.accessRestriction = AccessRestrictionType.PUSH;
    model.verifyCommitter = true;
    // grant user push permission
    user.setRepositoryPermission(model.name, AccessPermission.PUSH);
    gitblit().addUser(user);
    repositories().updateRepositoryModel(model.name, model, false);
    // clone temp bare repo to working copy
    File local = new File(GitBlitSuite.REPOSITORIES, "refchecks/verify-wc");
    if (local.exists()) {
        FileUtils.delete(local, FileUtils.RECURSIVE);
    }
    clone = Git.cloneRepository();
    clone.setURI(MessageFormat.format("{0}/{1}", url, model.name));
    clone.setDirectory(local);
    clone.setBare(false);
    clone.setCloneAllBranches(true);
    clone.setCredentialsProvider(cp);
    GitBlitSuite.close(clone.call());
    Git git = Git.open(local);
    // force an identity which may or may not match the account's identity
    git.getRepository().getConfig().setString("user", null, "name", displayName);
    git.getRepository().getConfig().setString("user", null, "email", emailAddress);
    git.getRepository().getConfig().save();
    // commit a file and push it
    File file = new File(local, "PUSHCHK");
    OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
    BufferedWriter w = new BufferedWriter(os);
    w.write("// " + new Date().toString() + "\n");
    w.close();
    git.add().addFilepattern(file.getName()).call();
    git.commit().setMessage("push test").call();
    Iterable<PushResult> results = git.push().setCredentialsProvider(cp).setRemote("origin").call();
    for (PushResult result : results) {
        RemoteRefUpdate ref = result.getRemoteUpdate("refs/heads/master");
        Status status = ref.getStatus();
        if (expectedSuccess) {
            assertTrue("Verification failed! User was NOT able to push commit! " + status.name(), Status.OK.equals(status));
        } else {
            assertTrue("Verification failed! User was able to push commit! " + status.name(), Status.REJECTED_OTHER_REASON.equals(status));
        }
    }
    GitBlitSuite.close(git);
    // close serving repository
    GitBlitSuite.close(verification);
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) Status(org.eclipse.jgit.transport.RemoteRefUpdate.Status) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) RepositoryModel(com.gitblit.models.RepositoryModel) PushResult(org.eclipse.jgit.transport.PushResult) Date(java.util.Date) BufferedWriter(java.io.BufferedWriter) Git(org.eclipse.jgit.api.Git) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File)

Example 17 with RemoteRefUpdate

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

the class GitServletTest method testMergeCommitterVerification.

private void testMergeCommitterVerification(boolean expectedSuccess) throws Exception {
    UserModel user = getUser();
    delete(user);
    CredentialsProvider cp = new UsernamePasswordCredentialsProvider(user.username, user.password);
    // fork from original to a temporary bare repo
    File verification = new File(GitBlitSuite.REPOSITORIES, "refchecks/verify-committer.git");
    if (verification.exists()) {
        FileUtils.delete(verification, FileUtils.RECURSIVE);
    }
    CloneCommand clone = Git.cloneRepository();
    clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
    clone.setDirectory(verification);
    clone.setBare(true);
    clone.setCloneAllBranches(true);
    clone.setCredentialsProvider(cp);
    GitBlitSuite.close(clone.call());
    // require push permissions and committer verification
    RepositoryModel model = repositories().getRepositoryModel("refchecks/verify-committer.git");
    model.authorizationControl = AuthorizationControl.NAMED;
    model.accessRestriction = AccessRestrictionType.PUSH;
    model.verifyCommitter = true;
    // grant user push permission
    user.setRepositoryPermission(model.name, AccessPermission.PUSH);
    gitblit().addUser(user);
    repositories().updateRepositoryModel(model.name, model, false);
    // clone temp bare repo to working copy
    File local = new File(GitBlitSuite.REPOSITORIES, "refchecks/verify-wc");
    if (local.exists()) {
        FileUtils.delete(local, FileUtils.RECURSIVE);
    }
    clone = Git.cloneRepository();
    clone.setURI(MessageFormat.format("{0}/{1}", url, model.name));
    clone.setDirectory(local);
    clone.setBare(false);
    clone.setCloneAllBranches(true);
    clone.setCredentialsProvider(cp);
    GitBlitSuite.close(clone.call());
    Git git = Git.open(local);
    // checkout a mergetest branch
    git.checkout().setCreateBranch(true).setName("mergetest").call();
    // change identity
    git.getRepository().getConfig().setString("user", null, "name", "mergetest");
    git.getRepository().getConfig().setString("user", null, "email", "mergetest@merge.com");
    git.getRepository().getConfig().save();
    // commit a file
    File file = new File(local, "MERGECHK2");
    OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
    BufferedWriter w = new BufferedWriter(os);
    w.write("// " + new Date().toString() + "\n");
    w.close();
    git.add().addFilepattern(file.getName()).call();
    RevCommit mergeTip = git.commit().setMessage(file.getName() + " test").call();
    // return to master
    git.checkout().setName("master").call();
    // restore identity
    if (expectedSuccess) {
        git.getRepository().getConfig().setString("user", null, "name", user.username);
        git.getRepository().getConfig().setString("user", null, "email", user.emailAddress);
        git.getRepository().getConfig().save();
    }
    // commit a file
    file = new File(local, "MERGECHK1");
    os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
    w = new BufferedWriter(os);
    w.write("// " + new Date().toString() + "\n");
    w.close();
    git.add().addFilepattern(file.getName()).call();
    git.commit().setMessage(file.getName() + " test").call();
    // merge the tip of the mergetest branch into master with --no-ff
    MergeResult mergeResult = git.merge().setFastForward(FastForwardMode.NO_FF).include(mergeTip.getId()).call();
    assertEquals(MergeResult.MergeStatus.MERGED, mergeResult.getMergeStatus());
    // push the merged master to the origin
    Iterable<PushResult> results = git.push().setCredentialsProvider(cp).setRemote("origin").call();
    for (PushResult result : results) {
        RemoteRefUpdate ref = result.getRemoteUpdate("refs/heads/master");
        Status status = ref.getStatus();
        if (expectedSuccess) {
            assertTrue("Verification failed! User was NOT able to push commit! " + status.name(), Status.OK.equals(status));
        } else {
            assertTrue("Verification failed! User was able to push commit! " + status.name(), Status.REJECTED_OTHER_REASON.equals(status));
        }
    }
    GitBlitSuite.close(git);
    // close serving repository
    GitBlitSuite.close(verification);
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) Status(org.eclipse.jgit.transport.RemoteRefUpdate.Status) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) MergeResult(org.eclipse.jgit.api.MergeResult) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) RepositoryModel(com.gitblit.models.RepositoryModel) PushResult(org.eclipse.jgit.transport.PushResult) Date(java.util.Date) BufferedWriter(java.io.BufferedWriter) UserModel(com.gitblit.models.UserModel) Git(org.eclipse.jgit.api.Git) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 18 with RemoteRefUpdate

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

the class GitServletTest method testAnonymousPush.

@Test
public void testAnonymousPush() throws Exception {
    GitBlitSuite.close(ticgitFolder);
    if (ticgitFolder.exists()) {
        FileUtils.delete(ticgitFolder, FileUtils.RECURSIVE | FileUtils.RETRY);
    }
    RepositoryModel model = repositories().getRepositoryModel("ticgit.git");
    model.accessRestriction = AccessRestrictionType.NONE;
    repositories().updateRepositoryModel(model.name, model, false);
    CloneCommand clone = Git.cloneRepository();
    clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
    clone.setDirectory(ticgitFolder);
    clone.setBare(false);
    clone.setCloneAllBranches(true);
    clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));
    GitBlitSuite.close(clone.call());
    assertTrue(true);
    Git git = Git.open(ticgitFolder);
    File file = new File(ticgitFolder, "TODO");
    OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
    BufferedWriter w = new BufferedWriter(os);
    w.write("// hellol中文 " + new Date().toString() + "\n");
    w.close();
    git.add().addFilepattern(file.getName()).call();
    git.commit().setMessage("test commit").call();
    Iterable<PushResult> results = git.push().setPushAll().call();
    GitBlitSuite.close(git);
    for (PushResult result : results) {
        for (RemoteRefUpdate update : result.getRemoteUpdates()) {
            assertEquals(Status.OK, update.getStatus());
        }
    }
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Git(org.eclipse.jgit.api.Git) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) RepositoryModel(com.gitblit.models.RepositoryModel) PushResult(org.eclipse.jgit.transport.PushResult) File(java.io.File) Date(java.util.Date) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 19 with RemoteRefUpdate

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

the class GitServletTest method testSubfolderPush.

@Test
public void testSubfolderPush() throws Exception {
    GitBlitSuite.close(jgitFolder);
    if (jgitFolder.exists()) {
        FileUtils.delete(jgitFolder, FileUtils.RECURSIVE | FileUtils.RETRY);
    }
    CloneCommand clone = Git.cloneRepository();
    clone.setURI(MessageFormat.format("{0}/test/jgit.git", url));
    clone.setDirectory(jgitFolder);
    clone.setBare(false);
    clone.setCloneAllBranches(true);
    clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));
    GitBlitSuite.close(clone.call());
    assertTrue(true);
    Git git = Git.open(jgitFolder);
    File file = new File(jgitFolder, "TODO");
    OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
    BufferedWriter w = new BufferedWriter(os);
    w.write("// " + new Date().toString() + "\n");
    w.close();
    git.add().addFilepattern(file.getName()).call();
    git.commit().setMessage("test commit").call();
    Iterable<PushResult> results = git.push().setPushAll().setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password)).call();
    GitBlitSuite.close(git);
    for (PushResult result : results) {
        for (RemoteRefUpdate update : result.getRemoteUpdates()) {
            assertEquals(Status.OK, update.getStatus());
        }
    }
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Git(org.eclipse.jgit.api.Git) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) PushResult(org.eclipse.jgit.transport.PushResult) File(java.io.File) Date(java.util.Date) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 20 with RemoteRefUpdate

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

the class TicketReferenceTest method commitTicketBranchDeletePostMergeReference.

@Test
public void commitTicketBranchDeletePostMergeReference() throws Exception {
    setPatchsetAvailable(false);
    TicketModel a = ticketService.createTicket(repo, newTicket("commitTicketBranchDeletePostMergeReference-A"));
    TicketModel b = ticketService.createTicket(repo, newTicket("commitTicketBranchDeletePostMergeReference-B"));
    TicketModel c = ticketService.createTicket(repo, newTicket("commitTicketBranchDeletePostMergeReference-C"));
    assertFalse(c.hasPatchsets());
    String branchName = String.format("ticket/%d", c.number);
    git.checkout().setCreateBranch(true).setName(branchName).call();
    String message = String.format("commit before amend for #%d and #%d", a.number, b.number);
    final RevCommit revCommit1 = makeCommit(message);
    final String commit1Sha = revCommit1.name();
    assertPushSuccess(commit1Sha, branchName);
    a = ticketService.getTicket(repo, a.number);
    b = ticketService.getTicket(repo, b.number);
    c = ticketService.getTicket(repo, c.number);
    assertTrue(a.hasReferences());
    assertTrue(b.hasReferences());
    assertFalse(c.hasReferences());
    List<Reference> cRefA = a.getReferences();
    assertNotNull(cRefA);
    assertEquals(1, cRefA.size());
    assertNull(cRefA.get(0).ticketId);
    assertEquals(commit1Sha, cRefA.get(0).hash);
    List<Reference> cRefB = b.getReferences();
    assertNotNull(cRefB);
    assertEquals(1, cRefB.size());
    assertNull(cRefB.get(0).ticketId);
    assertEquals(commit1Sha, cRefB.get(0).hash);
    git.checkout().setCreateBranch(false).setName("refs/heads/master").call();
    // merge the tip of the branch into master
    MergeResult mergeResult = git.merge().setFastForward(FastForwardMode.NO_FF).include(revCommit1.getId()).call();
    assertEquals(MergeResult.MergeStatus.MERGED, mergeResult.getMergeStatus());
    // push the merged master to the origin
    Iterable<PushResult> results = git.push().setCredentialsProvider(cp).setRemote("origin").call();
    for (PushResult result : results) {
        RemoteRefUpdate ref = result.getRemoteUpdate("refs/heads/master");
        assertEquals(Status.OK, ref.getStatus());
    }
    //As everything has been merged no references should be changed
    assertDeleteBranch(branchName);
    a = ticketService.getTicket(repo, a.number);
    b = ticketService.getTicket(repo, b.number);
    c = ticketService.getTicket(repo, c.number);
    assertTrue(a.hasReferences());
    assertTrue(b.hasReferences());
    assertFalse(c.hasReferences());
    cRefA = a.getReferences();
    assertNotNull(cRefA);
    assertEquals(1, cRefA.size());
    assertNull(cRefA.get(0).ticketId);
    assertEquals(commit1Sha, cRefA.get(0).hash);
    cRefB = b.getReferences();
    assertNotNull(cRefB);
    assertEquals(1, cRefB.size());
    assertNull(cRefB.get(0).ticketId);
    assertEquals(commit1Sha, cRefB.get(0).hash);
}
Also used : RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) Reference(com.gitblit.models.TicketModel.Reference) MergeResult(org.eclipse.jgit.api.MergeResult) TicketModel(com.gitblit.models.TicketModel) PushResult(org.eclipse.jgit.transport.PushResult) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Aggregations

RemoteRefUpdate (org.eclipse.jgit.transport.RemoteRefUpdate)32 PushResult (org.eclipse.jgit.transport.PushResult)28 Test (org.junit.Test)15 File (java.io.File)13 CloneCommand (org.eclipse.jgit.api.CloneCommand)13 Git (org.eclipse.jgit.api.Git)12 BufferedWriter (java.io.BufferedWriter)11 FileOutputStream (java.io.FileOutputStream)11 OutputStreamWriter (java.io.OutputStreamWriter)11 Date (java.util.Date)11 RepositoryModel (com.gitblit.models.RepositoryModel)10 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)9 RevCommit (org.eclipse.jgit.revwalk.RevCommit)7 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)6 RefSpec (org.eclipse.jgit.transport.RefSpec)6 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)5 UserModel (com.gitblit.models.UserModel)4 CredentialsProvider (org.eclipse.jgit.transport.CredentialsProvider)4 Status (org.eclipse.jgit.transport.RemoteRefUpdate.Status)4 PushCommand (org.eclipse.jgit.api.PushCommand)3