-
Notifications
You must be signed in to change notification settings - Fork 196
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-69917] - Snippetizer/index.jelly javascript un-inlined. #599
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs to be tested with snippet generator running in various contexts (root, job, multibranch project) and with different values for the Jenkins context path.
if (!json) { | ||
return; // just a separator | ||
} | ||
const url = '/jenkins/pipeline-syntax/generateSnippet' // ${rootURL}/${it.GENERATE_URL} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will not work if the context path is set to anything other than /jenkins
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how I can use/pass template variables (${rootURL}
) in the Javascript file...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not often work in JS, but there are various ways; IIRC the preferred technique is to define data
attributes on HTML elements and look those up from JS. Search core sources for examples.
@@ -67,27 +67,10 @@ THE SOFTWARE. | |||
<f:dropdownListBlock title="${%— Advanced/Deprecated —}"/> | |||
<local:listSteps quasiDescriptors="${it.getQuasiDescriptors(true)}"/> | |||
</f:dropdownList> | |||
<j:set var="id" value="${h.generateId()}"/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Loses uniquification of the textarea. OTOH
workflow-cps-plugin/src/main/resources/org/jenkinsci/plugins/workflow/cps/Snippetizer/index.jelly
Line 78 in 6467f4f
var json = Object.toJSON(JSON.parse(document.forms.config.elements.json.value).prototype); |
workflow-cps-plugin/src/main/resources/org/jenkinsci/plugins/workflow/cps/Snippetizer/index.jelly
Line 31 in 6467f4f
<f:form name="config"> |
@Artmorse do you plan to fix this or should it be closed? |
Yes, I'll find some time to have a look to it this week! |
Can you give me more explanation about how to test it ? |
The page is accessible from e.g.
And you need to check when the context root is empty (like in ci.jenkins.io) as well as when it has a value (like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks right. Tested successfully?
method: 'POST', | ||
parameters: {json: json}, | ||
onSuccess: function(r) { | ||
document.getElementById('prototypeText').value = r.responseText; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also like #599 (comment)
plugin/src/main/resources/org/jenkinsci/plugins/workflow/cps/Snippetizer/index.jelly
Outdated
Show resolved
Hide resolved
plugin/src/main/resources/org/jenkinsci/plugins/workflow/cps/Snippetizer/handle-prototype.js
Outdated
Show resolved
Hide resolved
Co-authored-by: Jesse Glick <jglick@cloudbees.com>
@jglick thanks for pinging me, I'll look at it this week! |
<input type="button" id="generatePipelineScript" value="${%Generate Pipeline Script}" | ||
class="submit-button primary" | ||
data-url="${rootURL}/${it.GENERATE_URL}" | ||
data-crumb="${h.getCrumb(request)}"/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the jenkins crumb to avoid 403.
That's the easiest way, I found, to pass it to the javascript script.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could also just add a CrumbExclusion
for
workflow-cps-plugin/plugin/src/main/java/org/jenkinsci/plugins/workflow/cps/Snippetizer.java
Line 487 in 4e2b31c
public HttpResponse doGenerateSnippet(StaplerRequest req, @QueryParameter String json) throws Exception { |
POST
only to handle larger quantities of input but it is in fact read-only so not a CSRF threat. Anyway, this is the straightforward translation of the original.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not think I am qualified to review this. Someone else?
plugin/src/main/resources/org/jenkinsci/plugins/workflow/cps/Snippetizer/handle-prototype.js
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to work.
<input type="button" id="generatePipelineScript" value="${%Generate Pipeline Script}" | ||
class="submit-button primary" | ||
data-url="${rootURL}/${it.GENERATE_URL}" | ||
data-crumb="${h.getCrumb(request)}"/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could also just add a CrumbExclusion
for
workflow-cps-plugin/plugin/src/main/java/org/jenkinsci/plugins/workflow/cps/Snippetizer.java
Line 487 in 4e2b31c
public HttpResponse doGenerateSnippet(StaplerRequest req, @QueryParameter String json) throws Exception { |
POST
only to handle larger quantities of input but it is in fact read-only so not a CSRF threat. Anyway, this is the straightforward translation of the original.
document.addEventListener('DOMContentLoaded', () => { | ||
|
||
const generatePipelineScript = document.getElementById("generatePipelineScript"); | ||
const url = generatePipelineScript.getAttribute("data-url"); | ||
const crumb = generatePipelineScript.getAttribute("data-crumb"); | ||
generatePipelineScript.onclick = (_) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a class
and Behaviour.specify
is more conventional I think, but this should be fine as well.
@@ -67,36 +67,13 @@ THE SOFTWARE. | |||
<f:dropdownListBlock title="${%— Advanced/Deprecated —}"/> | |||
<local:listSteps quasiDescriptors="${it.getQuasiDescriptors(true)}"/> | |||
</f:dropdownList> | |||
<j:set var="id" value="${h.generateId()}"/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that removing generateId
and reverting to a hard-coded id loses the ability for this code to exist in multiple places in a page. Not an issue in this case since it exists only in one place in a top-level page. The general approach is to use behaviors and look up elements via relative DOM path.
The issue
You can find all the issue details here.
My updates
I've moved the
onclick
call from theSnippetizer/index.jelly
file to thehandle-prototype.js
one.