Intro.
깃허브의 리포지토리의 정보를 기반으로 프로젝트에 대해 요약해주는 기능을 구현하고자 한다. 이 기능을 구현하기 위해선 리포지토리의 정보를 갖고 올 수 있어야 한다.그 방법이 바로 깃허브 API를 활용하는 것이다.이 포스트에선 깃허브 API를 통해 리포지토리의 내용을 갖고 오는 부분을 다루겠다.
Repository API 사용하기
Github가 제공하는 수많은 API중 나는 내 서비스에 맞는 Repository API를 사용해보도록 하겠다.
1. Github Token 생성
API를 사용하려면 Github 토큰이 필요하다.
- Github 홈페이지에서 [Settings] - [Developer Settings] - [Personal Access Tokens] -[Tokens(classic)]으로 이동하면 토큰을 발급받을 수 있다.
- 이제 본격적으로 API를 사용해보도록 하겠다. 테스트 해보는 것이 목적이기 때문에 Postman을 사용했다.
- 해당 리포지토리를 통해 테스트 해보겠다.
2. Repository Content
- 리포지토리의 디렉토리와 파일들의 정보를 받아오는 API이다.
- 공식 문서에는 다음 엔드포인트에 GET 방식으로 요청을 보내라고 하고 있다
- 앞서 받아왔던 토큰 정보를 해당 부분에 넣으면 된다.
- Header - Authorization부분에 ‘token {받아온토큰}’ 형식으로 넣어도 된다.
요청
응답 결과
[
{
"name": ".env.local.example",
"path": ".env.local.example",
"sha": "0c1b818c9b222b3300076829f79236a0b3a01968",
"size": 711,
"url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/.env.local.example?ref=main>",
"html_url": "<https://github.com/mckaywrigley/chatbot-ui/blob/main/.env.local.example>",
"git_url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/blobs/0c1b818c9b222b3300076829f79236a0b3a01968>",
"download_url": "<https://raw.githubusercontent.com/mckaywrigley/chatbot-ui/main/.env.local.example>",
"type": "file",
"_links": {
"self": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/.env.local.example?ref=main>",
"git": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/blobs/0c1b818c9b222b3300076829f79236a0b3a01968>",
"html": "<https://github.com/mckaywrigley/chatbot-ui/blob/main/.env.local.example>"
}
},
{
"name": ".eslintrc.json",
"path": ".eslintrc.json",
"sha": "6ec5479fec082dfb78ce1ea30831edc82b2cbda0",
"size": 496,
"url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/.eslintrc.json?ref=main>",
"html_url": "<https://github.com/mckaywrigley/chatbot-ui/blob/main/.eslintrc.json>",
"git_url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/blobs/6ec5479fec082dfb78ce1ea30831edc82b2cbda0>",
"download_url": "<https://raw.githubusercontent.com/mckaywrigley/chatbot-ui/main/.eslintrc.json>",
"type": "file",
"_links": {
"self": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/.eslintrc.json?ref=main>",
"git": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/blobs/6ec5479fec082dfb78ce1ea30831edc82b2cbda0>",
"html": "<https://github.com/mckaywrigley/chatbot-ui/blob/main/.eslintrc.json>"
}
},
{
"name": ".github",
"path": ".github",
"sha": "adbb9f8664e25af7ee03bd236e0f741f639b1753",
"size": 0,
"url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/.github?ref=main>",
"html_url": "<https://github.com/mckaywrigley/chatbot-ui/tree/main/.github>",
"git_url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/trees/adbb9f8664e25af7ee03bd236e0f741f639b1753>",
"download_url": null,
"type": "dir",
"_links": {
"self": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/.github?ref=main>",
"git": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/trees/adbb9f8664e25af7ee03bd236e0f741f639b1753>",
"html": "<https://github.com/mckaywrigley/chatbot-ui/tree/main/.github>"
}
},
{
"name": ".gitignore",
"path": ".gitignore",
"sha": "f5fa61b90925b1b0e44fb6994239dbb9c3129308",
"size": 486,
"url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/.gitignore?ref=main>",
"html_url": "<https://github.com/mckaywrigley/chatbot-ui/blob/main/.gitignore>",
"git_url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/blobs/f5fa61b90925b1b0e44fb6994239dbb9c3129308>",
"download_url": "<https://raw.githubusercontent.com/mckaywrigley/chatbot-ui/main/.gitignore>",
"type": "file",
"_links": {
"self": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/.gitignore?ref=main>",
"git": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/blobs/f5fa61b90925b1b0e44fb6994239dbb9c3129308>",
"html": "<https://github.com/mckaywrigley/chatbot-ui/blob/main/.gitignore>"
}
},
{
"name": ".husky",
"path": ".husky",
"sha": "bc2083f1e90268cba0fe97a60b4e42c7cf83744c",
"size": 0,
"url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/.husky?ref=main>",
"html_url": "<https://github.com/mckaywrigley/chatbot-ui/tree/main/.husky>",
"git_url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/trees/bc2083f1e90268cba0fe97a60b4e42c7cf83744c>",
"download_url": null,
"type": "dir",
"_links": {
"self": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/.husky?ref=main>",
"git": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/trees/bc2083f1e90268cba0fe97a60b4e42c7cf83744c>",
"html": "<https://github.com/mckaywrigley/chatbot-ui/tree/main/.husky>"
}
},
{
"name": ".nvmrc",
"path": ".nvmrc",
"sha": "7ea6a59d3471e7280029f62eafc80633926ff817",
"size": 9,
"url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/.nvmrc?ref=main>",
"html_url": "<https://github.com/mckaywrigley/chatbot-ui/blob/main/.nvmrc>",
"git_url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/blobs/7ea6a59d3471e7280029f62eafc80633926ff817>",
"download_url": "<https://raw.githubusercontent.com/mckaywrigley/chatbot-ui/main/.nvmrc>",
"type": "file",
"_links": {
"self": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/.nvmrc?ref=main>",
"git": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/blobs/7ea6a59d3471e7280029f62eafc80633926ff817>",
"html": "<https://github.com/mckaywrigley/chatbot-ui/blob/main/.nvmrc>"
}
},
{
"name": "README.md",
"path": "README.md",
"sha": "39d4df6bcc4ef3ad8fdc6a53b6535b8b3342872f",
"size": 8173,
"url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/README.md?ref=main>",
"html_url": "<https://github.com/mckaywrigley/chatbot-ui/blob/main/README.md>",
"git_url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/blobs/39d4df6bcc4ef3ad8fdc6a53b6535b8b3342872f>",
"download_url": "<https://raw.githubusercontent.com/mckaywrigley/chatbot-ui/main/README.md>",
"type": "file",
"_links": {
"self": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/README.md?ref=main>",
"git": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/blobs/39d4df6bcc4ef3ad8fdc6a53b6535b8b3342872f>",
"html": "<https://github.com/mckaywrigley/chatbot-ui/blob/main/README.md>"
}
},
{
"name": "__tests__",
"path": "__tests__",
"sha": "e432c40a5698aca21278f4f66128080a7a3a2e91",
"size": 0,
"url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/__tests__?ref=main>",
"html_url": "<https://github.com/mckaywrigley/chatbot-ui/tree/main/__tests__>",
"git_url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/trees/e432c40a5698aca21278f4f66128080a7a3a2e91>",
"download_url": null,
"type": "dir",
"_links": {
"self": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/__tests__?ref=main>",
"git": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/trees/e432c40a5698aca21278f4f66128080a7a3a2e91>",
"html": "<https://github.com/mckaywrigley/chatbot-ui/tree/main/__tests__>"
}
},
{
"name": "app",
"path": "app",
"sha": "87cb54f6bfa0a6909186dd148a5c5ac82152c406",
"size": 0,
"url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/app?ref=main>",
"html_url": "<https://github.com/mckaywrigley/chatbot-ui/tree/main/app>",
"git_url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/trees/87cb54f6bfa0a6909186dd148a5c5ac82152c406>",
"download_url": null,
"type": "dir",
"_links": {
"self": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/app?ref=main>",
"git": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/trees/87cb54f6bfa0a6909186dd148a5c5ac82152c406>",
"html": "<https://github.com/mckaywrigley/chatbot-ui/tree/main/app>"
}
},
.... 생략
- 리포지토리의 파일 및 디렉토리의 정보를 잘 받아온 것을 알 수 있다.
💡 여기서 엔드포인트 뒤에 추가적인 path를 입력하면 해당 파일 및 디렉토리 내부를 볼 수 있다. 특히 파일의 path를
입력하면 해당 파일의 내용을 볼 수 있다.
예시
{
"name": ".gitignore",
"path": ".gitignore",
"sha": "f5fa61b90925b1b0e44fb6994239dbb9c3129308",
"size": 486,
"url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/.gitignore?ref=main>",
"html_url": "<https://github.com/mckaywrigley/chatbot-ui/blob/main/.gitignore>",
"git_url": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/blobs/f5fa61b90925b1b0e44fb6994239dbb9c3129308>",
"download_url": "<https://raw.githubusercontent.com/mckaywrigley/chatbot-ui/main/.gitignore>",
"type": "file",
"content": "IyBTZWUgaHR0cHM6Ly9oZWxwLmdpdGh1Yi5jb20vYXJ0aWNsZXMvaWdub3Jp\\nbmctZmlsZXMvIGZvciBtb3JlIGFib3V0IGlnbm9yaW5nIGZpbGVzLgoKIyBk\\nZXBlbmRlbmNpZXMKL25vZGVfbW9kdWxlcwovLnBucAoucG5wLmpzCi55YXJu\\nL2luc3RhbGwtc3RhdGUuZ3oKCiMgdGVzdGluZwovY292ZXJhZ2UKCiMgbmV4\\ndC5qcwovLm5leHQvCi9vdXQvCgojIHByb2R1Y3Rpb24KL2J1aWxkCgojIG1p\\nc2MKLkRTX1N0b3JlCioucGVtCgojIGRlYnVnCm5wbS1kZWJ1Zy5sb2cqCnlh\\ncm4tZGVidWcubG9nKgp5YXJuLWVycm9yLmxvZyoKCiMgbG9jYWwgZW52IGZp\\nbGVzCi5lbnYKLmVudioubG9jYWwKCiMgdmVyY2VsCi52ZXJjZWwKCiMgdHlw\\nZXNjcmlwdAoqLnRzYnVpbGRpbmZvCm5leHQtZW52LmQudHMKCi5WU0NvZGVD\\nb3VudGVyCnRvb2wtc2NoZW1hcwpjdXN0b20tcHJvbXB0cwoKc3cuanMKc3cu\\nanMubWFwCndvcmtib3gtKi5qcwp3b3JrYm94LSouanMubWFw\\n",
"encoding": "base64",
"_links": {
"self": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/contents/.gitignore?ref=main>",
"git": "<https://api.github.com/repos/mckaywrigley/chatbot-ui/git/blobs/f5fa61b90925b1b0e44fb6994239dbb9c3129308>",
"html": "<https://github.com/mckaywrigley/chatbot-ui/blob/main/.gitignore>"
}
}
- 파일의 내용은 base64 기반으로 인코딩 되어 있어 utf-8로 디코딩 하면 원래의 내용을 수 있다.
base64를 utf-8로 디코딩하는 파이썬 코드
import base64
encoded_content = 'IyBTZWUgaHR0cHM6Ly9oZWxwLmdpdGh1Yi5jb20vYXJ0aWNsZXMvaWdub3Jp\\nbmctZmlsZXMvIGZvciBtb3JlIGFib3V0IGlnbm9yaW5nIGZpbGVzLgoKIyBk\\nZXBlbmRlbmNpZXMKL25vZGVfbW9kdWxlcwovLnBucAoucG5wLmpzCi55YXJu\\nL2luc3RhbGwtc3RhdGUuZ3oKCiMgdGVzdGluZwovY292ZXJhZ2UKCiMgbmV4\\ndC5qcwovLm5leHQvCi9vdXQvCgojIHByb2R1Y3Rpb24KL2J1aWxkCgojIG1p\\nc2MKLkRTX1N0b3JlCioucGVtCgojIGRlYnVnCm5wbS1kZWJ1Zy5sb2cqCnlh\\ncm4tZGVidWcubG9nKgp5YXJuLWVycm9yLmxvZyoKCiMgbG9jYWwgZW52IGZp\\nbGVzCi5lbnYKLmVudioubG9jYWwKCiMgdmVyY2VsCi52ZXJjZWwKCiMgdHlw\\nZXNjcmlwdAoqLnRzYnVpbGRpbmZvCm5leHQtZW52LmQudHMKCi5WU0NvZGVD\\nb3VudGVyCnRvb2wtc2NoZW1hcwpjdXN0b20tcHJvbXB0cwoKc3cuanMKc3cu\\nanMubWFwCndvcmtib3gtKi5qcwp3b3JrYm94LSouanMubWFw\\n'
decoded_content = base64.b64decode(encoded_content).decode('utf-8')
print(decoded_content)
결과
# See <https://help.github.com/articles/ignoring-files/> for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
*.pem
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# local env files
.env
.env*.local
# vercel
.vercel
# typescript
*.tsbuildinfo
next-env.d.ts
.VSCodeCounter
tool-schemas
custom-prompts
sw.js
sw.js.map
workbox-*.js
workbox-*.js.map
마치며
성공적으로 파일의 내용까지 불러왔다.
하지만 파일의 모든 내용을 불러온다면 그 내용이 너무 많아 한번 AI에게 요약 요청을 보낼 때마다 어마어마한 토큰 비용이 발생할 것이다.
어떠한 정보를 버리고 정보를 포함 시켜야 할지 진지하게 고민해봐야겠다.
'생성형 AI' 카테고리의 다른 글
Gemini API, LLama API로 깃허브 리포지토리 요약하기 (0) | 2024.04.05 |
---|---|
Github Repository정보 ChatGpt API로 요약하기 (0) | 2024.04.04 |
ChatGPT API 사용해보기 (0) | 2024.03.31 |