octokit.js

简介: octokit.js


暴食杀人多于利剑杀人。——伯里兹

分享一个框架封装了GitHub的API调用

GitHub - octokit/octokit.js: The all-batteries-included GitHub SDK for Browsers, Node.js, and Deno.

等于是一个SDK

目前,GitHub 维护以下语言/框架/平台的 SDK:

还有 2 个 SDK,它们是根据 GitHub 的 OpenAPI 描述生成的!

浏览器里安装:

<script type="module">
import { Octokit, App } from "https://esm.sh/octokit";
</script>

示例:获取经过身份验证的用户的用户名。

// Create a personal access token at https://github.com/settings/tokens/new?scopes=repo
const octokit = new Octokit({ auth: `personal-access-token123` });
// Compare: https://docs.github.com/en/rest/reference/users#get-the-authenticated-user
const {
  data: { login },
} = await octokit.rest.users.getAuthenticated();
console.log("Hello, %s", login);

这里支持两种方式,就拿创建issues举例

await octokit.rest.issues.create({
  owner: "octocat",
  repo: "hello-world",
  title: "Hello, world!",
  body: "I created this issue using Octokit!",
});

和下面的方式相同

await octokit.request("POST /repos/{owner}/{repo}/issues", {
  owner: "octocat",
  repo: "hello-world",
  title: "Hello, world!",
  body: "I created this issue using Octokit!",
});

还有分页查询仓库里的issues

const iterator = octokit.paginate.iterator(octokit.rest.issues.listForRepo, {
  owner: "octocat",
  repo: "hello-world",
  per_page: 100,
});
// iterate through each response
for await (const { data: issues } of iterator) {
  for (const issue of issues) {
    console.log("Issue #%d: %s", issue.number, issue.title);
  }
}

还可以使用异步的方式去做

const issues = await octokit.paginate(octokit.rest.issues.listForRepo, {
  owner: "octocat",
  repo: "hello-world",
  per_page: 100,
});

设置媒体格式:

const { data } = await octokit.rest.repos.getContent({
  mediaType: {
    format: "raw",
  },
  owner: "octocat",
  repo: "hello-world",
  path: "package.json",
});
console.log("package name: %s", JSON.parse(data).name);

错误处理

import { RequestError } from "octokit";


try {
  // your code here that sends at least one Octokit request
  await octokit.request("GET /");
} catch (error) {
  // Octokit errors are instances of RequestError, so they always have an `error.status` property containing the HTTP response code.
  if (error instanceof RequestError) {
    // handle Octokit error
    // error.message; // Oops
    // error.status; // 500
    // error.request; // { method, url, headers, body }
    // error.response; // { url, status, headers, data }
  } else {
    // handle all other errors
    throw error;
  }
}

也有GraphQL的支持,例如获取经过身份验证的用户的登录信息

const {
  viewer: { login },
} = await octokit.graphql(`{
  viewer {
    login
  }
}`);

可以把变量放到第二个参数传进去

const { lastIssues } = await octokit.graphql(
  `
    query lastIssues($owner: String!, $repo: String!, $num: Int = 3) {
      repository(owner: $owner, name: $repo) {
        issues(last: $num) {
          edges {
            node {
              title
            }
          }
        }
      }
    }
  `,
  {
    owner: "octokit",
    repo: "graphql.js",
  },
);

关于GraphQL的分页查询仓库的issues

const { allIssues } = await octokit.graphql.paginate(
  `
    query allIssues($owner: String!, $repo: String!, $num: Int = 10, $cursor: String) {
      repository(owner: $owner, name: $repo) {
        issues(first: $num, after: $cursor) {
          edges {
            node {
              title
            }
          }
          pageInfo {
            hasNextPage
            endCursor
          }
        }
      }
    }
  `,
  {
    owner: "octokit",
    repo: "graphql.js",
  },
);

创建Label

await octokit.graphql(
  `mutation createLabel($repositoryId:ID!,name:String!,color:String!) {
  createLabel(input:{repositoryId:$repositoryId,name:$name}) {
    label: {
      id
    }
  }
}`,
  {
    repositoryId: 1,
    name: "important",
    color: "cc0000",
    mediaType: {
      previews: ["bane"],
    },
  },
);


相关文章
【STM32】引脚GPIO批量操作数组&for循环流水灯
【STM32】引脚GPIO批量操作数组&for循环流水灯
1705 0
|
监控 安全 JavaScript
浅谈移动端设备标识码:DeviceID、IMEI、IDFA、UDID和UUID
场景 : 客户提出一个问题就是把用户的登录记录和设备绑定到一起,就是每个人都是固定的设备(可能是安全因素吧)。一开始想的是回去设备的IMEI号和用户账号绑定起来,结果发现IMEI不对外开发,只能另寻他法,最后通过获取设备序列号作为唯一标识。
浅谈移动端设备标识码:DeviceID、IMEI、IDFA、UDID和UUID
快速生成软著申请时所需的60页代码文档的免费工具
本篇文章主要讲解,制作软著代码文档的高效方法,当然不可能手动一个个复制了,这显然太笨拙,他浪费时间了。这里我给大家介绍一个更快的方式。
13430 1
EndNote文献管理工具!研究生必备软件!超详细下载安装教程(附下载地址)
EndNote是一款全球广泛使用的专业文献管理工具,集文献整理、引用写作与协同管理于一体,助力科研高效进行。安装时需关闭杀毒软件,按步骤完成即可。
6614 6
|
人工智能 数据库 决策智能
Archon – 开源 AI 智能体框架,自主生成代码构建 AI 智能体
Archon 是一个开源的 AI 智能体框架,能够自主生成代码并优化智能体性能,支持多智能体协作、领域知识集成和文档爬取等功能,适用于企业、教育、智能家居等多个领域。
2032 10
Archon – 开源 AI 智能体框架,自主生成代码构建 AI 智能体
|
存储 机器学习/深度学习 人工智能
多模态RAG实战指南:完整Python代码实现AI同时理解图片、表格和文本
本文探讨了多模态RAG系统的最优实现方案,通过模态特定处理与后期融合技术,在性能、准确性和复杂度间达成平衡。系统包含文档分割、内容提取、HTML转换、语义分块及向量化存储五大模块,有效保留结构和关系信息。相比传统方法,该方案显著提升了复杂查询的检索精度(+23%),并支持灵活升级。文章还介绍了查询处理机制与优势对比,为构建高效多模态RAG系统提供了实践指导。
3148 0
多模态RAG实战指南:完整Python代码实现AI同时理解图片、表格和文本
|
域名解析 存储 人工智能
Manus的技术实现原理浅析与简单复刻
作者参考网络相关信息并加上个人理解,对Manus的技术实现原理进行深入分析,并做了一个简单版本的复刻,欢迎大家在评论区互相交流探讨~
Manus的技术实现原理浅析与简单复刻
|
移动开发 小程序 前端开发
uni-app组件样式修改不生效
uni-app组件样式修改不生效
|
机器学习/深度学习 Web App开发 数据挖掘
经典神经网络论文超详细解读(七)——SENet(注意力机制)学习笔记(翻译+精读+代码复现)
经典神经网络论文超详细解读(七)——SENet(注意力机制)学习笔记(翻译+精读+代码复现)
5838 1
经典神经网络论文超详细解读(七)——SENet(注意力机制)学习笔记(翻译+精读+代码复现)
|
SQL 开发框架 数据库连接
uniapp中sqlite数据库常用操作的简单封装
uniapp中sqlite数据库常用操作的简单封装
2218 0