feat: github importer (#425)
* dev: init github importer * dev: add endpoint for creating import * dev: create endpoint to bulk create issues * dev: bulk issue importer * dev: bulk create endpoints for labels and updates in issue bulk create endpoint to create labels and links * dev: add comments in bluk create * dev: status import endpoint and user invitaion workflow * dev: initiate github repo sync * dev: bulk issue sync endpoint and fix key issue in bg task * dev: update endpoints for service imports * dev: update labels logic * dev: update importer task * dev: bulk issue activities * dev: update importer task for mapped users * dev: update importer endpoint to send github token * dev: update bulk import endpoint * fix: workspace get query * dev: update bulk import endpoints
This commit is contained in:
parent
d3ca8560fc
commit
5d8f2b6b75
14 changed files with 751 additions and 12 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import os
|
||||
import jwt
|
||||
import requests
|
||||
from urllib.parse import urlparse, parse_qs
|
||||
from datetime import datetime, timedelta
|
||||
from cryptography.hazmat.primitives.serialization import load_pem_private_key
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
|
|
@ -30,7 +31,7 @@ def get_github_metadata(installation_id):
|
|||
|
||||
url = f"https://api.github.com/app/installations/{installation_id}"
|
||||
headers = {
|
||||
"Authorization": "Bearer " + token,
|
||||
"Authorization": "Bearer " + str(token),
|
||||
"Accept": "application/vnd.github+json",
|
||||
}
|
||||
response = requests.get(url, headers=headers).json()
|
||||
|
|
@ -41,7 +42,7 @@ def get_github_repos(access_tokens_url, repositories_url):
|
|||
token = get_jwt_token()
|
||||
|
||||
headers = {
|
||||
"Authorization": "Bearer " + token,
|
||||
"Authorization": "Bearer " + str(token),
|
||||
"Accept": "application/vnd.github+json",
|
||||
}
|
||||
|
||||
|
|
@ -50,9 +51,9 @@ def get_github_repos(access_tokens_url, repositories_url):
|
|||
headers=headers,
|
||||
).json()
|
||||
|
||||
oauth_token = oauth_response.get("token")
|
||||
oauth_token = oauth_response.get("token", "")
|
||||
headers = {
|
||||
"Authorization": "Bearer " + oauth_token,
|
||||
"Authorization": "Bearer " + str(oauth_token),
|
||||
"Accept": "application/vnd.github+json",
|
||||
}
|
||||
response = requests.get(
|
||||
|
|
@ -67,8 +68,63 @@ def delete_github_installation(installation_id):
|
|||
|
||||
url = f"https://api.github.com/app/installations/{installation_id}"
|
||||
headers = {
|
||||
"Authorization": "Bearer " + token,
|
||||
"Authorization": "Bearer " + str(token),
|
||||
"Accept": "application/vnd.github+json",
|
||||
}
|
||||
response = requests.delete(url, headers=headers)
|
||||
return response
|
||||
|
||||
|
||||
def get_github_repo_details(access_tokens_url, owner, repo):
|
||||
token = get_jwt_token()
|
||||
|
||||
headers = {
|
||||
"Authorization": "Bearer " + str(token),
|
||||
"Accept": "application/vnd.github+json",
|
||||
"X-GitHub-Api-Version": "2022-11-28",
|
||||
}
|
||||
|
||||
oauth_response = requests.post(
|
||||
access_tokens_url,
|
||||
headers=headers,
|
||||
).json()
|
||||
|
||||
oauth_token = oauth_response.get("token")
|
||||
headers = {
|
||||
"Authorization": "Bearer " + oauth_token,
|
||||
"Accept": "application/vnd.github+json",
|
||||
}
|
||||
open_issues = requests.get(
|
||||
f"https://api.github.com/repos/{owner}/{repo}",
|
||||
headers=headers,
|
||||
).json()["open_issues_count"]
|
||||
|
||||
total_labels = 0
|
||||
|
||||
labels_response = requests.get(
|
||||
f"https://api.github.com/repos/{owner}/{repo}/labels?per_page=100&page=1",
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
# Check if there are more pages
|
||||
if len(labels_response.links.keys()):
|
||||
# get the query parameter of last
|
||||
last_url = labels_response.links.get("last").get("url")
|
||||
parsed_url = urlparse(last_url)
|
||||
last_page_value = parse_qs(parsed_url.query)["page"][0]
|
||||
total_labels = total_labels + 100 * (last_page_value - 1)
|
||||
|
||||
# Get labels in last page
|
||||
last_page_labels = requests.get(last_url, headers=headers).json()
|
||||
total_labels = total_labels + len(last_page_labels)
|
||||
else:
|
||||
total_labels = len(labels_response.json())
|
||||
|
||||
# Currently only supporting upto 100 collaborators
|
||||
# TODO: Update this function to fetch all collaborators
|
||||
collaborators = requests.get(
|
||||
f"https://api.github.com/repos/{owner}/{repo}/collaborators?per_page=100&page=1",
|
||||
headers=headers,
|
||||
).json()
|
||||
|
||||
return open_issues, total_labels, collaborators
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue