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-69917] - Snippetizer/index.jelly javascript un-inlined. #599

Merged
merged 8 commits into from
Dec 7, 2023

Conversation

Artmorse
Copy link
Contributor

The issue
You can find all the issue details here.

My updates
I've moved the onclick call from the Snippetizer/index.jelly file to the handle-prototype.js one.

  • Make sure you are opening from a topic/feature/bugfix branch (right side) and not your main branch!
  • Ensure that the pull request title represents the desired changelog entry
  • Please describe what you did
  • Link to relevant issues in GitHub or Jira
  • Link to relevant pull requests, esp. upstream and downstream changes
  • Ensure you have provided tests - that demonstrates feature works or fixes the issue

@Artmorse Artmorse requested a review from a team as a code owner October 25, 2022 07:51
Copy link
Member

@jglick jglick left a 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}
Copy link
Member

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.

Copy link
Contributor Author

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...

Copy link
Member

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()}"/>
Copy link
Member

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

var json = Object.toJSON(JSON.parse(document.forms.config.elements.json.value).prototype);
already worked only with the singleton so I guess it does not matter.

@jglick
Copy link
Member

jglick commented Jan 9, 2023

@Artmorse do you plan to fix this or should it be closed?

@Artmorse
Copy link
Contributor Author

Yes, I'll find some time to have a look to it this week!

@Artmorse
Copy link
Contributor Author

Needs to be tested with snippet generator running in various contexts (root, job, multibranch project) and with different values for the Jenkins context path.

Can you give me more explanation about how to test it ?
I've only some information in the JENKINS-69917.

@jglick
Copy link
Member

jglick commented Jan 17, 2023

The page is accessible from e.g.

  • /pipeline-syntax/ (Jenkins root, e.g. https://ci.jenkins.io/pipeline-syntax/)
  • that URL appended to a multibranch folder (do not have a publicly accessible live example for you)
  • that URL appended to a job (including one branch project under a multibranch folder)

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 /jenkins prefix used when you mvn hpi:run this plugin).

@jglick jglick added the bug label Jan 18, 2023
@Artmorse
Copy link
Contributor Author

I've some issues with mvn hpi:run so it's not tested yet.
image

Copy link
Member

@jglick jglick left a 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;
Copy link
Member

Choose a reason for hiding this comment

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

also like #599 (comment)

Co-authored-by: Jesse Glick <jglick@cloudbees.com>
@jglick
Copy link
Member

jglick commented Oct 30, 2023

@Artmorse do you plan to resolve the merge conflict with #702 and retest?

@Artmorse
Copy link
Contributor Author

@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)}"/>
Copy link
Contributor Author

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.

Copy link
Member

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

public HttpResponse doGenerateSnippet(StaplerRequest req, @QueryParameter String json) throws Exception {
since it takes 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.

@Artmorse Artmorse requested a review from jglick November 6, 2023 13:13
Copy link
Member

@jglick jglick left a 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?

Copy link
Member

@jglick jglick left a 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)}"/>
Copy link
Member

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

public HttpResponse doGenerateSnippet(StaplerRequest req, @QueryParameter String json) throws Exception {
since it takes 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.

Comment on lines +33 to +38
document.addEventListener('DOMContentLoaded', () => {

const generatePipelineScript = document.getElementById("generatePipelineScript");
const url = generatePipelineScript.getAttribute("data-url");
const crumb = generatePipelineScript.getAttribute("data-crumb");
generatePipelineScript.onclick = (_) => {
Copy link
Member

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()}"/>
Copy link
Member

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.

@jglick jglick merged commit 9d3888c into jenkinsci:master Dec 7, 2023
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants