-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Init Agent Intergation #3
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
Changes from 5 commits
83a3a32
d878b66
76dd246
435d748
2a7ee87
e2d86d6
4cd10b4
a02c5ba
ec977f2
523ebcf
a4fb87a
34be8eb
f6bea4d
931601d
bac5f38
c434656
1986e98
28c7565
9cbe278
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"java.configuration.updateBuildConfiguration": "automatic" | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,167 @@ | ||||||||||||||||||||||
package io.jenkins.plugins.ctrlplane; | ||||||||||||||||||||||
|
||||||||||||||||||||||
import hudson.Extension; | ||||||||||||||||||||||
import hudson.ExtensionList; | ||||||||||||||||||||||
import hudson.util.FormValidation; | ||||||||||||||||||||||
import jenkins.model.GlobalConfiguration; | ||||||||||||||||||||||
import org.apache.commons.lang.StringUtils; | ||||||||||||||||||||||
import org.kohsuke.stapler.DataBoundSetter; | ||||||||||||||||||||||
import org.kohsuke.stapler.QueryParameter; | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* Global configuration for the Ctrlplane Agent plugin. | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
@Extension | ||||||||||||||||||||||
public class CtrlplaneGlobalConfiguration extends GlobalConfiguration { | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* Default API URL | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
public static final String DEFAULT_API_URL = "https://app.ctrlplane.dev"; | ||||||||||||||||||||||
|
||||||||||||||||||||||
public static final String DEFAULT_AGENT_ID = "jenkins-agent"; | ||||||||||||||||||||||
public static final int DEFAULT_POLLING_INTERVAL_SECONDS = 60; | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** @return the singleton instance */ | ||||||||||||||||||||||
public static CtrlplaneGlobalConfiguration get() { | ||||||||||||||||||||||
return ExtensionList.lookupSingleton(CtrlplaneGlobalConfiguration.class); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
private String apiUrl; | ||||||||||||||||||||||
private String apiKey; | ||||||||||||||||||||||
|
||||||||||||||||||||||
private String agentId; | ||||||||||||||||||||||
private String agentWorkspaceId; | ||||||||||||||||||||||
private int pollingIntervalSeconds; | ||||||||||||||||||||||
Comment on lines
+28
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Store - private String apiKey;
+ private String credentialsId; // Then retrieve the token from Jenkins Credentials Manager 📝 Committable suggestion
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Valid, but we need to integrate with the jenkins credentials plugin. If you have ideas of how to do that @coderabbitai would love to take them into consideration. Or maybe you can make a followup issue and pr to address that issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No description provided. |
||||||||||||||||||||||
|
||||||||||||||||||||||
public CtrlplaneGlobalConfiguration() { | ||||||||||||||||||||||
// When Jenkins is restarted, load any saved configuration from disk. | ||||||||||||||||||||||
load(); | ||||||||||||||||||||||
// Set defaults only if loaded values are null/blank/zero | ||||||||||||||||||||||
if (StringUtils.isBlank(apiUrl)) { | ||||||||||||||||||||||
apiUrl = DEFAULT_API_URL; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
if (pollingIntervalSeconds <= 0) { // Set default interval if not loaded or invalid | ||||||||||||||||||||||
pollingIntervalSeconds = DEFAULT_POLLING_INTERVAL_SECONDS; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** @return the currently configured API URL, or default if not set */ | ||||||||||||||||||||||
public String getApiUrl() { | ||||||||||||||||||||||
return StringUtils.isBlank(apiUrl) ? DEFAULT_API_URL : apiUrl; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* Sets the API URL | ||||||||||||||||||||||
* @param apiUrl the new API URL | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
@DataBoundSetter | ||||||||||||||||||||||
public void setApiUrl(String apiUrl) { | ||||||||||||||||||||||
this.apiUrl = apiUrl; | ||||||||||||||||||||||
save(); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** @return the currently configured API key */ | ||||||||||||||||||||||
public String getApiKey() { | ||||||||||||||||||||||
return apiKey; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* Sets the API key | ||||||||||||||||||||||
* @param apiKey the new API key | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
@DataBoundSetter | ||||||||||||||||||||||
public void setApiKey(String apiKey) { | ||||||||||||||||||||||
this.apiKey = apiKey; | ||||||||||||||||||||||
save(); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** @return the currently configured agent ID */ | ||||||||||||||||||||||
public String getAgentId() { | ||||||||||||||||||||||
return StringUtils.isBlank(agentId) ? DEFAULT_AGENT_ID : agentId; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* Sets the agent ID | ||||||||||||||||||||||
* @param agentId the new agent ID | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
@DataBoundSetter | ||||||||||||||||||||||
public void setAgentId(String agentId) { | ||||||||||||||||||||||
this.agentId = agentId; | ||||||||||||||||||||||
save(); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** @return the currently configured agent workspace ID */ | ||||||||||||||||||||||
public String getAgentWorkspaceId() { | ||||||||||||||||||||||
return agentWorkspaceId; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* Sets the agent workspace ID | ||||||||||||||||||||||
* @param agentWorkspaceId the new agent workspace ID | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
@DataBoundSetter | ||||||||||||||||||||||
public void setAgentWorkspaceId(String agentWorkspaceId) { | ||||||||||||||||||||||
this.agentWorkspaceId = agentWorkspaceId; | ||||||||||||||||||||||
save(); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** @return the currently configured polling interval in seconds */ | ||||||||||||||||||||||
public int getPollingIntervalSeconds() { | ||||||||||||||||||||||
// Return default if current value is invalid | ||||||||||||||||||||||
return pollingIntervalSeconds > 0 ? pollingIntervalSeconds : DEFAULT_POLLING_INTERVAL_SECONDS; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* Sets the polling interval. | ||||||||||||||||||||||
* @param pollingIntervalSeconds The new interval in seconds. | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
@DataBoundSetter | ||||||||||||||||||||||
public void setPollingIntervalSeconds(int pollingIntervalSeconds) { | ||||||||||||||||||||||
// Ensure a minimum value (e.g., 10 seconds) to prevent overly frequent polling | ||||||||||||||||||||||
this.pollingIntervalSeconds = Math.max(10, pollingIntervalSeconds); | ||||||||||||||||||||||
save(); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
public FormValidation doCheckApiUrl(@QueryParameter String value) { | ||||||||||||||||||||||
|
||||||||||||||||||||||
if (StringUtils.isEmpty(value)) { | ||||||||||||||||||||||
return FormValidation.warning("API URL is recommended. Defaults to " + DEFAULT_API_URL); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
return FormValidation.ok(); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
public FormValidation doCheckApiKey(@QueryParameter String value) { | ||||||||||||||||||||||
|
||||||||||||||||||||||
if (StringUtils.isEmpty(value)) { | ||||||||||||||||||||||
return FormValidation.warning("API Key is required for the agent to poll for jobs."); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
return FormValidation.ok(); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
public FormValidation doCheckAgentId(@QueryParameter String value) { | ||||||||||||||||||||||
|
||||||||||||||||||||||
if (StringUtils.isEmpty(value)) { | ||||||||||||||||||||||
return FormValidation.warning("Agent ID is required for the agent to identify itself."); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
return FormValidation.ok(); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
public FormValidation doCheckAgentWorkspaceId(@QueryParameter String value) { | ||||||||||||||||||||||
|
||||||||||||||||||||||
if (StringUtils.isEmpty(value)) { | ||||||||||||||||||||||
return FormValidation.warning("Agent Workspace ID is required for the agent to identify itself."); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
return FormValidation.ok(); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
public FormValidation doCheckPollingIntervalSeconds(@QueryParameter String value) { | ||||||||||||||||||||||
|
||||||||||||||||||||||
try { | ||||||||||||||||||||||
if (StringUtils.isEmpty(value)) { | ||||||||||||||||||||||
return FormValidation.ok("Using default interval: " + DEFAULT_POLLING_INTERVAL_SECONDS + " seconds."); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
int interval = Integer.parseInt(value); | ||||||||||||||||||||||
if (interval < 10) { | ||||||||||||||||||||||
return FormValidation.error("Polling interval must be at least 10 seconds."); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
return FormValidation.ok(); | ||||||||||||||||||||||
} catch (NumberFormatException e) { | ||||||||||||||||||||||
return FormValidation.error("Polling interval must be a valid integer."); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} | ||||||||||||||||||||||
} |
Uh oh!
There was an error while loading. Please reload this page.