1
0
mirror of https://github.com/Lunik/gitea_prometheus_exporter.git synced 2025-04-18 11:04:02 +03:00

remove external library

Signed-off-by: Lunik <lunik@tiwabbit.fr>
This commit is contained in:
Lunik 2021-03-29 19:58:07 +02:00
parent d2e70d2f7c
commit 9c4d222765
No known key found for this signature in database
GPG Key ID: AEC152C8AC2C85EE
9 changed files with 193 additions and 15 deletions

View File

@ -48,6 +48,7 @@ def module(app):
GAUGES['repo_branches']._metrics.clear()
GAUGES['repo_forks']._metrics.clear()
GAUGES['repo_issues']._metrics.clear()
for repo in gitea_metrics['repos']:
GAUGES['repo'].labels(name=repo.name, owner=repo.owner.username).set(1)
GAUGES['repo_size'].labels(name=repo.name, owner=repo.owner.username, unit="bytes").set(repo.size)

View File

@ -1,22 +1,15 @@
from gitea import *
def get_repo_commit_count(gitea, repo):
page_endpoint = Repository.REPO_COMMITS % (repo.owner.username, repo.name)
response = gitea.requests.get(gitea.url + "/api/v1" + page_endpoint + "?limit=1", headers=gitea.headers, params={})
if 'X-Total' in response.headers:
return int(response.headers.get('X-Total'))
else:
return 0
from lib.gitea import Gitea
def gitea_export(app):
metrics = dict()
print("========== Scraping Start ==========")
try:
gitea = Gitea(app.config['MODULE_CONFIG']['url'], app.config['MODULE_CONFIG']['auth']['token'])
gitea.get_version()
print(gitea.get_version())
except Exception as e:
print(e)
print("[WARNING] Unable to initiate connection with gitea.")
return metrics
@ -34,7 +27,6 @@ def gitea_export(app):
print("[WARNING] Unable to retrieve 'Gitea Users'.")
metrics['orgs'] = dict()
metrics['repos'] = []
try:
@ -54,7 +46,7 @@ def gitea_export(app):
try:
for repo in metrics['repos']:
repo.branches_count = len(repo.get_branches()) if not repo.empty else 0
repo.commits_count = get_repo_commit_count(gitea, repo) if not repo.empty else 0
repo.commits_count = repo.get_commit_count() if not repo.empty else 0
except Exception as e:
print(e)
print("[WARNING] Unable to retrieve 'Gitea repos git infos'.")

25
lib/gitea/Gitea.py Normal file
View File

@ -0,0 +1,25 @@
import requests
from types import SimpleNamespace
from .GiteaAPI import GiteaAPI
from .GiteaUser import GiteaUser
from .GiteaOrganization import GiteaOrganization
class GiteaVersion(SimpleNamespace):
pass
class Gitea:
def __init__(self, url, token):
self.api = GiteaAPI(url, token)
def get_version(self):
return GiteaVersion(**self.api.requests_get("version"))
def get_users(self):
res = self.api.requests_get("admin/users")
return [GiteaUser(self.api, **u) for u in res]
def get_orgs(self):
res = self.api.requests_get("admin/orgs")
return [GiteaOrganization(self.api, **o) for o in res]

65
lib/gitea/GiteaAPI.py Normal file
View File

@ -0,0 +1,65 @@
import requests
import json
class GiteaAPI:
def __init__(self, base_url, token, api_version="v1"):
self.base_url = "{url}/api/{version}".format(url=base_url, version=api_version)
self.headers = {
"Authorization": "token {}".format(token),
"Content-type": "application/json",
}
self.requests = requests.Session()
@staticmethod
def parse_response(result):
if result.text and len(result.text) > 3:
return json.loads(result.text)
return {}
def _get_url(self, endpoint):
return "{url}/{endpoint}".format(url=self.base_url, endpoint=endpoint)
def requests_get(self, endpoint, params={}, raw=False):
params = params.copy()
response = self.requests.get(self._get_url(endpoint), headers=self.headers, params=params)
print(response.url)
if raw:
return response
if response.status_code == 204:
return None
if response.status_code not in [200, 201]:
message = "Received status code: %s (%s)" % (
response.status_code,
response.url,
)
if response.status_code in [404]:
raise Exception(message)
if response.status_code in [403]:
raise Exception(
"Unauthorized: %s - Check your permissions and try again! (%s)"
% (response.url, message)
)
if response.status_code in [409]:
raise Exception(message)
raise Exception(message)
result = self.parse_response(response)
if "X-Total-Count" in response.headers and len(result) > 0:
if "page" in params:
params['page'] += 1
else:
params['page'] = 2
result += self.requests_get(endpoint, params=params)
return result

View File

@ -0,0 +1,26 @@
from types import SimpleNamespace
from .GiteaRepository import GiteaRepository
class GiteaOrganization(SimpleNamespace):
def __init__(self, api, **args):
self.api = api
self.base_endpoint = "orgs"
SimpleNamespace.__init__(self, **args)
def get_repositories(self):
res = self.api.requests_get("{base_endpoint}/{name}/repos".format(
base_endpoint=self.base_endpoint,
name=self.username)
)
repos = []
for r in res:
repo = GiteaRepository(self.api, **r)
repo.owner = self
repos.append(repo)
return repos

View File

@ -0,0 +1,40 @@
from types import SimpleNamespace
def get_repo_commit_count(gitea, repo):
page_endpoint = Repository.REPO_COMMITS % (repo.owner.username, repo.name)
response = gitea.requests.get(gitea.url + "/api/v1" + page_endpoint + "?limit=1", headers=gitea.headers, params={})
if 'X-Total' in response.headers:
return int(response.headers.get('X-Total'))
else:
return 0
class GiteaRepository(SimpleNamespace):
def __init__(self, api, **args):
self.api = api
self.base_endpoint = "repos"
SimpleNamespace.__init__(self, **args)
def get_branches(self):
res = self.api.requests_get("{base_endpoint}/{owner}/{name}/branches".format(
base_endpoint=self.base_endpoint,
name=self.name,
owner=self.owner.username)
)
return res
def get_commit_count(self):
res = self.api.requests_get("{base_endpoint}/{owner}/{name}/commits".format(
base_endpoint=self.base_endpoint,
name=self.name,
owner=self.owner.username),
params=dict(limit=1),
raw=True
)
if 'X-Total' in res.headers:
return int(res.headers.get('X-Total'))
else:
return 0

25
lib/gitea/GiteaUser.py Normal file
View File

@ -0,0 +1,25 @@
from types import SimpleNamespace
from .GiteaRepository import GiteaRepository
class GiteaUser(SimpleNamespace):
def __init__(self, api, **args):
self.api = api
self.base_endpoint = "users"
SimpleNamespace.__init__(self, **args)
def get_repositories(self):
res = self.api.requests_get("{base_endpoint}/{username}/repos".format(
base_endpoint=self.base_endpoint,
username=self.username)
)
repos = []
for r in res:
repo = GiteaRepository(self.api, **r)
repo.owner = self
repos.append(repo)
return repos

4
lib/gitea/__init__.py Normal file
View File

@ -0,0 +1,4 @@
from .Gitea import Gitea
from .GiteaOrganization import GiteaOrganization
from .GiteaRepository import GiteaRepository
from .GiteaUser import GiteaUser

View File

@ -1,3 +1,3 @@
prometheus_client
PyYaml
py-gitea
requests