Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EatMoreChicken/1ea16004984c8b2ab48b10426d6e4cc2 to your computer and use it in GitHub Desktop.
Save EatMoreChicken/1ea16004984c8b2ab48b10426d6e4cc2 to your computer and use it in GitHub Desktop.
This is meant to covert [permissions-reference](https://github.com/microsoftgraph/microsoft-graph-docs-contrib/blob/main/concepts/permissions-reference.md) to a CSV, but I haven't validated it yet. For it to work, you have to take what's inside just the "[All permissions](https://github.com/microsoftgraph/microsoft-graph-docs-contrib/blob/main/c…
import os
import re
import csv
input_file = 'input.md'
output_file = 'permission_table.csv'
# Delete output file if it exists
if os.path.exists(output_file):
os.remove(output_file)
# Read input file and split on '---' delimiter
all_permissions = []
with open(input_file, 'r') as file:
content = file.read()
permissions = re.split(r'---', content)
for permission in permissions:
lines = permission.split('\n')
# Trim blank lines
lines = [line for line in lines if line.strip()]
# Join lines into a single string
permission = '\n'.join(lines)
# Example Permission String
# ### WindowsUpdates.ReadWrite.All
# | Category | Application | Delegated |
# |--|--|--|
# | Identifier | 7dd1be58-6e76-4401-bf8d-31d1e8180d5b | 11776c0c-6138-4db3-a668-ee621bea2555
# | DisplayText | Read and write all Windows update deployment settings | Read and write all Windows update deployment settings
# | Description | Allows the app to read and write all Windows update deployment settings for the organization without a signed-in user. | Allows the app to read and write all Windows update deployment settings for the organization on behalf of the signed-in user.
# | AdminConsentRequired | Yes | Yes
# Extract values
permission_name = re.search(r'^#+\s*(.*)', lines[0]).group(1)
application_identifier = re.search(r'\| Identifier \| (.*) \| (.*)', permission).group(1)
application_display_text = re.search(r'\| DisplayText \| (.*) \| (.*)', permission).group(1)
application_description = re.search(r'\| Description \| (.*) \| (.*)', permission).group(1)
application_admin_consent_required = re.search(r'\| AdminConsentRequired \| (.*) \| (.*)', permission).group(1)
delegated_identifier = re.search(r'\| Identifier \| (.*) \| (.*)', permission).group(2)
delegated_display_text = re.search(r'\| DisplayText \| (.*) \| (.*)', permission).group(2)
delegated_description = re.search(r'\| Description \| (.*) \| (.*)', permission).group(2)
delegated_admin_consent_required = re.search(r'\| AdminConsentRequired \| (.*) \| (.*)', permission).group(2)
print(f'Permission Name: {permission_name}')
print(f'Application Identifier: {application_identifier}')
print(f'Application Display Text: {application_display_text}')
print(f'Application Description: {application_description}')
print(f'Application Admin Consent Required: {application_admin_consent_required}')
print(f'Delegated Identifier: {delegated_identifier}')
print(f'Delegated Display Text: {delegated_display_text}')
print(f'Delegated Description: {delegated_description}')
print(f'Delegated Admin Consent Required: {delegated_admin_consent_required}')
print()
print(f'Permission: {permission}')
# Append two different lines for application and delegated permissions
all_permissions.append({
'Permission Name': permission_name,
'Type': 'Application',
'Identifier': application_identifier,
'Display Text': application_display_text,
'Description': application_description,
'Admin Consent Required': application_admin_consent_required
})
all_permissions.append({
'Permission Name': permission_name,
'Type': 'Delegated',
'Identifier': delegated_identifier,
'Display Text': delegated_display_text,
'Description': delegated_description,
'Admin Consent Required': delegated_admin_consent_required
})
# Write to CSV
with open(output_file, 'w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=all_permissions[0].keys())
writer.writeheader()
writer.writerows(all_permissions)
@EatMoreChicken
Copy link
Author

Hmmm, no markdown support in the description?

Here's what it says:
This is meant to covert permissions-reference to a CSV, but I haven't validated it yet. For it to work, you have to take what's inside just the "All permissions" section and put it into a input.md file. From there, run the script and get the CSV output. Validate.

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