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

Commit dde48a3

Browse files
author
Tim Sowers
committed
Add support for GitHub Tagged Releases; (DOECODE-789)
1 parent d546056 commit dde48a3

File tree

3 files changed

+92
-6
lines changed

3 files changed

+92
-6
lines changed

src/main/java/gov/osti/connectors/GitHub.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ private static String getProjectFromUrl(String url) {
102102

103103
if(matcher.find()) {
104104
return matcher.group(1);
105+
}
105106
}
106107
}
107-
}
108108
} catch ( URISyntaxException e ) {
109109
// warn that URL is not a valid URI
110110
log.warn("Not a valid URI: " + url + " message: " + e.getMessage());
@@ -116,6 +116,27 @@ private static String getProjectFromUrl(String url) {
116116
return null;
117117
}
118118

119+
/**
120+
* Attempt to identify the TAG NAME from the given URL.
121+
*
122+
* Criteria: URL host should contain "github.com"
123+
*
124+
* @param url the URL to process
125+
* @return the TAG NAME if able to parse; null if not, or unrecognized URL
126+
*/
127+
public static String getTagFromUrl(String url) {
128+
String tag = null;
129+
130+
Pattern pattern = Pattern.compile("github[.]com.*\\/releases\\/tag\\/(.*)");
131+
Matcher matcher = pattern.matcher(url);
132+
133+
if(matcher.find()) {
134+
tag = matcher.group(1);
135+
}
136+
137+
return tag;
138+
}
139+
119140
/**
120141
* Obtain the connection-driven metadata elements from GitHub public API
121142
* requests.
@@ -213,4 +234,39 @@ public JsonNode read(String url) {
213234
return null;
214235
}
215236

237+
/**
238+
* Determine if tag reference exists in the repo
239+
*
240+
* @param url the URL to process
241+
*
242+
* @return a JsonElement of the DOECodeMetadata filled in as possible from
243+
* the API
244+
*/
245+
public static boolean isTagReferenceAndValid(String url) {
246+
try {
247+
// try to identify the NAME of the project
248+
String name = getProjectFromUrl(url);
249+
if (null==name)
250+
return false;
251+
252+
// try to identify the TAG of the project
253+
String tag = getTagFromUrl(url);
254+
if (null==tag)
255+
return false;
256+
257+
// acquire the GitHub API response as JSON
258+
HttpGet get = gitHubAPIGet(GITHUB_BASE_URL + name + "/git/ref/tags/" + tag);
259+
260+
return !(HttpUtil.fetch(get) == "");
261+
} catch ( IOException e ) {
262+
// here's where you'd warn about the IO error
263+
log.warn("IO Error reading GitHub information: " + e.getMessage());
264+
log.warn("Read from " + url);
265+
}
266+
267+
// unable to process this one
268+
return false;
269+
270+
}
271+
216272
}

src/main/java/gov/osti/services/Metadata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2391,7 +2391,7 @@ else if (m.getLicenses().contains(DOECodeMetadata.License.Other.value()) && Stri
23912391
reasons.add("A valid Landing Page URL is required for non-open source submissions.");
23922392
}
23932393
// if repository link is present, and not CO, it needs to be valid too
2394-
if (StringUtils.isNotBlank(m.getRepositoryLink()) && !DOECodeMetadata.Accessibility.CO.equals(m.getAccessibility()) && !Validation.isValidRepositoryLink(m.getRepositoryLink()))
2394+
if (StringUtils.isNotBlank(m.getRepositoryLink()) && !DOECodeMetadata.Accessibility.CO.equals(m.getAccessibility()) && !GitHub.isTagReferenceAndValid(m.getRepositoryLink()) && !Validation.isValidRepositoryLink(m.getRepositoryLink()))
23952395
reasons.add("Repository URL is not a valid repository.");
23962396

23972397
return reasons;

src/main/java/gov/osti/services/Validation.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import gov.osti.listeners.DoeServletContextListener;
1515
import gov.osti.repository.GitRepository;
1616
import gov.osti.repository.SubversionRepository;
17+
import gov.osti.connectors.GitHub;
1718
import java.io.IOException;
1819
import java.io.Serializable;
1920
import java.net.URLEncoder;
@@ -118,6 +119,7 @@ private static class ValidationRequest implements Serializable {
118119
private String type;
119120
private String value;
120121
private String error;
122+
private String extraInfo;
121123

122124
/**
123125
* @return the type
@@ -160,6 +162,20 @@ public String getError() {
160162
public void setError(String error) {
161163
this.error = error;
162164
}
165+
166+
/**
167+
* @return the value
168+
*/
169+
public String getExtraInfo() {
170+
return extraInfo;
171+
}
172+
173+
/**
174+
* @param info the value to set
175+
*/
176+
public void setExtraInfo(String info) {
177+
this.extraInfo = info;
178+
}
163179
}
164180

165181
/**
@@ -501,9 +517,19 @@ public Response checkEmail(@QueryParam("value") String value) {
501517
@Produces (MediaType.APPLICATION_JSON)
502518
@Path ("/repositorylink")
503519
public Response checkRepositoryLink(@QueryParam("value") String value) {
504-
return ( isValidRepositoryLink(value) ) ?
505-
Response.ok().entity(mapper.createObjectNode().put("value", "OK").toString()).build() :
506-
ErrorResponse.badRequest(generateURLErrorMsg(value, "repositorylink")).build();
520+
// is link a tag URL, or valid Repo
521+
String tag = GitHub.getTagFromUrl(value);
522+
523+
if (tag != null && GitHub.isTagReferenceAndValid(value))
524+
return Response.ok().entity(mapper.createObjectNode()
525+
.put("value", "OK").put("isTaggedVersion", true)
526+
.put("isTaggedVersion", true)
527+
.put("tagName", tag)
528+
.toString()).build();
529+
else if (isValidRepositoryLink(value))
530+
return Response.ok().entity(mapper.createObjectNode().put("value", "OK").toString()).build();
531+
else
532+
return ErrorResponse.badRequest(generateURLErrorMsg(value, "repositorylink")).build();
507533
}
508534

509535
/**
@@ -565,7 +591,11 @@ public Response request(String object) throws IOException {
565591
if (StringUtils.equalsIgnoreCase(req.getType(), "doi")) {
566592
req.setError((isValidDoi(req.getValue()) ? "" : req.getValue() + " is not a valid DOI."));
567593
} else if (StringUtils.equalsIgnoreCase(req.getType(), "repositorylink")) {
568-
req.setError(isValidRepositoryLink(req.getValue()) ? "" : generateURLErrorMsg(req.getValue(), req.getType()));
594+
String value = req.getValue();
595+
// is link a tag URL, or valid Repo
596+
String tag = GitHub.getTagFromUrl(value);
597+
req.setError(GitHub.isTagReferenceAndValid(value) || isValidRepositoryLink(value) ? "" : generateURLErrorMsg(value, req.getType()));
598+
req.setExtraInfo(tag);
569599
} else if (StringUtils.equalsIgnoreCase(req.getType(), "phonenumber")) {
570600
req.setError((isValidPhoneNumber(req.getValue()) ? "" : req.getValue() + " is not a valid phone number."));
571601
} else if (StringUtils.equalsIgnoreCase(req.getType(), "url")) {

0 commit comments

Comments
 (0)