Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-69032] Plugin manager update site URL can be saved empty #6886

Merged
merged 15 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions core/src/main/java/hudson/PluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -1923,6 +1924,35 @@ public HttpResponse doUploadPlugin(StaplerRequest req) throws IOException, Servl
return FormValidation.ok();
}

@Restricted(NoExternalUse.class)
@RequirePOST public FormValidation doCheckUpdateSiteUrl(StaplerRequest request, @QueryParameter String value) {
benebsiny marked this conversation as resolved.
Show resolved Hide resolved
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
if (StringUtils.isNotBlank(value)) {
try {
value += ((value.contains("?")) ? "&" : "?") + "version=" + Jenkins.VERSION + "&uctest";

URL url = new URL(value);

// Connect to the URL
HttpURLConnection conn = (HttpURLConnection) ProxyConfiguration.open(url);
conn.setRequestMethod("HEAD");
conn.setConnectTimeout(5000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this also have a read timeout?

Suggested change
conn.setConnectTimeout(5000);
conn.setReadTimeout(1000);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will 1 sec too short? How about 5 sec which is same as connect timeout?

if (100 <= conn.getResponseCode() && conn.getResponseCode() <= 399) {
return FormValidation.ok();
} else {
LOGGER.log(Level.FINE, "Obtained a non OK ({0}) response from the update center",
new Object[]{conn.getResponseCode(), url});
return FormValidation.error(Messages.PluginManager_connectionFailed());
NotMyFault marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (IOException e) {
LOGGER.log(Level.FINE, "Failed to check update site", e);
return FormValidation.error(Messages.PluginManager_connectionFailed());
NotMyFault marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
return FormValidation.error(Messages.PluginManager_emptyUpdateSiteUrl());
}
}

@Restricted(NoExternalUse.class)
@RequirePOST public HttpResponse doCheckUpdatesServer() throws IOException {
Jenkins.get().checkPermission(Jenkins.SYSTEM_READ);
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/resources/hudson/Messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ PluginManager.insecureUrl=\
PluginManager.invalidUrl=\
You are using an invalid URL to download the plugin, only https and http (not recommended) are supported.

PluginManager.emptyUpdateSiteUrl=\
The update site cannot be empty. Please enter a valid url.

PluginManager.connectionFailed=\
Unable to connect to the URL.

AboutJenkins.DisplayName=About Jenkins
AboutJenkins.Description=See the version and license information.
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/resources/hudson/Messages_zh_TW.properties
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ PluginWrapper.NoSuchPlugin=找不到名為「{0}」的外掛
PluginWrapper.Error.Disabling=停用外掛「{0}」時發生錯誤,錯誤\: 「{1}」
TcpSlaveAgentListener.PingAgentProtocol.displayName=Ping 協定

PluginManager.emptyUpdateSiteUrl=更新站台網址不得為空,請輸入網址
PluginManager.connectionFailed=無法連線至這個URL
23 changes: 23 additions & 0 deletions core/src/main/resources/hudson/PluginManager/_updateSite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(function () {
showResetToDefaultButtonOrNot();
})();

function showResetToDefaultButtonOrNot() {
const resetButton = document.getElementById("reset-to-default");
const siteUrlInput = document.getElementById("update-site-url");
if (siteUrlInput.value === "https://updates.jenkins.io/update-center.json") {
resetButton.style.display = "none";
} else {
resetButton.style.display = "";
}
}

document.getElementById("update-site-url").onchange =
showResetToDefaultButtonOrNot;

document.getElementById("reset-to-default").onclick = function (event) {
event.preventDefault();
const siteUrlInput = document.getElementById("update-site-url");
siteUrlInput.value = "https://updates.jenkins.io/update-center.json";
siteUrlInput.dispatchEvent(new Event("change"));
timja marked this conversation as resolved.
Show resolved Hide resolved
};
8 changes: 7 additions & 1 deletion core/src/main/resources/hudson/PluginManager/advanced.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,15 @@ THE SOFTWARE.
<h2 class="jenkins-section__title">${%Update Site}</h2>
<f:form method="post" action="siteConfigure" name="siteConfigure">
<f:entry title="${%URL}">
<j:if test="${app.updateCenter.ID_DEFAULT.equals(app.updateCenter.PREDEFINED_UPDATE_SITE_ID)}">
<a id="reset-to-default" href="#">${%Reset to default}</a>
</j:if>
<f:textbox name="site"
value="${app.updateCenter.getSite(app.updateCenter.ID_DEFAULT).url}"/>
id="update-site-url"
value="${app.updateCenter.getSite(app.updateCenter.ID_DEFAULT).url}"
checkUrl="checkUpdateSiteUrl" checkDependsOn=""/>
</f:entry>
<st:adjunct includes="hudson.PluginManager._updateSite"/>
<l:isAdmin>
<f:block>
<f:submit/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ Deploy\ Plugin=部署外掛
Deploy=部署
Advanced\ Settings=進階設定
Or=或
Reset\ to\ default=重設回預設值