Skip to content

Commit

Permalink
Website: Add line number to edit page links on policies, queries and …
Browse files Browse the repository at this point in the history
…vitals pages. (#25965)

Changes:
- Updated the `build-static-content` script to set a `lineNumberInYaml`
value on queries, policies, and vitals.
- Updated the edit page button on the policy-details, query-detail, and
vital-details pages to take users to the specified query's line number
in the YAML file.
  • Loading branch information
eashaw authored Feb 6, 2025
1 parent 6c512ec commit 4b2814a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
23 changes: 22 additions & 1 deletion website/scripts/build-static-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,27 @@ module.exports = {
async()=>{// Parse queries.yml library (Informational queries and host vital queries) from YAML and prepare to bake them into the Sails app's configuration.
let RELATIVE_PATH_TO_QUERY_LIBRARY_YML_IN_FLEET_REPO = 'docs/queries.yml';
let yaml = await sails.helpers.fs.read(path.join(topLvlRepoPath, RELATIVE_PATH_TO_QUERY_LIBRARY_YML_IN_FLEET_REPO)).intercept('doesNotExist', (err)=>new Error(`Could not find standard query library YAML file at "${RELATIVE_PATH_TO_QUERY_LIBRARY_YML_IN_FLEET_REPO}". Was it accidentally moved? Raw error: `+err.message));

let linesInYamlFile = yaml.split('\n');
let queriesWithProblematicDiscovery = [];
let queriesWithProblematicContributors = [];
let queriesWithProblematicTags = [];
let queries = YAML.parseAllDocuments(yaml).map((yamlDocument)=>{
let query = yamlDocument.toJSON().spec;
query.kind = yamlDocument.toJSON().kind;
query.slug = _.kebabCase(query.name);// « unique slug to use for routing to this query's detail page

// Determine the line in the yaml that this query starts on.
// this will allow us to link users directly to the query's position in the YAML file (currently >1500 lines) when users want to make a change.
let lineWithTheQueriesNameKey = _.find(linesInYamlFile, (line)=>{
return line.includes('name: '+query.name);
});
let lineNumberForEdittingThisQuery = linesInYamlFile.indexOf(lineWithTheQueriesNameKey);
query.lineNumberInYaml = lineNumberForEdittingThisQuery; // Set the line number for edits.

// Remove the platform name from query names. This allows us to keep queries at their existing URLs while hiding them in the UI.
query.name = query.name.replace(/\s\(macOS\)|\(Windows\)|\(Linux\)|\(Chrome\)|\(macOS\/Linux\)$/, '');


if ((query.discovery !== undefined && !_.isString(query.discovery)) || (query.kind !== 'built-in' && _.isString(query.discovery))) {
queriesWithProblematicDiscovery.push(query);
}
Expand Down Expand Up @@ -167,6 +178,7 @@ module.exports = {
let RELATIVE_PATH_TO_POLICY_LIBRARY_YML_IN_FLEET_REPO = 'docs/01-Using-Fleet/standard-query-library/standard-query-library.yml';
// let RELATIVE_PATH_TO_QUERY_LIBRARY_YML_IN_FLEET_REPO = 'docs/queries.yml';
let yaml = await sails.helpers.fs.read(path.join(topLvlRepoPath, RELATIVE_PATH_TO_POLICY_LIBRARY_YML_IN_FLEET_REPO)).intercept('doesNotExist', (err)=>new Error(`Could not find standard query library YAML file at "${RELATIVE_PATH_TO_POLICY_LIBRARY_YML_IN_FLEET_REPO}". Was it accidentally moved? Raw error: `+err.message));
let linesInYamlFile = yaml.split('\n');
let queriesWithProblematicResolutions = [];
let queriesWithProblematicContributors = [];
let queriesWithProblematicTags = [];
Expand All @@ -177,6 +189,15 @@ module.exports = {
if(query.kind === 'query'){
return undefined;
}
// Determine the line in the yaml that this query starts on.
// this will allow us to link users directly to the query's position in the YAML file (currently >1500 lines) when users want to make a change.
let lineWithTheQueriesNameKey = _.find(linesInYamlFile, (line)=>{
return line.includes('name: '+query.name);
});
let lineNumberForEdittingThisQuery = linesInYamlFile.indexOf(lineWithTheQueriesNameKey);
query.lineNumberInYaml = lineNumberForEdittingThisQuery; // Set the line number for edits.


query.slug = _.kebabCase(query.name);// « unique slug to use for routing to this query's detail page
// Remove the platform name from query names. This allows us to keep queries at their existing URLs while hiding them in the UI.
query.name = query.name.replace(/\s\(macOS\)|\(Windows\)|\(Linux\)$/, '');
Expand Down
2 changes: 1 addition & 1 deletion website/views/pages/policy-details.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
<a href="/docs">Docs</a>
<a href="/docs/rest-api">REST API</a>
<a href="/guides">Guides</a>
<a purpose="edit-button" class="d-flex align-items-center text-nowrap" target="_blank" :href="'https://github.com/fleetdm/fleet/edit/main/'+queryLibraryYmlRepoPath"><img alt="A pencil icon" src="/images/[email protected]">Edit page</a>
<a purpose="edit-button" class="d-flex align-items-center text-nowrap" target="_blank" :href="'https://github.com/fleetdm/fleet/edit/main/'+queryLibraryYmlRepoPath+'#L'+policy.lineNumberInYaml"><img alt="A pencil icon" src="/images/[email protected]">Edit page</a>
</div>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion website/views/pages/query-detail.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
</div>
</div>
<div purpose="docs-links" class="order-3">
<a purpose="sidebar-link" :href="'https://github.com/fleetdm/fleet/edit/main/'+queryLibraryYmlRepoPath"> <img src="/images/[email protected]" alt="Suggest an edit">Edit</a>
<a purpose="sidebar-link" :href="'https://github.com/fleetdm/fleet/edit/main/'+queryLibraryYmlRepoPath+'#L'+query.lineNumberInYaml"> <img src="/images/[email protected]" alt="Suggest an edit">Edit</a>
<a purpose="sidebar-link" href="/contact"><img alt="Talk to an engineer" src="/images/[email protected]">Talk to us</a>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion website/views/pages/vital-details.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
</div>
<% } %>
<div purpose="edit-button-container">
<a purpose="edit-button" class="d-flex align-items-center text-nowrap" target="_blank" :href="'https://github.com/fleetdm/fleet/edit/main/'+queryLibraryYmlRepoPath">Suggest an edit <img src="/images/[email protected]" alt="external link"></a>
<a purpose="edit-button" class="d-flex align-items-center text-nowrap" target="_blank" :href="'https://github.com/fleetdm/fleet/edit/main/'+queryLibraryYmlRepoPath+'#L'+thisVital.lineNumberInYaml">Suggest an edit <img src="/images/[email protected]" alt="external link"></a>
</div>
</div>
</div>
Expand Down

0 comments on commit 4b2814a

Please sign in to comment.