Skip to content

Commit 5607688

Browse files
authored
Enhance token generation by including isPullRequest flag in xetWriteToken and related functions for better handling of pull requests (#1746)
I was getting error when trying to create pull request on repos that I do **not** own: ``` Error: Forbidden: pass `create_pr=1` as a query parameter to create a Pull Request. URL: https://huggingface.co/api/models/reach-vb/TinyLlama-1.1B-Chat-v1.0-q4_k_m-GGUF/xet-write-token/main ``` I was using new API from #1718 ```ts // Edit first kB of file await commit({ repo, accessToken: "hf_...", operations: [{ type: "edit", originalContent: originalFile, edits: [{ start: 0, end: 1000, content: new Blob(["blablabla"]) }] }] }) ``` Let me know if this PR is the right way to handle this issue
1 parent 9e8c7ff commit 5607688

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

packages/hub/src/lib/commit.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ export async function* commitIter(params: CommitParams): AsyncGenerator<CommitPr
410410
repo: repoId,
411411
// todo: maybe leave empty if PR?
412412
rev: params.branch ?? "main",
413+
isPullRequest: params.isPullRequest,
413414
yieldCallback: (event) => yieldCallback({ ...event, state: "uploading" }),
414415
})) {
415416
if (event.event === "file") {

packages/hub/src/utils/createXorbs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export async function* createXorbs(
199199

200200
let cacheData = chunkCache.getChunk(chunk.hash, chunkModule.compute_hmac);
201201
if (cacheData === undefined && chunk.dedup && bytesSinceRemoteDedup >= INTERVAL_BETWEEN_REMOTE_DEDUP) {
202-
const token = await xetWriteToken(params);
202+
const token = await xetWriteToken({ ...params, isPullRequest: params.isPullRequest });
203203
bytesSinceRemoteDedup = 0;
204204

205205
const shardResp = await (params.fetch ?? fetch)(token.casUrl + "/v1/chunks/default/" + chunk.hash, {
@@ -680,7 +680,7 @@ async function loadDedupInfoToCache(
680680

681681
// Try remote dedup lookup if conditions are met
682682
if (chunk.dedup && bytesSinceRemoteDedup >= INTERVAL_BETWEEN_REMOTE_DEDUP) {
683-
const token = await xetWriteToken(params);
683+
const token = await xetWriteToken({ ...params, isPullRequest: params.isPullRequest });
684684
bytesSinceRemoteDedup = 0;
685685

686686
const shardResp = await (params.fetch ?? fetch)(token.casUrl + "/v1/chunks/default/" + chunk.hash, {

packages/hub/src/utils/uploadShards.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ interface UploadShardsParams {
5757
fetch?: typeof fetch;
5858
repo: RepoId;
5959
rev: string;
60+
isPullRequest?: boolean;
6061
yieldCallback: (event: { event: "fileProgress"; path: string; progress: number }) => void;
6162
}
6263

@@ -357,7 +358,7 @@ async function uploadXorb(
357358
xorb: { hash: string; xorb: Uint8Array; files: Array<{ path: string; progress: number; lastSentProgress: number }> },
358359
params: UploadShardsParams
359360
) {
360-
const token = await xetWriteToken(params);
361+
const token = await xetWriteToken({ ...params, isPullRequest: params.isPullRequest });
361362

362363
const resp = await (params.fetch ?? fetch)(`${token.casUrl}/v1/xorbs/default/${xorb.hash}`, {
363364
method: "POST",
@@ -386,7 +387,7 @@ async function uploadXorb(
386387
}
387388

388389
async function uploadShard(shard: Uint8Array, params: UploadShardsParams) {
389-
const token = await xetWriteToken(params);
390+
const token = await xetWriteToken({ ...params, isPullRequest: params.isPullRequest });
390391

391392
const resp = await (params.fetch ?? fetch)(`${token.casUrl}/v1/shards`, {
392393
method: "POST",

packages/hub/src/utils/xetWriteToken.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ export interface XetWriteTokenParams {
77
fetch?: typeof fetch;
88
repo: RepoId;
99
rev: string;
10+
isPullRequest?: boolean;
1011
}
1112

1213
const JWT_SAFETY_PERIOD = 60_000;
1314
const JWT_CACHE_SIZE = 1_000;
1415

1516
function cacheKey(params: Omit<XetWriteTokenParams, "fetch">): string {
16-
return JSON.stringify([params.hubUrl, params.repo, params.rev, params.accessToken]);
17+
return JSON.stringify([params.hubUrl, params.repo, params.rev, params.accessToken, params.isPullRequest]);
1718
}
1819

1920
const jwtPromises: Map<string, Promise<{ accessToken: string; casUrl: string }>> = new Map();
@@ -46,7 +47,8 @@ export async function xetWriteToken(params: XetWriteTokenParams): Promise<{ acce
4647

4748
const promise = (async () => {
4849
const resp = await (params.fetch ?? fetch)(
49-
`${params.hubUrl}/api/${params.repo.type}s/${params.repo.name}/xet-write-token/${params.rev}`,
50+
`${params.hubUrl}/api/${params.repo.type}s/${params.repo.name}/xet-write-token/${params.rev}` +
51+
(params.isPullRequest ? "?create_pr=1" : ""),
5052
{
5153
headers: params.accessToken
5254
? {

0 commit comments

Comments
 (0)