Skip to content

Commit 21b28cd

Browse files
author
danf
committed
Add Repository creation support
see: RTFACT-7576
1 parent a16d953 commit 21b28cd

File tree

6 files changed

+182
-26
lines changed

6 files changed

+182
-26
lines changed

api/src/main/java/com/jfrog/bintray/client/api/details/RepositoryDetails.java

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.jfrog.bintray.client.api.details;
22

3+
import org.codehaus.jackson.annotate.JsonIgnore;
34
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
45
import org.codehaus.jackson.annotate.JsonProperty;
56
import org.codehaus.jackson.map.ObjectMapper;
@@ -17,31 +18,67 @@
1718
@JsonIgnoreProperties(ignoreUnknown = true)
1819
public class RepositoryDetails {
1920

20-
@JsonProperty(value = "name")
21+
//Properties marked with @JsonPropery here are serialized to the create \ update version requests, the rest are
22+
// only deserialized when getting the version info
23+
@JsonIgnore
2124
String name;
22-
@JsonProperty(value = "owner")
25+
@JsonIgnore
2326
String owner;
27+
@JsonProperty
28+
String type;
29+
@JsonProperty(value = "private")
30+
Boolean isPrivate;
31+
@JsonProperty
32+
Boolean premium;
2433
@JsonProperty(value = "desc")
2534
String description;
26-
@JsonProperty(value = "created")
27-
DateTime created;
28-
@JsonProperty(value = "labels")
35+
@JsonProperty
2936
List<String> labels;
30-
@JsonProperty(value = "package_count")
37+
@JsonIgnore
38+
DateTime created;
39+
@JsonIgnore
3140
Integer packageCount;
41+
@JsonIgnore
42+
Boolean updateExisting; //Property is not used in the Bintray API but Artifactory uses is in it's Bintray integration
3243

3344
public static ObjectMapper getObjectMapper() {
3445
return ObjectMapperHelper.objectMapper;
3546
}
3647

48+
@JsonIgnore
3749
public String getName() {
3850
return name;
3951
}
4052

53+
@JsonProperty(value = "name")
4154
public void setName(String name) {
4255
this.name = name;
4356
}
4457

58+
public String getType() {
59+
return type;
60+
}
61+
62+
public Boolean getIsPrivate() {
63+
return isPrivate;
64+
}
65+
66+
public void setIsPrivate(Boolean isPrivate) {
67+
this.isPrivate = isPrivate;
68+
}
69+
70+
public Boolean getPremium() {
71+
return premium;
72+
}
73+
74+
public void setPremium(Boolean premium) {
75+
this.premium = premium;
76+
}
77+
78+
public void setType(String type) {
79+
this.type = type;
80+
}
81+
4582
public String getOwner() {
4683
return owner;
4784
}
@@ -58,10 +95,12 @@ public void setDescription(String description) {
5895
this.description = description;
5996
}
6097

98+
@JsonIgnore
6199
public DateTime getCreated() {
62100
return created;
63101
}
64102

103+
@JsonProperty(value = "created")
65104
public void setCreated(DateTime created) {
66105
this.created = created;
67106
}
@@ -74,11 +113,23 @@ public void setLabels(List<String> labels) {
74113
this.labels = labels;
75114
}
76115

116+
@JsonIgnore
77117
public Integer getPackageCount() {
78118
return packageCount;
79119
}
80120

121+
@JsonProperty(value = "package_count")
81122
public void setPackageCount(Integer packageCount) {
82123
this.packageCount = packageCount;
83124
}
125+
126+
@JsonIgnore
127+
public Boolean getUpdateExisting() {
128+
return updateExisting;
129+
}
130+
131+
@JsonProperty(value = "updateExisting")
132+
public void setUpdateExisting(Boolean updateExisting) {
133+
this.updateExisting = updateExisting;
134+
}
84135
}

api/src/main/java/com/jfrog/bintray/client/api/handle/SubjectHandle.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jfrog.bintray.client.api.handle;
22

33
import com.jfrog.bintray.client.api.BintrayCallException;
4+
import com.jfrog.bintray.client.api.details.RepositoryDetails;
45
import com.jfrog.bintray.client.api.model.Subject;
56

