UNPKG

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