UNPKG

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