hudson.model.CauseAction Java Examples

The following examples show how to use hudson.model.CauseAction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BuildFlowAction.java    From yet-another-build-visualizer-plugin with MIT License 6 votes vote down vote up
private static Run getUpstreamBuild(@Nonnull Run build) {
  CauseAction causeAction = build.getAction(CauseAction.class);
  if (causeAction == null) {
    return null;
  }
  for (Cause cause : causeAction.getCauses()) {
    if (cause instanceof Cause.UpstreamCause) {
      Cause.UpstreamCause upstreamCause = (Cause.UpstreamCause) cause;
      Job upstreamJob =
          Jenkins.getInstance().getItemByFullName(upstreamCause.getUpstreamProject(), Job.class);
      // We want to ignore rebuilds, rebuilds have the same parent as
      // original build, see BuildCache#updateCache().
      if (upstreamJob == null || build.getParent() == upstreamJob) {
        continue;
      }
      return upstreamJob.getBuildByNumber(upstreamCause.getUpstreamBuild());
    }
  }
  return null;
}
 
Example #2
Source File: CauseActionConverterTest.java    From DotCi with MIT License 6 votes vote down vote up
@Test
public void should_convert_cause_action_to_old_format() throws Exception {
    Cause cause1 = new NullBuildCause();
    Mapper mapper = Mockito.mock(Mapper.class);
    when(mapper.toDBObject(cause1)).thenReturn(new BasicDBObject("cause1", "cause1"));

    CauseAction causeAction = new CauseAction(cause1);
    CauseActionConverter converter = new CauseActionConverter();
    converter.setMapper(mapper);

    DBObject dbObject = (DBObject) converter.encode(causeAction, null);

    Assert.assertEquals(dbObject.get("className"), CauseAction.class.getName());
    Assert.assertNotNull(dbObject.get("causes"));
    List dbCauses = ((List) dbObject.get("causes"));
    Assert.assertEquals(1, dbCauses.size());
    Assert.assertEquals("cause1", ((BasicDBObject) dbCauses.get(0)).get("cause1"));
}
 
Example #3
Source File: CauseActionConverterTest.java    From DotCi with MIT License 6 votes vote down vote up
@Test
public void should_get_cause_from_dbObject() throws Exception {
    BasicDBObject cause1DbObject = new BasicDBObject("cause1", "cause1");
    DBObject causes = new BasicDBObjectBuilder().add("causes", Arrays.asList(cause1DbObject)).get();

    Mapper mapper = Mockito.mock(Mapper.class);
    Cause cause1 = new NullBuildCause();
    when(mapper.fromDBObject(null, cause1DbObject, null)).thenReturn(cause1);

    CauseActionConverter converter = new CauseActionConverter();
    converter.setMapper(mapper);
    CauseAction action = converter.decode(CauseAction.class, causes, Mockito.mock(MappedField.class));

    Assert.assertEquals(1, action.getCauses().size());
    Assert.assertEquals(cause1, action.getCauses().get(0));

}
 
Example #4
Source File: PipelineApiTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Test
public void testPipelineQueue() throws Exception {
    FreeStyleProject p1 = j.createFreeStyleProject("pipeline1");

    p1.setConcurrentBuild(true);
    p1.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("test","test")));
    p1.getBuildersList().add(new Shell("echo hello!\nsleep 300"));

    p1.scheduleBuild2(0).waitForStart();
    p1.scheduleBuild2(0).waitForStart();
    Jenkins.getInstance().getQueue().schedule(p1, 0, new ParametersAction(new StringParameterValue("test","test1")), new CauseAction(new Cause.UserIdCause()));
    Jenkins.getInstance().getQueue().schedule(p1, 0, new ParametersAction(new StringParameterValue("test","test2")), new CauseAction(new Cause.UserIdCause()));

    List queue = request().get("/organizations/jenkins/pipelines/pipeline1/queue").build(List.class);
    Assert.assertEquals(2, queue.size());
    Assert.assertEquals(4, ((Map) queue.get(0)).get("expectedBuildNumber"));
    Assert.assertEquals(3, ((Map) queue.get(1)).get("expectedBuildNumber"));
    Assert.assertEquals("Waiting for next available executor", ((Map) queue.get(0)).get("causeOfBlockage"));
    Assert.assertEquals("Waiting for next available executor", ((Map) queue.get(1)).get("causeOfBlockage"));

    Run r = QueueUtil.getRun(p1, Long.parseLong((String)((Map)queue.get(0)).get("id")));
    assertNull(r); //its not moved out of queue yet
}
 
Example #5
Source File: JobRunnerForBranchCause.java    From github-integration-plugin with MIT License 6 votes vote down vote up
/**
 * Cancel previous builds for specified PR id.
 */
private static boolean cancelQueuedBuildByBranchName(final String branch) {
    Queue queue = getJenkinsInstance().getQueue();

    for (Queue.Item item : queue.getItems()) {
        Optional<Cause> cause = from(item.getAllActions())
                .filter(instanceOf(CauseAction.class))
                .transformAndConcat(new CausesFromAction())
                .filter(instanceOf(GitHubBranchCause.class))
                .firstMatch(new CauseHasBranch(branch));

        if (cause.isPresent()) {
            queue.cancel(item);
            return true;
        }
    }

    return false;
}
 
Example #6
Source File: CommitStatusUpdateQueueListenerTest.java    From DotCi with MIT License 5 votes vote down vote up
@Test
public void should_set_building_status_on_queue_enter() throws IOException {
    final GHRepository githubRepository = mock(GHRepository.class);
    final CommitStatusUpdateQueueListener commitStatusUpdateQueueListener = new CommitStatusUpdateQueueListener() {
        @Override
        protected GHRepository getGithubRepository(final DynamicProject project) {
            return githubRepository;
        }

        @Override
        protected String getJenkinsRootUrl() {
            return "jenkins.root.url";
        }
    };

    final BuildCause buildCause = mock(BuildCause.class);

    final ArrayList<Action> causeActions = new ArrayList<>();
    causeActions.add(new CauseAction(buildCause));
    final Queue.WaitingItem queueItem = new Queue.WaitingItem(null, mock(DynamicProject.class), causeActions);


    when(buildCause.getSha()).thenReturn("sha");

    commitStatusUpdateQueueListener.onEnterWaiting(queueItem);

    verify(githubRepository).createCommitStatus("sha", GHCommitState.PENDING, "jenkins.root.url", "Build in queue.", "DotCi/push");
}
 
Example #7
Source File: DynamicSubProject.java    From DotCi with MIT License 5 votes vote down vote up
public boolean scheduleBuild(final List<? extends Action> actions, final Cause c) {
    final List<Action> allActions = new ArrayList<>();
    if (actions != null) {
        allActions.addAll(actions);
    }

    allActions.add(new CauseAction(c));

    return Jenkins.getInstance().getQueue().schedule(this, getQuietPeriod(), allActions) != null;
}
 
Example #8
Source File: DynamicBuild.java    From DotCi with MIT License 5 votes vote down vote up
public void addCause(final Cause manualCause) {
    final List<Cause> exisitingCauses = this.getAction(CauseAction.class).getCauses();
    final ArrayList<Cause> causes = new ArrayList<>();
    causes.add(manualCause);
    causes.addAll(exisitingCauses);
    this.replaceAction(new CauseAction(causes));
}
 
Example #9
Source File: JenkinsEmbeddedMapper.java    From DotCi with MIT License 5 votes vote down vote up
private Class getClass(final DBObject dbObj) {
    // see if there is a className value
    final String className = (String) dbObj.get(Mapper.CLASS_NAME_FIELDNAME);
    if (className != null && className.equals(CauseAction.class.getName())) {
        // try to Class.forName(className) as defined in the dbObject first,
        // otherwise return the entityClass
        try {
            return Class.forName(className, true, getClassLoaderForClass(className, dbObj));
        } catch (final ClassNotFoundException e) {
            //Ignore if notfound
        }
    }
    return null;
}
 
Example #10
Source File: CauseActionConverter.java    From DotCi with MIT License 5 votes vote down vote up
@Override
public Object encode(final Object value, final MappedField optionalExtraInfo) {
    if (value == null) return null;
    final CauseAction action = (CauseAction) value;
    final List causes = new BasicDBList();

    for (final Object obj : action.getCauses()) {
        causes.add(getMapper().toDBObject(obj));
    }
    return BasicDBObjectBuilder.start("causes", causes).add("className", CauseAction.class.getName()).get();
}
 
Example #11
Source File: CauseActionConverter.java    From DotCi with MIT License 5 votes vote down vote up
@Override
public CauseAction decode(final Class targetClass, final Object fromDBObject, final MappedField optionalExtraInfo) {
    try (ACLContext _ = ACL.as(Jenkins.ANONYMOUS)) {
        if (fromDBObject == null) return null;

        final List causes = new ArrayList();
        final List rawList = (List) ((DBObject) fromDBObject).get("causes");
        for (final Object obj : rawList) {
            final DBObject dbObj = (DBObject) obj;
            final Object cause = getMapper().fromDBObject(optionalExtraInfo.getSubClass(), dbObj, getMapper().createEntityCache());
            causes.add(cause);
        }
        return new CauseAction(causes);
    }
}
 
Example #12
Source File: AbstractWebHookTriggerHandler.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
protected Action[] createActions(Job<?, ?> job, H hook) {
    ArrayList<Action> actions = new ArrayList<>();
    actions.add(new CauseAction(new GitLabWebHookCause(retrieveCauseData(hook))));
    try {
        SCMTriggerItem item = SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job);
        GitSCM gitSCM = getGitSCM(item);
        actions.add(createRevisionParameter(hook, gitSCM));
    } catch (NoRevisionToBuildException e) {
        LOGGER.log(Level.WARNING, "unknown handled situation, dont know what revision to build for req {0} for job {1}",
                new Object[]{hook, (job != null ? job.getFullName() : null)});
    }
    return actions.toArray(new Action[actions.size()]);
}
 
Example #13
Source File: OpenMergeRequestPushHookTriggerHandler.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
private void handleMergeRequest(Job<?, ?> job, PushHook hook, boolean ciSkip, BranchFilter branchFilter, GitLabClient client, MergeRequest mergeRequest) {
    if (ciSkip && mergeRequest.getDescription() != null && mergeRequest.getDescription().contains("[ci-skip]")) {
        LOGGER.log(Level.INFO, "Skipping MR " + mergeRequest.getTitle() + " due to ci-skip.");
        return;
    }

    Boolean workInProgress = mergeRequest.getWorkInProgress();
    if (skipWorkInProgressMergeRequest && workInProgress != null && workInProgress) {
        LOGGER.log(Level.INFO, "Skip WIP Merge Request #{0} ({1})", toArray(mergeRequest.getIid(), mergeRequest.getTitle()));
        return;
    }

    String sourceBranch = mergeRequest.getSourceBranch();
    String targetBranch = mergeRequest.getTargetBranch();
    if (targetBranch != null && branchFilter.isBranchAllowed(sourceBranch, targetBranch) && hook.getRef().equals("refs/heads/"+targetBranch) && sourceBranch != null) {
        LOGGER.log(Level.INFO, "{0} triggered for push to target branch of open merge request #{1}.",
                LoggerUtil.toArray(job.getFullName(), mergeRequest.getId()));

        Branch branch = client.getBranch(mergeRequest.getSourceProjectId().toString(), sourceBranch);
        Project project = client.getProject(mergeRequest.getSourceProjectId().toString());
        String commit = branch.getCommit().getId();
        setCommitStatusPendingIfNecessary(job, mergeRequest.getSourceProjectId(), commit, branch.getName());
        List<Action> actions = Arrays.<Action>asList(new CauseAction(new GitLabWebHookCause(retrieveCauseData(hook, project, mergeRequest, branch))),
                                                     new RevisionParameterAction(commit, retrieveUrIish(hook)));
        scheduleBuild(job, actions.toArray(new Action[actions.size()]));
    }
}
 
Example #14
Source File: JobRunnerForCauseTest.java    From github-integration-plugin with MIT License 5 votes vote down vote up
public static QueueTaskFuture schedule(Job<?, ?> job, int number, String param, int queuetPeriod) {
    ParameterizedJobMixIn jobMixIn = JobInfoHelpers.asParameterizedJobMixIn(job);
    GitHubPRCause cause = newGitHubPRCause().withNumber(number);
    ParametersAction parametersAction = new ParametersAction(
            Collections.<ParameterValue>singletonList(new StringParameterValue("value", param))
    );
    return jobMixIn.scheduleBuild2(queuetPeriod, new CauseAction(cause), parametersAction);
}
 
Example #15
Source File: GitHubSCMSource.java    From github-integration-plugin with MIT License 5 votes vote down vote up
@Nonnull
@Override
protected List<Action> retrieveActions(@Nonnull SCMRevision revision, @CheckForNull SCMHeadEvent event,
                                       @Nonnull TaskListener listener) throws IOException, InterruptedException {
    GitHubSCMRevision gitHubSCMRevision = (GitHubSCMRevision) revision;
    GitHubCause<?> cause = gitHubSCMRevision.getCause();
    if (nonNull(cause)) {
        List<ParameterValue> params = new ArrayList<>();
        cause.fillParameters(params);
        return Arrays.asList(new CauseAction(cause), new GitHubParametersAction(params));
    }

    return Collections.emptyList();
}
 
