-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
28,538 additions
and
1,935 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const GitHubApiVersion = "2022-11-28"; | ||
|
||
const GithubOwner = "hankliu62"; | ||
|
||
// 博客Repo | ||
const GithubRepoBlog = "hankliu62.github.com"; | ||
|
||
const AccessToken = (process.env.NEXT_GITHUB_BACKEND_TOKEN || '').split(''); | ||
|
||
module.exports = { | ||
AccessToken, | ||
GitHubApiVersion, | ||
GithubOwner, | ||
GithubRepoBlog | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
const { Octokit } = require('octokit'); | ||
const { AccessToken, GitHubApiVersion, GithubOwner } = require('../constant'); | ||
|
||
const auth = AccessToken.join(''); | ||
|
||
/** | ||
* 根据分页获取数据 | ||
* | ||
* @param {*} page | ||
* @returns | ||
*/ | ||
async function fetchIssues(page, repo, options = {}) { | ||
const octokit = new Octokit({ | ||
auth: auth | ||
}) | ||
|
||
const res = await octokit.request(`GET /repos/${GithubOwner}/${repo}/issues`, { | ||
owner: GithubOwner, | ||
repo, | ||
per_page: 30, | ||
page: page, | ||
headers: { | ||
'X-GitHub-Api-Version': GitHubApiVersion | ||
}, | ||
direction: 'asc', | ||
...options, | ||
}); | ||
|
||
|
||
// 请求成功 | ||
if (res.status === 200) { | ||
return res.data; | ||
} else { | ||
throw res; | ||
} | ||
} | ||
|
||
/** | ||
* 获取所有问题 | ||
* @returns | ||
*/ | ||
async function fetchAllIssues(repo, options = {}) { | ||
return new Promise((resolve, reject) => { | ||
// 问题列表 | ||
const questions = []; | ||
let page = 1; | ||
async function loopFetchIssue() { | ||
const currentQuestions = await fetchIssues(page, repo, options); | ||
if (currentQuestions.length) { | ||
for (const question of currentQuestions) { | ||
questions.push(question); | ||
} | ||
page ++; | ||
setTimeout(loopFetchIssue, 100); | ||
} else { | ||
resolve(questions); | ||
} | ||
} | ||
|
||
loopFetchIssue(); | ||
}); | ||
} | ||
|
||
/** | ||
* 生成问题 | ||
* | ||
* @param {*} repo | ||
* @param {*} title | ||
* @param {*} answer | ||
* @param {*} options | ||
* @returns | ||
*/ | ||
async function createIssue(repo, title, answer, options = {}) { | ||
const octokit = new Octokit({ | ||
auth: auth | ||
}) | ||
|
||
const res = await octokit.request(`POST /repos/${GithubOwner}/${repo}/issues`, { | ||
owner: GithubOwner, | ||
repo: repo, | ||
title: title, | ||
body: answer, | ||
assignees: [ | ||
GithubOwner | ||
], | ||
headers: { | ||
'X-GitHub-Api-Version': GitHubApiVersion | ||
}, | ||
...options, | ||
}); | ||
|
||
return res; | ||
} | ||
|
||
/** | ||
* 生成问题 | ||
* | ||
* @param {*} repo | ||
* @param {*} id | ||
* @param {*} options | ||
* @returns | ||
*/ | ||
async function closeIssue(repo, id, options = {}) { | ||
const octokit = new Octokit({ | ||
auth: auth | ||
}) | ||
|
||
const res = await octokit.request(`POST /repos/${GithubOwner}/${repo}/issues/${id}`, { | ||
owner: GithubOwner, | ||
repo: repo, | ||
issue_number: +id, | ||
state: "closed", | ||
state_reason: "completed", | ||
headers: { | ||
'X-GitHub-Api-Version': GitHubApiVersion | ||
}, | ||
...options, | ||
}); | ||
|
||
return res; | ||
} | ||
|
||
/** | ||
* 更新问题 | ||
* | ||
* @param {*} repo | ||
* @param {*} id | ||
* @param {*} body | ||
* @param {*} options | ||
* @returns | ||
*/ | ||
async function updateIssue(repo, id, body, options = {}) { | ||
const octokit = new Octokit({ | ||
auth: auth | ||
}) | ||
|
||
const res = await octokit.request(`POST /repos/${GithubOwner}/${repo}/issues/${id}`, { | ||
owner: GithubOwner, | ||
repo: repo, | ||
issue_number: +id, | ||
body: body, | ||
headers: { | ||
'X-GitHub-Api-Version': GitHubApiVersion | ||
}, | ||
...options, | ||
}); | ||
|
||
return res; | ||
} | ||
|
||
/** | ||
* 获得所有问题的名称Set | ||
* | ||
* @param {*} repo | ||
* @param {*} options | ||
* @returns | ||
*/ | ||
async function getIssuesTitleSet(repo, options = {}) { | ||
const set = new Set() | ||
const issues = await fetchAllIssues(repo, options); | ||
for (const issue of issues) { | ||
set.add(issue.title.trim()); | ||
} | ||
|
||
return set; | ||
} | ||
|
||
/** | ||
* 获得所有问题的名称Map | ||
* | ||
* @param {*} repo | ||
* @param {*} options | ||
* @returns | ||
*/ | ||
async function getIssuesTitleMap(repo, options = {}) { | ||
const map = new Map() | ||
const issues = await fetchAllIssues(repo, options); | ||
for (const issue of issues) { | ||
map.set(issue.title.trim(), issue); | ||
} | ||
|
||
return map; | ||
} | ||
|
||
|
||
module.exports = { | ||
fetchIssues, | ||
fetchAllIssues, | ||
createIssue, | ||
closeIssue, | ||
updateIssue, | ||
getIssuesTitleSet, | ||
getIssuesTitleMap, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
const { Octokit } = require('octokit'); | ||
const { AccessToken, GitHubApiVersion, GithubOwner } = require('../constant'); | ||
|
||
const auth = AccessToken.join(''); | ||
|
||
/** | ||
* 根据获取所有的标签 | ||
* | ||
* @param {*} repo | ||
* @returns | ||
*/ | ||
async function fetchLabels(repo) { | ||
const octokit = new Octokit({ | ||
auth: auth | ||
}) | ||
|
||
const res = await octokit.request(`GET /repos/${GithubOwner}/${repo}/labels`, { | ||
owner: GithubOwner, | ||
repo: repo, | ||
per_page: 100, | ||
page: 1, | ||
headers: { | ||
'X-GitHub-Api-Version': GitHubApiVersion | ||
}, | ||
}); | ||
|
||
|
||
// 请求成功 | ||
if (res.status === 200) { | ||
return res.data; | ||
} else { | ||
throw res; | ||
} | ||
} | ||
|
||
/** | ||
* 生成标签 | ||
* | ||
* @param {*} repo | ||
* @param {*} name | ||
* @param {*} color | ||
* @param {*} description | ||
* @returns | ||
*/ | ||
async function createLabel(repo, name, color, description) { | ||
const octokit = new Octokit({ | ||
auth: auth | ||
}) | ||
|
||
const res = await octokit.request(`POST /repos/${GithubOwner}/${repo}/labels`, { | ||
owner: GithubOwner, | ||
repo: repo, | ||
name: name, | ||
description: description, | ||
color: color, | ||
headers: { | ||
'X-GitHub-Api-Version': GitHubApiVersion | ||
}, | ||
}); | ||
|
||
return res; | ||
} | ||
|
||
/** | ||
* 获得所有标签的名称Set | ||
* | ||
* @param {*} repo | ||
* @returns | ||
*/ | ||
async function getLabelsNameSet(repo) { | ||
const set = new Set() | ||
const issues = await fetchLabels(repo); | ||
for (const issue of issues) { | ||
set.add(issue.name.trim()); | ||
} | ||
|
||
return set; | ||
} | ||
|
||
module.exports = { | ||
fetchLabels, | ||
getLabelsNameSet, | ||
createLabel, | ||
} |
Oops, something went wrong.