Skip to content

Commit

Permalink
Add support for Gitlab
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Sep 7, 2023
1 parent cad5a05 commit c10aff1
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 29 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

Show bug and issue status

Supported:
- bsc#: SUSE's Bugzilla
- gh#: Github
- gl#: Gitlab
- gsd#: SUSE's Gitlab
- poo#: openSUSE's Redmine

## Example usage

Copy [creds-example.json](creds-example.json) to `~/creds.json` and run.
Expand Down
105 changes: 76 additions & 29 deletions bugme.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from pytz import utc

from github import Github, Auth, GithubException
from gitlab import Gitlab
from gitlab.exceptions import GitlabError
from bugzilla import Bugzilla
from bugzilla.exceptions import BugzillaError
from redminelib import Redmine
Expand All @@ -28,7 +30,68 @@ def dateit(date, time_format: str = "%a %b %d %H:%M:%S %Z %Y") -> str:
return date.astimezone().strftime(time_format)


class GithubIssue: # pylint: disable=too-few-public-methods
def do_bugzilla(url: str, bugs: list, creds):
"""
Bugzilla
"""
if len(bugs) == 0:
return
try:
mybugz = Bugzilla(url, force_rest=True, **creds)
for bug in mybugz.getbugs(bugs):
print(f"bsc#{bug.id}\t{bug.status}\t\t{dateit(bug.last_change_time)}\t{bug.summary}")
mybugz.disconnect()
except BugzillaError as exc:
print(f"Bugzilla: {exc}")


def do_github(issues: list, creds: dict):
"""
Github
"""
if len(issues) == 0:
return
auth = Auth.Token(**creds)
mygh = Github(auth=auth)
for issue in issues:
try:
info = mygh.get_repo(issue.repo).get_issue(issue.number)
print(f"gh#{info.number}\t{info.state}\t\t{dateit(info.last_modified)}\t{info.title}")
except GithubException as exc:
print(f"gh#{issue.repo}#{issue.number}: {exc}", file=sys.stderr)


def do_gitlab(url: str, issues: list, creds: dict):
"""
Gitlab
"""
if len(issues) == 0:
return
with Gitlab(url=url, **creds) as mygl:
for issue in issues:
try:
info = mygl.projects.get(issue.repo).issues.get(issue.number)
print(f"gl#{info.iid}\t{info.state}\t\t{dateit(info.updated_at)}\t{info.title}")
except GitlabError as exc:
print(f"gl#{issue.repo}#{issue.number}: {exc}", file=sys.stderr)


def do_redmine(url: str, tickets: list, creds: dict):
"""
Redmine
"""
if len(tickets) == 0:
return
redmine = Redmine(url=url, **creds)
for ticket in tickets:
try:
info = redmine.issue.get(ticket)
print(f"poo#{info.id}\t{info.status}\t{dateit(info.updated_on)}\t{info.subject}")
except BaseRedmineError as exc:
print(f"poo#{ticket}: {exc}", file=sys.stderr)


class RepoIssue: # pylint: disable=too-few-public-methods
"""
Simple class to hold GitHub issue
"""
Expand All @@ -45,14 +108,20 @@ def main():
bsc_list = []
poo_list = []
gh_list = []
gl_list = []
gsd_list = []

for arg in sys.argv[1:]:
if arg.startswith(("bnc#", "boo#", "bsc#")):
bsc_list.append(int(arg.split("#", 1)[1]))
elif arg.startswith("poo#"):
poo_list.append(int(arg.split("#", 1)[1]))
elif arg.startswith("gh#"):
gh_list.append(GithubIssue(*arg.split("#", 2)[1:]))
gh_list.append(RepoIssue(*arg.split("#", 2)[1:]))
elif arg.startswith("gl#"):
gl_list.append(RepoIssue(*arg.split("#", 2)[1:]))
elif arg.startswith("gsd#"):
gsd_list.append(RepoIssue(*arg.split("#", 2)[1:]))
else:
print(f"Unsupported {arg}", file=sys.stderr)

Expand All @@ -61,33 +130,11 @@ def main():
sys.exit(f"ERROR: {CREDENTIALS_FILE} has insecure permissions")
creds = json.load(file)

# Bugzilla
try:
mybsc = Bugzilla("https://bugzilla.suse.com", force_rest=True, **creds["bugzilla.suse.com"])
for bsc in mybsc.getbugs(bsc_list):
print(f"bsc#{bsc.id}\t{bsc.status}\t\t{dateit(bsc.last_change_time)}\t{bsc.summary}")
mybsc.disconnect()
except BugzillaError as exc:
print(f"Bugzilla: {exc}")

# Github
auth = Auth.Token(**creds["github.com"])
mygh = Github(auth=auth)
for issue in gh_list:
try:
info = mygh.get_repo(issue.repo).get_issue(issue.number)
print(f"gh#{info.number}\t{info.state}\t\t{dateit(info.last_modified)}\t{info.title}")
except GithubException as exc:
print(f"gh#{issue.repo}#{issue.number}: {exc}", file=sys.stderr)

# Redmine
redmine = Redmine(url="https://progress.opensuse.org", **creds["progress.opensuse.org"])
for poo in poo_list:
try:
info = redmine.issue.get(poo)
print(f"poo#{info.id}\t{info.status}\t{dateit(info.updated_on)}\t{info.subject}")
except BaseRedmineError as exc:
print(f"poo#{poo}: {exc}", file=sys.stderr)
do_bugzilla("https://bugzilla.suse.com", bsc_list, creds["bugzilla.suse.com"])
do_github(gh_list, creds["github.com"])
do_gitlab(None, gl_list, creds["gitlab.com"])
do_gitlab("https://gitlab.suse.de", gsd_list, creds["gitlab.suse.de"])
do_redmine("https://progress.opensuse.org", poo_list, creds["progress.opensuse.org"])


if __name__ == "__main__":
Expand Down
6 changes: 6 additions & 0 deletions creds-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
"username": "rbranco",
"key": "REDMINE_API_KEY"
},
"gitlab.suse.de": {
"private_token": "GITLAB_API_KEY"
},
"gitlab.com": {
"private_token": "GITLAB_API_KEY"
},
"github.com": {
"token": "GITHUB_TOKEN"
}
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ python-bugzilla
python-redmine
python-dateutil
pytz
python-gitlab
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ PyJWT==2.8.0
PyNaCl==1.5.0
python-bugzilla==3.2.0
python-dateutil==2.8.2
python-gitlab==3.15.0
python-redmine==2.4.0
pytz==2023.3.post1
requests==2.31.0
requests-toolbelt==1.0.0
six==1.16.0
urllib3==2.0.4
wrapt==1.15.0

0 comments on commit c10aff1

Please sign in to comment.