Example #16
Source File: PhabricatorBuildWrapperTest.java    From phabricator-jenkins-plugin with MIT License 5 votes vote down vote up
@Test
public void getUpstreamRunIfAvailable() throws Exception {
    FreeStyleBuild build = buildWithConduit(getFetchDiffResponse(), null, null, true);
    FreeStyleBuild upstream = buildWithConduit(getFetchDiffResponse(), null, null, true);
    assertNull(PhabricatorBuildWrapper.getUpstreamRun(build));

    List<Cause> causes = build.getAction(CauseAction.class).getCauses();
    ArrayList<Cause> newCauses = new ArrayList<Cause>(causes);
    newCauses.add((new Cause.UpstreamCause(upstream)));
    build.replaceAction(new CauseAction(newCauses));

    assertEquals(upstream, PhabricatorBuildWrapper.getUpstreamRun(build));
}
 
Example #17
Source File: PhabricatorBuildWrapper.java    From phabricator-jenkins-plugin with MIT License 5 votes vote down vote up
@VisibleForTesting
static Run<?, ?> getUpstreamRun(AbstractBuild build) {
    CauseAction action = build.getAction(hudson.model.CauseAction.class);
    if (action != null) {
        Cause.UpstreamCause upstreamCause = action.findCause(hudson.model.Cause.UpstreamCause.class);
        if (upstreamCause != null) {
            return upstreamCause.getUpstreamRun();
        }
    }
    return null;
}
 
Example #18
Source File: RunContainerImpl.java    From blueocean-plugin with MIT License 5 votes vote down vote up
/**
 * Schedules a build. If build already exists in the queue and the pipeline does not
 * support running multiple builds at the same time, return a reference to the existing
 * build.
 *
 * @return Queue item.
 */
@Override
public BlueRun create(StaplerRequest request) {
    job.checkPermission(Item.BUILD);
    if (job instanceof Queue.Task) {
        ScheduleResult scheduleResult;

        List<ParameterValue> parameterValues = getParameterValue(request);
        int expectedBuildNumber = job.getNextBuildNumber();
        if(parameterValues.size() > 0) {
            scheduleResult = Jenkins.getInstance()
                    .getQueue()
                    .schedule2((Queue.Task) job, 0, new ParametersAction(parameterValues),
                            new CauseAction(new Cause.UserIdCause()));
        }else {
            scheduleResult = Jenkins.getInstance()
                    .getQueue()
                    .schedule2((Queue.Task) job, 0, new CauseAction(new Cause.UserIdCause()));
        }
        // Keep FB happy.
        // scheduleResult.getItem() will always return non-null if scheduleResult.isAccepted() is true
        final Queue.Item item = scheduleResult.getItem();
        if(scheduleResult.isAccepted() && item != null) {
            return new QueueItemImpl(
                pipeline.getOrganization(),
                item,
                pipeline,
                expectedBuildNumber, pipeline.getLink().rel("queue").rel(Long.toString(item.getId())),
                pipeline.getLink()
            ).toRun();
        } else {
            throw new ServiceException.UnexpectedErrorException("Queue item request was not accepted");
        }
    } else {
        throw new ServiceException.NotImplementedException("This pipeline type does not support being queued.");
    }
}
 
Example #19
Source File: AbstractRunImpl.java    From blueocean-plugin with MIT License 5 votes vote down vote up
static Collection<BlueCause> getCauses(Run run) {
    CauseAction action = run.getAction(CauseAction.class);
    if (action == null) {
        return null;
    }
    return getCauses(action.getCauses());
}
 
Example #20
Source File: MultibranchPipelineRunContainer.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Override
public BlueRun create(StaplerRequest request) {
    blueMbPipeline.mbp.checkPermission(Item.BUILD);
    Queue.Item queueItem = blueMbPipeline.mbp.scheduleBuild2(0, new CauseAction(new Cause.UserIdCause()));
    if(queueItem == null){ // possible mbp.isBuildable() was false due to no sources fetched yet
        return null;
    }
    return new QueueItemImpl(
            blueMbPipeline.getOrganization(),
            queueItem,
            blueMbPipeline,
            1
    ).toRun();
}
 
Example #21
Source File: PipelinePluginAnalyticsTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
private void createAndRunPipeline(String jenkinsFileName) throws java.io.IOException, InterruptedException, java.util.concurrent.ExecutionException {
    // Create the pipeline and run it
    WorkflowJob scriptedSingle = createWorkflowJobWithJenkinsfile(getClass(), jenkinsFileName);
    WorkflowRun scriptedSingleRun = scriptedSingle.scheduleBuild2(0, new CauseAction()).waitForStart();
    j.waitForCompletion(scriptedSingleRun);
    // RunListeners can sometimes be executed after the run is reported as completed. This fixes a race condition.
    AnalyticsImpl analytics = (AnalyticsImpl) Analytics.get();
    Assert.assertNotNull(analytics);
    while (analytics.lastReq == null) {
        Thread.sleep(100);
    }
}
 
Example #22
Source File: JobHelper.java    From github-integration-plugin with MIT License 5 votes vote down vote up
public static boolean rebuild(Run<?, ?> run) {
    final QueueTaskFuture queueTaskFuture = asParameterizedJobMixIn(run.getParent())
            .scheduleBuild2(
                    0,
                    run.getAction(ParametersAction.class),
                    run.getAction(CauseAction.class),
                    run.getAction(BuildBadgeAction.class)
            );
    return queueTaskFuture != null;
}
 
Example #23
Source File: OrganizationFolderRunImpl.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Override
public BlueRun replay() {
    if(isReplayable()) {
        return new QueueItemImpl(this.pipeline.getOrganization(), pipeline.folder.scheduleBuild2(0,new CauseAction(new hudson.model.Cause.UserIdCause())), pipeline, 1).toRun();
    }
    return null;
}
 
Example #24
Source File: OrganizationFolderRunContainerImpl.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Override
public BlueRun create(StaplerRequest request) {
    pipeline.folder.checkPermission(Item.BUILD);
    Queue.Item queueItem = pipeline.folder.scheduleBuild2(0, new CauseAction(new Cause.UserIdCause()));
    if(queueItem == null){ // possible folder.isBuildable() was false due to no repo fetched yet
        return null;
    }
    return new QueueItemImpl(
            pipeline.getOrganization(),
            queueItem,
            pipeline,
            1
    ).toRun();
}
 
Example #25
Source File: JobAnalyticsTest.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Test
public void testJobAnalytics() throws Exception {
    // Freestyle jobs
    j.createFreeStyleProject("freestyle1").save();
    j.createFreeStyleProject("freestyle2").save();

    // Matrix job
    j.createProject(MatrixProject.class, "bob");

    // Create single scripted pipeline
    WorkflowJob scriptedSingle = createWorkflowJobWithJenkinsfile(getClass(),"JobAnalyticsTest-scripted.jenkinsfile");
    WorkflowRun scriptedSingleRun = scriptedSingle.scheduleBuild2(0, new CauseAction()).waitForStart();
    j.waitForCompletion(scriptedSingleRun);

    // Create single declarative pipeline
    WorkflowJob declarativeSingle = createWorkflowJobWithJenkinsfile(getClass(),"JobAnalyticsTest-declarative.jenkinsfile");
    WorkflowRun declarativeSingleRun = declarativeSingle.scheduleBuild2(0, new CauseAction()).waitForStart();
    j.waitForCompletion(declarativeSingleRun);

    // Create Scripted MultiBranch
    createMultiBranch(sampleRepo);

    // Create Declarative MultiBranch
    createMultiBranch(sampleRepo2);

    AnalyticsImpl analytics = (AnalyticsImpl)Analytics.get();
    Assert.assertNotNull(analytics);

    JobAnalytics jobAnalytics = new JobAnalytics();
    jobAnalytics.calculateAndSend();

    Assert.assertNotNull(analytics.lastReq);
    Assert.assertEquals("job_stats", analytics.lastReq.name);

    Map<String, Object> properties = analytics.lastReq.properties;
    Assert.assertEquals("singlePipelineDeclarative", 1, properties.get("singlePipelineDeclarative"));
    Assert.assertEquals("singlePipelineScripted",1, properties.get("singlePipelineScripted"));
    Assert.assertEquals("pipelineDeclarative",1, properties.get("pipelineDeclarative"));
    Assert.assertEquals("pipelineScripted",1, properties.get("pipelineScripted"));
    Assert.assertEquals("freestyle",2, properties.get("freestyle"));
    Assert.assertEquals("matrix",1, properties.get("matrix"));
    Assert.assertEquals("other",0, properties.get("other"));
}
 
