깃허브 코드 검색 API 기본 구조 (curl 예시)
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer github_pat_tooooooooooooooookeeeeeeeeeeeeeeen" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"[https://api.github.com/search/code?q=](https://api.github.com/search/code?q=Q)쿼리문"
헤더
- Accect: 서버가 반환한 응답 형식 지정
-H "Accept: application/vnd.github+json"
: 서버가 반환하는 응답을 JSON 형식으로 받음 (권고)
- Authorization: GitHub API에 대한 인증 토큰 지정
-H "Authorization: Bearer <your_github_token>"
:<your_github_token>
부분에는 실제로 사용자의 GitHub Personal Access Token이 들어가야 함
- X-GitHub-Api-Version: 깃허브 API 버전 지정
본문
코드 검색에 대한 쿼리문 작성 필요
쿼리문 형식
SEARCH_KEYWORD_1 SEARCH_KEYWORD_N QUALIFIER_1 QUALIFIER_N
검색 조건들은 ‘+’로 이어 씀
검색 조건 요소
- 포함 단어
- 키워드를 레이블 없이 바로 작성
- 파일 컨텐츠 또는 파일 경로
한정자 | 예시 |
---|---|
in:file | https://github.com/search?q=octocat+in%3Afile&type=Code은 파일 콘텐츠에 “octocat”이 있는 코드를 찾습니다. |
in:path | https://github.com/search?q=octocat+in%3Apath&type=Code는 파일 경로에 “octocat”이 있는 코드를 찾습니다. |
in:file,path | https://github.com/search?q=octocat+in%3Afile%2Cpath&type=Code는 파일 콘텐츠 또는 파일 경로에 “octocat”이 있는 코드를 찾습니다. |
- 사용자 또는 조직의 레포지토리
한정자 | 예시 |
---|---|
user:USERNAME | https://github.com/search?q=user%3Agithub+extension%3Arb&type=Code는 .rb로 끝나는 @defunkt에서 코드를 찾습니다. |
org:ORGNAME | https://github.com/search?utf8=%E2%9C%93&q=org%3Agithub+extension%3Ajs&type=Code는 .js로 끝나는 GitHub에서 코드를 찾습니다. |
repo:USERNAME/REPOSITORY | https://github.com/search?q=repo%3Amozilla%2Fshumway+extension%3Aas&type=Code는 .as로 끝나는 @mozilla의 shumway 프로젝트에서 코드를 찾습니다. |
- 파일 위치
한정자 | 예시 |
---|---|
path:/ | https://github.com/search?utf8=%E2%9C%93&q=octocat+filename%3Areadme+path%3A%2F&type=Code는 리포지토리의 루트 수준에 “octocat”이라는 단어가 있는 readme 파일을 찾습니다. |
path:DIRECTORY | https://github.com/search?q=form+path%3Acgi-bin+language%3Aperl&type=Code은 cgi-bin 디렉터리 또는 해당 하위 디렉터리에 “form”이라는 단어가 있는 Perl 파일을 찾습니다. |
path:PATH/TO/DIRECTORY | https://github.com/search?q=console+path%3A%22app%2Fpublic%22+language%3Ajavascript&type=Code는 app/public 디렉터리 또는 해당 하위 디렉터리에 “console”이라는 단어가 있는 JavaScript 파일을 찾습니다(해당 파일이 app/public/js/form-validators에 있더라도 검색됨). |
- 언어
- 지원 언어 및 별칭: github-linguist/linguist
한정자 | 예시 |
---|---|
language:LANGUAGE | https://github.com/search?q=element+language%3Axml+size%3A100&type=Code은 XML로 표시되어 있고, 정확히 100바이트이며, “element”라는 단어가 있는 코드를 찾습니다. |
language:LANGUAGE | https://github.com/search?q=display+language%3Ascss&type=Code는 SCSS로 표시되어 있고 “display”라는 단어가 있는 코드를 찾습니다. |
language:LANGUAGE | https://github.com/search?utf8=%E2%9C%93&q=org%3Amozilla+language%3Amarkdown&type=Code은 Markdown으로 표시된 모든 @mozilla의 리포지토리에서 코드를 찾습니다. |
- 파일 크기
한정자 | 예시 |
---|---|
size:n | https://github.com/search?q=function+size%3A%3E10000+language%3Apython&type=Code은 “function”이라는 단어가 있고, Python으로 작성되었으며, 10KB를 초과하는 파일에 있는 코드를 찾습니다. |
- 파일 이름
한정자 | 예시 |
---|---|
filename:FILENAME | https://github.com/search?utf8=%E2%9C%93&q=filename%3Alinguist&type=Code는 “linguist”라는 파일을 찾습니다. |
filename:FILENAME | https://github.com/search?q=filename%3A.vimrc+commands&type=Code는 “commands”라는 단어가 있는 .vimrc 파일을 찾습니다. |
filename:FILENAME | https://github.com/search?q=minitest+filename%3Atest_helper+path%3Atest+language%3Aruby&type=Code는 test 디렉터리 내의 _test_helper_라는 Ruby 파일을 찾습니 |
- 파일 확장명
한정자 | 예시 |
---|---|
extension:EXTENSION | https://github.com/search?q=form+path%3Acgi-bin+extension%3Apm&type=Code은 cgi-bin에서 “form”이라는 단어와 .pm 파일 확장명이 있는 코드를 찾습니다. |
extension:EXTENSION | https://github.com/search?utf8=%E2%9C%93&q=icon+size%3A%3E200000+extension%3Acss&type=Code는 .css로 끝나고, “icon”이라는 단어가 있으며, 200KB를 초과하는 파일을 찾습니다. |
쿼리 제한 사항 (유효성 검사 실패 오류)
- 256자보다 긴 쿼리 (연산자 또는 한정자 포함 x)
- 5개 이상의 AND, OR, NOT 연산자
예시
$ curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer github_pat_toooooooooooookeeeeeeeeeeeeeen" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/search/code?q=SentryFlow+istio+user:boanlab"
위 예시는 “SentryFlow”와 “Istio” 단어를 포함하며 유저가 “boanlab”인 코드를 검색
'인턴 메모' 카테고리의 다른 글
Kafka 정리 (0) | 2024.07.29 |
---|---|
GitHub Code Crawling (Selenium) (0) | 2024.07.21 |
Github API Search Code Trouble Shooting(?) (0) | 2024.07.09 |
GitHub File SHA Hash Test (0) | 2024.07.05 |
인턴 기간 중 트러블슈팅 메모 (계속해서 수정) (0) | 2024.07.05 |