Prefect GitHub

0.4.2 · active · verified Mon Apr 13

Prefect-github provides Prefect integrations for interacting with GitHub. It includes a `GitHubCredentials` block for secure authentication using Personal Access Tokens (PATs) and various utilities to interact with GitHub repositories, such as cloning, querying repository details, and performing mutations (e.g., adding stars, creating issues). The tasks within this collection were initially generated using the GitHub GraphQL schema. While the standalone `prefect-github` package is currently at version 0.4.2, active development for this integration has been merged into the main PrefectHQ/prefect repository, which follows its own release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with GitHub using `GitHubCredentials` and perform a GraphQL query to get repository details, followed by attempting to add a star to a repository. Ensure you have a GitHub Personal Access Token (PAT) with appropriate scopes (e.g., `public_repo` or `repo`) set as an environment variable named `GITHUB_PAT`.

import os
from prefect import flow
from prefect_github import GitHubCredentials
from prefect_github.repository import query_repository
from prefect_github.mutations import add_star_starrable

@flow(log_prints=True)
def github_interaction_flow():
    # It's recommended to store tokens securely, e.g., in Prefect Blocks
    # and load them by name. For a quickstart, we use an env var.
    github_token = os.environ.get('GITHUB_PAT', 'YOUR_GITHUB_TOKEN')
    if github_token == 'YOUR_GITHUB_TOKEN' or not github_token:
        print("Please set the GITHUB_PAT environment variable or replace 'YOUR_GITHUB_TOKEN' with a valid GitHub Personal Access Token.")
        return

    # Create a GitHubCredentials block (or load an existing one)
    github_credentials = GitHubCredentials(token=github_token)
    # Optionally save the block for re-use in the Prefect UI or other flows:
    # github_credentials.save(name="my-github-pat", overwrite=True)

    print(f"Querying repository details for PrefectHQ/prefect...")
    repository_info = query_repository(
        owner="PrefectHQ",
        name="prefect",
        github_credentials=github_credentials,
        return_fields="id name url", # Request specific fields
    )
    print(f"Repository Name: {repository_info.get('name')}")
    print(f"Repository URL: {repository_info.get('url')}")
    repository_id = repository_info.get("id")

    if repository_id:
        print(f"Attempting to add a star to {repository_info.get('name')} (ID: {repository_id})...")
        # Note: Starring requires a PAT with 'public_repo' or 'repo' scope
        # This action might fail if the token doesn't have sufficient permissions
        # or if the repository is already starred by the user associated with the token.
        try:
            starrable_status = add_star_starrable(
                starrable_id=repository_id,
                github_credentials=github_credentials,
            )
            print(f"Star operation successful: {starrable_status.get('starrable', {}).get('viewerHasStarred')}")
        except Exception as e:
            print(f"Failed to add star: {e}")
    else:
        print("Could not retrieve repository ID.")

if __name__ == "__main__":
    github_interaction_flow()

view raw JSON →