Skip to content
This repository was archived by the owner on Oct 28, 2024. It is now read-only.

Commit 8d0b978

Browse files
authored
feat: make destinationFolderName optional to allow to move directly in destinationParentFolder (#6)
1 parent bbde51a commit 8d0b978

File tree

6 files changed

+42
-30
lines changed

6 files changed

+42
-30
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ pipeline {
5555

5656
# Build
5757

58-
`gw :plugins:gdrive-upload:server` to run jenkins with that plugin
58+
`./gradlew :gdrive-jenkins:server` to run jenkins with that plugin
5959

60-
`gw :plugins:gdrive-upload:jpi` to produce the plugin
60+
`./gradlew :gdrive-jenkins:jpi` to produce the plugin
6161

6262
The compilation uses `kapt` for annotation processing along with `net.java.sezpoz:sezpoz` to process `@Extension` annotations
6363

gdrive-cli/README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ To run debug mode on your IDE, add `--debug-jvm`
1313

1414
## How to move folder
1515

16-
./gradlew run --args " -c path/credentialFile.json move sourceFolderId folderName destinationParentFolderId destinationFolderName --renameTo newFolderName" --debug-jvm
16+
./gradlew run --args " -c path/credentialFile.json move sourceFolderId folderName destinationParentFolderId --destinationFolderName destinationFolderName --renameTo newFolderName" --debug-jvm

gdrive-cli/src/main/kotlin/org/bonitasoft/gdrive/cli/MoveCommand.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class MoveCommand : BaseCommand() {
1818
@CommandLine.Parameters(paramLabel = "destinationParentFolderId", description = ["Id of the folder containing the destination folder."])
1919
lateinit var destinationParentFolderId: String
2020

21-
@CommandLine.Parameters(paramLabel = "destinationFolderName", description = ["Name of folder which will contain the element to move."])
22-
lateinit var destinationFolderName: String
21+
@CommandLine.Option(names = ["--destinationFolderName"], description = ["Name of folder which will contain the element to move."])
22+
var destinationFolderName: String = ""
2323

2424

2525
override fun run() {

gdrive-core/src/main/kotlin/org/bonitasoft/gdrive/core/GDriveMoveTask.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@ class GDriveMoveTask(googleCredentials: String,
2222

2323
val sourceElement = execute.files.firstOrNull() ?: throw Exception("No folder $elementName found $sourceId folder")
2424

25-
val parentFolder = drive.files().list()
26-
.setQ("'$destinationParentFolderId' in parents and name = '$destinationFolderName' and trashed = false and mimeType = '${FOLDER_MIMETYPE}'")
27-
.setFields("files(id, name, parents)")
28-
.setSupportsAllDrives(true)
29-
.setIncludeItemsFromAllDrives(true)
30-
.execute().files.firstOrNull() ?: createFolder(drive, destinationFolderName, destinationParentFolderId)
25+
var parentFolderId = destinationParentFolderId
3126

27+
if(!destinationFolderName.isBlank()) {
28+
parentFolderId = (drive.files().list()
29+
.setQ("'$destinationParentFolderId' in parents and name = '$destinationFolderName' and trashed = false and mimeType = '${FOLDER_MIMETYPE}'")
30+
.setFields("files(id, name, parents)")
31+
.setSupportsAllDrives(true)
32+
.setIncludeItemsFromAllDrives(true)
33+
.execute().files.firstOrNull()
34+
?: createFolder(drive, destinationFolderName, destinationParentFolderId)).id
35+
}
3236
drive.files().update(sourceElement.id, File().apply { name = renameTo })
33-
.setAddParents(parentFolder.id)
37+
.setAddParents(parentFolderId)
3438
.setRemoveParents(sourceElement.parents[0])
3539
.setSupportsAllDrives(true)
3640
.execute()

gdrive-jenkins/src/main/kotlin/org/bonitasoft/gdrive/jenkins/GDriveMove.kt

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,35 @@ import org.jenkinsci.plugins.workflow.steps.Step
77
import org.jenkinsci.plugins.workflow.steps.StepContext
88
import org.jenkinsci.plugins.workflow.steps.StepDescriptor
99
import org.kohsuke.stapler.DataBoundConstructor
10+
import org.kohsuke.stapler.DataBoundSetter
1011

1112
class GDriveMove
12-
1313
@DataBoundConstructor
1414
constructor(
15-
val googleCredentials: String,
16-
val sourceId: String,
17-
val elementName: String,
18-
val destinationParentFolderId: String,
19-
val destinationFolderName: String,
20-
val renameTo: String
15+
val googleCredentials: String,
16+
val sourceId: String,
17+
val elementName: String,
18+
val destinationParentFolderId: String,
19+
val renameTo: String
2120
) : Step() {
22-
override fun start(context: StepContext) = GDriveMoveStepExecution(
23-
googleCredentials,
24-
sourceId,
25-
elementName,
26-
destinationParentFolderId,
27-
destinationFolderName,
28-
renameTo,
29-
context
30-
)
21+
22+
private var destinationFolderName: String? = null
23+
24+
@DataBoundSetter
25+
fun setDestinationFolderName(destinationFolderName: String) {
26+
this.destinationFolderName = destinationFolderName
27+
}
28+
29+
30+
override fun start(context: StepContext) = GDriveMoveStepExecution(
31+
googleCredentials,
32+
sourceId,
33+
elementName,
34+
destinationParentFolderId,
35+
destinationFolderName,
36+
renameTo,
37+
context
38+
)
3139

3240

3341
@Extension

gdrive-jenkins/src/main/kotlin/org/bonitasoft/gdrive/jenkins/GDriveMoveStepExecution.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ class GDriveMoveStepExecution(
1313
private val sourceId: String,
1414
private val elementName: String,
1515
private val destinationParentFolderId: String,
16-
private val destinationFolderName: String,
16+
private val destinationFolderName: String?,
1717
private val renameTo: String,
1818
context: StepContext) : SynchronousNonBlockingStepExecution<Void>(context) {
1919

2020
override fun run(): Void? {
2121
val logger = context.get(TaskListener::class.java)?.logger!!
2222
try {
23-
GDriveMoveTask(googleCredentials, sourceId, elementName, destinationParentFolderId, destinationFolderName, object : Logger {
23+
GDriveMoveTask(googleCredentials, sourceId, elementName, destinationParentFolderId, destinationFolderName.orEmpty(), object : Logger {
2424
override fun debug(message: String) = logger.println("DEBUG: $message")
2525
override fun info(message: String) = logger.println("INFO: $message")
2626
override fun warn(message: String) = logger.println("WARN: $message")

0 commit comments

Comments
 (0)