* Update the target repo to ensure the provided golden file contents are used for the files * with the same path in the repo.
(github: Octokit, repo: string, goldenFiles: Files)
| 66 | * with the same path in the repo. |
| 67 | */ |
| 68 | async function updateRepoWithFiles(github: Octokit, repo: string, goldenFiles: Files) { |
| 69 | core.startGroup(`Update files in "${repo}" repo`); |
| 70 | /** The current files, or lack of files, for synchronizing in target repo. */ |
| 71 | const repoFiles = await getFilesForRepo(github, repo); |
| 72 | |
| 73 | for (let [path, goldenFile] of goldenFiles.entries()) { |
| 74 | // If the golden file does not exist, we have nothing to syncronize. |
| 75 | if (goldenFile === null) { |
| 76 | continue; |
| 77 | } |
| 78 | /** The target repository's File for the path. */ |
| 79 | const repoFile = repoFiles.get(path) || null; |
| 80 | /** The SHA of the last time the file was updated in the target repo. */ |
| 81 | let repoSha: string | undefined = undefined; |
| 82 | /** The current content of the file in the target repo. */ |
| 83 | let repoFileContent: string | undefined = undefined; |
| 84 | |
| 85 | // If the repo file is null, there is not previous information to use for comparisons |
| 86 | if (repoFile !== null) { |
| 87 | repoSha = repoFile.sha; |
| 88 | repoFileContent = repoFile.content; |
| 89 | } |
| 90 | |
| 91 | if (repoFileContent !== goldenFile.content) { |
| 92 | core.info(`Updating "${path}" in "${repo}" repo`); |
| 93 | try { |
| 94 | await github.repos.createOrUpdateFileContents({ |
| 95 | content: goldenFile.content, |
| 96 | owner: context.repo.owner, |
| 97 | repo, |
| 98 | path, |
| 99 | message: `build: update \`${path}\` to match the content of \`${context.repo.owner}/${context.repo.repo}\``, |
| 100 | // The SHA of the previous file content change is used if an update is occuring. |
| 101 | sha: repoSha, |
| 102 | }); |
| 103 | } catch (e) { |
| 104 | core.info(`Failed to update "${path}"`); |
| 105 | console.error(e); |
| 106 | } |
| 107 | } else { |
| 108 | core.info(`"${path}" is already in sync`); |
| 109 | } |
| 110 | } |
| 111 | core.endGroup(); |
| 112 | } |
| 113 | |
| 114 | async function main() { |
| 115 | const github = new Octokit({auth: await getAuthTokenFor(ANGULAR_ROBOT)}); |
no test coverage detected