-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Liftric/feat/add_create_project_task
feat: add createProject Task
- Loading branch information
Showing
11 changed files
with
219 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/integrationTest/kotlin/com/liftric/dtcp/CreateProjectTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.liftric.dtcp | ||
|
||
import com.liftric.dtcp.service.ApiService | ||
import org.gradle.testkit.runner.GradleRunner | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Test | ||
import java.io.File | ||
import kotlinx.coroutines.runBlocking | ||
import org.gradle.testkit.runner.UnexpectedBuildFailure | ||
|
||
class CreateProjectTest : IntegrationTestBase() { | ||
@Test | ||
fun testCreateProjectTest() { | ||
val projectName = "createProjectTest" | ||
val version = "1.0.0" | ||
|
||
val apiService = ApiService(dependencyTrackApiEndpoint) | ||
|
||
val dependencyTrackAccessKey = | ||
runBlocking { apiService.getDependencyTrackAccessKey() } | ||
|
||
assertTrue(dependencyTrackAccessKey.isNotEmpty()) | ||
|
||
val projectDir = File("build/createProjectTest") | ||
|
||
projectDir.mkdirs() | ||
projectDir.resolve("settings.gradle.kts").writeText("") | ||
projectDir.resolve("build.gradle.kts").writeText( | ||
""" | ||
import com.liftric.dtcp.extensions.* | ||
plugins { | ||
kotlin("jvm") version "1.8.21" | ||
id("com.liftric.dependency-track-companion-plugin") | ||
} | ||
repositories { | ||
mavenCentral() | ||
} | ||
group = "com.liftric.$projectName" | ||
version = "$version" | ||
dependencyTrackCompanion { | ||
url.set("$dependencyTrackApiEndpoint") | ||
apiKey.set("$dependencyTrackAccessKey") | ||
projectName.set("$projectName") | ||
projectVersion.set("$version") | ||
projectActive.set(false) | ||
} | ||
""" | ||
) | ||
|
||
/** | ||
* GradleRunner fails under the hood, but the Project is created successfully. | ||
* see [IgnoreErrorApiService] for more info. | ||
* */ | ||
try { | ||
GradleRunner | ||
.create() | ||
.withProjectDir(projectDir) | ||
.withArguments("build", "createProject") | ||
.withPluginClasspath().build() | ||
} catch (e: UnexpectedBuildFailure) { | ||
assertTrue(e.message!!.contains("/api/v1/project: 500 Server Error")) | ||
} | ||
|
||
runBlocking { | ||
assertTrue(apiService.verifyProjectCreation(dependencyTrackAccessKey, projectName, version)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.liftric.dtcp.tasks | ||
|
||
import com.liftric.dtcp.model.CreateProject | ||
import com.liftric.dtcp.model.ProjectTag | ||
import com.liftric.dtcp.service.DependencyTrack | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.provider.ListProperty | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.TaskAction | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.Optional | ||
|
||
abstract class CreateProject : DefaultTask() { | ||
@get:Input | ||
abstract val apiKey: Property<String> | ||
|
||
@get:Input | ||
abstract val url: Property<String> | ||
|
||
@get:Input | ||
abstract val projectName: Property<String> | ||
|
||
@get:Input | ||
@get:Optional | ||
abstract val projectVersion: Property<String> | ||
|
||
@get:Input | ||
@get:Optional | ||
abstract val projectActive: Property<Boolean> | ||
|
||
@get:Input | ||
@get:Optional | ||
abstract val projectTags: ListProperty<ProjectTag> | ||
|
||
@get:Input | ||
@get:Optional | ||
abstract val parentUUID: Property<String> | ||
|
||
@get:Input | ||
@get:Optional | ||
abstract val ignoreProjectAlreadyExists: Property<Boolean> | ||
|
||
@TaskAction | ||
fun createProjectTask() { | ||
val dt = DependencyTrack(apiKey.get(), url.get()) | ||
|
||
val project = CreateProject( | ||
name = projectName.get(), | ||
version = projectVersion.orNull, | ||
active = projectActive.orNull ?: true, | ||
tags = projectTags.getOrElse(emptyList()), | ||
parent = parentUUID.orNull?.let { CreateProject.Parent(it) } | ||
) | ||
|
||
try { | ||
dt.createProject(project) | ||
} catch (e: Exception) { | ||
if (ignoreProjectAlreadyExists.getOrElse(false) && e.message?.contains("already exists") == true) { | ||
logger.info("Project already exists, ignoring") | ||
return | ||
} | ||
logger.error("Error creating project: ${e.message}") | ||
throw e | ||
} | ||
} | ||
} |