def do_import_github(project_id, github_user, github_project, github_branch, delete_project=False): print("Running do_import_github:", project_id, github_user, github_project, github_branch, delete_project) try: url = "https://github.com/%s/%s/archive/%s.zip" % (github_user, github_project, github_branch) if file_exists(url): u = urllib2.urlopen(url) print("Forwarding to do_import_archive") return do_import_archive(project_id, u.read()) else: raise Exception("The branch '%s' does not exist." % github_branch) except Exception as e: try: project = Project.objects.get(pk=project_id) user = project.owner except: project = None user = None if delete_project and project is not None: try: project.delete() except: pass send_td_event('cloudpebble_github_import_failed', data={ 'data': { 'reason': e.message, 'github_user': github_user, 'github_project': github_project, 'github_branch': github_branch } }, user=user) raise
def do_import_github(project_id, github_user, github_project, github_branch, delete_project=False): try: url = "https://github.com/%s/%s/archive/%s.zip" % (github_user, github_project, github_branch) if file_exists(url): u = urllib2.urlopen(url) return do_import_archive(project_id, u.read()) else: raise Exception("The branch '%s' does not exist." % github_branch) except Exception as e: try: project = Project.objects.get(pk=project_id) user = project.owner except: project = None user = None if delete_project and project is not None: try: project.delete() except: pass send_keen_event('cloudpebble', 'cloudpebble_github_import_failed', user=user, data={ 'data': { 'reason': e.message, 'github_user': github_user, 'github_project': github_project, 'github_branch': github_branch } }) raise
def do_import_github(project_id, github_user, github_project, github_branch, github_token, delete_project=False): try: url = "https://github.com/%s/%s/archive/%s.zip" % (github_user, github_project, github_branch) logger.info("URL: %s", url) r = urllib2.Request(url) if github_token is not None: r.add_header('Authorization', 'token ' + github_token) u = urllib2.urlopen(r) return do_import_archive(project_id, u.read()) except Exception as e: try: project = Project.objects.get(pk=project_id) user = project.owner except: project = None user = None if delete_project and project is not None: try: project.delete() except: pass send_td_event('cloudpebble_github_import_failed', data={ 'data': { 'reason': e.message, 'github_user': github_user, 'github_project': github_project, 'github_branch': github_branch, 'github_token': github_token } }, user=user) raise
def do_import_github(project_id, github_user, github_project, github_branch, delete_project=False): try: url = "https://github.com/%s/%s/archive/%s.zip" % (github_user, github_project, github_branch) if file_exists(url): u = urllib2.urlopen(url) return do_import_archive(project_id, u.read()) else: raise Exception("The branch '%s' does not exist." % github_branch) except Exception as e: try: project = Project.objects.get(pk=project_id) user = project.owner except: project = None user = None if delete_project and project is not None: try: project.delete() except: pass send_td_event( "cloudpebble_github_import_failed", data={ "data": { "reason": e.message, "github_user": github_user, "github_project": github_project, "github_branch": github_branch, } }, user=user, ) raise
def github_pull(user, project): g = get_github(user) repo_name = project.github_repo if repo_name is None: raise Exception("No GitHub repo defined.") repo = g.get_repo(repo_name) # If somehow we don't have a branch set, this will use the "master_branch" branch_name = project.github_branch or repo.master_branch try: branch = repo.get_branch(branch_name) except GithubException: raise Exception("Unable to get the branch.") if project.github_last_commit == branch.commit.sha: # Nothing to do. return False commit = repo.get_git_commit(branch.commit.sha) tree = repo.get_git_tree(commit.tree.sha, recursive=True) paths = {x.path: x for x in tree.tree} paths_notags = {get_root_path(x) for x in paths} root = find_project_root(paths) # First try finding the resource map so we don't fail out part-done later. # TODO: transaction support for file contents would be nice... resource_root = root + 'resources/' manifest_path = root + 'appinfo.json' if manifest_path in paths: manifest_sha = paths[manifest_path].sha manifest = json.loads(git_blob(repo, manifest_sha)) media = manifest.get('resources', {}).get('media', []) else: raise Exception("appinfo.json not found") project_type = manifest.get('projectType', 'native') for resource in media: path = resource_root + resource['file'] if project_type == 'pebblejs' and resource['name'] in { 'MONO_FONT_14', 'IMAGE_MENU_ICON', 'IMAGE_LOGO_SPLASH', 'IMAGE_TILE_SPLASH'}: continue if path not in paths_notags: raise Exception("Resource %s not found in repo." % path) # Now we grab the zip. zip_url = repo.get_archive_link('zipball', branch_name) u = urllib2.urlopen(zip_url) # And wipe the project! project.source_files.all().delete() project.resources.all().delete() # This must happen before do_import_archive or we'll stamp on its results. project.github_last_commit = branch.commit.sha project.github_last_sync = now() project.save() import_result = do_import_archive(project.id, u.read()) send_keen_event('cloudpebble', 'cloudpebble_github_pull', user=user, data={ 'data': { 'repo': project.github_repo } }) return import_result
def github_pull(user, project): g = get_github(user) repo_name = project.github_repo if repo_name is None: raise Exception("No GitHub repo defined.") repo = g.get_repo(repo_name) # If somehow we don't have a branch set, this will use the "master_branch" branch_name = project.github_branch or repo.master_branch try: branch = repo.get_branch(branch_name) except GithubException: raise Exception("Unable to get the branch.") if project.github_last_commit == branch.commit.sha: # Nothing to do. return False commit = repo.get_git_commit(branch.commit.sha) tree = repo.get_git_tree(commit.tree.sha, recursive=True) paths = {x.path: x for x in tree.tree} root = find_project_root(paths) # First try finding the resource map so we don't fail out part-done later. # TODO: transaction support for file contents would be nice... resource_root = root + 'resources/' manifest_path = root + 'appinfo.json' if manifest_path in paths: manifest_sha = paths[manifest_path].sha manifest = json.loads(git_blob(repo, manifest_sha)) media = manifest.get('resources', {}).get('media', []) else: raise Exception("appinfo.json not found") project_type = manifest.get('projectType', 'native') for resource in media: path = resource_root + resource['file'] if project_type == 'pebblejs' and resource['name'] in { 'MONO_FONT_14', 'IMAGE_MENU_ICON', 'IMAGE_LOGO_SPLASH', 'IMAGE_TILE_SPLASH' }: continue if path not in paths: raise Exception("Resource %s not found in repo." % path) # Now we grab the zip. zip_url = repo.get_archive_link('zipball', branch_name) u = urllib2.urlopen(zip_url) # And wipe the project! project.source_files.all().delete() project.resources.all().delete() # This must happen before do_import_archive or we'll stamp on its results. project.github_last_commit = branch.commit.sha project.github_last_sync = now() project.save() import_result = do_import_archive(project.id, u.read()) send_keen_event('cloudpebble', 'cloudpebble_github_pull', user=user, data={'data': { 'repo': project.github_repo }}) return import_result
def github_pull(user, project): g = get_github(user) repo_name = project.github_repo if repo_name is None: raise Exception("No GitHub repo defined.") repo = g.get_repo(repo_name) # If somehow we don't have a branch set, this will use the "master_branch" branch_name = project.github_branch or repo.master_branch try: branch = repo.get_branch(branch_name) except GithubException: raise Exception("Unable to get the branch.") if project.github_last_commit == branch.commit.sha: # Nothing to do. return False commit = repo.get_git_commit(branch.commit.sha) tree = repo.get_git_tree(commit.tree.sha, recursive=True) paths = {x.path: x for x in tree.tree} paths_notags = {get_root_path(x) for x in paths} # First try finding the resource map so we don't fail out part-done later. try: root, manifest_item = find_project_root_and_manifest([GitProjectItem(repo, x) for x in tree.tree]) except ValueError as e: raise ValueError("In manifest file: %s" % str(e)) resource_root = root + project.resources_path + "/" manifest = json.loads(manifest_item.read()) media = manifest.get("resources", {}).get("media", []) project_type = manifest.get("projectType", "native") for resource in media: path = resource_root + resource["file"] if project_type == "pebblejs" and resource["name"] in { "MONO_FONT_14", "IMAGE_MENU_ICON", "IMAGE_LOGO_SPLASH", "IMAGE_TILE_SPLASH", }: continue if path not in paths_notags: raise Exception("Resource %s not found in repo." % path) # Now we grab the zip. zip_url = repo.get_archive_link("zipball", branch_name) u = urllib2.urlopen(zip_url) # And wipe the project! # TODO: transaction support for file contents would be nice... project.source_files.all().delete() project.resources.all().delete() # This must happen before do_import_archive or we'll stamp on its results. project.github_last_commit = branch.commit.sha project.github_last_sync = now() project.save() import_result = do_import_archive(project.id, u.read()) send_td_event("cloudpebble_github_pull", data={"data": {"repo": project.github_repo}}, user=user) return import_result