1 | import fetch from "node-fetch";
|
2 | import { replaceWithOneTimeExecutionMethod } from "one-time-execution-method";
|
3 |
|
4 | import { Provider, mapAttributes } 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 | const 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 |
|
23 |
|
24 |
|
25 | export class GiteaProvider extends Provider {
|
26 | |
27 |
|
28 |
|
29 |
|
30 |
|
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 |
|
49 |
|
50 |
|
51 | static areOptionsSufficciant(options) {
|
52 | return options.token !== undefined && options.api !== undefined;
|
53 | }
|
54 |
|
55 | |
56 |
|
57 |
|
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 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
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 |
|
128 |
|
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 |
|
147 | replaceWithOneTimeExecutionMethod(
|
148 | GiteaProvider.prototype,
|
149 | "initializeRepositories"
|
150 | );
|
151 |
|
152 | export default GiteaProvider; |
\ | No newline at end of file |