Skip to content

Commit

Permalink
Merge pull request #100 from fedora-infra/python-gitlab
Browse files Browse the repository at this point in the history
Move all HTTP requests to be made with Python bindings for GitLab
  • Loading branch information
gridhead authored Feb 27, 2024
2 parents 04b1e5b + 8ca68b3 commit bf37277
Show file tree
Hide file tree
Showing 56 changed files with 9,006 additions and 6,479 deletions.
6 changes: 6 additions & 0 deletions pagure_exporter/conf/standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@
srcecloc = "UNAVAILABLE"
destcloc = "UNAVAILABLE"

# GitLab client object to be used while interacting with the destination namespace
gobj = None

# Project object of the destination namespace type of the GitLab client class
gpro = None

rateindx = 0

# Time in seconds to wait for when the rate limit for API requests is reached
Expand Down
1 change: 1 addition & 0 deletions pagure_exporter/work/keep.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ def keeptkts(status, tktgroup, comments, labels, commit, secret):
# Vote if the privacy associated with the issue tickets are to be moved
# Default False
standard.movehush = secret

25 changes: 18 additions & 7 deletions pagure_exporter/work/stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
"""



import requests
from gitlab import Gitlab as gtlb
from gitlab import GitlabAuthenticationError, GitlabGetError

from pagure_exporter.conf import standard
from pagure_exporter.view.dcrt import conceal
Expand Down Expand Up @@ -73,12 +76,18 @@ def __init__(self):
self.loca = standard.gtlblink
self.code = standard.gtlbcode
self.head = {"Authorization": "Bearer %s" % self.code}
standard.gobj = gtlb(
session=requests.Session(),
url="https://gitlab.com",
private_token=self.code,
retry_transient_errors=True,
timeout=standard.rqsttime,
)

def obtninfo(self):
rqstloca = f"{self.loca}/{self.repo}"
response = requests.get(rqstloca, headers=self.head, timeout=standard.rqsttime)
if response.status_code == 200:
jsondict = response.json()
try:
standard.gpro = standard.gobj.projects.get(id=standard.destname)
jsondict = standard.gpro.asdict()
standard.destdict = {
"makedate": jsondict["created_at"],
"lastmode": jsondict["last_activity_at"],
Expand All @@ -88,8 +97,8 @@ def obtninfo(self):
"identity": jsondict["id"],
"tagslist": jsondict["tag_list"],
"maintain": {
"username": jsondict["namespace"]["name"],
"fullname": jsondict["namespace"]["path"],
"username": jsondict["namespace"]["path"],
"fullname": jsondict["namespace"]["name"],
},
}
standard.desthuto = jsondict["http_url_to_repo"]
Expand All @@ -105,4 +114,6 @@ def obtninfo(self):
standard.frgedest,
standard.destdict["reponame"],
)
return response.status_code, response.reason
return 200, "OK"
except (GitlabAuthenticationError, GitlabGetError, Exception) as expt:
return 0, str(expt)
54 changes: 18 additions & 36 deletions pagure_exporter/work/tkts.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from datetime import datetime

import requests
from gitlab import GitlabCreateError, GitlabGetError, GitlabUpdateError

from pagure_exporter.conf import standard

Expand Down Expand Up @@ -128,7 +129,6 @@ def iteriden(self, tkid):
def itertkts(self, dictobjc):
try:
strttime = time.time()
standard.rateindx += 1
standard.issuname = dictobjc["title"]
standard.issuiden = dictobjc["id"]
standard.isclosed = True if dictobjc["status"] == "Closed" else False
Expand Down Expand Up @@ -167,30 +167,22 @@ def itertkts(self, dictobjc):
"""
rqstdata = {"title": headdata, "description": bodydata.replace("@", "&")}
if standard.movetags:
rqstdata["labels"] = ",".join(standard.issutags)
rqstdata["labels"] = standard.issutags
if standard.movehush:
rqstdata["confidential"] = standard.issecret
response = requests.post(
url=f"{self.gurl}/issues",
data=rqstdata,
headers=self.ghed,
timeout=standard.rqsttime,
)
respcode, respresn = response.status_code, response.reason
if respcode == 201:
respresn = response.json()["web_url"]
standard.gtlbtkid = response.json()["iid"]
standard.issutnfs += 1
rslt = standard.gpro.issues.create(data=rqstdata)
respcode, respresn = 201, rslt.web_url
standard.gtlbtkid = rslt.iid
standard.issutnfs += 1
stoptime = time.time()
timereqd = "%.2f" % (stoptime - strttime)
return respcode, respresn, timereqd
except Exception as expt:
except (GitlabCreateError, Exception) as expt:
return False, expt, "0"

def itercmts(self, dictobjc):
try:
strttime = time.time()
standard.rateindx += 1
standard.cmtsiden = dictobjc["id"]
standard.cmtslink = f"{standard.issulink}#comment-{standard.cmtsiden}"
standard.cmtsauth = dictobjc["user"]["fullname"]
Expand Down Expand Up @@ -219,39 +211,29 @@ def itercmts(self, dictobjc):
the problem
"""
rqstdata = {"body": bodydata.replace("@", "&")}
response = requests.post(
url=f"{self.gurl}/issues/{standard.gtlbtkid}/notes",
data=rqstdata,
headers=self.ghed,
timeout=standard.rqsttime,
rslt = standard.gpro.issues.get(id=standard.gtlbtkid).discussions.create(data=rqstdata)
respcode, respresn = (
201, f"{standard.destdict['repolink']}/-/issues/{standard.gtlbtkid}#note_{rslt.id}"
)
respcode, respresn = response.status_code, response.reason
if respcode == 201:
respresn = f"{standard.destdict['repolink']}/-/issues/{standard.gtlbtkid}#note_{response.json()['id']}" # noqa: E501
standard.cmtsqant += 1
standard.cmtsqant += 1
stoptime = time.time()
timereqd = "%.2f" % (stoptime - strttime)
return respcode, respresn, timereqd
except Exception as expt:
except (GitlabCreateError, Exception) as expt:
return False, expt, "0"

def iterstat(self):
try:
strttime, respcode, respresn = time.time(), 0, ""
standard.rateindx += 1
if standard.isclosed:
rqstdata = {"state_event": "close"}
response = requests.put(
url=f"{self.gurl}/issues/{standard.gtlbtkid}",
data=rqstdata,
headers=self.ghed,
timeout=standard.rqsttime,
)
respcode, respresn = response.status_code, response.reason
tkto = standard.gpro.issues.get(id=standard.gtlbtkid)
tkto.state_event = "close"
tkto.save()
respcode, respresn = 200, "0"
else:
respcode, respresn = 0, "0"
stoptime = time.time()
timereqd = "%.2f" % (stoptime - strttime)
return respcode, respresn, timereqd
except Exception as expt:
return False, expt, "0"
except (GitlabUpdateError, GitlabGetError, Exception) as expt:
return False, str(expt), "0"
Loading

0 comments on commit bf37277

Please sign in to comment.