Example #26
Source File: GitLabMergeRequestCommentTrigger.java    From gitlab-branch-source-plugin with MIT License 4 votes vote down vote up
@Override
public void isMatch() {
    if (getPayload().getObjectAttributes().getNoteableType()
        .equals(NoteEvent.NoteableType.MERGE_REQUEST)) {
        Integer mergeRequestId = getPayload().getMergeRequest().getIid();
        final Pattern mergeRequestJobNamePattern = Pattern
            .compile("^MR-" + mergeRequestId + "\\b.*$",
                Pattern.CASE_INSENSITIVE);
        final String commentBody = getPayload().getObjectAttributes().getNote();
        final String commentUrl = getPayload().getObjectAttributes().getUrl();
        try (ACLContext ctx = ACL.as(ACL.SYSTEM)) {
            boolean jobFound = false;
            for (final SCMSourceOwner owner : SCMSourceOwners.all()) {
                LOGGER.log(Level.FINEST, String.format("Source Owner: %s", owner.getFullDisplayName()));
                // This is a hack to skip owners which are children of a SCMNavigator
                if (owner.getFullDisplayName().contains(" ยป ")) {
                    continue;
                }
                for (SCMSource source : owner.getSCMSources()) {
                    if (!(source instanceof GitLabSCMSource)) {
                        continue;
                    }
                    GitLabSCMSource gitLabSCMSource = (GitLabSCMSource) source;
                    final GitLabSCMSourceContext sourceContext = new GitLabSCMSourceContext(
                        null, SCMHeadObserver.none())
                        .withTraits(gitLabSCMSource.getTraits());
                    if (!sourceContext.mrCommentTriggerEnabled()) {
                        continue;
                    }
                    if (gitLabSCMSource.getProjectId() == getPayload().getMergeRequest()
                        .getTargetProjectId() && isTrustedMember(gitLabSCMSource, sourceContext.onlyTrustedMembersCanTrigger())) {
                        for (Job<?, ?> job : owner.getAllJobs()) {
                            if (mergeRequestJobNamePattern.matcher(job.getName()).matches()) {
                                String expectedCommentBody = sourceContext.getCommentBody();
                                Pattern pattern = Pattern.compile(expectedCommentBody,
                                    Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
                                if (commentBody == null || pattern.matcher(commentBody)
                                    .matches()) {
                                    ParameterizedJobMixIn.scheduleBuild2(job, 0,
                                        new CauseAction(
                                            new GitLabMergeRequestCommentCause(commentUrl)));
                                    LOGGER.log(Level.INFO,
                                        "Triggered build for {0} due to MR comment on {1}",
                                        new Object[]{
                                            job.getFullName(),
                                            getPayload().getProject().getPathWithNamespace()
                                        }
                                    );
                                } else {
                                    LOGGER.log(Level.INFO,
                                        "MR comment does not match the trigger build string ({0}) for {1}",
                                        new Object[]{expectedCommentBody, job.getFullName()}
                                    );
                                }
                                break;
                            }
                            jobFound = true;
                        }
                    }
                }
            }
            if (!jobFound) {
                LOGGER.log(Level.INFO, "MR comment on {0} did not match any job",
                    new Object[]{
                        getPayload().getProject().getPathWithNamespace()
                    }
                );
            }
        }
    }
}
 
Example #27
Source File: PRUpdateGHEventSubscriber.java    From github-pr-comment-build-plugin with MIT License 4 votes vote down vote up
/**
 * Handles updates of pull requests.
 * @param event only PULL_REQUEST events
 * @param payload payload of gh-event. Never blank
 */
@Override
protected void onEvent(GHEvent event, String payload) {
    JSONObject json = JSONObject.fromObject(payload);

    // Since we receive both pull request and issue comment events in this same code,
    //  we check first which one it is and set values from different fields
    JSONObject pullRequest = json.getJSONObject("pull_request");
    final String pullRequestUrl = pullRequest.getString("html_url");
    Integer pullRequestId = pullRequest.getInt("number");

    // Make sure the action is edited
    String action = json.getString("action");
    if (!ACTION_EDITED.equals(action)) {
        LOGGER.log(Level.FINER, "Pull request action is not edited ({0}) for PR {1}, ignoring",
                new Object[] { action, pullRequestUrl }
        );
        return;
    }

    // Set some values used below
    final Pattern pullRequestJobNamePattern = Pattern.compile("^PR-" + pullRequestId + "\\b.*$",
            Pattern.CASE_INSENSITIVE);

    // Make sure the repository URL is valid
    String repoUrl = json.getJSONObject("repository").getString("html_url");
    Matcher matcher = REPOSITORY_NAME_PATTERN.matcher(repoUrl);
    if (!matcher.matches()) {
        LOGGER.log(Level.WARNING, "Malformed repository URL {0}", repoUrl);
        return;
    }
    final GitHubRepositoryName changedRepository = GitHubRepositoryName.create(repoUrl);
    if (changedRepository == null) {
        LOGGER.log(Level.WARNING, "Malformed repository URL {0}", repoUrl);
        return;
    }

    LOGGER.log(Level.FINE, "Received update on PR {1} for {2}", new Object[] { pullRequestId, repoUrl });
    ACL.impersonate(ACL.SYSTEM, new Runnable() {
        @Override
        public void run() {
            boolean jobFound = false;
            for (final SCMSourceOwner owner : SCMSourceOwners.all()) {
                for (SCMSource source : owner.getSCMSources()) {
                    if (!(source instanceof GitHubSCMSource)) {
                        continue;
                    }
                    GitHubSCMSource gitHubSCMSource = (GitHubSCMSource) source;
                    if (gitHubSCMSource.getRepoOwner().equalsIgnoreCase(changedRepository.getUserName()) &&
                            gitHubSCMSource.getRepository().equalsIgnoreCase(changedRepository.getRepositoryName())) {
                        for (Job<?, ?> job : owner.getAllJobs()) {
                            if (pullRequestJobNamePattern.matcher(job.getName()).matches()) {
                                if (!(job.getParent() instanceof MultiBranchProject)) {
                                    continue;
                                }
                                boolean propFound = false;
                                for (BranchProperty prop : ((MultiBranchProject) job.getParent()).getProjectFactory().
                                        getBranch(job).getProperties()) {
                                    if (!(prop instanceof TriggerPRUpdateBranchProperty)) {
                                        continue;
                                    }
                                    propFound = true;
                                    ParameterizedJobMixIn.scheduleBuild2(job, 0,
                                            new CauseAction(new GitHubPullRequestUpdateCause(pullRequestUrl)));
                                    break;
                                }

                                if (!propFound) {
                                    LOGGER.log(Level.FINE,
                                            "Job {0} for {1}:{2}/{3} does not have a trigger PR update branch property",
                                            new Object[] {
                                                    job.getFullName(),
                                                    changedRepository.getHost(),
                                                    changedRepository.getUserName(),
                                                    changedRepository.getRepositoryName()
                                            }
                                    );
                                }

                                jobFound = true;
                            }
                        }
                    }
                }
            }
            if (!jobFound) {
                LOGGER.log(Level.FINE, "PR update on {0}:{1}/{2} did not match any job",
                        new Object[] {
                                changedRepository.getHost(), changedRepository.getUserName(),
                                changedRepository.getRepositoryName()
                        }
                );
            }
        }
    });
}
 
Example #28
Source File: PRReviewGHEventSubscriber.java    From github-pr-comment-build-plugin with MIT License 4 votes vote down vote up
/**
 * Handles updates of pull requests.
 * @param event only PULL_REQUEST_REVIEW events
 * @param payload payload of gh-event. Never blank
 */
@Override
protected void onEvent(GHEvent event, String payload) {
    JSONObject json = JSONObject.fromObject(payload);

    JSONObject pullRequest = json.getJSONObject("pull_request");
    final String pullRequestUrl = pullRequest.getString("html_url");
    Integer pullRequestId = pullRequest.getInt("number");

    // Set some values used below
    final Pattern pullRequestJobNamePattern = Pattern.compile("^PR-" + pullRequestId + "\\b.*$",
            Pattern.CASE_INSENSITIVE);

    // Make sure the repository URL is valid
    String repoUrl = json.getJSONObject("repository").getString("html_url");
    Matcher matcher = REPOSITORY_NAME_PATTERN.matcher(repoUrl);
    if (!matcher.matches()) {
        LOGGER.log(Level.WARNING, "Malformed repository URL {0}", repoUrl);
        return;
    }
    final GitHubRepositoryName changedRepository = GitHubRepositoryName.create(repoUrl);
    if (changedRepository == null) {
        LOGGER.log(Level.WARNING, "Malformed repository URL {0}", repoUrl);
        return;
    }

    LOGGER.log(Level.FINE, "Received review on PR {1} for {2}", new Object[] { pullRequestId, repoUrl });
    ACL.impersonate(ACL.SYSTEM, new Runnable() {
        @Override
        public void run() {
            boolean jobFound = false;
            for (final SCMSourceOwner owner : SCMSourceOwners.all()) {
                for (SCMSource source : owner.getSCMSources()) {
                    if (!(source instanceof GitHubSCMSource)) {
                        continue;
                    }
                    GitHubSCMSource gitHubSCMSource = (GitHubSCMSource) source;
                    if (gitHubSCMSource.getRepoOwner().equalsIgnoreCase(changedRepository.getUserName()) &&
                            gitHubSCMSource.getRepository().equalsIgnoreCase(changedRepository.getRepositoryName())) {
                        for (Job<?, ?> job : owner.getAllJobs()) {
                            if (pullRequestJobNamePattern.matcher(job.getName()).matches()) {
                                if (!(job.getParent() instanceof MultiBranchProject)) {
                                    continue;
                                }
                                boolean propFound = false;
                                for (BranchProperty prop : ((MultiBranchProject) job.getParent()).getProjectFactory().
                                        getBranch(job).getProperties()) {
                                    if (!(prop instanceof TriggerPRReviewBranchProperty)) {
                                        continue;
                                    }
                                    propFound = true;
                                    ParameterizedJobMixIn.scheduleBuild2(job, 0,
                                            new CauseAction(new GitHubPullRequestReviewCause(pullRequestUrl)));
                                    break;
                                }

                                if (!propFound) {
                                    LOGGER.log(Level.FINE,
                                            "Job {0} for {1}:{2}/{3} does not have a trigger PR review branch property",
                                            new Object[] {
                                                    job.getFullName(),
                                                    changedRepository.getHost(),
                                                    changedRepository.getUserName(),
                                                    changedRepository.getRepositoryName()
                                            }
                                    );
                                }

                                jobFound = true;
                            }
                        }
                    }
                }
            }
            if (!jobFound) {
                LOGGER.log(Level.FINE, "PR review on {0}:{1}/{2} did not match any job",
                        new Object[] {
                                changedRepository.getHost(), changedRepository.getUserName(),
                                changedRepository.getRepositoryName()
                        }
                );
            }
        }
    });
}
 
Example #29
Source File: CauseActionConverter.java    From DotCi with MIT License 4 votes vote down vote up
public CauseActionConverter() {
    super(CauseAction.class);
}
 
Example #30
Source File: HistoryAggregatedFlakyTestResultActionTest.java    From flaky-test-handler-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void testAggregate() throws Exception {
  FreeStyleProject project = jenkins.createFreeStyleProject("project");
  List<FlakyTestResultAction> flakyTestResultActions = setUpFlakyTestResultAction();

  List<FlakyTestResultAction> flakyTestResultActionList = new ArrayList<FlakyTestResultAction>(
      flakyTestResultActions);

  // First non-deflake build
  Run firstBuild = project
      .scheduleBuild2(0, flakyTestResultActionList.get(0)).get();
  while (firstBuild.isBuilding()) {
    Thread.sleep(100);
  }

  // Second deflake build
  Run secondBuild = project
      .scheduleBuild2(0, flakyTestResultActionList.get(1),
          new CauseAction(new DeflakeCause(firstBuild))).get();
  while (secondBuild.isBuilding()) {
    Thread.sleep(100);
  }

  // Third deflake build with HistoryAggregatedFlakyTestResultAction
  Run thirdBuild = project
      .scheduleBuild2(0, flakyTestResultActionList.get(2),
          new HistoryAggregatedFlakyTestResultAction(project)).get();
  while (thirdBuild.isBuilding()) {
    Thread.sleep(100);
  }

  HistoryAggregatedFlakyTestResultAction action = thirdBuild
      .getAction(HistoryAggregatedFlakyTestResultAction.class);
  action.aggregate();

  Map<String, SingleTestFlakyStats> aggregatedFlakyStatsMap = action.getAggregatedFlakyStats();

  // Make sure revisions are inserted in the order of their appearance
  Map<String, SingleTestFlakyStats> revisionMap = action.getAggregatedTestFlakyStatsWithRevision()
      .get(TEST_ONE);
  assertArrayEquals("Incorrect revision history", new String[]{REVISION_ONE, REVISION_TWO},
      revisionMap.keySet().toArray(new String[revisionMap.size()]));

  assertEquals("wrong number of entries for flaky stats", 4, aggregatedFlakyStatsMap.size());

  SingleTestFlakyStats testOneStats = aggregatedFlakyStatsMap.get(TEST_ONE);
  SingleTestFlakyStats testTwoStats = aggregatedFlakyStatsMap.get(TEST_TWO);
  SingleTestFlakyStats testThreeStats = aggregatedFlakyStatsMap.get(TEST_THREE);
  SingleTestFlakyStats testFourStats = aggregatedFlakyStatsMap.get(TEST_FOUR);

  assertEquals("wrong number passes", 1, testOneStats.getPass());
  assertEquals("wrong number fails", 0, testOneStats.getFail());
  assertEquals("wrong number flakes", 1, testOneStats.getFlake());

  assertEquals("wrong number passes", 1, testTwoStats.getPass());
  assertEquals("wrong number fails", 0, testTwoStats.getFail());
  assertEquals("wrong number flakes", 1, testTwoStats.getFlake());

  assertEquals("wrong number passes", 0, testThreeStats.getPass());
  assertEquals("wrong number fails", 1, testThreeStats.getFail());
  assertEquals("wrong number flakes", 1, testThreeStats.getFlake());

  assertEquals("wrong number passes", 1, testFourStats.getPass());
  assertEquals("wrong number fails", 0, testFourStats.getFail());
  assertEquals("wrong number flakes", 1, testFourStats.getFlake());
}