0

Create a commit

Published Oct 25, 2023

Creates a new Git [commit object](https://git-scm.

Script github
  • Submitted by hugo697 Typescript (fetch-only)
    Created 575 days ago
    1
    type Github = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Create a commit
    6
     * Creates a new Git [commit object](https://git-scm.
    7
     */
    8
    export async function main(
    9
      auth: Github,
    10
      owner: string,
    11
      repo: string,
    12
      body: {
    13
        author?: {
    14
          date?: string;
    15
          email: string;
    16
          name: string;
    17
          [k: string]: unknown;
    18
        };
    19
        committer?: {
    20
          date?: string;
    21
          email?: string;
    22
          name?: string;
    23
          [k: string]: unknown;
    24
        };
    25
        message: string;
    26
        parents?: string[];
    27
        signature?: string;
    28
        tree: string;
    29
        [k: string]: unknown;
    30
      }
    31
    ) {
    32
      const url = new URL(
    33
        `https://api.github.com/repos/${owner}/${repo}/git/commits`
    34
      );
    35
    
    
    36
      const response = await fetch(url, {
    37
        method: "POST",
    38
        headers: {
    39
          "Content-Type": "application/json",
    40
          Authorization: "Bearer " + auth.token,
    41
        },
    42
        body: JSON.stringify(body),
    43
      });
    44
      if (!response.ok) {
    45
        const text = await response.text();
    46
        throw new Error(`${response.status} ${text}`);
    47
      }
    48
      return await response.json();
    49
    }
    50