Skip to content

Commit 476b2c5

Browse files
AlexeiVainshteineyalbe4
authored andcommitted
issue 240 - Prevent publish task operations run multiple time for the same values (#248)
1 parent 3fe02df commit 476b2c5

File tree

10 files changed

+87
-18
lines changed

10 files changed

+87
-18
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ buildscript {
88
}
99
dependencies {
1010
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1"
11-
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.3"
11+
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4"
1212
}
1313
}
1414

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
currentVersion=1.8.3
1+
currentVersion=1.8.4

src/main/groovy/com/jfrog/bintray/gradle/tasks/BintrayPublishTask.groovy

+32-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.jfrog.bintray.gradle.tasks
22

33
import com.jfrog.bintray.gradle.Utils
4+
import com.jfrog.bintray.gradle.tasks.entities.Repository
5+
import com.jfrog.bintray.gradle.tasks.entities.Package
6+
import com.jfrog.bintray.gradle.tasks.entities.Version
47
import org.gradle.api.DefaultTask
58
import org.gradle.api.GradleException
69
import org.gradle.api.tasks.TaskAction
@@ -15,6 +18,7 @@ import static groovyx.net.http.Method.POST
1518
*/
1619
class BintrayPublishTask extends DefaultTask {
1720
static final String TASK_NAME = "bintrayPublish"
21+
private HashMap<String, Repository> repositories = new HashMap<>()
1822

1923
@TaskAction
2024
void taskAction() throws IOException {
@@ -25,14 +29,36 @@ class BintrayPublishTask extends DefaultTask {
2529
HashSet<BintrayUploadTask> tasks = project.getTasksByName(BintrayUploadTask.TASK_NAME, true)
2630
for (BintrayUploadTask task : tasks) {
2731
if (task.getEnabled() && task.getDidWork()) {
28-
if (task.getSignVersion()) {
29-
gpgSignVersion(task.repoName, task.packageName, task.versionName, task)
32+
Package pkg = new Package(task.packageName)
33+
Repository repository = new Repository(task.repoName)
34+
Repository existingRepo = repositories.putIfAbsent(task.repoName, repository)
35+
if (!existingRepo) {
36+
existingRepo = repository
3037
}
31-
if (task.publish) {
32-
publishVersion(task.repoName, task.packageName, task.versionName, task)
38+
39+
Package existingPackage = existingRepo.packages.putIfAbsent(task.packageName, pkg)
40+
if (!existingPackage) {
41+
existingPackage = pkg
42+
}
43+
44+
Version v = new Version(
45+
task.versionName, task.signVersion, task.gpgPassphrase, task.publish, task.shouldSyncToMavenCentral())
46+
Version existingVersion = existingPackage.getVersionIfExists(v)
47+
48+
if (existingVersion == null) {
49+
existingPackage.addVersionIfAbsent(v)
50+
existingVersion = new Version(v.name, false, v.gpgPassphrase, false, false)
3351
}
34-
if (task.shouldSyncToMavenCentral()) {
35-
mavenCentralSync(task.repoName, task.packageName, task.versionName, task)
52+
if (existingVersion.shouldGpgSign(v.gpgSign)) {
53+
gpgSignVersion(existingRepo.name, existingPackage.name, existingVersion.name, task)
54+
}
55+
56+
if (existingVersion.shouldPerformPublish(v.publish)) {
57+
publishVersion(existingRepo.name, existingPackage.name, existingVersion.name, task)
58+
}
59+
60+
if (existingVersion.shouldPerformMavenSync(v.mavenCentralSync)) {
61+
mavenCentralSync(existingRepo.name, existingPackage.name, existingVersion.name, task)
3662
}
3763
}
3864
}

src/main/groovy/com/jfrog/bintray/gradle/tasks/entities/Package.groovy

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ class Package {
2727
return v
2828
}
2929

30+
// Returns the version of the Map if exists
31+
// Null if there is no such version in the map
32+
Version getVersionIfExists(Version v) {
33+
return versions.get(v)
34+
}
35+
3036
boolean equals(o) {
3137
if (this.is(o)) {
3238
return true

src/main/groovy/com/jfrog/bintray/gradle/tasks/entities/Version.groovy

+42-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.jfrog.bintray.gradle.tasks.entities
22

3+
import org.apache.commons.lang.StringUtils
4+
35
class Version {
46
private String name
57
private boolean created
68
private boolean gpgSign
79
private String gpgPassphrase
810
private boolean publish
9-
private boolean mavenCentralSync
11+
private boolean mavenCentralSync
1012

1113
Version(String name, boolean gpgSign, String gpgPassphrase, boolean publish, boolean mavenCentralSync) {
1214
this.name = name
@@ -49,16 +51,51 @@ class Version {
4951
}
5052
}
5153

52-
boolean equals(o) {
53-
if (this.is(o)) {
54-
return true
54+
boolean shouldPerformPublish(boolean publish) {
55+
// If publish already occurred for this version
56+
// Or
57+
// If no publish is needed
58+
// don't perform publishing
59+
if (this.publish || !publish) {
60+
return false
61+
}
62+
this.publish = publish
63+
return true
64+
}
65+
66+
boolean shouldPerformMavenSync(boolean mavenCentralSync) {
67+
// If maven Central Sync already occurred for this version
68+
// Or
69+
// If mavenCentralSync is false
70+
// don't perform maven central sync
71+
if (this.mavenCentralSync || !mavenCentralSync) {
72+
return false
5573
}
56-
if (getClass() != o.class || name != ((Version) o).name) {
74+
this.mavenCentralSync = mavenCentralSync
75+
return true
76+
}
77+
78+
boolean shouldGpgSign(boolean gpgSign) {
79+
// If signing of the version already occurred
80+
// Or
81+
// If no signing is required
82+
// return false
83+
if (this.gpgSign || !gpgSign) {
5784
return false
5885
}
86+
this.gpgSign = gpgSign
5987
return true
6088
}
6189

90+
boolean equals(o) {
91+
if (!(o instanceof String)) {
92+
return false
93+
}
94+
95+
String name = (String) o
96+
return StringUtils.equals(this.name, name)
97+
}
98+
6299
int hashCode() {
63100
name != null ? name.hashCode() : 0
64101
}

src/test/resources/gradle/projects/configuration/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ buildscript {
44
jcenter()
55
}
66
dependencies {
7-
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.3"
7+
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4"
88
}
99
}
1010

src/test/resources/gradle/projects/configurationWithSubModules/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ buildscript {
44
jcenter()
55
}
66
dependencies {
7-
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.3"
7+
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4"
88
}
99
}
1010

src/test/resources/gradle/projects/fileSpec/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
}
66

77
dependencies {
8-
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.3"
8+
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4"
99
}
1010
}
1111

src/test/resources/gradle/projects/publication/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
}
66

77
dependencies {
8-
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.3"
8+
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4"
99
}
1010
}
1111

src/test/resources/gradle/projects/publicationWithJavaGradlePlugin/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
}
66

77
dependencies {
8-
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.3"
8+
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4"
99
}
1010
}
1111

0 commit comments

Comments
 (0)