### Get GitHub CLI Help Source: https://docs.github.com/en/github-cli/github-cli/quickstart Provides help information for GitHub CLI commands. Appending `--help` to any command displays its usage details. ```bash gh --help gh issue --help gh issue create --help ``` -------------------------------- ### Start a New Repository and Publish to GitHub with Git Source: https://docs.github.com/en/get-started/using-git/about-git This example guides you through initializing a new local Git repository, adding files, committing them, connecting to a remote GitHub repository, and pushing your initial commit. Ensure you create an empty repository on GitHub first. ```bash # create a new directory, and initialize it with git-specific functions git init my-repo # change into the `my-repo` directory cd my-repo # create the first file in the project touch README.md # git isn't aware of the file, stage it git add README.md # take a snapshot of the staging area git commit -m "add README to initial commit" # provide the path for the repository you created on github git remote add origin https://github.com/YOUR-USERNAME/YOUR-REPOSITORY-NAME.git # push changes to github git push --set-upstream origin main ``` -------------------------------- ### Install Go dependencies using go get (YAML) Source: https://docs.github.com/en/actions/tutorials/build-and-test-code/go This workflow snippet illustrates how to install project dependencies using the `go get` command. It first checks out the code and sets up a specific Go version ('1.21.x') using the `actions/setup-go@v5` action. Then, it uses a multi-line `run` command to execute `go get .` to install current dependencies, followed by `go get example.com/octo-examplemodule` and `go get example.com/octo-examplemodule@v1.3.4` to install specific modules and a particular version of a module. ```yaml steps: - uses: actions/checkout@v5 - name: Setup Go uses: actions/setup-go@v5 with: go-version: '1.21.x' - name: Install dependencies run: | go get . go get example.com/octo-examplemodule go get example.com/octo-examplemodule@v1.3.4 ``` -------------------------------- ### Install Git LFS using Windows setup wizard Source: https://docs.github.com/en/repositories/working-with-files/managing-large-files/installing-git-large-file-storage_platform=linux Installs Git LFS on Windows by running the downloaded executable file. This launches a setup wizard that guides the user through the installation process. ```powershell .git-lfs-windows-1.X.X.exe ``` -------------------------------- ### GET /octocat Source: https://docs.github.com/en/rest/quickstart This example makes a request to the "Get Octocat" endpoint, which uses the method GET and the path /octocat. ```APIDOC ## GET /octocat ### Description Retrieves information about the "Octocat" endpoint. ### Method GET ### Endpoint /octocat ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ``` gh api /octocat --method GET ``` ### Response #### Success Response (200) - **(type)** - Description #### Response Example ```json { "example": "response body" } ``` ``` -------------------------------- ### Analyze with Downloaded CodeQL Packs Source: https://docs.github.com/en/code-security/codeql-cli/codeql-cli-reference/about-codeql-packs This example demonstrates downloading and analyzing with CodeQL packs using the `codeql database analyze --download` command. It shows how to specify packs, versions, and individual queries, and includes output indicating successful downloads and query execution. ```bash $ echo $OCTO-ORG_ACCESS_TOKEN | codeql database analyze --download /codeql-dbs/example-repo \ octo-org/security-queries \ octo-org/optional-security-queries@~1.0.1:queries/csrf.ql \ --format=sarif-latest --output=/temp/example-repo-js.sarif > Download location: /Users/mona/.codeql/packages > Installed fresh octo-org/security-queries@1.0.0 > Installed fresh octo-org/optional-security-queries@1.0.2 > Running queries. > Compiling query plan for /Users/mona/.codeql/packages/octo-org/security-queries/1.0.0/potential-sql-injection.ql. > [1/2] Found in cache: /Users/mona/.codeql/packages/octo-org/security-queries/1.0.0/potential-sql-injection.ql. > Starting evaluation of octo-org/security-queries/query1.ql. > Compiling query plan for /Users/mona/.codeql/packages/octo-org/optional-security-queries/1.0.2/queries/csrf.ql. > [2/2] Found in cache: /Users/mona/.codeql/packages/octo-org/optional-security-queries/1.0.2/queries/csrf.ql. > Starting evaluation of octo-org/optional-security-queries/queries/csrf.ql. > [2/2 eval 694ms] Evaluation done; writing results to octo-org/security-queries/query1.bqrs. > Shutting down query evaluator. > Interpreting results. ``` -------------------------------- ### Create Repository using GitHub CLI Source: https://docs.github.com/en/repositories/creating-and-managing-repositories/quickstart-for-repositories_tool=webui This snippet demonstrates how to create a new repository on GitHub using the GitHub CLI. It covers interactive prompts for setup and an alternative method using flags for repository name and visibility. The `--clone` flag can be used to clone the repository locally. ```bash gh repo create --public gh repo create --private gh repo create --internal gh repo create --public --clone gh repo create / ``` -------------------------------- ### Get Installation by ID - JavaScript Request Example Source: https://docs.github.com/en/rest/apps/apps This JavaScript snippet shows how to make a request to get installation details using the installation ID. It utilizes the Fetch API to send a GET request with the necessary headers. Ensure you replace '' with your JWT. ```javascript async function getInstallation(installationId) { const response = await fetch(`https://api.github.com/app/installations/${installationId}`, { method: 'GET', headers: { 'Accept': 'application/vnd.github+json', 'Authorization': `Bearer `, // Replace with your JWT 'X-GitHub-Api-Version': '2022-11-28' } }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); return data; } // Example usage: // getInstallation(1).then(installation => console.log(installation)); ``` -------------------------------- ### Create GitHub Repository using GitHub CLI Source: https://docs.github.com/en/repositories/creating-and-managing-repositories/quickstart-for-repositories This snippet demonstrates how to create a new GitHub repository using the GitHub CLI. It covers creating a repository from scratch, specifying organization ownership, and setting visibility. It also includes an option to clone the repository locally. ```shell gh repo create project-name --public gh repo create organization-name/project-name --private --clone ``` -------------------------------- ### GET /octocat Source: https://docs.github.com/en/rest/quickstart_apiversion=2022-11-28&tool=cli This endpoint retrieves information about 'Octocat'. The example demonstrates using GitHub CLI to make a GET request to this endpoint. ```APIDOC ## GET /octocat ### Description Retrieves information about 'Octocat'. ### Method GET ### Endpoint /octocat ### Parameters ### Request Example ```shell gh api /octocat --method GET ``` ### Response #### Success Response (200) - **field** (type) - Description #### Response Example ```json { "example": "response body" } ``` ``` -------------------------------- ### Get Organization Installation (JavaScript) Source: https://docs.github.com/en/rest/apps/apps Retrieves installation information for a GitHub App within a specific organization using JavaScript. This example demonstrates making a GET request to the GitHub API, including necessary headers and the API endpoint. ```javascript async function getOrgInstallation(octokit, org) { return octokit.request('GET /orgs/{org}/installation', { org: org }); } ``` -------------------------------- ### Set up Node.js Environment with actions/setup-node Source: https://docs.github.com/en/actions/tutorials/creating-an-example-workflow This step utilizes the `actions/setup-node` action to install a specified version of Node.js (e.g., version 20). This action ensures that `node` and `npm` commands are available in the system's PATH for subsequent steps. ```yaml - uses: actions/setup-node@v4 with: node-version: '20' ``` -------------------------------- ### Clone Repository and Initialize Project Source: https://docs.github.com/en/packages/quickstart This snippet demonstrates how to clone a GitHub repository and initialize a new Node.js project using npm. It includes commands for cloning, navigating into the directory, and initializing the npm package. ```bash git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY.git cd YOUR-REPOSITORY ``` -------------------------------- ### Get Installation by ID - GitHub CLI Example Source: https://docs.github.com/en/rest/apps/apps This GitHub CLI command retrieves installation information for a given installation ID. It automatically handles authentication and header management. Replace '1' with the desired installation ID. ```bash gh api /app/installations/1 -H "X-GitHub-Api-Version: 2022-11-28" ``` -------------------------------- ### Authenticated API Request with Curl (Shell) Source: https://docs.github.com/en/rest/using-the-rest-api/getting-started-with-the-rest-api_tool=curl This example demonstrates how to make an authenticated request to the GitHub REST API using `curl`. It requires `curl` to be installed and an authentication token to be set in the `Authorization` header. Specific endpoints and parameters would need to be configured based on the desired API interaction. ```shell # Example placeholder for a curl request # Replace with actual endpoint, method, and parameters curl -X GET \ -H "Authorization: Bearer YOUR_TOKEN" \ -H "Accept: application/vnd.github.v3+json" \ "https://api.github.com/user/repos" ``` -------------------------------- ### Authenticated Request with Curl (Shell) Source: https://docs.github.com/en/rest/using-the-rest-api/getting-started-with-the-rest-api_apiversion=2022-11-28&apiversion=2022-11-28&apiversion=2022-11-28&tool=javascript This example outlines the process of making an authenticated request to the GitHub REST API using `curl`. It assumes `curl` is installed and details the steps for choosing an endpoint, identifying HTTP methods and paths, and constructing authentication credentials, typically an access token sent in the `Authorization` header. ```shell # Example placeholder for curl command structure # curl -X POST \ # -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ # -H "Accept: application/vnd.github+json" \ # -d '{"title":"My Issue Title","body":"My issue body"}' ``` -------------------------------- ### Checkout Repository and Setup Node.js Source: https://docs.github.com/en/actions/tutorials/creating-an-example-workflow Includes steps to checkout the repository code using 'actions/checkout@v5' and set up Node.js version 20 using 'actions/setup-node@v4'. These are prerequisites for using npm. ```yaml - uses: actions/checkout@v5 - uses: actions/setup-node@v4 with: node-version: '20' ``` -------------------------------- ### Get Installation by ID - cURL Request Example Source: https://docs.github.com/en/rest/apps/apps This cURL command demonstrates how to fetch installation information using the installation ID. It requires setting the 'Accept', 'Authorization', and 'X-GitHub-Api-Version' headers. Replace '' with your actual JWT. ```shell curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer " \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/app/installations/1 ``` -------------------------------- ### Initialize CodeQL Action with Custom Configuration and Queries Source: https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning This example demonstrates initializing the CodeQL action with a custom configuration file and additional queries. The '+' prefix for `queries` and `packs` merges them with settings from the configuration file. It specifies a `config-file` and lists extra queries and packs. ```yaml - uses: github/codeql-action/init@v3 with: config-file: ./.github/codeql/codeql-config.yml queries: +security-and-quality,octo-org/python-qlpack/show_ifs.ql@main packs: +scope/pack1,scope/pack2@1.2.3,scope/pack3@4.5.6:path/to/queries ``` ```yaml - uses: github/codeql-action/init@v3 with: config-file: ./.github/codeql/codeql-config.yml queries: +security-and-quality,octo-org/python-qlpack/show_ifs.ql@main packs: +scope/pack1,scope/pack2@1.2.3,scope/pack3@4.5.6:path/to/queries ``` -------------------------------- ### Example User-Agent Header for GitHub REST API Source: https://docs.github.com/en/rest/using-the-rest-api/getting-started-with-the-rest-api_apiversion=2022-11-28&apiversion=2022-11-28&apiversion=2022-11-28&tool=javascript This snippet shows an example of a User-Agent header for an application interacting with the GitHub REST API. It's crucial for identifying your application and ensuring requests are not rejected. ```text User-Agent: Awesome-Octocat-App ``` -------------------------------- ### Create a GitHub Repository using GitHub CLI Source: https://docs.github.com/en/repositories/creating-and-managing-repositories/quickstart-for-repositories_tool=cli This snippet demonstrates how to create a new GitHub repository from the command line using the `gh repo create` command. It supports creating repositories from scratch, specifying visibility, and optionally cloning the repository locally. Ensure you have GitHub CLI installed and authenticated. ```bash gh repo create project-name --public --clone ``` -------------------------------- ### List Installations for Authenticated App (JavaScript) Source: https://docs.github.com/en/rest/apps/apps Example of how to list installations for an authenticated GitHub App using JavaScript. This snippet demonstrates making a GET request to the GitHub API with necessary headers. ```javascript async function listInstallations() { const token = ""; const response = await fetch("https://api.github.com/app/installations", { headers: { "Accept": "application/vnd.github+json", "Authorization": `Bearer ${token}`, "X-GitHub-Api-Version": "2022-11-28" } }); const data = await response.json(); console.log(data); } listInstallations(); ``` -------------------------------- ### Install Dependencies with npm Source: https://docs.github.com/en/apps/creating-github-apps/writing-code-for-a-github-app/quickstart This command installs the necessary Node.js dependencies for the GitHub App. Ensure you have Node.js version 12 or greater installed. This step is crucial for the application to function correctly. ```bash npm install ``` -------------------------------- ### GET /repos/{owner}/{repo}/issues Source: https://docs.github.com/en/rest/quickstart This example workflow uses the List repository issues endpoint, and requests a list of issues in the octocat/Spoon-Knife repository. ```APIDOC ## GET /repos/{owner}/{repo}/issues ### Description Lists issues in a specified repository. ### Method GET ### Endpoint /repos/{owner}/{repo}/issues ### Parameters #### Path Parameters - **owner** (string) - Required - The owner of the repository. - **repo** (string) - Required - The name of the repository. #### Query Parameters None #### Request Body None ### Request Example ```yaml on: workflow_dispatch: jobs: use_api: runs-on: ubuntu-latest permissions: issues: read steps: - env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | gh api https://api.github.com/repos/octocat/Spoon-Knife/issues ``` ### Response #### Success Response (200) - **(type)** - Description #### Response Example ```json { "example": "response body" } ``` ``` -------------------------------- ### Create CodeQL Databases for Multiple Languages (Python, C/C++) Source: https://docs.github.com/en/code-security/codeql-cli/getting-started-with-the-codeql-cli/preparing-your-code-for-codeql-analysis This example demonstrates creating CodeQL databases for multiple languages (Python and C/C++) simultaneously using the `--db-cluster` and `--language` options. It specifies the build command with `--command` and uses `--no-run-unnecessary-builds` to optimize the process by skipping unnecessary build commands for certain languages. The `--source-root` option points to the codebase directory. ```shell codeql database create /codeql-dbs/example-repo-multi \ --db-cluster --language python,c-cpp \ --command make --no-run-unnecessary-builds \ --source-root /checkouts/example-repo-multi ``` -------------------------------- ### View GitHub Repository Source: https://docs.github.com/en/github-cli/github-cli/quickstart Shows the description and README.md of a specified GitHub repository. It can also open the repository in a web browser. ```bash gh repo view OWNER/REPO gh repo view OWNER/REPO --web ``` -------------------------------- ### Get Repository README - JavaScript Source: https://docs.github.com/en/rest/repos/contents_apiversion=2022-11-28 This JavaScript example shows how to retrieve a repository's README file using the GitHub API. It requires setting the appropriate headers and making a GET request to the API endpoint. Ensure you have the necessary libraries (e.g., node-fetch or Axios) installed. ```javascript async function getRepoReadme(owner, repo, token) { const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/readme`, { headers: { 'Accept': 'application/vnd.github+json', 'Authorization': `Bearer ${token}`, 'X-GitHub-Api-Version': '2022-11-28' } }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); return data; } ``` -------------------------------- ### GET /repos/{owner}/{repo}/issues Source: https://docs.github.com/en/rest/using-the-rest-api/getting-started-with-the-rest-api_apiversion=2022-11-28&apiversion=2022-11-28&apiversion=2022-11-28&tool=javascript Retrieves a list of issues for a specified repository. The response includes status codes and custom GitHub headers like rate limit information. Examples are provided for both `gh api` CLI and Octokit.js. ```APIDOC ## GET /repos/{owner}/{repo}/issues ### Description Fetches a list of issues for a given repository. This endpoint returns standard HTTP status codes and custom GitHub headers, such as rate limiting information. ### Method GET ### Endpoint `/repos/{owner}/{repo}/issues` ### Parameters #### Path Parameters - **owner** (string) - Required - The owner of the repository. - **repo** (string) - Required - The name of the repository. #### Query Parameters - **per_page** (integer) - Optional - The number of results per page. ### Request Example (CLI) ```bash curl --request GET \ --url "https://api.github.com/repos/octocat/Spoon-Knife/issues?per_page=2" \ --header "Accept: application/vnd.github+json" \ --header "Authorization: Bearer YOUR-TOKEN" \ --include ``` ### Request Example (Octokit.js) ```javascript try { const result = await octokit.request("GET /repos/{owner}/{repo}/issues", { owner: "REPO-OWNER", repo: "REPO-NAME", per_page: 2, }); console.log(`Success! Status: ${result.status}. Rate limit remaining: ${result.headers["x-ratelimit-remaining"]}`) } catch (error) { console.log(`Error! Status: ${error.status}. Rate limit remaining: ${error.headers["x-ratelimit-remaining"]}. Message: ${error.response.data.message}`) } ``` ### Response #### Success Response (200) - **status** (integer) - The HTTP status code of the response. - **headers** (object) - An object containing the response headers, including custom GitHub headers like `x-ratelimit-remaining`. #### Response Example (Headers) ``` HTTP/2.0 200 OK Access-Control-Allow-Origin: * Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset Cache-Control: private, max-age=60, s-maxage=60 Content-Security-Policy: default-src 'none' Content-Type: application/json; charset=utf-8 Date: Thu, 04 Aug 2022 19:56:41 GMT Etag: W/"a63dfbcfdb73621e9d2e89551edcf9856731ced534bd7f1e114a5da1f5f73418" Link: ; rel="next", ; rel="last" Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin Server: GitHub.com Strict-Transport-Security: max-age=31536000; includeSubdomains; preload Vary: Accept, Authorization, Cookie, Accept-Encoding, Accept, X-Requested-With X-Accepted-Oauth-Scopes: repo X-Content-Type-Options: nosniff X-Frame-Options: deny X-Github-Api-Version-Selected: 2022-08-09 X-Github-Media-Type: github.v3; format=json X-Github-Request-Id: 1C73:26D4:E2E500:1EF78F4:62EC2479 X-Oauth-Client-Id: 178c6fc778ccc68e1d6a X-Oauth-Scopes: gist, read:org, repo, workflow X-Ratelimit-Limit: 15000 X-Ratelimit-Remaining: 14996 X-Ratelimit-Reset: 1659645499 X-Ratelimit-Resource: core X-Ratelimit-Used: 4 X-Xss-Protection: 0 ``` #### Error Response Examples - **status** (integer) - The HTTP status code indicating an error. - **headers** (object) - The response headers, which may include rate limit information. - **response.data.message** (string) - A message describing the error. ```