0

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" }

trzczy
  • 1,325
  • 2
  • 18
  • 43
  • 2
    why is that `let text` above your import statements? – Mike 'Pomax' Kamermans Mar 01 '23 at 22:23
  • 2
    Try using `const result = await octokit.request(...)` and log the entire result to see what you're getting. – Barmar Mar 01 '23 at 22:24
  • @Mike'Pomax'Kamermans I use a special template for console.log and it has text variable as you can see in the code. I copy and paste the template and I do not change the variable. I can change it if you want. – trzczy Mar 01 '23 at 22:27
  • 1
    @trzczy what Mike is saying is that your `import` statements should be above any other code – Phil Mar 01 '23 at 22:27
  • @Barmar I edited the question accordingly to your suggestion – trzczy Mar 01 '23 at 22:50
  • 1
    That seems to have retrieved the contents of your file which isn't what it's supposed to do. If I use the REST API directly via curl, I get the expected metadata about the file instead... `curl -v "https://api.github.com/repos/na-czczo/r/contents/f.json"` – Phil Mar 01 '23 at 22:50
  • I compared the result of JS to curl and it is the SHA in JS api actually but in the key "etag". So it resolved the problem. But I still find it strange because several days ago the key was sha in JS api – trzczy Mar 01 '23 at 22:57
  • 2
    `etag` comes from the HTTP header, `sha` is in the github API's JSON. It's not clear how Octokit is processing the response. – Barmar Mar 01 '23 at 22:59

1 Answers1

0

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

trzczy
  • 1,325
  • 2
  • 18
  • 43