Skip to content

Commit

Permalink
fix: GiteaContentEntry string / buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
arlac77 committed May 26, 2024
1 parent 0c44151 commit d4b0640
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
25 changes: 7 additions & 18 deletions src/gitea-branch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class GiteaBranch extends Branch {
*/
async writeEntry(entry, message) {
const buffer = await entry.buffer;
const decoder = new TextDecoder('utf8');
const decoder = new TextDecoder("utf8");
const content = btoa(decoder.decode(buffer));

const { json, response } = await this.provider.fetchJSON(
Expand All @@ -99,7 +99,7 @@ export class GiteaBranch extends Branch {
}
);

if(!response.ok) {
if (!response.ok) {
throw new Error(response.statusText);
}

Expand All @@ -119,17 +119,6 @@ export class GiteaBranch extends Branch {
entries.map(entry => this.writeEntry(entry, message))
);

//console.log(updates);

/*
const { json, response } = await this.provider.fetchJSON(
join("repos", this.repository.fullName, "git/trees/", updates.sha),
{
method: "PUT",
body: JSON.stringify(updates)
}
);*/

// TODO hack
return {
ref: `refs/heads/${this.name}`
Expand Down Expand Up @@ -165,9 +154,8 @@ class GiteaContentEntry extends BufferContentEntryMixin(ContentEntry) {
);

const result = await this.provider.fetch(url);

console.log("GiteaContentEntry", await result.body);
return streamToUint8Array(await result.body);
const body = await result.json();
return Buffer.from(body.content, "base64");
}

async getString() {
Expand All @@ -179,8 +167,8 @@ class GiteaContentEntry extends BufferContentEntryMixin(ContentEntry) {
);

const result = await this.provider.fetch(url);

return streamToString(await result.body);
const body = await result.json();
return Buffer.from(body.content, "base64").toString();
}

get string() {
Expand Down Expand Up @@ -216,6 +204,7 @@ class GiteaMasterOnlyContentEntry extends StreamContentEntryMixin(
"raw",
this.name
);

const result = await this.provider.fetch(url);

return await result.body;
Expand Down
49 changes: 35 additions & 14 deletions tests/branch-ava.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import test from "ava";
import { assertCommit, createMessageDestination } from "repository-provider-test-support";
import {
assertCommit,
createMessageDestination
} from "repository-provider-test-support";
import GiteaProvider from "gitea-repository-provider";

const messageDestination = createMessageDestination().messageDestination;
Expand All @@ -14,9 +17,15 @@ async function checkEntry(t, entry, fixture) {
if (fixture.isCollection) {
t.true(entry.isCollection);
} else {
const string = await entry.string;

if (!string.startsWith(fixture.startsWith)) {
t.log(`'${string}' does not start with ${fixture.startsWith}`);
}

t.true(
(await entry.string).startsWith(fixture.startsWith),
"string"
string.startsWith(fixture.startsWith),
`content startsWith ${fixture.startsWith}`
);

const stream = await entry.readStream;
Expand All @@ -27,15 +36,15 @@ async function checkEntry(t, entry, fixture) {

const all = Buffer.concat(chunks);

t.true(
all.toString("utf8").startsWith(fixture.startsWith),
"readStream"
);
t.true(all.toString("utf8").startsWith(fixture.startsWith), "readStream");
}
}

test("branch list entries", async t => {
const provider = GiteaProvider.initialize( {messageDestination}, process.env);
const provider = GiteaProvider.initialize(
{ messageDestination },
process.env
);
const branch = await provider.branch("markus/Omnia");

t.plan(Object.keys(entryFixtures).length + 2);
Expand All @@ -50,7 +59,10 @@ test("branch list entries", async t => {
});

test("branch list entries filtered", async t => {
const provider = GiteaProvider.initialize({messageDestination}, process.env);
const provider = GiteaProvider.initialize(
{ messageDestination },
process.env
);
const branch = await provider.branch("markus/Omnia");

t.plan(1);
Expand All @@ -61,28 +73,37 @@ test("branch list entries filtered", async t => {
});

test("branch entry master", async t => {
const provider = GiteaProvider.initialize({messageDestination}, process.env);
const provider = GiteaProvider.initialize(
{ messageDestination },
process.env
);
const branch = await provider.branch("markus/Omnia");

const entry = await branch.entry("Makefile");
await checkEntry(t, entry, entryFixtures.Makefile);
});

test.skip("branch entry none master", async t => {
const provider = GiteaProvider.initialize({messageDestination}, process.env);
test("branch entry none master", async t => {
const provider = GiteaProvider.initialize(
{ messageDestination },
process.env
);
const branch = await provider.branch(
"markus/sync-test-repository#pr-test/source-1"
);

const entry = await branch.entry("README.md");

t.is(entry.name,'README.md');
t.is(entry.name, "README.md");

await checkEntry(t, entry, { startsWith: "# pr-source-1" });
});

test("branch commmit", async t => {
const provider = GiteaProvider.initialize({messageDestination}, process.env);
const provider = GiteaProvider.initialize(
{ messageDestination },
process.env
);

await assertCommit(
t,
Expand Down

0 comments on commit d4b0640

Please sign in to comment.