Search in sources :

Example 6 with Repository

use of com.atlassian.stash.repository.Repository in project stashbot by palantir.

the class RepoConfigurationServlet method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    Repository rep = getRepository(req);
    if (rep == null) {
        res.sendError(404);
        return;
    }
    try {
        permissionValidationService.validateForRepository(rep, Permission.REPO_ADMIN);
    } catch (AuthorisationException notRepoAdmin) {
        log.warn("User {} tried to access the stashbot admin page for {}", req.getRemoteUser(), rep.getSlug());
        res.sendError(HttpServletResponse.SC_UNAUTHORIZED, "You do not have permission to access this page.");
        return;
    }
    RepositoryConfiguration rc;
    JenkinsServerConfiguration theJsc;
    try {
        rc = configurationPersistanceManager.getRepositoryConfigurationForRepository(rep);
        theJsc = configurationPersistanceManager.getJenkinsServerConfiguration(rc.getJenkinsServerName());
    } catch (SQLException e1) {
        throw new ServletException(e1);
    }
    res.setContentType("text/html;charset=UTF-8");
    try {
        List<Map<String, String>> jenkinsServersData = new ArrayList<Map<String, String>>();
        for (JenkinsServerConfiguration jsc : configurationPersistanceManager.getAllJenkinsServerConfigurations()) {
            HashMap<String, String> m = new HashMap<String, String>();
            m.put("text", jsc.getName());
            m.put("value", jsc.getName());
            if (rc.getJenkinsServerName().equals(jsc.getName())) {
                m.put("selected", "true");
            }
            jenkinsServersData.add(m);
        }
        pageBuilderService.assembler().resources().requireContext("plugin.page.stashbot");
        pageBuilderService.assembler().resources().requireWebResource("com.palantir.stash.stashbot:stashbot-resources");
        soyTemplateRenderer.render(res.getWriter(), "com.palantir.stash.stashbot:stashbotConfigurationResources", "plugin.page.stashbot.repositoryConfigurationPanel", ImmutableMap.<String, Object>builder().put("repository", rep).put("ciEnabled", rc.getCiEnabled()).put("publishBranchRegex", rc.getPublishBranchRegex()).put("publishBuildCommand", rc.getPublishBuildCommand()).put("verifyBranchRegex", rc.getVerifyBranchRegex()).put("verifyBuildCommand", rc.getVerifyBuildCommand()).put("prebuildCommand", rc.getPrebuildCommand()).put("jenkinsServerName", rc.getJenkinsServerName()).put("maxVerifyChain", rc.getMaxVerifyChain().toString()).put("rebuildOnUpdate", rc.getRebuildOnTargetUpdate()).put("isVerifyPinned", rc.getVerifyPinned()).put("verifyLabel", rc.getVerifyLabel()).put("isPublishPinned", rc.getPublishPinned()).put("publishLabel", rc.getPublishLabel()).put("isJunit", rc.getJunitEnabled()).put("junitPath", rc.getJunitPath()).put("artifactsEnabled", rc.getArtifactsEnabled()).put("artifactsPath", rc.getArtifactsPath()).put("jenkinsServersData", jenkinsServersData).put("isEmailNotificationsEnabled", rc.getEmailNotificationsEnabled()).put("isEmailForEveryUnstableBuild", rc.getEmailForEveryUnstableBuild()).put("isEmailPerModuleEmail", rc.getEmailPerModuleEmail()).put("emailRecipients", rc.getEmailRecipients()).put("isEmailSendToIndividuals", rc.getEmailSendToIndividuals()).put("isStrictVerifyMode", rc.getStrictVerifyMode()).put("isPreserveJenkinsJobConfig", rc.getPreserveJenkinsJobConfig()).put("isLocked", isLocked(theJsc)).put("verificationEnabled", configurationPersistanceManager.getJobTypeStatusMapping(rc, JobType.VERIFY_COMMIT)).put("verifyPREnabled", configurationPersistanceManager.getJobTypeStatusMapping(rc, JobType.VERIFY_PR)).put("publishEnabled", configurationPersistanceManager.getJobTypeStatusMapping(rc, JobType.PUBLISH)).build());
    } catch (SoyException e) {
        Throwable cause = e.getCause();
        if (cause instanceof IOException) {
            throw (IOException) cause;
        } else {
            throw new ServletException(e);
        }
    } catch (SQLException e) {
        throw new ServletException(e);
    }
}
Also used : SQLException(java.sql.SQLException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SoyException(com.atlassian.soy.renderer.SoyException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) Repository(com.atlassian.stash.repository.Repository) AuthorisationException(com.atlassian.stash.exception.AuthorisationException) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration) JenkinsServerConfiguration(com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 7 with Repository

use of com.atlassian.stash.repository.Repository in project stashbot by palantir.

the class IsCiEnabledForRepoCondition method shouldDisplay.

@Override
public boolean shouldDisplay(Map<String, Object> context) {
    // request, principal, changeset, repository
    Repository repo = (Repository) context.get("repository");
    RepositoryConfiguration rc;
    if (repo == null) {
        return false;
    }
    try {
        rc = cpm.getRepositoryConfigurationForRepository(repo);
    } catch (SQLException e) {
        rc = null;
        log.error("Failed to get RepositoryConfiguration for repo: " + repo.toString(), e);
    }
    if (rc != null && rc.getCiEnabled()) {
        return true;
    }
    return false;
}
Also used : Repository(com.atlassian.stash.repository.Repository) SQLException(java.sql.SQLException) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration)

Example 8 with Repository

use of com.atlassian.stash.repository.Repository in project stashbot by palantir.

the class ConfigurationTest method getsStoredRepoData.

