Skip to content

Commit ef0701c

Browse files
committed
Great HTML report, not handlbar-ized
1 parent 127fe56 commit ef0701c

6 files changed

Lines changed: 187 additions & 12 deletions

File tree

get-workspace-admins/README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,32 @@ This directory contains scripts to retrieve information about Postman workspaces
44

55
## Scripts
66

7-
- `get-team-workspaces.js`: Retrieves all Postman workspaces with visibility set to 'team'
7+
- `get-team-workspaces.js`: Retrieves all Postman workspaces with visibility set to 'team' and generates an HTML report of workspace admins
88

99
## Usage
1010

1111
### Prerequisites
1212

1313
- Node.js (v14 or later recommended)
1414
- A Postman API key with read access to your workspaces
15+
- Handlebars (will be installed when you run `npm install`)
1516

1617
### Setup
1718

1819
1. Ensure you're in the project root directory
1920
2. Install dependencies if you haven't already:
2021
```
21-
npm install
22+
npm install handlebars
2223
```
2324

2425
3. Set your Postman API key as an environment variable:
2526
```
2627
export POSTMAN_API_KEY="your-api-key-here"
2728
```
2829

29-
### Retrieving Team Workspaces
30+
### Retrieving Team Workspaces and Admins
3031

31-
Run the script to fetch all workspaces with 'team' visibility:
32+
Run the script to fetch all workspaces with 'team' visibility and their admin users:
3233

3334
```
3435
node get-workspace-admins/get-team-workspaces.js [output-directory]
@@ -38,12 +39,12 @@ If `output-directory` is not specified, data will be saved to `../postman-export
3839

3940
## Output
4041

41-
The script will create the following file:
42+
The script will create the following files:
4243

4344
- `team-workspaces.json`: Contains metadata for all workspaces with 'team' visibility
45+
- `workspace-admins-report.json`: Contains workspace details and information about their admin users
46+
- `admin-report.html`: HTML report displaying workspace admins in a formatted table
4447

45-
## Next Steps
48+
## Customizing the HTML Report
4649

47-
Future updates will include:
48-
- Retrieving admin users for each team workspace
49-
- Detailed user information for workspace administrators
50+
The HTML report is generated using a Handlebars template. You can customize the appearance by modifying the `report-template.hbs` file in the `get-workspace-admins` directory.

get-workspace-admins/create-admin-report.js

Whitespace-only changes.

get-workspace-admins/get-team-workspaces.js

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
*
1212
* Output:
1313
* - team-workspaces.json: Contains all team workspaces metadata
14-
* - workspace-admins.json: Contains workspace IDs, names, types, and admin user IDs
14+
* - workspace-admins-report.json: Contains workspace IDs, names, types, and admin user details
15+
* - admin-report.html: HTML report of workspace admins rendered using the Handlebars template
1516
*/
1617

1718
const axios = require('axios');
1819
const fs = require('fs');
1920
const path = require('path');
21+
const handlebars = require('handlebars');
2022
let curReportItem = {};
2123
let adminReport = [];
2224
let finalReport = {};
@@ -87,6 +89,107 @@ async function getTeamWorkspaces() {
8789
}
8890
}
8991

92+
/**
93+
* Generate a static HTML report from the workspace admins report data
94+
* @param {string} reportJsonPath - Path to the workspace admins JSON report
95+
* @param {string} templatePath - Path to the Handlebars template file
96+
* @param {string} outputPath - Path where the HTML report should be saved
97+
*/
98+
async function generateHtmlReport(reportJsonPath, templatePath, outputPath) {
99+
try {
100+
console.log('Generating HTML report...');
101+
102+
// Check if report JSON exists
103+
if (!fs.existsSync(reportJsonPath)) {
104+
console.error(`Error: Report JSON file not found at ${reportJsonPath}`);
105+
return false;
106+
}
107+
108+
// Check if template exists
109+
if (!fs.existsSync(templatePath)) {
110+
console.error(`Error: Template file not found at ${templatePath}`);
111+
return false;
112+
}
113+
114+
// Ensure the output directory exists
115+
const outputDir = path.dirname(outputPath);
116+
if (!fs.existsSync(outputDir)) {
117+
fs.mkdirSync(outputDir, { recursive: true });
118+
console.log(`Created output directory: ${outputDir}`);
119+
}
120+
121+
// Read the template and report data
122+
const styleTemplate = fs.readFileSync(templatePath, 'utf-8');
123+
const reportData = JSON.parse(fs.readFileSync(reportJsonPath, 'utf-8'));
124+
125+
// Create a complete HTML template with the styles
126+
const fullTemplate = `
127+
<!DOCTYPE html>
128+
<html lang="en">
129+
<head>
130+
<meta charset="UTF-8">
131+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
132+
<title>Postman Workspace Admins Report</title>
133+
${styleTemplate}
134+
</head>
135+
<body>
136+
<h1>Postman Workspace Admins Report</h1>
137+
<p>Generated: {{generated_at}}</p>
138+
139+
<table class="tftable">
140+
<tr>
141+
<th>Workspace</th>
142+
<th>Type</th>
143+
<th>Visibility</th>
144+
<th>Admin User</th>
145+
<th>Username</th>
146+
<th>Email</th>
147+
</tr>
148+
{{#each workspaces}}
149+
{{#each admin_users}}
150+
<tr>
151+
{{#if @index}}
152+
<td></td>
153+
<td></td>
154+
<td></td>
155+
{{else}}
156+
<td>{{../workspace_name}}</td>
157+
<td>{{../workspace_type}}</td>
158+
<td>{{../workspace_visibility}}</td>
159+
{{/if}}
160+
<td>{{user_name}}</td>
161+
<td>{{user_username}}</td>
162+
<td>{{user_email}}</td>
163+
</tr>
164+
{{else}}
165+
<tr>
166+
<td>{{workspace_name}}</td>
167+
<td>{{workspace_type}}</td>
168+
<td>{{workspace_visibility}}</td>
169+
<td colspan="3">No admin users found</td>
170+
</tr>
171+
{{/each}}
172+
{{/each}}
173+
</table>
174+
</body>
175+
</html>
176+
`;
177+
178+
// Compile and render the template
179+
const template = handlebars.compile(fullTemplate);
180+
const htmlOutput = template(reportData);
181+
182+
// Write the HTML output to the file
183+
fs.writeFileSync(outputPath, htmlOutput);
184+
console.log(`HTML report generated successfully: ${outputPath}`);
185+
186+
return true;
187+
} catch (error) {
188+
console.error('Error generating HTML report:', error.message);
189+
return false;
190+
}
191+
}
192+
90193
/**
91194
* Get admin users for a specific workspace with detailed user information
92195
* @param {string} workspaceId - The ID of the workspace to get admins for
@@ -216,6 +319,11 @@ async function main() {
216319
fs.writeFileSync(adminsFilePath, JSON.stringify(finalReport, null, 2));
217320
console.log(`\nSaved workspace admin report to: ${adminsFilePath}`);
218321

322+
// Generate HTML report
323+
const templatePath = path.join(__dirname, 'report-template.hbs');
324+
const htmlOutputPath = path.join(outputDir, 'admin-report.html');
325+
await generateHtmlReport(adminsFilePath, templatePath, htmlOutputPath);
326+
219327
} catch (error) {
220328
console.error('Error in main execution:', error);
221329
process.exit(1);

package-lock.json

Lines changed: 66 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"get-team-workspaces": "node get-workspace-admins/get-team-workspaces.js"
2222
},
2323
"dependencies": {
24-
"axios": "^1.12.2"
24+
"axios": "^1.12.2",
25+
"handlebars": "^4.7.8"
2526
}
2627
}

0 commit comments

Comments
 (0)