Skip to content

Instantly share code, notes, and snippets.

@ElliotG
Created September 15, 2023 21:00
Show Gist options
  • Save ElliotG/8e88d17c380f8b1e514c507eceb8a8fd to your computer and use it in GitHub Desktop.
Save ElliotG/8e88d17c380f8b1e514c507eceb8a8fd to your computer and use it in GitHub Desktop.
Asana <> GitHub Integration
import requests
import os
GITHUB_TOKEN = 'YOUR_GITHUB_TOKEN'
ASANA_TOKEN = 'YOUR_ASANA_TOKEN'
ASANA_PROJECT_ID = 'YOUR_ASANA_PROJECT_ID'
GITHUB_REPOS = ["user/repo1", "user/repo2", ...] # List your repos here
HEADERS_GITHUB = {
'Authorization': f'token {GITHUB_TOKEN}',
'Accept': 'application/vnd.github.v3+json',
}
HEADERS_ASANA = {
'Authorization': f'Bearer {ASANA_TOKEN}',
'Accept': 'application/json',
'Content-Type': 'application/json',
}
def fetch_github_issues(repo):
url = f"https://api.github.com/repos/{repo}/issues"
response = requests.get(url, headers=HEADERS_GITHUB)
return response.json()
def find_or_create_asana_section(project_id, name):
# Check if section exists
sections_url = f"https://app.asana.com/api/1.0/projects/{project_id}/sections"
response = requests.get(sections_url, headers=HEADERS_ASANA)
for section in response.json().get("data", []):
if section["name"] == name:
return section["gid"]
# Create new section
data = {
"data": {
"name": name,
}
}
response = requests.post(sections_url, headers=HEADERS_ASANA, json=data)
return response.json()["data"]["gid"]
def create_asana_task(project_id, section_gid, issue):
url = f"https://app.asana.com/api/1.0/tasks"
data = {
"data": {
"projects": [project_id],
"memberships": [
{
"project": project_id,
"section": section_gid,
}
],
"name": issue["title"],
"notes": issue["body"],
}
}
requests.post(url, headers=HEADERS_ASANA, json=data)
def main():
for repo in GITHUB_REPOS:
issues = fetch_github_issues(repo)
section_name = repo.split("/")[-1] # Assuming you want the repo name as the section name
section_gid = find_or_create_asana_section(ASANA_PROJECT_ID, section_name)
for issue in issues:
create_asana_task(ASANA_PROJECT_ID, section_gid, issue)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment