Skip to content

Commit c562d56

Browse files
Add checkSamples task
Closes gh-9846
1 parent 6370906 commit c562d56

File tree

6 files changed

+299
-0
lines changed

6 files changed

+299
-0
lines changed

.github/workflows/continuous-integration-workflow.yml

+20
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,26 @@ jobs:
111111
export GRADLE_ENTERPRISE_CACHE_PASSWORD="$GRADLE_ENTERPRISE_CACHE_PASSWORD"
112112
export GRADLE_ENTERPRISE_ACCESS_KEY="$GRADLE_ENTERPRISE_SECRET_ACCESS_KEY"
113113
./gradlew sonarqube -PartifactoryUsername="$ARTIFACTORY_USERNAME" -PartifactoryPassword="$ARTIFACTORY_PASSWORD" -PexcludeProjects='**/samples/**' -Dsonar.host.url="$SONAR_URL" -Dsonar.login="$SONAR_TOKEN" --stacktrace
114+
check_samples:
115+
name: Check Samples project
116+
needs: [build_jdk_11]
117+
runs-on: ubuntu-latest
118+
steps:
119+
- uses: actions/checkout@v2
120+
- name: Set up JDK
121+
uses: actions/setup-java@v1
122+
with:
123+
java-version: '11'
124+
- name: Setup gradle user name
125+
run: |
126+
mkdir -p ~/.gradle
127+
echo 'systemProp.user.name=spring-builds' >> ~/.gradle/gradle.properties
128+
- name: Check samples project
129+
run: |
130+
export GRADLE_ENTERPRISE_CACHE_USERNAME="$GRADLE_ENTERPRISE_CACHE_USER"
131+
export GRADLE_ENTERPRISE_CACHE_PASSWORD="$GRADLE_ENTERPRISE_CACHE_PASSWORD"
132+
export GRADLE_ENTERPRISE_ACCESS_KEY="$GRADLE_ENTERPRISE_SECRET_ACCESS_KEY"
133+
./gradlew checkSamples --stacktrace
114134
deploy_artifacts:
115135
name: Deploy Artifacts
116136
needs: [build_jdk_11, snapshot_tests, sonar_analysis]

build.gradle

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ buildscript {
1515
apply plugin: 'io.spring.nohttp'
1616
apply plugin: 'locks'
1717
apply plugin: 'io.spring.convention.root'
18+
apply plugin: 'io.spring.convention.include-check-remote'
1819
apply plugin: 'org.jetbrains.kotlin.jvm'
1920
apply plugin: 'org.springframework.security.update-dependencies'
2021
apply plugin: 'org.springframework.security.sagan'
@@ -145,3 +146,11 @@ nohttp {
145146
source.exclude "buildSrc/build/**"
146147

147148
}
149+
150+
tasks.register('checkSamples') {
151+
includeCheckRemote {
152+
repository = 'spring-projects/spring-security-samples'
153+
ref = samplesBranch
154+
}
155+
dependsOn checkRemote
156+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
18+
package io.spring.gradle
19+
20+
import groovy.transform.CompileStatic
21+
import groovy.transform.TypeChecked
22+
import groovy.transform.TypeCheckingMode
23+
import org.gradle.api.DefaultTask
24+
import org.gradle.api.Task
25+
import org.gradle.api.provider.Property
26+
import org.gradle.api.tasks.Input
27+
import org.gradle.api.tasks.OutputDirectory
28+
import org.gradle.api.tasks.TaskAction
29+
30+
/**
31+
* Checkout a project template from a git repository.
32+
*
33+
* @author Marcus Da Coregio
34+
*/
35+
@CompileStatic
36+
abstract class IncludeRepoTask extends DefaultTask {
37+
38+
private static final String DEFAULT_URI_PREFIX = 'https://github.com/'
39+
40+
/**
41+
* Git repository to use. Will be prefixed with {@link #DEFAULT_URI_PREFIX} if it isn't already
42+
* @return
43+
*/
44+
@Input
45+
abstract Property<String> getRepository();
46+
47+
/**
48+
* Git reference to use.
49+
*/
50+
@Input
51+
abstract Property<String> getRef()
52+
53+
/**
54+
* Directory where the project template should be copied.
55+
*/
56+
@OutputDirectory
57+
final File outputDirectory = project.file("$project.buildDir/$name")
58+
59+
@TaskAction
60+
void checkoutAndCopy() {
61+
outputDirectory.deleteDir()
62+
File checkoutDir = checkout(this, getRemoteUri(), ref.get())
63+
moveToOutputDir(checkoutDir, outputDirectory)
64+
}
65+
66+
private static File cleanTemporaryDir(Task task, File tmpDir) {
67+
if (tmpDir.exists()) {
68+
task.project.delete(tmpDir)
69+
}
70+
return tmpDir
71+
}
72+
73+
static File checkout(Task task, String remoteUri, String ref) {
74+
checkout(task, remoteUri, ref, task.getTemporaryDir())
75+
}
76+
77+
@TypeChecked(TypeCheckingMode.SKIP)
78+
static File checkout(Task task, String remoteUri, String ref, File checkoutDir) {
79+
cleanTemporaryDir(task, checkoutDir)
80+
task.project.exec {
81+
commandLine = ["git", "clone", "--no-checkout", remoteUri, checkoutDir.absolutePath]
82+
errorOutput = System.err
83+
}
84+
task.project.exec {
85+
commandLine = ["git", "checkout", ref]
86+
workingDir = checkoutDir
87+
errorOutput = System.err
88+
}
89+
return checkoutDir
90+
}
91+
92+
private static void moveToOutputDir(File tmpDir, File outputDirectory) {
93+
File baseDir = tmpDir
94+
baseDir.renameTo(outputDirectory)
95+
}
96+
97+
private String getRemoteUri() {
98+
String remoteUri = this.repository.get()
99+
if (remoteUri.startsWith(DEFAULT_URI_PREFIX)) {
100+
return remoteUri
101+
}
102+
return DEFAULT_URI_PREFIX + remoteUri
103+
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package io.spring.gradle.convention
18+
19+
import io.spring.gradle.IncludeRepoTask
20+
import org.gradle.api.Plugin
21+
import org.gradle.api.Project
22+
import org.gradle.api.provider.Property
23+
import org.gradle.api.tasks.GradleBuild
24+
import org.gradle.api.tasks.TaskProvider
25+
26+
/**
27+
* Adds a set of tasks that make easy to clone a remote repository and perform some task
28+
*
29+
* @author Marcus Da Coregio
30+
*/
31+
class IncludeCheckRemotePlugin implements Plugin<Project> {
32+
@Override
33+
void apply(Project project) {
34+
IncludeCheckRemoteExtension extension = project.extensions.create('includeCheckRemote', IncludeCheckRemoteExtension)
35+
TaskProvider<IncludeRepoTask> includeRepoTask = project.tasks.register('includeRepo', IncludeRepoTask) { IncludeRepoTask it ->
36+
it.repository = extension.repository.get()
37+
it.ref = extension.ref.get()
38+
}
39+
project.tasks.register('checkRemote', GradleBuild) {
40+
it.dependsOn 'includeRepo'
41+
it.dir = includeRepoTask.get().outputDirectory
42+
it.tasks = extension.getTasks()
43+
}
44+
}
45+
46+
abstract static class IncludeCheckRemoteExtension {
47+
/**
48+
* Git repository to clone
49+
*/
50+
abstract Property<String> getRepository();
51+
/**
52+
* Git ref to checkout
53+
*/
54+
abstract Property<String> getRef();
55+
/**
56+
* Task to run in the repository
57+
*/
58+
List<String> tasks = ['check']
59+
60+
void setTask(List<String> tasks) {
61+
this.tasks = tasks
62+
}
63+
}
64+
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
implementation-class=io.spring.gradle.convention.IncludeCheckRemotePlugin
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package io.spring.gradle.convention;
18+
19+
import io.spring.gradle.IncludeRepoTask;
20+
import org.apache.commons.io.FileUtils;
21+
import org.gradle.api.Project;
22+
import org.gradle.api.tasks.GradleBuild;
23+
import org.gradle.testfixtures.ProjectBuilder;
24+
import org.junit.jupiter.api.AfterEach;
25+
import org.junit.jupiter.api.Test;
26+
27+
import java.util.Arrays;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
class IncludeCheckRemotePluginTest {
32+
33+
Project rootProject;
34+
35+
@AfterEach
36+
public void cleanup() throws Exception {
37+
if (rootProject != null) {
38+
FileUtils.deleteDirectory(rootProject.getProjectDir());
39+
}
40+
}
41+
42+
@Test
43+
void applyWhenExtensionPropertiesNoTasksThenCreateCheckRemoteTaskWithDefaultTask() {
44+
this.rootProject = ProjectBuilder.builder().build();
45+
this.rootProject.getPluginManager().apply(IncludeCheckRemotePlugin.class);
46+
this.rootProject.getExtensions().configure(IncludeCheckRemotePlugin.IncludeCheckRemoteExtension.class,
47+
(includeCheckRemoteExtension) -> {
48+
includeCheckRemoteExtension.setProperty("repository", "my-project/my-repository");
49+
includeCheckRemoteExtension.setProperty("ref", "main");
50+
});
51+
52+
GradleBuild checkRemote = (GradleBuild) this.rootProject.getTasks().named("checkRemote").get();
53+
assertThat(checkRemote.getTasks()).containsExactly("check");
54+
}
55+
56+
@Test
57+
void applyWhenExtensionPropertiesTasksThenCreateCheckRemoteWithProvidedTasks() {
58+
this.rootProject = ProjectBuilder.builder().build();
59+
this.rootProject.getPluginManager().apply(IncludeCheckRemotePlugin.class);
60+
this.rootProject.getExtensions().configure(IncludeCheckRemotePlugin.IncludeCheckRemoteExtension.class,
61+
(includeCheckRemoteExtension) -> {
62+
includeCheckRemoteExtension.setProperty("repository", "my-project/my-repository");
63+
includeCheckRemoteExtension.setProperty("ref", "main");
64+
includeCheckRemoteExtension.setProperty("tasks", Arrays.asList("clean", "build", "test"));
65+
});
66+
67+
GradleBuild checkRemote = (GradleBuild) this.rootProject.getTasks().named("checkRemote").get();
68+
assertThat(checkRemote.getTasks()).containsExactly("clean", "build", "test");
69+
}
70+
71+
@Test
72+
void applyWhenExtensionPropertiesThenRegisterIncludeRepoTaskWithExtensionProperties() {
73+
this.rootProject = ProjectBuilder.builder().build();
74+
this.rootProject.getPluginManager().apply(IncludeCheckRemotePlugin.class);
75+
this.rootProject.getExtensions().configure(IncludeCheckRemotePlugin.IncludeCheckRemoteExtension.class,
76+
(includeCheckRemoteExtension) -> {
77+
includeCheckRemoteExtension.setProperty("repository", "my-project/my-repository");
78+
includeCheckRemoteExtension.setProperty("ref", "main");
79+
});
80+
81+
IncludeRepoTask includeRepo = (IncludeRepoTask) this.rootProject.getTasks().named("includeRepo").get();
82+
assertThat(includeRepo).isNotNull();
83+
assertThat(includeRepo.getRepository().get()).isEqualTo("my-project/my-repository");
84+
assertThat(includeRepo.getRef().get()).isEqualTo("main");
85+
}
86+
87+
@Test
88+
void applyWhenRegisterTasksThenCheckRemoteDirSameAsIncludeRepoOutputDir() {
89+
this.rootProject = ProjectBuilder.builder().build();
90+
this.rootProject.getPluginManager().apply(IncludeCheckRemotePlugin.class);
91+
this.rootProject.getExtensions().configure(IncludeCheckRemotePlugin.IncludeCheckRemoteExtension.class,
92+
(includeCheckRemoteExtension) -> {
93+
includeCheckRemoteExtension.setProperty("repository", "my-project/my-repository");
94+
includeCheckRemoteExtension.setProperty("ref", "main");
95+
});
96+
IncludeRepoTask includeRepo = (IncludeRepoTask) this.rootProject.getTasks().named("includeRepo").get();
97+
GradleBuild checkRemote = (GradleBuild) this.rootProject.getTasks().named("checkRemote").get();
98+
assertThat(checkRemote.getDir()).isEqualTo(includeRepo.getOutputDirectory());
99+
}
100+
}

0 commit comments

Comments
 (0)