67
import java.io.IOException;
@@ -15,4 +16,6 @@ public interface SubjectHandle extends Handle<Subject> {
1516
Subject get() throws IOException, BintrayCallException;
1617

1718
RepositoryHandle repository(String name);
19+
20+
RepositoryHandle createRepo(RepositoryDetails repoDetails) throws IOException, BintrayCallException;
1821
}

impl/src/main/java/com/jfrog/bintray/client/impl/handle/SubjectHandleImpl.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22

33
import com.jfrog.bintray.client.api.BintrayCallException;
44
import com.jfrog.bintray.client.api.details.ObjectMapperHelper;
5+
import com.jfrog.bintray.client.api.details.RepositoryDetails;
56
import com.jfrog.bintray.client.api.details.SubjectDetails;
67
import com.jfrog.bintray.client.api.handle.RepositoryHandle;
78
import com.jfrog.bintray.client.api.handle.SubjectHandle;
89
import com.jfrog.bintray.client.api.model.Subject;
10+
import com.jfrog.bintray.client.impl.model.RepositoryImpl;
911
import com.jfrog.bintray.client.impl.model.SubjectImpl;
12+
import org.apache.commons.io.IOUtils;
1013
import org.apache.http.HttpResponse;
1114
import org.codehaus.jackson.map.ObjectMapper;
1215
import org.slf4j.Logger;
1316
import org.slf4j.LoggerFactory;
1417

1518
import java.io.IOException;
1619
import java.io.InputStream;
20+
import java.util.HashMap;
21+
import java.util.Map;
1722

