Skip to content

Instantly share code, notes, and snippets.

@jitran
Last active March 26, 2024 06:44
Show Gist options
  • Save jitran/75a5c9fa131a52c21b98b9b9b268c6b3 to your computer and use it in GitHub Desktop.
Save jitran/75a5c9fa131a52c21b98b9b9b268c6b3 to your computer and use it in GitHub Desktop.
API: JavaScript List Repo Exercise

Write JavaScript to list the repositories in an organisation.

  1. Install dependencies
mkdir exercise
cd exercise
brew install node
npm install dotenv octokit
  1. Start VSCode and add the exercise directory to it.

  2. Create .env file with GITHUB_TOKEN and GITHUB_ORG.

GITHUB_TOKEN="<TOKEN>"
GITHUB_ORG="<ORG NAME>"
  1. Create repos.mjs file in VSCode.

  2. Add authentication to repos.mjs:

import { Octokit } from "octokit";
import dotenv from "dotenv";

dotenv.config();

const octokit = new Octokit({ 
    auth: process.env.GITHUB_TOKEN,
});

Ref https://docs.github.com/en/rest/guides/scripting-with-the-rest-api-and-javascript?apiVersion=2022-11-28#authenticating-in-github-actions

  1. Type octokit.rest. into the script, and see the available objects to use.

  2. Run the script:

node repos.mjs
  1. Enjoy coding!

Reference: https://docs.github.com/en/rest/guides/scripting-with-the-rest-api-and-javascript?apiVersion=2022-11-28

View Answer
import { Octokit } from "octokit";
import dotenv from "dotenv";

dotenv.config();

const octokit = new Octokit({ 
    auth: process.env.GITHUB_TOKEN,
});

const repos = await octokit.rest.repos.listForOrg({org: process.env.GITHUB_ORG});
repos.data.map(x => console.log(x.name));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment