1 | import fetch from "node-fetch";
|
2 | import { replaceWithOneTimeExecutionMethod } from "one-time-execution-method";
|
3 |
|
4 | import { Provider } from "repository-provider";
|
5 | import { GiteaRepository } from "./gitea-repository.mjs";
|
6 | import { GiteaPullRequest } from "./gitea-pull-request.mjs";
|
7 | import { GiteaOrganization } from "./gitea-organization.mjs";
|
8 | import { GiteaUser } from "./gitea-user.mjs";
|
9 | import { join } from "./util.mjs";
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | export class GiteaProvider extends Provider {
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
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 |
|
39 |
|
40 |
|
41 | static areOptionsSufficciant(options) {
|
42 | return options.token !== undefined && options.api !== undefined;
|
43 | }
|
44 |
|
45 | |
46 |
|
47 |
|
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 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
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 |
|
135 |
|
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 |
|
154 | replaceWithOneTimeExecutionMethod(
|
155 | GiteaProvider.prototype,
|
156 | "initializeRepositories"
|
157 | );
|