Skip to content

Commit 4286deb

Browse files
committed
add gitea connect supporrt
1 parent 0907056 commit 4286deb

File tree

8 files changed

+312
-21
lines changed

8 files changed

+312
-21
lines changed

.env.template

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ GIT_DEPLOYMENTKEY_PRIVATE_B64=
66
# Github webhook configuration
77
GITHUB_WEBHOOK_URL=https://
88
GITHUB_WEBHOOK_SECRET=mysecret
9+
GITHUB_PERSONAL_ACCESS_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
910

1011
GITEA_WEBHOOK_SECRET=mysecret
11-
GITEA_WEBHOOK_URL=http://localhost:3000/api/gitea/webhooks
12-
GITEA_PERSONAL_ACCESS_TOKEN=
13-
GITEA_URL_API=https://localhost:3000/api/v1/
12+
GITEA_WEBHOOK_URL=http://localhost:2000/api/gitea/webhooks
13+
GITEA_PERSONAL_ACCESS_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
14+
GITEA_BASEURL=http://localhost:3000
1415

1516
KUBECONFIG_PATH=./kubeconfig
1617
KUBERO_CONFIG_PATH=./config.yaml

client/src/components/pipelines/new.vue

+49-7
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
<v-tabs icons-and-text v-model="repotab" color="#8560A9">
3636
<v-tab href="#tab-github">Github <v-icon>mdi-github</v-icon> </v-tab>
3737
<v-tab href="#tab-gitea">Gitea <v-icon class="gitea"></v-icon></v-tab>
38-
<v-tab href="#tab-gitlab">Gitlab <v-icon>mdi-gitlab</v-icon></v-tab>
38+
<v-tab href="#tab-gitlab" disabled>Gitlab <v-icon>mdi-gitlab</v-icon></v-tab>
3939
<v-tab href="#tab-bitbucket" disabled>Bitbucket <v-icon>mdi-bitbucket</v-icon></v-tab>
4040
</v-tabs>
41-
41+
<!--
4242
<v-tabs-items v-model="repotab">
4343
<v-tab-item
4444
key="1"
@@ -162,11 +162,11 @@
162162
</v-tabs-items>
163163
164164
165+
-->
165166

166167
</v-col>
167168
</v-row>
168169

169-
<!--
170170
<v-row>
171171
<v-col
172172
cols="12"
@@ -192,12 +192,11 @@
192192
<v-btn
193193
color="primary"
194194
elevation="2"
195-
@click="connectGithub()"
195+
@click="connectRepo()"
196196
>Connect</v-btn>
197197
</v-col>
198198
</v-row>
199199

