Skip to content
Permalink
Browse files

Merge pull request #71 from jenkins-infra/develop

Promote Plugin Site API 1.8.0 and Plugin Site 1.6.1 to production
  • Loading branch information...
oleg-nenashev committed Sep 19, 2019
2 parents a1e7442 + 8561639 commit 64787a0efa3734c11245d1dfdcf1cb939148fd38
@@ -1,5 +1,6 @@
= Plugin Site API

image:https://badges.gitter.im/jenkinsci/docs.svg[link="https://gitter.im/jenkinsci/docs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge"]
image:https://img.shields.io/github/release/jenkins-infra/plugin-site-api.svg?label=release[link="https://github.com/jenkins-infra/plugin-site-api/releases/latest"]

:toc:
@@ -249,8 +249,9 @@
</build>

<scm>
<developerConnection>scm:git:ssh://git@github.com/jenkins-infra/plugin-site.git</developerConnection>
<connection>scm:git:ssh://git@github.com/jenkins-infra/plugin-site.git</connection>
<developerConnection>scm:git:ssh://git@github.com/jenkins-infra/plugin-site-api.git</developerConnection>
<connection>scm:git:ssh://git@github.com/jenkins-infra/plugin-site-api.git</connection>
<url>https://github.com/jenkins-infra/plugin-site-api</url>
</scm>

