Skip to content

add example scripts #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions python/delete_empty_space.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This script will delete all empty Spaces in a given Project
from lightdash.api_client import LightdashApiClient

# Update these variables
TARGET_URL = 'https://app.lightdash.cloud/api/v1/'
TARGET_API_KEY = '' # Your personal Personal Access Token from Lightdash
TARGET_PROJECT_ID = '' # Project ID to delete empty Spaces

if __name__ == '__main__':
target_client = LightdashApiClient(TARGET_URL, TARGET_API_KEY, TARGET_PROJECT_ID)

# Get all spaces
print('Getting all spaces')
all_spaces = target_client.spaces(summary=True)

# Identify empty spaces
print('Identifying empty spaces')
empty_spaces = []
for space in all_spaces:
if space['chartCount'] == '0' and space['dashboardCount'] == '0':
empty_spaces.append([space['uuid'], space['name']])
print(f'Identified {len(empty_spaces)} empty spaces to delete')

# Delete empty spaces Sapces
print('Deleting empty spaces . . .')
for i in empty_spaces:
print(f'Deleting space: {i[1]}')
target_client.delete_space(i[0])
print('All empty spaces deleted!')
107 changes: 107 additions & 0 deletions python/preview_purge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# This script is designed to be used along with the start-preview Github Action
# This scipt deletes any preview project not tied to a PR currently Open or in Draft
# To run, update the variables in Lines 13-17 accordingly and adjust Lines 57-60 as needed

import os
import requests
import json
import subprocess
from github import Github
from github import Auth

# Update these variables
lightdash_url = 'https://app.lightdash.cloud/api/v1/' # URL used for Lightdash API requests
api_key = '' # Token for Lightdash API
lightdash_domain = 'https://app.lightdash.cloud/' # URL used to login with Lightdash CLI
github_token = '' # Token for Github API
git_repo = '' # Name of your Github Repo
# Headers for Lightdash API requests
headers = {
'Authorization': f'ApiKey {api_key}',
'Content-Type': 'application/json',
}

if __name__ == '__main__':
## USE LIGHTDASH API TO GET LIST OF ACTIVE PREVIEW PROJECTS
# GET request to get list of all active projects
r = requests.get(f'{lightdash_url}org/projects', headers = headers)
print(r)
# Convert response to json and results
r_json = json.loads(r.text)
projects = r_json['results']
# Filter to preview projects only
previews = []
for i in range(0, len(projects)):
if projects[i]['type'] == 'PREVIEW':
previews.append(projects[i])
# Print results
tot_previews = len(previews)
print(f'Identified {tot_previews} total Lightdash previews')

## USE GITHUB API TO GET LIST OF BRANCHES FROM OPEN PR'S (Draft or Ready for Review)
# Token authentication
auth = Auth.Token(github_token)
g = Github(auth=auth)
g.get_user().login
# Connect to data-platform repo
repo = g.get_repo(git_repo)
# Get all open pull requests
pull_requests = list(repo.get_pulls(base = 'main'))
# Close connection
g.close()
# Convert to list of branch names (delete leading "collectorsgroup:{name}/")
branches = []
for i in pull_requests:
# Isolate head branch name
val = i.head.label
# OPTIONAL: If part of an organization, remove '{org_name}:' from beginning of branch name
new = val.replace('{org_name}:','')
# OPTIONAL: If using naming convention {author_name}/{branch-name}, remove all characters before '/' and add to list
idx = new.find('/') + 1
clean = new[idx:]
branches.append(clean)
# Print results
tot_branches = len(branches)
print(f'Identified {tot_branches} total branches for open PRs')

## COMPARE PREVIEWS TO BRANCHES FROM OPEN PR'S - ID NON-PR PREVIEWS
to_delete = []
for i in range(0,len(previews)):
if previews[i]['name'] not in branches:
to_delete.append(previews[i])
# Print results
tot_deletes = len(to_delete)
if tot_deletes == 0:
print('No previews to be deleted, process completed')
else:
print(f'Identified {tot_deletes} Lightdash previews to be deleted')

## CLOSE UNNEEDED LIGHTDASH PREVIEWS
to_delete = []
for i in range(0,len(previews)):
if previews[i]['name'] not in branches:
to_delete.append(previews[i])
# Print results
tot_deletes = len(to_delete)
if tot_deletes == 0:
print('No previews to be deleted, process completed')
else:
print(f"Identified {tot_deletes} Lightdash previews to be deleted")
## CLOSE UNNEEDED LIGHTDASH PREVIEWS
deleted = 0
not_deleted = []
for i in to_delete:
print(f"Deleting preview: {i['name']} ...")
delete = requests.delete(f"{lightdash_url}org/projects/{i['projectUuid']}", headers = headers)
if delete.status_code == 200:
deleted += 1
else:
not_deleted.append(i)
print(f"Failed to delete preview: {i['name']}")
print(f'Successfully deleted {deleted} previews')
if len(not_deleted) == 0:
print('No previews failed to be deleted')
else:
print(f'Could not delete {len(not_deleted)} previews:')
for i in not_deleted:
print(i['name'])