← Back to Cookbook

OAuth GitHub Integration

Demonstrates OAuth auth blocks with GitHub client credentials.

http

Source

/**
 * Demonstrates OAuth auth blocks. Fetches GitHub repo stats
 * using client credentials.
 */

secret github_creds {
  label: "GitHub OAuth"
  vars: [GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET, GITHUB_API_URL]
}

auth github_oauth {
  label: "GitHub API"
  type: oauth
  secrets: github_creds
  grant_type: "client_credentials"
  client_id: GITHUB_CLIENT_ID
  client_secret: GITHUB_CLIENT_SECRET
  token_url: "https://github.com/login/oauth/access_token"
}

schedule daily_stats {
  label: "Daily Repo Stats"
  cron: "0 9 * * *"
}

graph fetch_repo_stats {
  label: "Fetch Repo Stats"

  root {
    type: code
    label: "Configure"
    code: @ts {
      return { repo: "owner/repo" }
    }
    outputSchema: @json {
      {
        "type": "object",
        "properties": {
          "repo": { "type": "string" }
        }
      }
    }
  }

  node fetch_repo {
    type: http
    label: "Fetch repo info"
    url: @ts {
      const base = context.secrets.github_creds.GITHUB_API_URL || "https://api.github.com"
      return base + "/repos/" + context.nodes.root.output.repo
    }
    auth: github_oauth
    secrets: {
      github_creds: [GITHUB_API_URL]
    }
  }

  node extract {
    type: code
    label: "Extract stats"
    code: @ts {
      const repo = context.nodes.fetch_repo.output
      return {
        name: repo.full_name || context.nodes.root.output.repo,
        stars: repo.stargazers_count || 0,
        forks: repo.forks_count || 0,
        open_issues: repo.open_issues_count || 0,
        watchers: repo.watchers_count || 0
      }
    }
  }

  flow {
    root -> fetch_repo
    fetch_repo -> extract
  }
}

trigger on_stats {
  schedule:daily_stats -> fetch_repo_stats
  enabled: true
}

Flow

Trigger → graph

Graph nodes