下面列出了hudson.model.Run#getCause ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void buildEnvironmentFor(@Nonnull Run r, @Nonnull EnvVars envs, @Nonnull TaskListener listener) {
GitLabWebHookCause gitLabWebHookCause = null;
if (r instanceof WorkflowRun) {
gitLabWebHookCause = (GitLabWebHookCause) r.getCause(GitLabWebHookCause.class);
}
envs.override("GITLAB_OBJECT_KIND", "none");
if (gitLabWebHookCause != null) {
if(gitLabWebHookCause.getGitLabPushCauseData() != null) {
envs.overrideAll(gitLabWebHookCause.getGitLabPushCauseData().getBuildVariables());
} else if(gitLabWebHookCause.getGitLabMergeRequestCauseData() != null) {
envs.overrideAll(gitLabWebHookCause.getGitLabMergeRequestCauseData().getBuildVariables());
} else if(gitLabWebHookCause.getGitLabTagPushCauseData() != null) {
envs.overrideAll(gitLabWebHookCause.getGitLabTagPushCauseData().getBuildVariables());
}
}
}
private void notifyCoverage(String jobName, @Nullable CodeCoverage coverageInfo, Run<?, ?> run) {
if (coverageInfo != null) {
String buildUrl = run.getUrl();
int buildNumber = run.getNumber();
Cause cause = run.getCause(Cause.class);
String buildCause = cause == null ? BuildNotifierConstants.DEFAULT_STRING : cause.getShortDescription();
String data = config.getSchema().formatCoverage(jobName,
repoOwner,
repoName,
branchName,
coverageInfo.getClasses(),
coverageInfo.getConditionals(),
coverageInfo.getFiles(),
coverageInfo.getLines(),
coverageInfo.getMethods(),
coverageInfo.getPackages(),
coverageInfo.getInstructions(),
buildUrl,
buildNumber,
buildCause);
postData(data);
}
}
private void notifyTestResults(String jobName, @Nullable TestResults testResults, Run<?, ?> run) {
if (testResults != null) {
String buildUrl = run.getUrl();
int buildNumber = run.getNumber();
Cause cause = run.getCause(Cause.class);
String buildCause = cause == null ? BuildNotifierConstants.DEFAULT_STRING : cause.getShortDescription();
String data = config.getSchema().formatTests(jobName,
repoOwner,
repoName,
branchName,
testResults.getPassedTestCaseCount(),
testResults.getSkippedTestCaseCount(),
testResults.getFailedTestCaseCount(),
buildUrl,
buildNumber,
buildCause);
postData(data);
for (TestSuite testSuite : testResults.getTestSuites()) {
notifyTestSuite(jobName, testSuite, run);
}
}
}
private String notifyTestCase(String jobName, String suiteName, TestCase testCase, Run<?, ?> run) {
String buildUrl = run.getUrl();
int buildNumber = run.getNumber();
Cause cause = run.getCause(Cause.class);
String buildCause = cause == null ? BuildNotifierConstants.DEFAULT_STRING : cause.getShortDescription();
String data = config.getSchema().formatTestCase(jobName,
repoOwner,
repoName,
branchName,
suiteName,
testCase.getName(),
testCase.getPassedCount(),
testCase.getSkippedCount(),
testCase.getFailedCount(),
buildUrl,
buildNumber,
buildCause);
return data;
}
private BuildStatus constructBuildStatus(BuildStage.State buildState, Map<String, Object> parameters) {
Run<?, ?> run = (Run<?, ?>) parameters.get(BuildNotifierConstants.BUILD_OBJECT);
String jobName = (String) parameters.getOrDefault(BuildNotifierConstants.JOB_NAME, BuildNotifierConstants.DEFAULT_STRING);
long blockedDuration = BuildNotifierConstants.getLong(parameters, BuildNotifierConstants.BLOCKED_DURATION);
String buildUrl = run.getUrl();
int buildNumber = run.getNumber();
long buildDuration = BuildNotifierConstants.getLong(parameters, BuildNotifierConstants.JOB_DURATION) - blockedDuration;
Cause cause = run.getCause(Cause.class);
String buildCause = cause == null ? BuildNotifierConstants.DEFAULT_STRING : cause.getShortDescription();
BuildStatus result = new org.jenkinsci.plugins.githubautostatus.model.BuildStatus();
result.setRepoOwner(repoOwner);
result.setRepoName(repoName);
result.setJobName(jobName);
result.setBranch(branchName);
result.setBuildUrl(buildUrl);
result.setBuildNumber(buildNumber);
result.setTrigger(buildCause);
result.setBlocked(blockedDuration > 0);
result.setBlockedTime(blockedDuration);
result.setDuration(buildDuration);
result.setPassed(buildState == BuildStage.State.CompletedSuccess);
result.setResult(buildState);
result.setTimestamp(Clock.system(TimeZone.getTimeZone("UTC").toZoneId()).millis() / 1000);
return result;
}
/**
* Sends the final build status to InfluxDB.
*
* @param buildState the new state
* @param parameters build parameters
*/
@Override
public void notifyFinalBuildStatus(BuildStage.State buildState, Map<String, Object> parameters) {
Run<?, ?> run = (Run<?, ?>) parameters.get(BuildNotifierConstants.BUILD_OBJECT);
String jobName = (String) parameters.getOrDefault(BuildNotifierConstants.JOB_NAME, BuildNotifierConstants.DEFAULT_STRING);
int passed = buildState == BuildStage.State.CompletedSuccess ? 1 : 0;
long blockedDuration = BuildNotifierConstants.getLong(parameters, BuildNotifierConstants.BLOCKED_DURATION);
int blocked = blockedDuration > 0 ? 1 : 0;
String buildUrl = run.getUrl();
int buildNumber = run.getNumber();
Cause cause = run.getCause(Cause.class);
String buildCause = cause == null ? BuildNotifierConstants.DEFAULT_STRING : cause.getShortDescription();
String data = config.getSchema().formatJob(jobName,
repoOwner,
repoName,
branchName,
buildState.toString(),
blocked,
BuildNotifierConstants.getLong(parameters, BuildNotifierConstants.JOB_DURATION) - blockedDuration,
blockedDuration,
passed,
buildUrl,
buildNumber,
buildCause);
postData(data);
if (!this.config.getIgnoreSendingTestResultsToInflux()) {
notifyTestResults(jobName, (TestResults) parameters.get(BuildNotifierConstants.TEST_CASE_INFO), run);
}
if (!this.config.getIgnoreSendingTestCoverageToInflux()) {
notifyCoverage(jobName, (CodeCoverage) parameters.get(BuildNotifierConstants.COVERAGE_INFO), run);
}
}
private void notifyTestSuite(String jobName, TestSuite testSuite, Run<?, ?> run) {
String suiteName = testSuite.getName();
String buildUrl = run.getUrl();
int buildNumber = run.getNumber();
Cause cause = run.getCause(Cause.class);
String buildCause = cause == null ? BuildNotifierConstants.DEFAULT_STRING : cause.getShortDescription();
List<String> testSuiteQuery = new ArrayList<>();
String data = config.getSchema().formatTestSuite(jobName,
repoOwner,
repoName,
branchName,
suiteName,
testSuite.getDuration(),
testSuite.getPassedTestCaseCount(),
testSuite.getSkippedTestCaseCount(),
testSuite.getFailedTestCaseCount(),
buildUrl,
buildNumber,
buildCause);
testSuiteQuery.add(data);
for (TestCase testCase : testSuite.getTestCases()) {
testSuiteQuery.add(notifyTestCase(jobName, suiteName, testCase, run));
}
postData(String.join("\\n", testSuiteQuery));
}
/**
* matrix-project requires special extraction.
*/
@CheckForNull
public static <T extends Cause> T ghCauseFromRun(Run<?, ?> run, Class<T> tClass) {
if (run instanceof MatrixRun) {
MatrixBuild parentBuild = ((MatrixRun) run).getParentBuild();
if (nonNull(parentBuild)) {
return parentBuild.getCause(tClass);
}
} else {
return run.getCause(tClass);
}
return null;
}
@Override
public void onStarted(Run<?, ?> build, TaskListener listener) {
GitLabPushTrigger trigger = GitLabPushTrigger.getFromJob(build.getParent());
if (trigger != null && trigger.getSetBuildDescription()) {
Cause cause = build.getCause(GitLabWebHookCause.class);
if (cause != null && !cause.getShortDescription().isEmpty()) {
try {
build.setDescription(cause.getShortDescription());
} catch (IOException e) {
listener.getLogger().println("Failed to set build description");
}
}
}
}
@Override
public void buildEnvironmentFor(@Nonnull Run r, @Nonnull EnvVars envs, @Nonnull TaskListener listener) throws IOException, InterruptedException {
GitLabWebHookCause cause = null;
if (r instanceof MatrixRun) {
MatrixBuild parent = ((MatrixRun)r).getParentBuild();
if (parent != null) {
cause = (GitLabWebHookCause) parent.getCause(GitLabWebHookCause.class);
}
} else {
cause = (GitLabWebHookCause) r.getCause(GitLabWebHookCause.class);
}
if (cause != null) {
envs.overrideAll(cause.getData().getBuildVariables());
}
}
public Set<String> getCommitters(Run<?, ?> run) {
Set<String> committers = getCommittersForRun(run);
//If no committers were found, recursively get upstream committers:
if (committers.isEmpty()) {
Cause.UpstreamCause upstreamCause = run.getCause(Cause.UpstreamCause.class);
if (upstreamCause != null) {
Run<?, ?> upstreamRun = upstreamCause.getUpstreamRun();
if (upstreamRun != null) {
committers.addAll(getCommitters(upstreamRun));
}
}
}
return committers;
}
public synchronized int abortRunning(int number) throws IllegalAccessException {
int aborted = 0;
Computer[] computers = getJenkinsInstance().getComputers();
for (Computer computer : computers) {
if (isNull(computer)) {
continue;
}
List<Executor> executors = computer.getExecutors();
executors.addAll(computer.getOneOffExecutors());
for (Executor executor : executors) {
if (isNull(executor) || !executor.isBusy() || nonNull(executor.getCauseOfDeath()) ||
!getInterruptCauses(executor).isEmpty() || getInterruptStatus(executor) == Result.ABORTED) {
continue;
}
Queue.Executable executable = executor.getCurrentExecutable();
final SubTask parent = executable.getParent();
if (!(executable instanceof Run)) {
continue;
}
final Run executableRun = (Run) executable;
if (!(parent instanceof Job)) {
continue;
}
final Job parentJob = (Job) parent;
if (!parentJob.getFullName().equals(job.getFullName())) {
// name doesn't match
continue;
}
if (executableRun.getResult() == Result.ABORTED) {
// was already aborted
continue;
}
if (executableRun instanceof MatrixRun) {
// the whole MatrixBuild will be aborted
continue;
}
// if (executable instanceof MatrixBuild) {
// final MatrixBuild executable1 = (MatrixBuild) executable;
// executable1.doStop()
// }
final GitHubPRCause causeAction = (GitHubPRCause) executableRun.getCause(GitHubPRCause.class);
if (nonNull(causeAction) && causeAction.getNumber() == number) {
LOGGER.info("Aborting '{}', by interrupting '{}'", executableRun, executor);
executor.interrupt(Result.ABORTED, new NewPRInterruptCause());
aborted++;
}
}
}
return aborted;
}
MergeRequest getMergeRequest(Run<?, ?> run) {
GitLabWebHookCause cause = run.getCause(GitLabWebHookCause.class);
return cause == null ? null : cause.getData().getMergeRequest();
}
private String getTargetBranchFromBuild(Run<?, ?> mergeBuild) {
GitLabWebHookCause cause = mergeBuild.getCause(GitLabWebHookCause.class);
return cause == null ? null : cause.getData().getTargetBranch();
}