Skip to content

Commit

Permalink
Merge pull request #63 from demisto/open_last_rn
Browse files Browse the repository at this point in the history
Add `Open Last RN` command
  • Loading branch information
shmuel44 authored Nov 26, 2024
2 parents 331c086 + 72c2797 commit 0ae3961
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# Unreleased

- Added **Open last release note** command to open the last release note (`Cmd+R` on Mac, `Ctrl+Alt+R` on linux and Windows)

# [0.7.3] (2024-09-24)

- Documentation improvements.
Expand Down
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 26 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"onCommand:xsoar.lint",
"onCommand:xsoar.lintUsingGit",
"onCommand:xsoar.readProblems",
"onCommand:xsoar.openLastRN",
"*"
],
"main": "./dist/extension.js",
Expand Down Expand Up @@ -129,6 +130,11 @@
"command": "xsoar.developDemistoSDK",
"title": "Develop and Debug Demisto-SDK",
"category": "XSOAR"
},
{
"command": "xsoar.openLastRN",
"title": "Open last release note",
"category": "XSOAR"
}
],
"menus": {
Expand Down Expand Up @@ -176,6 +182,10 @@
{
"command": "xsoar.configureXSOAR",
"when": "resourcePath =~ /.*Packs/|\\.+/"
},
{
"command": "xsoar.openLastRN",
"when": "resourcePath =~ /.*Packs/|\\.+/"
}
],
"explorer/context": [
Expand Down Expand Up @@ -222,6 +232,10 @@
{
"command": "xsoar.configureXSOAR",
"when": "resourcePath =~ /.*Packs/|\\.+/"
},
{
"command": "xsoar.openLastRN",
"when": "resourcePath =~ /.*Packs/|\\.+/"
}
]
},
Expand Down Expand Up @@ -286,7 +300,17 @@
"description": "Whether to show the running process of demisto-sdk in the terminal windows and will not dispose it after finish."
}
}
}
},
"keybindings": [
{
"command": "xsoar.openLastRN",
"key": "ctrl+r",
"when": "editorTextFocus",
"win": "ctrl+alt+r",
"linux": "ctrl+alt+r",
"mac": "cmd+r"
}
]
},
"scripts": {
"vscode:prepublish": "webpack --mode production",
Expand All @@ -305,6 +329,7 @@
"@types/minimatch": "^3.0.4",
"@types/mocha": "^8.0.4",
"@types/node": "^12.20.47",
"@types/semver": "^7.5.8",
"@types/vscode": "^1.54.0",
"@typescript-eslint/eslint-plugin": "^4.26.1",
"@typescript-eslint/parser": "^4.26.1",
Expand Down
10 changes: 10 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Logger } from './logger';
import { setupIntegrationEnv, installDevEnv as installDevEnv, configureDemistoVars, developDemistoSDK } from './devEnvs';
import JSON5 from 'json5'
import * as runAndDebug from './runAndDebug'
import { openLastRN } from './openLastRN';

// this function returns the directory path of the file
export function getDirPath(file: vscode.Uri | undefined): string {
Expand Down Expand Up @@ -154,6 +155,15 @@ export function activate(context: vscode.ExtensionContext): void {

})
)


context.subscriptions.push(
vscode.commands.registerCommand('xsoar.openLastRN', (file: vscode.Uri | undefined) => {
const fileToRun = getDirPath(file)
openLastRN(fileToRun)
})
)

// Create a file listener
const workspaces = vscode.workspace.workspaceFolders;
if (workspaces) {
Expand Down
50 changes: 50 additions & 0 deletions src/openLastRN.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import * as vscode from 'vscode';
import * as path from "path";
import { findDirUnderPacks } from './tools';
import { promises as fsp } from 'fs';
import * as semver from 'semver';


export function openLastRN(dirPath: string): void {
findDirUnderPacks(dirPath, 'ReleaseNotes').then(
ReleaseNotesDir => {
if (ReleaseNotesDir === undefined) {
vscode.window.showErrorMessage('Cortex "Open last release note" only works on files under the Packs directory.');
return;
}
if (!ReleaseNotesDir) {
vscode.window.showErrorMessage('Unable to find a ReleaseNotes directory.');
return;
}
getLastRNFile(ReleaseNotesDir).then(
lastReleaseNotesFile => {
const relativeFilePath = path.join(ReleaseNotesDir, lastReleaseNotesFile);
vscode.workspace.openTextDocument(vscode.Uri.file(relativeFilePath))
.then(doc => vscode.window.showTextDocument(doc));
});
}
)
}

async function getLastRNFile(directoryPath: string): Promise<string> {
try {
const entries = await fsp.readdir(directoryPath, { withFileTypes: true });
let maxSum = "0.0.0";
let latestFile = "";

entries.forEach(entry => {
if (entry.isFile()) {
const version = path.parse(entry.name).name.replace(/_/g, '.')
if (semver.gt(version, maxSum)) {
maxSum = version;
latestFile = entry.name;
}
}
});

return latestFile;
} catch (error) {
console.error('Error finding last release note in', directoryPath, error);
throw error;
}
}
36 changes: 35 additions & 1 deletion src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as fs from "fs-extra";
import { Logger } from './logger';
import { TerminalManager } from './terminalManager';


export function sendCommandExtraArgsWithUserInput(command: string[]): void {
vscode.window.showInputBox(
{
Expand Down Expand Up @@ -336,4 +337,37 @@ export function stringify(obj: Input): string {
}
}
return result.join('\n')
}
}


/**
* Searches for a specific directory under the 'Packs' directory in the given base path.
*
* @param basePath - The starting path to begin the search from.
* @param dirName - The name of the directory to find under the 'Packs' directory.
*
* @returns A Promise that resolves to:
* - The full path of the found directory as a string if it exists.
* - null if the 'Packs' directory is found but the specified directory does not exist.
* - undefined if the 'Packs' directory is not found in the base path.
*/
export async function findDirUnderPacks(basePath: string, dirName: string): Promise<string | null | undefined> {
const parts = basePath.split(path.sep);
const packsIndex = parts.indexOf('Packs');
if (packsIndex === -1 ) {
return;
}

let dirPath = path.join(...parts.slice(0, packsIndex + 2), dirName);

// Add '/' for linux operating systems
const os = process.platform;
if (os === 'linux') {
dirPath = '/' + dirPath;
}
if (fs.existsSync(dirPath)) {
return dirPath;
}

return null;
}

0 comments on commit 0ae3961

Please sign in to comment.