Getting sha of file on GitHub not working

261 Views Asked by At

When I try to get the sha of a certain file on GitHub the undefined value is returned. I was using the same method several days ago and everything was ok. How to get sha?

let text = '';
import {Octokit} from "https://cdn.skypack.dev/octokit";
import token from "./config.js";
const token_string = token.token_string;
export const octokit = new Octokit({
    auth: token_string,
});
const put_ob_01 = {
    owner: "na-czczo", repo: "r", file_path: "f.json"
};
const {data: {sha}} = await octokit.request('GET /repos/{owner}/{repo}/contents/{file_path}', put_ob_01);
text = sha;
console.log(`%c${text}`, 'color: brown')

edit

I changed the code to get the whole result. Then you can see the result. SHA is not present over there.

import {Octokit} from "https://cdn.skypack.dev/octokit";
import token from "./config.js";

const token_string = token.token_string;
export const octokit = new Octokit({
    auth: token_string,
});
const put_ob_01 = {
    owner: "na-czczo", repo: "r", file_path: "f.json"
};
const result = await octokit.request('GET /repos/{owner}/{repo}/contents/{file_path}', put_ob_01);
console.log(JSON.stringify(result, null, "\n"))

{

"status": 200,

"url": "https://api.github.com/repos/na-czczo/r/contents/f.json",

"headers": {

"cache-control": "private, max-age=60, s-maxage=60",

"content-length": "185",

"content-type": "application/vnd.github.v3.raw; charset=utf-8",

"etag": ""078b905d0bde1d7dc38efe61a42a4b47def7ac4d"",

"last-modified": "Wed, 01 Mar 2023 19:31:58 GMT",

"x-accepted-oauth-scopes": "",

"x-github-media-type": "github.v3; param=raw",

"x-github-request-id": "4E4A:304B:9608AE2:98A4C52:63FFD3F0",

"x-oauth-scopes": "repo",

"x-ratelimit-limit": "5000",

"x-ratelimit-remaining": "4997",

"x-ratelimit-reset": "1677711871",

"x-ratelimit-resource": "core",

"x-ratelimit-used": "3"

},

"data": "{\n "array": [\n 1,\n 2,\n 3\n ],\n "boolean": true,\n "color": "gold",\n "null": null,\n "number": 123,\n "object": {\n "a": "b",\n "c": "d"\n },\n "string": "Hello World"\n}\n" }

1

There are 1 best solutions below

0
trzczy On

Finally, I found the resolution. We must add the branch.

import {Octokit} from "https://cdn.skypack.dev/octokit";
import token from "./config.js";

const token_string = token.token_string;
export const octokit = new Octokit({
    auth: token_string,
});
const put_ob_01 = {
    owner: "na-czczo", repo: "r", file_path: "f.json", branch: "main"
};
const result = await octokit.request('GET /repos/{owner}/{repo}/contents/{file_path}', put_ob_01);
console.log('The sha is', result.data.sha);

The sha is 078b905d0bde1d7dc38efe61a42a4b47def7ac4d