테스트 사이트 - 개발 중인 베타 버전입니다

버전 업데이트 플러그인 개발 - Github API (2) - 활용

· 3년 전 · 2298 · 3

이전 글에 이어서 버전 업데이트에서 활용한 Github API 기능에 대해서 작성하도록 하겠습니다.

 

 

Github API 활용

먼저 아래는 버전 업데이트에 대한 프로세스를 간략하게 정리한 내용입니다. 아래 내용 중에서 하이라이트 된 부분이 Github API를 활용한 곳입니다.

 

그누보드5 버전 업데이트 프로세스

  1. 그누보드5 버전 목록 조회
  2. 가장 최신 버전추출
  3. 버전정보 조회
    • 업데이트 할 버전 재 선택 시, 버전정보 다시 조회
  4. FTP/SFTP 접속 체크 & 로그인
  5. 설치 가능한 디스크 공간 체크
  6. 두 버전 간 변경된 파일목록을 조회
    • 현재 그누보드 버전의 압축파일 다운로드 (원본파일 다운로드)
    • 변경된 파일목록 중에서 다운로드 파일(원본)과 다른 파일 표시
  7. 업데이트 버전 압축파일 다운로드
  8. 현재 그누보드 백업
  9. 변경된 파일목록 업데이트
  10. 업데이트 로그 기록

 

사용된 기능은 크게 4가지이며, 요청 Parameter & Response Data를 다 설명할 수 없으므로 활용한 부분만 명시했습니다. 자세한 내용은 각 항목 링크를 참고해주시기 바랍니다.

 

 

1. List releases

해당 저장소의 릴리즈 목록을 조회합니다.

업데이트를 진행 할 버전 목록을 확인하기 위해 사용했습니다.

  • 'tag_name'에서 버전 이름을 추출해서 사용합니다.

 

API 설명

코드 예시

[code]

/* Github API Test */

$ch             = curl_init();

$header         = array();

$githubToken    = "";

$url            = "https://api.github.com/repos/gnuboard/gnuboard5/releases?per_page=2";

 

if (!empty($githubToken)) {

    array_push($header, "Authorization: token {$githubToken}");

}

 

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

curl_setopt($ch, CURLOPT_USERAGENT, "gnuboard");

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_TIMEOUT, 3600);

 

$response = curl_exec($ch);

echo "<pre style='font-size:1.3em'>";

print_r(json_encode(json_decode($response), JSON_PRETTY_PRINT));

echo "</pre>";

/* Github API Test */

[/code]

 

요청결과

3717582117_1666857150.3827.png

 

 

2. Get a release by tag name

릴리즈 정보를 태그이름(버전)으로 가져옵니다.

버전 선택 시, 변경사항(commit) 내용을 출력하기 위해 사용했습니다.

  • 'body'에서 릴리즈 정보를 토대로 문자열을 파싱한 후 사용합니다.

 

API 설명

코드 예시

[code]

/* Github API Test */

$ch             = curl_init();

$header         = array();

$githubToken    = "";

$url            = "https://api.github.com/repos/gnuboard/gnuboard5/releases/tags/v5.5.8.2.3";

 

if (!empty($githubToken)) {

    array_push($header, "Authorization: token {$githubToken}");

}

 

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

curl_setopt($ch, CURLOPT_USERAGENT, "gnuboard");

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_TIMEOUT, 3600);

 

$response = curl_exec($ch);

echo "<pre style='font-size:1.3em'>";

print_r(json_encode(json_decode($response), JSON_PRETTY_PRINT));

echo "</pre>";

/* Github API Test */

[/code]

 

요청결과

3717582117_1666857505.5867.png

 

body 데이터를 변환한 내용

3717582117_1666857512.9165.png

 

 

3. Compare two commits 

두 버전을 비교, 변경된 내용을 조회합니다.

두 버전 간의 변경된 파일을 비교한 후, 해당 파일만 업데이트 하기 위해 사용했습니다.

  • 'files' 항목에서 변경된 파일 목록을 추출해서 사용합니다.

 

API 설명

  • URL
    • GET https://api.github.com/repos/{owner}/{repo}/compare/{basehead}
  • Path parameter
    • owner - 저장소의 계정 소유자
    • repos - 저장소 이름
    • basehead - 기본 분기와 head 분기
      • 입력 형식: {base}...{head}
      • base, head에는 tag_name대신 branch, commit도 입력 할 수 있습니다.
  • API 문서 링크

코드 예시

[code]

/* Github API Test */

$ch             = curl_init();

$header         = array();

$githubToken    = "";

/* tag name */

$url            = "https://api.github.com/repos/gnuboard/gnuboard5/compare/v5.5.8.2.2...v5.5.8.2.3";

/* commit */

// $url            = "https://api.github.com/repos/gnuboard/gnuboard5/compare/a21d02f4c50fd8b8d09bf51443c196ba0818952f...dbc8e1676a4cd3561a74f1d756320a7d82b45bb4";

/* branch */

// $url            = "https://api.github.com/repos/gnuboard/gnuboard5/compare/master...webp";

 

if (!empty($githubToken)) {

    array_push($header, "Authorization: token {$githubToken}");

}

 

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

curl_setopt($ch, CURLOPT_USERAGENT, "gnuboard");

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_TIMEOUT, 3600);

 

$response = curl_exec($ch);

echo "<pre style='font-size:1.3em'>";

print_r(json_encode(json_decode($response), JSON_PRETTY_PRINT));

echo "</pre>";

/* Github API Test */

[/code]

 

요청결과

3717582117_1666857818.2692.png

 

 

4. Download a repository archive

저장소의 파일을 압축파일로 다운로드 합니다. (tar, zip)

현재 버전의 원본데이터 비교 및 업데이트 진행을 위해 사용했습니다.

  • 다른 API요청과 다르게 응답 데이터 전체를 사용해서 압축파일을 생성합니다.

윈도우의 경우 명령어를 통해 zip파일을 처리하기 위해서는 별도의 환경설정이 필요하므로 tar파일을 사용했습니다.

 

API 설명

코드 예시

[code]

/* Github API Test */

$ch             = curl_init();

$header         = array();

$githubToken    = "";

$url            = "https://api.github.com/repos/gnuboard/gnuboard5/zipball/v5.5.8.2.3";

 

if (!empty($githubToken)) {

    array_push($header, "Authorization: token {$githubToken}");

}

 

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

curl_setopt($ch, CURLOPT_USERAGENT, "gnuboard");

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_TIMEOUT, 3600);

 

// http status : 302

$response = curl_exec($ch);

/* Github API Test */

[/code]

 

요청결과 데이터 사용

[code]

// API에서 받아온 압축파일 데이터 쓰기

$result = self::$g5GithubApi->getArchiveData($exe, $version);

if (!fwrite($archiveFileResource, (string)$result)) {

    throw new Exception('압축파일 데이터 생성에 실패했습니다.');

}

[/code]

 

 

원래는 개발시 겪었던 문제들과 해결방법에 대해서 쓰려고 했지만, 정리하다보니 강좌까지 쓸 정도의 내용이 아니라서 .. 추후 기회가 된다면 팁게시판 같은 곳에 글 남기도록 하겠습니다.

 

긴 글 읽어주셔서 감사합니다 :)

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

댓글 3개

대단하십니다.
감사합니다.
우와..
2년 전
WOW 대단하십니다.

게시글 목록

번호 제목
1050
그누보드5 toto slot gacor
1047
1045
1044
1019
1017
1016
1007
1005
1004
967
964
917
889
879
851
848
762
741
740
727
726
725
724
723
722
721
720
719
717