200-
201200
<v-row
202201
v-if="gitrepo_connected"
203202
>
@@ -219,7 +218,9 @@
219218
</v-alert>
220219
</v-col>
221220
</v-row>
222-
-->
221+
222+
223+
223224
<v-row v-for="phase in phases" :key="phase.name">
224225
<v-col
225226
cols="12"
@@ -323,8 +324,43 @@ export default {
323324
}
324325
});
325326
},
327+
connectRepo() {
328+
this.gitrepo_connected = true;
329+
console.log(this.gitrepo);
330+
console.log(this.repotab);
331+
switch (this.repotab) {
332+
case 'tab-github':
333+
this.connectGithub();
334+
break;
335+
case 'tab-gitea':
336+
this.connectGitea();
337+
break;
338+
case 'tab-gitlab':
339+
this.connectGitlab();
340+
break;
341+
case 'tab-bitbucket':
342+
this.connectBitbucket();
343+
break;
344+
default:
345+
break;
346+
}
347+
},
326348
connectGithub() {
327-
axios.post('/api/github/connect', {
349+
axios.post('/api/repo/github/connect', {
350+
gitrepo: this.gitrepo
351+
}).then(response => {
352+
//TODO check if connectiondata is valid
353+
this.github.repository = response.data.repository.data;
354+
this.github.webhook = response.data.webhook.data;
355+
356+
this.gitrepo_connected = true;
357+
}).catch(error => {
358+
console.log(error);
359+
//TODO show error message
360+
});
361+
},
362+
connectGitea() {
363+
axios.post('/api/repo/gitea/connect', {
328364
gitrepo: this.gitrepo
329365
}).then(response => {
330366
//TODO check if connectiondata is valid
@@ -337,6 +373,12 @@ export default {
337373
//TODO show error message
338374
});
339375
},
376+
connectGitlab() {
377+
alert('Gitlab not supported yet');
378+
},
379+
connectBitbucket() {
380+
alert('Bitbucket not supported yet');
381+
},
340382
saveForm() {
341383
axios.post(`/api/pipelines`, {
342384
pipelineName: this.pipelineName,

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
"axios": "^0.27.2",
1919
"connect-history-api-fallback": "^1.6.0",
2020
"cors": "^2.8.5",
21+
"cross-fetch": "^3.1.5",
2122
"debug": "^4.3.4",
2223
"dotenv": "^16.0.1",
2324
"express": "^4.18.1",
25+
"gitea-js": "^1.2.0",
2426
"kubernetes-client": "^9.0.0",
2527
"lodash.get": "^4.4.2",
2628
"lodash.set": "^4.3.2",

src/git/gitea.ts

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
import { debug } from "debug";
2+
debug('app:kubero:gitea:api')
3+
4+
//https://www.npmjs.com/package/gitea-js
5+
import { giteaApi } from "gitea-js"
6+
import { fetch as fetchGitea } from 'cross-fetch';
7+
8+
export class GiteaApi {
9+
private gitea: any;
10+
11+
constructor(baseURL: string, token: string) {
12+
this.gitea = giteaApi(baseURL, {
13+
token: token,
14+
customFetch: fetchGitea,
15+
});
16+
}
17+
18+
public async getRepository(gitrepo: string) {
19+
let ret = {
20+
status: 500,
21+
statusText: 'error',
22+
data: {
23+
id: 0,
24+
node_id: "",
25+
name: "",
26+
description: "",
27+
owner: "",
28+
private : false,
29+
ssh_url: "",
30+
}
31+
}
32+
// TODO : Improve matching here
33+
let owner = gitrepo.match(/^git@.*:(.*)\/.*$/)?.[1] as string;
34+
let repo = gitrepo.match(/^git@.*:.*\/(.*)\.git$/)?.[1] as string;
35+
36+
let res = await this.gitea.repos.repoGet(owner, repo)
37+
38+
ret = {
39+
status: res.status,
40+
statusText: 'found',
41+
data: {
42+
id: res.data.id,
43+
node_id: res.data.node_id,
44+
name: res.data.name,
45+
description: res.data.description,
46+
owner: res.data.owner.login,
47+
private : res.data.private,
48+
ssh_url: res.data.ssh_url,
49+
}
50+
}
51+
return ret;
52+
53+
}
54+
55+
public async addWebhook(owner: string, repo: string, url: string, secret: string) {
56+
57+
let ret = {
58+
status: 500,
59+
statusText: 'error',
60+
data: {
61+
id: 0,
62+
active: false,
63+
created_at: '2020-01-01T00:00:00Z',
64+
url: '',
65+
insecure: true,
66+
events: [],
67+
}
68+
}
69+
70+
//https://try.gitea.io/api/swagger#/repository/repoListHooks
71+
const webhooksList = await this.gitea.repos.repoListHooks(owner, repo)
72+
.catch((error: any) => {
73+
console.log(error)
74+
})
75+
76+
// try to find the webhook
77+
for (let webhook of webhooksList.data) {
78+
if (webhook.config.url === url &&
79+
webhook.config.content_type === 'json' &&
80+
webhook.active === true) {
81+
ret = {
82+
status: 422,
83+
statusText: 'found',
84+
data: webhook,
85+
}
86+
return ret;
87+
}
88+
}
89+
//console.log(webhooksList)
90+
91+
// create the webhook since it does not exist
92+
try {
93+
94+
//https://try.gitea.io/api/swagger#/repository/repoCreateHook
95+
let res = await this.gitea.repos.repoCreateHook(owner, repo, {
96+
active: true,
97+
config: {
98+
url: url,
99+
content_type: "json",
100+
secret: secret,
101+
insecure_ssl: '0'
102+
},
103+
events: [
104+
"push",
105+
"pull_request"
106+
],
107+
type: "gitea"
108+
});
109+
110+
ret = {
111+
status: res.status,
112+
statusText: 'created',
113+
data: {
114+
id: res.data.id,
115+
active: res.data.active,
116+
created_at: res.data.created_at,
117+
url: res.data.url,
118+
insecure: res.data.config.insecure_ssl,
119+
events: res.data.events,
120+
}
121+
}
122+
} catch (e) {
123+
console.log(e)
124+
}
125+
return ret;
126+
}
127+
128+
129+
public async addDeployKey(owner: string, repo: string, key: string) {
130+
131+
const title: string = "bot@kubero";
132+
let ret = {
133+
status: 500,
134+
statusText: 'error',
135+
data: {
136+
id: 0,
137+
title: title,
138+
verified: false,
139+
created_at: '2020-01-01T00:00:00Z',
140+
url: '',
141+
read_only: true,
142+
}
143+
}
144+
//https://try.gitea.io/api/swagger#/repository/repoListKeys
145+
const keysList = await this.gitea.repos.repoListKeys(owner, repo)
146+
.catch((error: any) => {
147+
console.log(error)
148+
})
149+
150+
// try to find the key
151+
for (let key of keysList.data) {
152+
if (key.title === title &&
153+
key.read_only === true) {
154+
ret = {
155+
status: 422,
156+
statusText: 'found',
157+
data: key,
158+
}
159+
return ret;
160+
}
161+
}
162+
163+
try {
164+
//https://try.gitea.io/api/swagger#/repository/repoCreateKey
165+
let res = await this.gitea.repos.repoCreateKey(owner, repo, {
166+
title: title,
167+
key: key,
168+
read_only: true
169+
});
170+
171+
ret = {
172+
status: res.status,
173+
statusText: 'created',
174+
data: {
175+
id: res.data.id,
176+
title: res.data.title,
177+
verified: res.data.verified,
178+
created_at: res.data.created_at,
179+
url: res.data.url,
180+
read_only: res.data.read_only,
181+
}
182+
}
183+
} catch (e) {
184+
console.log(e)
185+
}
186+
187+
return ret
188+
}
189+
}

src/github/api.ts src/git/github.ts

-4
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,5 @@ export class GithubApi {
214214
}
215215

216216
return ret
217-
218-
219-
220-
221217
}
222218
}

0 commit comments

Comments
 (0)