Skip to content
Permalink
Browse files

[JENKINS-33578] Displays error or warning messages when applied Downs…

…treamBuildSelector to Workflow jobs.
  • Loading branch information...
ikedam committed Apr 3, 2016
1 parent 1f34b25 commit cafa6135bccb864c5aaadb8af3614d343498a15c
@@ -90,6 +90,7 @@ public String getUpstreamBuildNumber() {
@Override
protected boolean isSelectable(Run<?, ?> run, EnvVars env) {
if (!(run instanceof AbstractBuild<?,?>)) {
// As this feature depends on `AbstractBuild#getUpstreamRelationshipBuild(AbstractProject<?,?>)`
LOGGER.warning(String.format("Only applicable to AbstractBuild: but is %s.", run.getClass().getName()));
return false;
}
@@ -123,18 +124,31 @@ protected boolean isSelectable(Run<?, ?> run, EnvVars env) {
return false;
}

AbstractProject<?,?> upstreamProject = jenkins.getItem(
Job<?,?> upstreamJob = jenkins.getItem(
projectName,
copier,
AbstractProject.class
Job.class
);
if (upstreamProject == null || !upstreamProject.hasPermission(Item.READ)) {
if (upstreamJob == null || !upstreamJob.hasPermission(Item.READ)) {
LOGGER.warning(String.format("Upstream project '%s' is not found.", projectName));
return false;
}
AbstractBuild<?,?> upstreamBuild = ((AbstractBuild<?,?>)run).getUpstreamRelationshipBuild(upstreamProject);
if (!(upstreamJob instanceof AbstractProject)) {
// As this feature depends on `AbstractBuild#getUpstreamRelationshipBuild(AbstractProject<?,?>)`
LOGGER.log(
Level.WARNING,
"Only applicable to AbstractProject: but {0} is a {1}.",
new Object[] {
upstreamJob.getFullName(),
upstreamJob.getClass().getName(),
}
);
return false;
}

AbstractBuild<?,?> upstreamBuild = ((AbstractBuild<?,?>)run).getUpstreamRelationshipBuild((AbstractProject<?, ?>)upstreamJob);
if (upstreamBuild == null || !upstreamBuild.hasPermission(Item.READ)) {
LOGGER.fine(String.format("No upstream build of project '%s' is found for build %s-%s.", upstreamProject.getFullName(), run.getParent().getFullName(), run.getDisplayName()));
LOGGER.fine(String.format("No upstream build of project '%s' is found for build %s-%s.", upstreamJob.getFullName(), run.getParent().getFullName(), run.getDisplayName()));
return false;
}

@@ -214,6 +228,15 @@ public FormValidation doCheckUpstreamProjectName(
if (upstreamProject == null || !upstreamProject.hasPermission(Item.READ)) {
return FormValidation.error(Messages.DownstreamBuildSelector_UpstreamProjectName_NotFound());
}

if (!(upstreamProject instanceof AbstractProject)) {
return FormValidation.error(
Messages.DownstreamBuildSelector_UpstreamProjectName_NotAbstractProject(
upstreamProject.getClass().getName()
)
);
}

return FormValidation.ok();
}

@@ -8,4 +8,15 @@
That is, a build that is triggerd from a build isn't always considered downstream,
but you need to fingerprint files used in builds to let Jenkins track them.
</p>
<p>
Note: "Downsteram build of" is applicable only to AbstractProject based projects (both upstream and downstream projects).
<dl>
<dt>AbstractProject based projects:</dt>
<dd>Freestyle projects</dd>
<dd>Multi-configuration projects</dd>
<dd>Maven projects</dd>
<dt>Non AbstractProject based projects:</dt>
<dd>Pipeline jobs (aka. Workflow jobs)</dd>
</dl>
</p>
</div>
@@ -7,4 +7,16 @@
下流ビルドはファイル指紋を使用して判定されます。
このため、単にビルドをトリガーしただけでは下流ビルドとはみなされず、ビルドで共通で使用したファイルの指紋を記録して Jenkins がファイルの利用状況を追跡できるようにする必要があることに注意してください。
</p>
<p>
「指定ビルドの下流ビルド」は上流、下流双方のプロジェクトが AbstractProject から派生したプロジェクトの場合のみ適用可能です。
<dl>
<dt>AbstractProject から派生したプロジェクト:</dt>
<dd>フリースタイルプロジェクト</dd>
<dd>マルチ構成プロジェクト</dd>
<dd>Maven プロジェクト</dd>
<dt>AbstractProject から派生していないプロジェクト:</dt>
<dd>Pipeline ジョブ (旧名 Workflow ジョブ)</dd>
</dl>
</p>

</div>
@@ -29,5 +29,6 @@ CopyArtifactPermissionProperty.MissingProject=Unable to find project: {0}
DownstreamBuildSelector.DisplayName=Downstream build of
DownstreamBuildSelector.UpstreamProjectName.Required=Required
DownstreamBuildSelector.UpstreamProjectName.NotFound=Not Found
DownstreamBuildSelector.UpstreamProjectName.NotAbstractProject="Downstream build of" is not applicable to this project ({0})
DownstreamBuildSelector.UpstreamBuildNumber.Required=Required
DownstreamBuildSelector.UpstreamBuildNumber.NotFound=Not Found
@@ -23,6 +23,8 @@ DownstreamBuildSelector.DisplayName=\u6307\u5b9a\u30d3\u30eb\u30c9\u306e\u4e0b\u
DownstreamBuildSelector.UpstreamProjectName.Required=\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044
#DownstreamBuildSelector.UpstreamProjectName.NotFound=指定のプロジェクトが見つかりません
DownstreamBuildSelector.UpstreamProjectName.NotFound=\u6307\u5b9a\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u304c\u898b\u3064\u304b\u308a\u307e\u305b\u3093
#DownstreamBuildSelector.UpstreamProjectName.NotAbstractProject=「指定ビルドの下流ビルド」はこの種類のプロジェクトには適用できません ({0})
DownstreamBuildSelector.UpstreamProjectName.NotAbstractProject=\u300c\u6307\u5b9a\u30d3\u30eb\u30c9\u306e\u4e0b\u6d41\u30d3\u30eb\u30c9\u300d\u306f\u3053\u306e\u7a2e\u985e\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u306f\u9069\u7528\u3067\u304d\u307e\u305b\u3093 ({0})
#DownstreamBuildSelector.UpstreamBuildNumber.Required=入力してください
DownstreamBuildSelector.UpstreamBuildNumber.Required=\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044
#DownstreamBuildSelector.UpstreamBuildNumber.NotFound=指定のビルドが見つかりません
@@ -57,6 +57,7 @@

import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
@@ -695,4 +696,24 @@ private void testAutoCompleteUpstreamProjectName(
actualValues = new TreeSet<String>(d.doAutoCompleteUpstreamProjectName(value, null).getValues());
assertArrayEquals(new String[] {}, actualValues.toArray(new String [actualValues.size()]));
}

@Test
public void testCheckUpstreamProjectNameForWorkflow() throws Exception {
FreeStyleProject context = j.createFreeStyleProject();
WorkflowJob target = j.jenkins.createProject(WorkflowJob.class, "workflow-test");

DownstreamBuildSelector.DescriptorImpl d = (DownstreamBuildSelector.DescriptorImpl)j.jenkins.getDescriptorOrDie(DownstreamBuildSelector.class);
// DownstreamBuildSelector is not applicable to workflow.
assertEquals(FormValidation.Kind.ERROR, d.doCheckUpstreamProjectName(context, target.getFullName()).kind);
}

@Test
public void testAutoCompleteUpstreamProjectNameForWorkflow() throws Exception {
FreeStyleProject context = j.createFreeStyleProject();
WorkflowJob target = j.jenkins.createProject(WorkflowJob.class, "workflow-test");

DownstreamBuildSelector.DescriptorImpl d = (DownstreamBuildSelector.DescriptorImpl)j.jenkins.getDescriptorOrDie(DownstreamBuildSelector.class);
// DownstreamBuildSelector is not applicable to workflow.
assertFalse(d.doAutoCompleteUpstreamProjectName(target.getFullDisplayName(), context).getValues().contains(target.getFullDisplayName()));
}
}

0 comments on commit cafa613

Please sign in to comment.
You can’t perform that action at this time.