@Test
public void getsStoredRepoData() throws Exception {
    Repository repo = Mockito.mock(Repository.class);
    Mockito.when(repo.getId()).thenReturn(10);
    RepositoryConfiguration rc = cpm.getRepositoryConfigurationForRepository(repo);
    Assert.assertEquals("publishBranchRegex", rc.getPublishBranchRegex());
    Assert.assertEquals("publishBuildCommand", rc.getPublishBuildCommand());
    Assert.assertEquals("verifyBranchRegex", rc.getVerifyBranchRegex());
    Assert.assertEquals("verifyBuildCommand", rc.getVerifyBuildCommand());
    Assert.assertTrue(rc.getCiEnabled());
    Assert.assertFalse(rc.getPreserveJenkinsJobConfig());
}
Also used : Repository(com.atlassian.stash.repository.Repository) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration) Test(org.junit.Test)

Example 9 with Repository

use of com.atlassian.stash.repository.Repository in project stashbot by palantir.

the class PullRequestListener method listenForComments.

@EventListener
public void listenForComments(PullRequestCommentEvent event) {
    try {
        final PullRequest pr = event.getPullRequest();
        final Repository repo = pr.getToRef().getRepository();
        final RepositoryConfiguration rc = cpm.getRepositoryConfigurationForRepository(repo);
        if (!rc.getCiEnabled()) {
            log.debug("Pull Request " + pr.toString() + " ignored, CI not enabled for target repo " + repo.toString());
            return;
        }
        Comment c = event.getComment();
        if (c.getText().contains(OVERRIDE_STRING)) {
            log.debug("Pull Request override set to true for PR " + pr.toString());
            cpm.setPullRequestMetadata(pr, null, null, true);
        }
    } catch (SQLException e) {
        log.error("Error getting repository configuration", e);
    }
}
Also used : Comment(com.atlassian.stash.comment.Comment) Repository(com.atlassian.stash.repository.Repository) SQLException(java.sql.SQLException) PullRequest(com.atlassian.stash.pull.PullRequest) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration) EventListener(com.atlassian.event.api.EventListener)

Example 10 with Repository

use of com.atlassian.stash.repository.Repository in project stashbot by palantir.

the class PullRequestListener method listenForMerged.

// This event signifies that the PR has already been merged, we don't need to worry about VERIFY_PR anymore, only VERIFY_COMMIT or PUBLISH.
@EventListener
public void listenForMerged(PullRequestMergedEvent event) {
    try {
        final PullRequest pr = event.getPullRequest();
        final Repository repo = pr.getToRef().getRepository();
        final RepositoryConfiguration rc = cpm.getRepositoryConfigurationForRepository(repo);
        if (!rc.getCiEnabled()) {
            log.debug("Pull Request " + pr.toString() + " ignored, CI not enabled for target repo " + repo.toString());
            return;
        }
        // just trigger a build of the new commit since the other hook doesn't catch merged PRs.
        String mergeSha1 = event.getChangeset().getId();
        String targetBranch = pr.getToRef().getId();
        boolean publishEnabled = cpm.getJobTypeStatusMapping(rc, JobType.PUBLISH);
        boolean verifyEnabled = cpm.getJobTypeStatusMapping(rc, JobType.VERIFY_COMMIT);
        if (publishEnabled && targetBranch.matches(rc.getPublishBranchRegex())) {
            log.info("Stashbot Trigger: Triggering PUBLISH build for commit " + mergeSha1 + " after merge of branch " + targetBranch);
            jenkinsManager.triggerBuild(repo, JobType.PUBLISH, mergeSha1, targetBranch);
        } else if (verifyEnabled && targetBranch.matches(rc.getVerifyBranchRegex())) {
            // TODO: Build any commits which are new, for now just build latest commit
            // Do this by doing a revwalk just like in TriggerJenkinsBuildHook, excluding the build we just published.
            log.info("Stashbot Trigger: Triggering VERIFICATION build for commit " + mergeSha1 + " after merge of branch " + targetBranch);
            jenkinsManager.triggerBuild(repo, JobType.VERIFY_COMMIT, mergeSha1, targetBranch);
        }
        return;
    } catch (SQLException e) {
        log.error("Error getting repository configuration", e);
    }
}
Also used : Repository(com.atlassian.stash.repository.Repository) SQLException(java.sql.SQLException) PullRequest(com.atlassian.stash.pull.PullRequest) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration) EventListener(com.atlassian.event.api.EventListener)

Aggregations

Repository (com.atlassian.stash.repository.Repository)24 RepositoryConfiguration (com.palantir.stash.stashbot.persistence.RepositoryConfiguration)13 SQLException (java.sql.SQLException)11 IOException (java.io.IOException)8 ServletException (javax.servlet.ServletException)8 PullRequest (com.atlassian.stash.pull.PullRequest)7 AuthorisationException (com.atlassian.stash.exception.AuthorisationException)6 PageRequest (com.atlassian.stash.util.PageRequest)5 PageRequestImpl (com.atlassian.stash.util.PageRequestImpl)5 ArrayList (java.util.ArrayList)5 Future (java.util.concurrent.Future)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 GlobalSettings (com.palantir.stash.codesearch.admin.GlobalSettings)3 JenkinsServerConfiguration (com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration)3 Test (org.junit.Test)3 EventListener (com.atlassian.event.api.EventListener)2 Changeset (com.atlassian.stash.content.Changeset)2 Branch (com.atlassian.stash.repository.Branch)2 EmailSettings (com.palantir.stash.stashbot.config.ConfigurationPersistenceService.EmailSettings)2 JobTemplate (com.palantir.stash.stashbot.persistence.JobTemplate)2