private static List<GitLabBranchBuild> findBuildsFromUpstreamCauses(List<Cause> causes) { for (Cause cause : causes) { if (cause instanceof UpstreamCause) { List<Cause> upCauses = ((UpstreamCause) cause).getUpstreamCauses(); // Non null, returns empty list when none are set for (Cause upCause : upCauses) { if (upCause instanceof GitLabWebHookCause) { GitLabWebHookCause gitlabCause = (GitLabWebHookCause) upCause; return Collections.singletonList( new GitLabBranchBuild(gitlabCause.getData().getSourceProjectId().toString(), gitlabCause.getData().getLastCommit())); } } List<GitLabBranchBuild> builds = findBuildsFromUpstreamCauses(upCauses); if (!builds.isEmpty()) { return builds; } } } return Collections.emptyList(); }
/** * Return the current build user. * * @param causes * build causes. * @return user name. */ protected static String prepareBuildUser(List<Cause> causes) { String buildUser = "anonymous"; if (causes != null && causes.size() > 0) { if (causes.get(0) instanceof UserIdCause) { buildUser = ((UserIdCause) causes.get(0)).getUserName(); } else if (causes.get(0) instanceof UpstreamCause) { List<Cause> upstreamCauses = ((UpstreamCause) causes.get(0)).getUpstreamCauses(); prepareBuildUser(upstreamCauses); } } return buildUser; }
/** * Return the current build user. * * @param causes build causes. * @return user name. */ protected static String prepareBuildUser(List<Cause> causes) { String buildUser = "anonymous"; if (causes != null && causes.size() > 0) { if (causes.get(0) instanceof UserIdCause) { buildUser = ((UserIdCause) causes.get(0)).getUserId(); } else if (causes.get(0) instanceof UpstreamCause) { List<Cause> upstreamCauses = ((UpstreamCause) causes.get(0)).getUpstreamCauses(); prepareBuildUser(upstreamCauses); } } return Util.fixEmpty(buildUser) == null ? "anonymous" : buildUser; }
/** * Return the current build user. * * @param causes build causes. * @return user name. */ protected static String prepareBuildUser(List<Cause> causes) { String buildUser = "anonymous"; if (causes != null && causes.size() > 0) { if (causes.get(0) instanceof UserIdCause) { buildUser = ((UserIdCause) causes.get(0)).getUserName(); } else if (causes.get(0) instanceof UpstreamCause) { List<Cause> upstreamCauses = ((UpstreamCause) causes.get(0)).getUpstreamCauses(); prepareBuildUser(upstreamCauses); } } return buildUser; }
@Nonnull private HashSet<Run<?, ?>> getAllUpstreamBuilds(@Nonnull Job<?, ?> job, @Nonnull RunSelectorContext context, @Nonnull Run<?, ?> parent) { HashSet<Run<?, ?>> result = new HashSet<Run<?, ?>>(); // Upstream job for matrix will be parent project, not only individual configuration: List<String> jobNames = new ArrayList<String>(); jobNames.add(job.getFullName()); if ((job instanceof AbstractProject<?,?>) && ((AbstractProject<?,?>)job).getRootProject() != job) { jobNames.add(((AbstractProject<?,?>)job).getRootProject().getFullName()); } List<Run<?, ?>> upstreamBuilds = new ArrayList<Run<?, ?>>(); for (Cause cause: parent.getCauses()) { if (cause instanceof UpstreamCause) { UpstreamCause upstream = (UpstreamCause) cause; Run<?, ?> upstreamRun = upstream.getUpstreamRun(); if (upstreamRun != null) { upstreamBuilds.add(upstreamRun); } } } if (isAllowUpstreamDependencies() && (parent instanceof AbstractBuild)) { AbstractBuild<?, ?> parentBuild = (AbstractBuild<?,?>)parent; Map<AbstractProject, Integer> parentUpstreamBuilds = parentBuild.getUpstreamBuilds(); for (Map.Entry<AbstractProject, Integer> buildEntry : parentUpstreamBuilds.entrySet()) { upstreamBuilds.add(buildEntry.getKey().getBuildByNumber(buildEntry.getValue())); } } for (Run<?, ?> upstreamBuild : upstreamBuilds) { if (jobNames.contains(upstreamBuild.getParent().getFullName())) { // Use the 'job' parameter instead of directly the 'upstreamBuild', because of Matrix jobs. result.add(job.getBuildByNumber(upstreamBuild.getNumber())); } else { // Figure out the parent job and do a recursive call to getBuild result.addAll(getAllUpstreamBuilds(job, context, upstreamBuild)); } } return result; }
public DynamicBuildFactory upstreamStart(String branch) { Cause cause = mock(UpstreamCause.class); when(build.getCauses()).thenReturn(Lists.newArrayList(cause)); addBranchParam(branch); return this; }