1 | import fetch from "node-fetch";
|
2 | import {
|
3 | BufferContentEntryMixin,
|
4 | StreamContentEntryMixin,
|
5 | ContentEntry,
|
6 | BaseCollectionEntry
|
7 | } from "content-entry";
|
8 | import { Branch, match } from "repository-provider";
|
9 | import { join } from "./util.mjs";
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | export class GiteaBranch extends Branch {
|
16 | async *entries(patterns) {
|
17 | const url =
|
18 | join(
|
19 | this.provider.api,
|
20 | "repos",
|
21 | this.repository.fullName,
|
22 | "git/trees",
|
23 | await this.refId()
|
24 | ) + "?recursive=true";
|
25 |
|
26 | const result = await fetch(url, {
|
27 | headers: this.provider.headers,
|
28 | accept: "application/json"
|
29 | });
|
30 |
|
31 | const json = await result.json();
|
32 |
|
33 | for (const entry of match(json.tree, patterns, {
|
34 | getName: entry => entry.path
|
35 | })) {
|
36 | yield entry.type === "tree"
|
37 | ? new BaseCollectionEntry(entry.path)
|
38 | : new (this.name === "master"
|
39 | ? GiteaMasterOnlyContentEntry
|
40 | : GiteaContentEntry)(this, entry.path);
|
41 | }
|
42 | }
|
43 |
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | async commit(message, updates, options) {
|
52 | for (const u of updates) {
|
53 | const data = {
|
54 | message,
|
55 | branch: this.name,
|
56 | content: (await u.getBuffer()).toString("base64")
|
57 | };
|
58 |
|
59 | console.log(data);
|
60 | console.log(
|
61 | join(
|
62 | this.provider.api,
|
63 | "repos",
|
64 | this.repository.fullName,
|
65 | "contents",
|
66 | u.name
|
67 | )
|
68 | );
|
69 |
|
70 | const result = await fetch(
|
71 | join(
|
72 | this.provider.api,
|
73 | "repos",
|
74 | this.repository.fullName,
|
75 | "contents",
|
76 | u.name
|
77 | ),
|
78 | {
|
79 | method: "POST",
|
80 | headers: {
|
81 | "Content-Type": "application/json",
|
82 | ...this.provider.headers
|
83 | },
|
84 | body: JSON.stringify(data)
|
85 | }
|
86 | );
|
87 |
|
88 | console.log(result.ok, result.status, result.statusText);
|
89 | console.log(await result.text());
|
90 | }
|
91 | }
|
92 | }
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | class GiteaContentEntry extends BufferContentEntryMixin(ContentEntry) {
|
99 | constructor(branch, name) {
|
100 | super(name);
|
101 | Object.defineProperties(this, { branch: { value: branch } });
|
102 | }
|
103 |
|
104 | get provider() {
|
105 | return this.branch.provider;
|
106 | }
|
107 |
|
108 | async getBuffer() {
|
109 | const url = join(
|
110 | this.provider.api,
|
111 | "repos",
|
112 | this.branch.repository.fullName,
|
113 | "contents",
|
114 | this.name + "?ref=" + this.branch.name
|
115 | );
|
116 |
|
117 | const result = await fetch(url, {
|
118 | headers: this.provider.headers
|
119 | });
|
120 |
|
121 | const stream = await await result.body;
|
122 | const chunks = [];
|
123 | for await (const chunk of stream) {
|
124 | chunks.push(chunk);
|
125 | }
|
126 |
|
127 | const entry = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
128 | return Buffer.from(entry.content, "base64");
|
129 | }
|
130 | }
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | class GiteaMasterOnlyContentEntry extends StreamContentEntryMixin(
|
137 | ContentEntry
|
138 | ) {
|
139 | constructor(branch, name) {
|
140 | super(name);
|
141 | Object.defineProperties(this, { branch: { value: branch } });
|
142 | }
|
143 |
|
144 | get provider() {
|
145 | return this.branch.provider;
|
146 | }
|
147 |
|
148 | async getReadStream(options) {
|
149 | const url = join(
|
150 | this.provider.api,
|
151 | "repos",
|
152 | this.branch.repository.fullName,
|
153 | "raw",
|
154 | this.name
|
155 | );
|
156 |
|
157 | const result = await fetch(url, {
|
158 | headers: this.provider.headers
|
159 | });
|
160 |
|
161 | return await result.body;
|
162 | }
|
163 | }
|