Skip to content

Instantly share code, notes, and snippets.

@turboBasic
Last active August 9, 2020 08:26
Show Gist options
  • Select an option

  • Save turboBasic/90106f8f0dabc7855b2218284301ce2e to your computer and use it in GitHub Desktop.

Select an option

Save turboBasic/90106f8f0dabc7855b2218284301ce2e to your computer and use it in GitHub Desktop.
Get all file elements from jenkins job workspace view in browser #jenkins #javascript

Get file listing from web page with Jenkins job workspace

Situation: you see some folder in a job workspace, like on screenshot below and you want to copy all file names with sizes and mtime data.

Jenkins job worspace listing

Solution: paste the following code into your browser console:

$x('//table[@class="fileList"]/tbody/tr[count(./td)>2]//td[position()<last() and position()!=1]')
.reduce( 
    (result, current) => { 
        if (current.className != "fileSize") { 
            result.push({
                name: current.textContent, 
                info: [] 
            }) 
        } else { 
            result[result.length - 1].info.push(current.textContent) 
        }
        return result 
    }, 
    [] 
)
.map(a => { return { 
    name: a.name, 
    mtime: a.info[0], 
    size: a.info[1] 
}})

Result 1: You will get an array of objects representing the files in current folder of workspace:

Workspace listing result

In order to convert it to plain text representation, append the following to the code snippet above:

.map(a => [a.name, a.mtime, a.size] .join('\t'))
.join('\n')

Result 2:

Workspace listing as plain text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment