Skip to content

DRAFT: Sync tasks between two consoles #21

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Please provide a brief description of your changes and the context for this inte

- [ ] **New Integration Folder:** A new folder has been created for the integration.
- [ ] **Updated README:** The README has been updated based on the boilerplate to reflect the new integration details.
- [ ] **customer-integration.star File:** The `customer-integration-<name>.star` file has been created/updated as required.
- [ ] **custom-integration.star File:** The `custom-integration-<name>.star` file has been created/updated as required.
- [ ] **config.json File:** The `config.json` is updated with the `name` (product name) and `type` (inbound or outbound) of integration.

---
Expand Down
2 changes: 1 addition & 1 deletion docs/integrations.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"lastUpdated": "2025-04-08T16:09:55.802912Z",
"lastUpdated": "2025-04-16T13:38:57.530777Z",
"totalIntegrations": 10,
"integrationDetails": [
{
Expand Down
1 change: 1 addition & 0 deletions task-sync/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "name": "runZero", "type": "inbound" }
74 changes: 74 additions & 0 deletions task-sync/custom-integration-task-sync.star
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
load('http', http_get='get', http_post='post', http_put='put', 'url_encode')
load('json', json_decode='decode')
load('gzip', gzip_decompress='decompress', gzip_compress='compress')

# Parameters from kwargs
SAAS_ORG_ID = "4ffe8ffb-18e7-451b-9aea-7c967ad07f8e"
SAAS_SITE_ID = "a7b2287e-51fa-47fc-bd00-6a68eed0786b"
SAAS_BASE_URL = "https://console.runzero.com"
SELF_ORG_ID = "e9004d87-e127-4863-aa5e-5c302e87694e"
SELF_SITE_ID = "0396debb-1874-41d2-933e-c2cf4f59a8e5"
SELF_BASE_URL = "https://console.runzero.com"
SAAS_TASK_SEARCH_FILTER = 'name:="test"'

# Flags
HIDE_TASKS_ON_SYNC = False

def get_tasks(saas_token):
params = {"_oid": SAAS_ORG_ID, "search": SAAS_TASK_SEARCH_FILTER}
url = "{}{}{}".format(SAAS_BASE_URL, "/api/v1.0/org/tasks?", url_encode(params))
headers = {"Content-Type": "application/json", "Authorization": "Bearer {}".format(saas_token)}
response = http_get(url, headers=headers)
if response.status_code != 200:
print("Failed to get tasks", response.status_code)
return []
return json_decode(response.body)

def sync_task(task_id, saas_token, self_token):
# Download data from SaaS
print("Pulling task with ID {}".format(task_id))
download_url = "{}/api/v1.0/org/tasks/{}/data".format(SAAS_BASE_URL, task_id)
download = http_get(download_url, headers={"Authorization": "Bearer {}".format(saas_token), "Accept": "application/octet-stream", "Content-Encoding": "gzip"}, timeout=3600)
if download.status_code != 200:
print("Failed to download task:", task_id)
return False

# Upload data to self-hosted
print("Uploading task with ID {}".format(task_id))
unzipped = gzip_decompress(download.body)
upload_url = "{}/api/v1.0/org/sites/{}/import?_oid={}".format(SELF_BASE_URL, SELF_SITE_ID, SELF_ORG_ID)
upload = http_put(upload_url, headers={"Authorization": "Bearer {}".format(self_token), "Content-Type": "application/octet-stream", "Content-Encoding": "gzip"}, body=gzip_compress(unzipped), timeout=3600)

if upload.status_code != 200:
print("Failed to upload task:", task_id)
return False

print("Successfully synced task:", task_id)

if HIDE_TASKS_ON_SYNC:
hide_url = "{}/api/v1.0/org/tasks/{}/hide?_oid={}".format(SAAS_BASE_URL, task_id, SAAS_ORG_ID)
hide = http_post(hide_url, headers={"Authorization": "Bearer {}".format(saas_token), "Content-Type": "application/json"})
if hide.status_code == 200:
print("Task hidden:", task_id)

return True

def main(**kwargs):
saas_token = kwargs["access_key"] # SaaS token
self_token = kwargs["access_secret"] # Self-hosted token

tasks = get_tasks(saas_token)
print("Got {} task(s) to sync".format(len(tasks)))
if not tasks:
print("No tasks found.")
return

for task in tasks:
task_id = task.get("id", "")
if not task_id:
continue
success = sync_task(task_id, saas_token, self_token)
if not success:
print("Sync failed for task:", task_id)

return None