GitHub——通过接口获取仓库下的最新版本

2024-08-15 14:34:24 浏览数 (1)

前言

主要通过调用GitHub官方提供的API来实现,因为我们主要是为了获取某一仓库下的最新版本,所以主要的操作接口又都在存储库下,直接到存储库下找对应的接口即可; REST API: https://docs.github.com/cn/rest repos: https://docs.github.com/cn/rest/repos/repos

内容

代码语言:javascript复制
# 接口
https://api.github.com/repos/<user-name>/<repos-name>/releases/latest

# 示例
https://api.github.com/repos/wangyang0210/cnblogs-theme/releases/latest

内容

代码语言:javascript复制
async function request(url = '', method = 'GET', data = {}) {
    let options = {
        method: method,
        mode: 'cors',
        redirect: 'follow',
        referrerPolicy: 'no-referrer',
    }
    if(Object.keys(data).length) {
        options.body=JSON.stringify(data)
    }
    const response = await fetch(url, options)
    return response.json()
}

request('https://api.github.com/repos/wangyang0210/cnblogs-theme/releases/latest').then(r => console.log(r))
代码语言:javascript复制
{
    "url": "https://api.github.com/repos/wangyang0210/cnblogs-theme/releases/83672101",
    "assets_url": "https://api.github.com/repos/wangyang0210/cnblogs-theme/releases/83672101/assets",
    "upload_url": "https://uploads.github.com/repos/wangyang0210/cnblogs-theme/releases/83672101/assets{?name,label}",
    "html_url": "https://github.com/wangyang0210/cnblogs-theme/releases/tag/v2.2.4",
    "id": 83672101,
    "author": {
        "login": "wangyang0210",
        "id": 36377605,
        "node_id": "MDQ6VXNlcjM2Mzc3NjA1",
        "avatar_url": "https://avatars.githubusercontent.com/u/36377605?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/wangyang0210",
        "html_url": "https://github.com/wangyang0210",
        "followers_url": "https://api.github.com/users/wangyang0210/followers",
        "following_url": "https://api.github.com/users/wangyang0210/following{/other_user}",
        "gists_url": "https://api.github.com/users/wangyang0210/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/wangyang0210/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/wangyang0210/subscriptions",
        "organizations_url": "https://api.github.com/users/wangyang0210/orgs",
        "repos_url": "https://api.github.com/users/wangyang0210/repos",
        "events_url": "https://api.github.com/users/wangyang0210/events{/privacy}",
        "received_events_url": "https://api.github.com/users/wangyang0210/received_events",
        "type": "User",
        "site_admin": false
    },
    "node_id": "RE_kwDOCc6VqM4E_Lwl",
    "tag_name": "v2.2.4",
    "target_commitish": "v2",
    "name": "",
    "draft": false,
    "prerelease": false,
    "created_at": "2022-11-20T08:32:47Z",
    "published_at": "2022-11-20T08:36:10Z",
    "assets": [],
    "tarball_url": "https://api.github.com/repos/wangyang0210/cnblogs-theme/tarball/v2.2.4",
    "zipball_url": "https://api.github.com/repos/wangyang0210/cnblogs-theme/zipball/v2.2.4",
    "body": "# 升级部署rnrn本次升级只需要替换JS文件,如果从较老版本升级需替换JS和CSS文件;rnrn```rn<script src="https://cdn.jsdelivr.net/gh/wangyang0210/cnblogs-theme@v2.2.4/dist/simple-memory.js" defer></script>rn<script src="https://unpkg.com/cnblogs-theme@2.2.4/dist/simple-memory.js" defer></script>rn<script src="https://cdn.jsdelivr.net/npm/cnblogs-theme@2.2.4/dist/simple-memory.js" defer></script>rn```rnrn# 更新日志rn```rn- 优化博客状态栏显示rn- 只保留v2版本文档rn- iconfont使用CDN资源rn- 更新文档备用地址rn- 谷歌字体适用CDN资源rn- 使用fetch代替requestrn- 编译统一使用webpackrn```"
}

0 人点赞