Skip to content

Commit

Permalink
feat(file): add commit files
Browse files Browse the repository at this point in the history
  • Loading branch information
hankliu62 committed Apr 1, 2024
1 parent d5dd3c6 commit 54aea3d
Show file tree
Hide file tree
Showing 36 changed files with 29,021 additions and 1,934 deletions.
15 changes: 15 additions & 0 deletions .bin/constant.js
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
}
194 changes: 194 additions & 0 deletions .bin/libs/issues.js
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;

/**
* 根据分页获取数据
*
* @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,
}
84 changes: 84 additions & 0 deletions .bin/libs/labels.js
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;

/**
* 根据获取所有的标签
*
* @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,
}
Loading

0 comments on commit 54aea3d

Please sign in to comment.