Skip to content

Commit

Permalink
Support user, org, repo in the github plugin
Browse files Browse the repository at this point in the history
Implement filtering by `user`, `org` and `repo` keys to allow
limiting the search to specific location. Add basic test coverage.
  • Loading branch information
psss committed Oct 3, 2024
1 parent 017887f commit 13243a1
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 11 deletions.
31 changes: 28 additions & 3 deletions did/plugins/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
token = <authentication-token>
login = <username>
Optionally the search query can be limited to repositories owned
by the given user or organization. You can also use the full name
of the project to only search in the given repository::
user = <repository-owner>
org = <organization-name>
repo = <full-project-name>
The authentication token is optional. However, unauthenticated
queries are limited. For more details see `GitHub API`__ docs.
Use ``login`` to override the default email address for searching.
Expand Down Expand Up @@ -45,20 +53,29 @@
class GitHub(object):
""" GitHub Investigator """

def __init__(self, url, token):
def __init__(self, url, token=None, user=None, org=None, repo=None):
""" Initialize url and headers """
self.url = url.rstrip("/")
if token is not None:
self.headers = {'Authorization': 'token {0}'.format(token)}
else:
self.headers = {}

# Prepare the org, user, repo filter
self.filter = ""
if user:
self.filter += f"+user:{user}"
if org:
self.filter += f"+org:{org}"
if repo:
self.filter += f"+repo:{repo}"

self.token = token

def search(self, query):
""" Perform GitHub query """
result = []
url = self.url + "/" + query + f"&per_page={PER_PAGE}"
url = self.url + "/" + query + self.filter + f"&per_page={PER_PAGE}"

while True:
# Fetch the query
Expand Down Expand Up @@ -250,15 +267,23 @@ class GitHubStats(StatsGroup):
def __init__(self, option, name=None, parent=None, user=None):
StatsGroup.__init__(self, option, name, parent, user)
config = dict(Config().section(option))

# Check server url
try:
self.url = config["url"]
except KeyError:
raise ReportError(
"No github url set in the [{0}] section".format(option))

# Check authorization token
self.token = get_token(config)
self.github = GitHub(self.url, self.token)
self.github = GitHub(
url=self.url,
token=self.token,
org=config.get("org"),
user=config.get("user"),
repo=config.get("repo"))

# Create the list of stats
self.stats = [
IssuesCreated(
Expand Down
18 changes: 18 additions & 0 deletions plans/main.fmf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
discover:
how: fmf
provision:
how: local
execute:
how: tmt

/basic:
summary:
Basic functionality
discover+:
filter: "tag:basic"

/plugins:
summary:
Plugin features
discover+:
filter: "tag:plugin"
8 changes: 0 additions & 8 deletions plans/smoke.fmf

This file was deleted.

1 change: 1 addition & 0 deletions tests/docs/main.fmf
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ summary:
description:
Create a minimal config file.
Check help message and man page.
tag: basic
7 changes: 7 additions & 0 deletions tests/github/default.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[general]
email = "Petr Šplíchal" <[email protected]>

[gh]
type = github
url = https://api.github.com/
login = psss
5 changes: 5 additions & 0 deletions tests/github/main.fmf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
summary: Verify features of the github plugin
description:
For now just a set of basic checks for supported options.
Ensure that filtering per user, org and repo works.
tag: plugin
10 changes: 10 additions & 0 deletions tests/github/org.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[general]
email = "Petr Šplíchal" <[email protected]>

[gh]
type = github
url = https://api.github.com/
login = psss

# Limit to issues & pull request under the 'teemtee' organization
org = teemtee
10 changes: 10 additions & 0 deletions tests/github/repo.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[general]
email = "Petr Šplíchal" <[email protected]>

[gh]
type = github
url = https://api.github.com/
login = psss

# Limit to issues & pull request under the 'teemtee/tmt' repository
repo = teemtee/fmf
46 changes: 46 additions & 0 deletions tests/github/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash
. /usr/share/beakerlib/beakerlib.sh || exit 1

# The default search interval
INTERVAL_2024="--since 2024-03-03 --until 2024-09-09"
INTERVAL_2021="--since 2021-03-03 --until 2021-11-11"

rlJournalStart
rlPhaseStartTest "Help"
rlRun -s "did --config ./default.ini --help"
for action in created commented closed; do
for what in issues pull-requests; do
rlAssertGrep "--gh-$what-$action" $rlRun_LOG
done
done
rlPhaseEnd

rlPhaseStartTest "Issues Created"
rlRun -s "did --config ./default.ini --gh-issues-created $INTERVAL_2024"
rlAssertGrep "vim/vim#14964 - Support for syntax highlighting" $rlRun_LOG
rlAssertGrep "teemtee/tmt#3042 - Make the result of an empty plan" $rlRun_LOG
rlAssertGrep "rpm-software-management/ci-dnf-stack#1487" $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "Issues Created (org:teemtee)"
rlRun -s "did --config ./org.ini --gh-issues-created $INTERVAL_2024"
rlAssertNotGrep "vim/vim#14964 - Support for syntax highlighting" $rlRun_LOG
rlAssertGrep "teemtee/tmt#3042 - Make the result of an empty plan" $rlRun_LOG
rlAssertNotGrep "rpm-software-management/ci-dnf-stack#1487" $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "Issues Created (user:psss)"
rlRun -s "did --config ./user.ini --gh-issues-created $INTERVAL_2021"
rlAssertGrep "psss/did#247 - Implement pagination for the GitHub plugin" $rlRun_LOG
rlAssertNotGrep "packit/packit#1386 - Allow to disable web access" $rlRun_LOG
rlAssertNotGrep "teemtee/tmt#910 - Shall we introduce a uuid for tests?" $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "Issues Created (repo:teemtee/fmf)"
rlRun -s "did --config ./repo.ini --gh-issues-created $INTERVAL_2021"
rlAssertGrep "teemtee/fmf#141 - Drop support for Python 2" $rlRun_LOG
rlAssertNotGrep "psss/did#247 - Implement pagination for the GitHub plugin" $rlRun_LOG
rlAssertNotGrep "teemtee/tmt#926 - Copying reboot scripts" $rlRun_LOG
rlPhaseEnd

rlJournalEnd
10 changes: 10 additions & 0 deletions tests/github/user.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[general]
email = "Petr Šplíchal" <[email protected]>

[gh]
type = github
url = https://api.github.com/
login = psss

# Limit to issues & pull request under user 'psss'
user = psss
1 change: 1 addition & 0 deletions tests/smoke/main.fmf
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
summary: Basic smoke test against github
tag: basic

0 comments on commit 13243a1

Please sign in to comment.