UNPKG

3.8 kBJavaScriptView Raw
1import fetch from "node-fetch";
2import { replaceWithOneTimeExecutionMethod } from "one-time-execution-method";
3
4import { Provider } from "repository-provider";
5import { GiteaRepository } from "./gitea-repository.mjs";
6import { GiteaPullRequest } from "./gitea-pull-request.mjs";
7import { GiteaOrganization } from "./gitea-organization.mjs";
8import { GiteaUser } from "./gitea-user.mjs";
9import { join } from "./util.mjs";
10
11/**
12 * Gitea provider
13 *
14 */
15export class GiteaProvider extends Provider {
16 /**
17 * known environment variables
18 * @return {Object}
19 * @return {string} GITEA_TOKEN api token
20 * @return {string} GITEA_API api url
21 */
22 static get environmentOptions() {
23 return {
24 GITEA_TOKEN: "token",
25 GITEA_API: "api"
26 };
27 }
28
29 static get defaultOptions() {
30 return {
31 api: undefined,
32 token: undefined,
33 ...super.defaultOptions
34 };
35 }
36
37 /**
38 * @param {Object} options
39 * @return {boolean} true if token an api are present
40 */
41 static areOptionsSufficciant(options) {
42 return options.token !== undefined && options.api !== undefined;
43 }
44
45 /**
46 * fetch headers
47 * @return {Object} suitable as fetch headers
48 */
49 get headers() {
50 return {
51 authorization: "token " + this.token
52 };
53 }
54
55 async initializeRepositories() {
56 for (let page = 1; ; page++) {
57 const result = await fetch(
58 join(this.api, `repos/search?limit=50&page=${page}`),
59 {
60 headers: this.headers,
61 accept: "application/json"
62 }
63 );
64
65 const json = await result.json();
66 if (json.data.length === 0) {
67 break;
68 }
69
70 const mapAttributesNames = {
71 archived: "isArchived",
72 template: "isTemplate",
73 private: "isPrivate",
74 homepage: "homePageURL",
75 default_branch: "defaultBranchName"
76 };
77
78 for (const r of json.data) {
79 const [gn, rn] = r.full_name.split(/\//);
80 const group = await this.addRepositoryGroup(gn, r.owner);
81 group.addRepository(
82 rn,
83 Object.fromEntries(
84 Object.entries(r).map(([name, value]) => [
85 mapAttributesNames[name] ? mapAttributesNames[name] : name,
86 value
87 ])
88 )
89 );
90 }
91 }
92 }
93
94 async addRepositoryGroup(name, options) {
95 let repositoryGroup = this._repositoryGroups.get(name);
96 if (repositoryGroup) {
97 return repositoryGroup;
98 }
99
100 /*
101 if(options && options.username) {
102 console.log(name, options);
103 repositoryGroup = new GiteaUser(this, name, options);
104 await repositoryGroup.initialize();
105 this._repositoryGroups.set(repositoryGroup.name, repositoryGroup);
106 return repositoryGroup;
107 }*/
108
109
110 const fetchOptions = {
111 headers: this.headers,
112 accept: "application/json"
113 };
114
115 let clazz;
116 let result = await fetch(join(this.api, "orgs", name), fetchOptions);
117
118 if (result.ok) {
119 clazz = GiteaOrganization;
120 } else {
121 clazz = GiteaUser;
122 result = await fetch(join(this.api, "users", name), fetchOptions);
123 }
124
125 const data = await result.json();
126
127 repositoryGroup = new clazz(this, name, data);
128 await repositoryGroup.initialize();
129 this._repositoryGroups.set(repositoryGroup.name, repositoryGroup);
130 return repositoryGroup;
131 }
132
133 /**
134 * All possible base urls
135 * @return {string[]} common base urls of all repositories
136 */
137 get repositoryBases() {
138 return [this.api.replace(/api\/v.+$/, "")];
139 }
140
141 get repositoryClass() {
142 return GiteaRepository;
143 }
144
145 get pullRequestClass() {
146 return GiteaPullRequest;
147 }
148
149 get repositoryGroupClass() {
150 return GiteaOrganization;
151 }
152}
153
154replaceWithOneTimeExecutionMethod(
155 GiteaProvider.prototype,
156 "initializeRepositories"
157);