<profiles>
@@ -0,0 +1,52 @@
package io.jenkins.plugins.services.impl;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GithubContentsExtractor extends GithubExtractor {
private final static class GithubContentMatcher implements GithubMatcher {
private final Matcher matcher;

private GithubContentMatcher(Matcher matcher) {
this.matcher = matcher;
}

@Override
public String getEndpoint() {
return String.format(CONTENTS_ENDPOINT, matcher.group(3));
}

@Override
public String getDirectory() {
String filePath = matcher.group(3);
return "/" + filePath.substring(0, filePath.lastIndexOf("/") + 1);
}

@Override
public String getBranch() {
return matcher.group(2);
}

@Override
public boolean find() {
return matcher.find();
}

@Override
public String getRepo() {
return matcher.group(1);
}
}

private static final Pattern REPO_PATTERN = Pattern
.compile("https?://github.com/jenkinsci/([^/.]+)/blob/([^/]+)/(.+\\.(md|adoc))$");

private static final String CONTENTS_ENDPOINT = "contents/%s";

@Override
protected GithubMatcher getDelegate(String url) {
final Matcher matcher = REPO_PATTERN.matcher(url);
return new GithubContentMatcher(matcher);
}

}
@@ -4,8 +4,6 @@
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
@@ -15,21 +13,19 @@
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class GithubExtractor implements WikiExtractor {
public abstract class GithubExtractor implements WikiExtractor {
/**
* Bootstrap class setting !important padding. Scrapped by extractor
* to avoid !important override.
*/
public static final String BOOTSTRAP_PADDING_5 = "p-5";

private static final String README_ENDPOINT = "https://api.github.com/repos/jenkinsci/%s/readme?client_id=%s&client_secret=%s";
private static final Pattern REPO_PATTERN = Pattern
.compile("https?://github.com/jenkinsci/([^/.]+)(\\.git)?(/|/blob/master/README\\.md)?$");
private static final Logger LOGGER = Logger.getLogger(GithubExtractor.class.getName());
private static final Logger LOGGER = Logger.getLogger(GithubReadmeExtractor.class.getName());
private static final String API_URL_PATTERN =
"https://api.github.com/repos/jenkinsci/%s/%s?ref=%s&client_id=%s&client_secret=%s";

@Override
public String getApiUrl(String wikiUrl) {
Matcher matcher = REPO_PATTERN.matcher(wikiUrl);
GithubMatcher matcher = getDelegate(wikiUrl);
if (!matcher.find()) {
return null;
}
@@ -40,31 +36,35 @@ public String getApiUrl(String wikiUrl) {
return null;
}

return String.format(README_ENDPOINT, matcher.group(1), clientId, System.getenv("GITHUB_SECRET"));
}

private String getClientId() {
String clientId = StringUtils.trimToNull(System.getenv("GITHUB_CLIENT_ID"));
if (clientId != null) {
return clientId;
}
return StringUtils.trimToNull(System.getProperty("github.client.id"));
return String.format(API_URL_PATTERN, matcher.getRepo(),
matcher.getEndpoint(), matcher.getBranch(), clientId, System.getenv("GITHUB_SECRET"));
}

@Override
public String extractHtml(String apiContent, String url, HttpClientWikiService service) {
Matcher matcher = REPO_PATTERN.matcher(url);
GithubMatcher matcher = getDelegate(url);
if (!matcher.find()) {
throw new IllegalArgumentException("Invalid github URL" + url);
}
final Document html = Jsoup.parse(apiContent);
final Element mainDiv = html.getElementsByTag("body").get(0).child(0);
//TODO(oleg_nenashev): Support organization and branch customization?
convertLinksToAbsolute(service, mainDiv, "jenkinsci", matcher.group(1), "master");
convertLinksToAbsolute(service, mainDiv, "jenkinsci", matcher);
return mainDiv.toString();
}

private void convertLinksToAbsolute(HttpClientWikiService service, Element wikiContent, String orgName, String repoName, String branch) {
@Override
public List<Header> getHeaders() {
Header header = new BasicHeader("Accept", "application/vnd.github.v3.html");
return Collections.singletonList(header);
}

protected abstract GithubMatcher getDelegate(String url);

protected void convertLinksToAbsolute(HttpClientWikiService service, Element wikiContent, String orgName, GithubMatcher matcher) {
String repoName = matcher.getRepo();
String branch = matcher.getBranch();
String path = matcher.getDirectory();
String documentationHost = String.format("https://github.com/%s/%s/blob/%s", orgName, repoName, branch);
String imageHost = String.format("https://cdn.jsdelivr.net/gh/%s/%s@%s", orgName, repoName, branch);
Elements headings = wikiContent.getElementsByTag("H1");
@@ -73,16 +73,18 @@ private void convertLinksToAbsolute(HttpClientWikiService service, Element wikiC
}
wikiContent.getElementsByClass(BOOTSTRAP_PADDING_5).forEach(element -> element.removeClass(BOOTSTRAP_PADDING_5));
// Relative hyperlinks, we resolve "/docs/rest-api.adoc" as https://github.com/jenkinsci/folder-auth-plugin/blob/master/docs/rest-api.adoc
wikiContent.getElementsByAttribute("href").forEach(element -> service.replaceAttribute(element, "href", documentationHost, "/"));
wikiContent.getElementsByAttribute("href").forEach(element -> service.replaceAttribute(element, "href", documentationHost, path));

// Relative image inclusions, we resolve /docs/images/screenshot.png as https://cdn.jsdelivr.net/gh/jenkinsci/folder-auth-plugin@master/docs/images/screenshot.png
wikiContent.getElementsByAttribute("src").forEach(element -> service.replaceAttribute(element, "src", imageHost, "/"));
wikiContent.getElementsByAttribute("src").forEach(element -> service.replaceAttribute(element, "src", imageHost, path));
}

@Override
public List<Header> getHeaders() {
Header header = new BasicHeader("Accept", "application/vnd.github.v3.html");
return Collections.singletonList(header);
private String getClientId() {
String clientId = StringUtils.trimToNull(System.getenv("GITHUB_CLIENT_ID"));
if (clientId != null) {
return clientId;
}
return StringUtils.trimToNull(System.getProperty("github.client.id"));
}

}
@@ -0,0 +1,31 @@
package io.jenkins.plugins.services.impl;

/**
* Maps browser URLs to path components and API URLs
*/
public interface GithubMatcher {
/**
* @return per-repository API endpoint, without leading slash
*/
String getEndpoint();

/**
* @return directory within repo, including trailing slash
*/
String getDirectory();

/**
* @return branch or tag
*/
String getBranch();

/**
* @return whether URL is valid
*/
boolean find();

/**
* @return repository name
*/
String getRepo();
}
@@ -0,0 +1,52 @@
package io.jenkins.plugins.services.impl;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GithubReadmeExtractor extends GithubExtractor {

private final static class GithubReadmeMatcher implements GithubMatcher {
private final Matcher matcher;

private GithubReadmeMatcher(Matcher matcher) {
this.matcher = matcher;
}

@Override
public String getEndpoint() {
return README_ENDPOINT;
}

@Override
public String getDirectory() {
return "/";
}

@Override
public String getBranch() {
String branch = matcher.group(3);
return branch == null ? "master" : branch;
}

@Override
public boolean find() {
return matcher.find();
}

@Override
public String getRepo() {
return matcher.group(1);
}
}

private static final String README_ENDPOINT = "readme";
private static final Pattern REPO_PATTERN = Pattern
.compile("https?://github.com/jenkinsci/([^/.]+)(\\.git|/tree/([^/]+))?/?$");

@Override
protected GithubMatcher getDelegate(String url) {
final Matcher matcher = REPO_PATTERN.matcher(url);
return new GithubReadmeMatcher(matcher);
}

}
@@ -48,7 +48,8 @@
static {
WIKI_URLS.add(new ConfluenceApiExtractor());
WIKI_URLS.add(new ConfluenceDirectExtractor());
WIKI_URLS.add(new GithubExtractor());
WIKI_URLS.add(new GithubReadmeExtractor());
WIKI_URLS.add(new GithubContentsExtractor());
}

@PostConstruct
No changes.

0 comments on commit 64787a0

Please sign in to comment.
You can’t perform that action at this time.