UNPKG

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