개발/Git, GitHub
깃허브 - RESTful API로 브랜치 SHA 구하기 (Find Github Branch SHA blob)
피로물든딸기
2023. 9. 2. 14:35
반응형
깃허브 데스크탑으로 프로젝트 관리하기 강의 오픈!! (인프런 바로가기)
참고
메인 브랜치에서 특정 파일에 대한 SHA를 구하는 방법은 아래와 같다.
const getSHAforMainFile = async (octokit) => {
const result = await octokit.request(
`GET /repos/bloodstrawberry/${repo}/contents/${path}`,
{
owner: "bloodstrawberry",
repo: `${repo}`,
path: `${path}`,
}
);
return result.data.sha;
};
만약 Main이 아닌 다른 브랜치의 파일을 알고 싶다면 ref에 브랜치 이름을 추가하면 된다.
const getSHAforBranchFile = async (octokit, branchName) => {
const result = await octokit.request(
`GET /repos/bloodstrawberry/${repo}/contents/${path}`,
{
owner: "bloodstrawberry",
repo: `${repo}`,
path: `${path}`,
ref: branchName, // 브랜치 이름을 ref에 지정
}
);
return result.data.sha;
};
파일이 아닌 브랜치 자체의 SHA를 알고 싶다면 octokit.git의 getRef를 이용하면 된다.
const getSHAforMain = async (octokit) => {
const response = await octokit.git.getRef({
owner: "bloodstrawberry",
repo: `${repo}`,
ref: "heads/main", // main 브랜치의 이름
});
return response.data.object.sha;
};
브랜치의 경우는 branch에 브랜치 이름을 추가하면 된다.
const getSHAforBranch = async (octokit, branchName) => {
const branch = await octokit.repos.getBranch({
owner: "bloodstrawberry",
repo: repo,
branch: branchName,
});
return branch.data.commit.sha;
};
반응형