1823
/**
1924
* @author Dan Feldman
@@ -39,6 +44,15 @@ public RepositoryHandle repository(String repoName) {
3944
return new RepositoryHandleImpl(bintrayHandle, this, repoName);
4045
}
4146

47+
@Override
48+
public RepositoryHandle createRepo(RepositoryDetails repoDetails) throws IOException, BintrayCallException {
49+
Map<String, String> headers = new HashMap<>();
50+
String jsonContent = RepositoryImpl.getCreateUpdateJson(repoDetails);
51+
BintrayImpl.addContentTypeJsonHeader(headers);
52+
bintrayHandle.post(getRepoUri(repoDetails), headers, IOUtils.toInputStream(jsonContent));
53+
return new RepositoryHandleImpl(bintrayHandle, this, repoDetails.getName());
54+
}
55+
4256
@Override
4357
public Subject get() throws IOException, BintrayCallException {
4458
HttpResponse response = bintrayHandle.get("users/" + subject, null);
@@ -53,4 +67,8 @@ public Subject get() throws IOException, BintrayCallException {
5367
}
5468
return new SubjectImpl(subjectDetails);
5569
}
70+
71+
private String getRepoUri(RepositoryDetails repoDetails) {
72+
return String.format("repos/%s/%s", subject, repoDetails.getName());
73+
}
5674
}

impl/src/main/java/com/jfrog/bintray/client/impl/model/RepositoryImpl.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
package com.jfrog.bintray.client.impl.model;
22

3+
import com.jfrog.bintray.client.api.details.ObjectMapperHelper;
34
import com.jfrog.bintray.client.api.details.RepositoryDetails;
45
import com.jfrog.bintray.client.api.model.Repository;
6+
import org.codehaus.jackson.map.ObjectMapper;
57
import org.joda.time.DateTime;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
610

11+
import java.io.IOException;
712
import java.util.List;
813

914
/**
1015
* @author Noam Y. Tenne
1116
*/
1217
public class RepositoryImpl implements Repository {
18+
private static final Logger log = LoggerFactory.getLogger(RepositoryImpl.class);
1319

1420
private String name;
1521
private String owner;
22+
private String type;
23+
private Boolean isPrivate;
24+
private Boolean premium;
1625
private String desc;
1726
private List<String> labels;
1827
private DateTime created;
@@ -24,21 +33,40 @@ public RepositoryImpl() {
2433
public RepositoryImpl(RepositoryDetails repositoryDetails) {
2534
this.name = repositoryDetails.getName();
2635
this.owner = repositoryDetails.getOwner();
36+
this.type = repositoryDetails.getType();
37+
this.isPrivate = repositoryDetails.getIsPrivate();
38+
this.premium = repositoryDetails.getPremium();
2739
this.desc = repositoryDetails.getDescription();
2840
this.labels = repositoryDetails.getLabels();
2941
this.created = repositoryDetails.getCreated();
3042
this.packageCount = repositoryDetails.getPackageCount();
3143
}
3244

33-
public RepositoryImpl(String name, String owner, String desc, List<String> labels, DateTime created, Integer packageCount) {
45+
public RepositoryImpl(String name, String owner, String type, Boolean isPrivate, Boolean premium, String desc,
46+
List<String> labels, DateTime created, Integer packageCount) {
3447
this.name = name;
3548
this.owner = owner;
49+
this.type = type;
50+
this.isPrivate = isPrivate;
51+
this.premium = premium;
3652
this.desc = desc;
3753
this.labels = labels;
3854
this.created = created;
3955
this.packageCount = packageCount;
4056
}
4157

58+
public static String getCreateUpdateJson(RepositoryDetails repositoryDetails) throws IOException {
59+
ObjectMapper mapper = ObjectMapperHelper.objectMapper;
60+
try {
61+
return mapper.writeValueAsString(repositoryDetails);
62+
} catch (IOException e) {
63+
log.error("Can't process the json file: " + e.getMessage());
64+
log.debug("{}", e);
65+
throw e;
66+
}
67+
}
68+
69+
4270
public String getName() {
4371
return name;
4472
}
@@ -47,6 +75,18 @@ public String getOwner() {
4775
return owner;
4876
}
4977

78+
public String getType() {
79+
return type;
80+
}
81+
82+
public Boolean getIsPrivate() {
83+
return isPrivate;
84+
}
85+
86+
public Boolean getPremium() {
87+
return premium;
88+
}
89+
5090
public String getDesc() {
5191
return desc;
5292
}

impl/src/test/groovy/com/jfrog/bintray/client/test/BintraySpecSuite.groovy

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import static org.apache.http.HttpStatus.SC_NOT_FOUND
3232
@Suite.SuiteClasses([RepoSpec.class, PackageSpec.class, VersionSpec.class, BintrayClientSpec.class])
3333
class BintraySpecSuite {
3434

35+
public static final String REPO_CREATE_NAME = 'repoTest'
3536
public static final String REPO_NAME = 'generic'
3637
public static final String PKG_NAME = 'bla'
3738
public static final String VERSION = '1.0'
@@ -50,11 +51,13 @@ class BintraySpecSuite {
5051
@Shared
5152
public static String tempPkgName = "PkgTest"
5253
@Shared
53-
public static String pkgJson;
54-
@Shared
5554
public static String tempVerName = "3.0"
5655
@Shared
57-
public static String verJson;
56+
public static String pkgJson
57+
@Shared
58+
public static String verJson
59+
@Shared
60+
public static String repoJson
5861

5962
public static ArrayList<Attribute<String>> attributes = [
6063
new Attribute<String>('a', Attribute.Type.string, "ay1", "ay2"),
@@ -137,6 +140,15 @@ class BintraySpecSuite {
137140
" {\"name\": \"VerAtt3\",\"values\": [\"2015-01-01T19:43:37+0100\"],\"type\": \"date\"}]\n" +
138141
"}"
139142

143+
repoJson = "{\n" +
144+
" \"name\": \"" + REPO_CREATE_NAME + "\",\n" +
145+
" \"type\": \"maven\",\n" +
146+
" \"private\": false,\n" +
147+
" \"premium\": false,\n" +
148+
" \"desc\": \"Test Repo\",\n" +
149+
" \"labels\":[\"lable1\", \"label2\"]\n" +
150+
"}"
151+
140152
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
141153
//Set level for root logger
142154
loggerContext.getLogger("ROOT").setLevel(Level.INFO)

0 commit comments

Comments
 (0)