Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support non-utf8 files in backports #311

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"devDependencies": {
"@types/fs-extra": "^11.0.1",
"@types/jest": "^29.0.0",
"@types/node": "^18.11.8",
"@types/node": "^20.11.8",
"@types/node-fetch": "^2.5.4",
"@types/pino-std-serializers": "^4.0.0",
"@typescript-eslint/eslint-plugin": "^5.50.0",
Expand All @@ -52,7 +52,7 @@
"{src,spec}/**/*.ts": "prettier --write **/*.ts"
},
"engines": {
"node": ">= 7.7.0",
"node": ">= 20.7.0",
"npm": ">= 4.0.0"
},
"resolutions": {
Expand Down
21 changes: 19 additions & 2 deletions src/operations/backport-commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import simpleGit from 'simple-git';
import { BackportOptions } from '../interfaces';
import { log } from '../utils/log-util';
import { LogLevel } from '../enums';
import { isUtf8 } from 'buffer';

const cleanRawGitString = (s: string) => {
let nS = s.trim();
Expand Down Expand Up @@ -138,14 +139,30 @@ export const backportCommitsToBranch = async (options: BackportOptions) => {
sha: null as any,
};
}
const fileContents = await fs.readFile(onDiskPath, 'utf-8');
const fileContents = await fs.readFile(onDiskPath);
const stat = await fs.stat(onDiskPath);
const userMode = (stat.mode & parseInt('777', 8)).toString(8)[0];
if (isUtf8(fileContents)) {
return <const>{
path: changedFile,
mode: userMode === '6' ? '100644' : '100755',
type: 'blob',
content: fileContents.toString('utf-8'),
};
}

const blob = await options.github.git.createBlob(
options.context.repo({
content: fileContents.toString('base64'),
encoding: 'base64',
}),
);

return <const>{
path: changedFile,
mode: userMode === '6' ? '100644' : '100755',
type: 'blob',
content: fileContents,
sha: blob.data.sha,
};
}),
),
Expand Down
15 changes: 11 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1312,10 +1312,12 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.41.tgz#d0b939d94c1d7bd53d04824af45f1139b8c45615"
integrity sha512-dueRKfaJL4RTtSa7bWeTK1M+VH+Gns73oCgzvYfHZywRCoPSd8EkXBL0mZ9unPTveBn+D9phZBaxuzpwjWkW0g==

"@types/node@^18.11.8":
version "18.11.18"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.11.18.tgz#8dfb97f0da23c2293e554c5a50d61ef134d7697f"
integrity sha512-DHQpWGjyQKSHj3ebjFI/wRKcqQcdR+MoFBygntYOZytCqNfkd2ZC4ARDJ2DQqhjH5p85Nnd3jhUJIXrszFX/JA==
"@types/node@^20.11.8":
version "20.16.8"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.16.8.tgz#36f5c22e2ac6b61f3728f756d27e759246f0e98a"
integrity sha512-sbo5JmfbZNkyDv+2HCccr9Y9ZkKJBMTru7UdAsCojMGjKNjdaOV73bqEW242QrHEZL8R4LbHMrW+FHB5lZ5/bw==
dependencies:
undici-types "~6.19.2"

"@types/parse-json@^4.0.0":
version "4.0.0"
Expand Down Expand Up @@ -5599,6 +5601,11 @@ unbox-primitive@^1.0.2:
has-symbols "^1.0.3"
which-boxed-primitive "^1.0.2"

undici-types@~6.19.2:
version "6.19.8"
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.19.8.tgz#35111c9d1437ab83a7cdc0abae2f26d88eda0a02"
integrity sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==

universal-github-app-jwt@^1.0.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/universal-github-app-jwt/-/universal-github-app-jwt-1.1.1.tgz#d57cee49020662a95ca750a057e758a1a7190e6e"
Expand Down