* Get file content from GitHub repository using the API * @param {Object} github - GitHub API client (@actions/github) * @param {string} owner - Repository owner * @param {string} repo - Repository name * @param {string} path - File path within the repository * @param {string} ref - Git referen
(github, owner, repo, path, ref)
| 67 | * @returns {Promise<{content: string|null, errorStatus: number|null}>} File content and HTTP error status (if any) |
| 68 | */ |
| 69 | async function getFileContent(github, owner, repo, path, ref) { |
| 70 | try { |
| 71 | const response = await github.rest.repos.getContent({ |
| 72 | owner, |
| 73 | repo, |
| 74 | path, |
| 75 | ref, |
| 76 | }); |
| 77 | |
| 78 | // Handle case where response is an array (directory listing) |
| 79 | if (Array.isArray(response.data)) { |
| 80 | core.info(`Path ${path} is a directory, not a file`); |
| 81 | return { content: null, errorStatus: null }; |
| 82 | } |
| 83 | |
| 84 | // Check if this is a file (not a symlink or submodule) |
| 85 | if (response.data.type !== "file") { |
| 86 | core.info(`Path ${path} is not a file (type: ${response.data.type})`); |
| 87 | return { content: null, errorStatus: null }; |
| 88 | } |
| 89 | |
| 90 | // Decode base64 content |
| 91 | if (response.data.encoding === "base64" && response.data.content) { |
| 92 | return { content: Buffer.from(response.data.content, "base64").toString("utf8"), errorStatus: null }; |
| 93 | } |
| 94 | |
| 95 | return { content: response.data.content || null, errorStatus: null }; |
| 96 | } catch (error) { |
| 97 | const errorMessage = getErrorMessage(error); |
| 98 | core.info(`Could not fetch content for ${path}: ${errorMessage}`); |
| 99 | return { content: null, errorStatus: error.status ?? null }; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Fetches all labels from a repository, paginating through all pages. |
no test coverage detected