From 13243a18fe07f0433ff730cdd8c530c9cf843f4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20=C5=A0pl=C3=ADchal?= Date: Thu, 3 Oct 2024 13:06:45 +0200 Subject: [PATCH] Support `user`, `org`, `repo` in the `github` plugin Implement filtering by `user`, `org` and `repo` keys to allow limiting the search to specific location. Add basic test coverage. --- did/plugins/github.py | 31 ++++++++++++++++++++++++--- plans/main.fmf | 18 ++++++++++++++++ plans/smoke.fmf | 8 ------- tests/docs/main.fmf | 1 + tests/github/default.ini | 7 ++++++ tests/github/main.fmf | 5 +++++ tests/github/org.ini | 10 +++++++++ tests/github/repo.ini | 10 +++++++++ tests/github/test.sh | 46 ++++++++++++++++++++++++++++++++++++++++ tests/github/user.ini | 10 +++++++++ tests/smoke/main.fmf | 1 + 11 files changed, 136 insertions(+), 11 deletions(-) create mode 100644 plans/main.fmf delete mode 100644 plans/smoke.fmf create mode 100644 tests/github/default.ini create mode 100644 tests/github/main.fmf create mode 100644 tests/github/org.ini create mode 100644 tests/github/repo.ini create mode 100755 tests/github/test.sh create mode 100644 tests/github/user.ini diff --git a/did/plugins/github.py b/did/plugins/github.py index 31621471..4d53a41e 100644 --- a/did/plugins/github.py +++ b/did/plugins/github.py @@ -10,6 +10,14 @@ token = login = +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 = + org = + repo = + 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. @@ -45,7 +53,7 @@ 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: @@ -53,12 +61,21 @@ def __init__(self, url, 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 @@ -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( diff --git a/plans/main.fmf b/plans/main.fmf new file mode 100644 index 00000000..61236586 --- /dev/null +++ b/plans/main.fmf @@ -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" diff --git a/plans/smoke.fmf b/plans/smoke.fmf deleted file mode 100644 index 0bd2f54f..00000000 --- a/plans/smoke.fmf +++ /dev/null @@ -1,8 +0,0 @@ -summary: - Basic smoke test -discover: - how: fmf -provision: - how: local -execute: - how: tmt diff --git a/tests/docs/main.fmf b/tests/docs/main.fmf index 2d4af28f..75600124 100644 --- a/tests/docs/main.fmf +++ b/tests/docs/main.fmf @@ -3,3 +3,4 @@ summary: description: Create a minimal config file. Check help message and man page. +tag: basic diff --git a/tests/github/default.ini b/tests/github/default.ini new file mode 100644 index 00000000..d433ccd5 --- /dev/null +++ b/tests/github/default.ini @@ -0,0 +1,7 @@ +[general] +email = "Petr Šplíchal" + +[gh] +type = github +url = https://api.github.com/ +login = psss diff --git a/tests/github/main.fmf b/tests/github/main.fmf new file mode 100644 index 00000000..760c85c9 --- /dev/null +++ b/tests/github/main.fmf @@ -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 diff --git a/tests/github/org.ini b/tests/github/org.ini new file mode 100644 index 00000000..adb29b03 --- /dev/null +++ b/tests/github/org.ini @@ -0,0 +1,10 @@ +[general] +email = "Petr Šplíchal" + +[gh] +type = github +url = https://api.github.com/ +login = psss + +# Limit to issues & pull request under the 'teemtee' organization +org = teemtee diff --git a/tests/github/repo.ini b/tests/github/repo.ini new file mode 100644 index 00000000..a762b35c --- /dev/null +++ b/tests/github/repo.ini @@ -0,0 +1,10 @@ +[general] +email = "Petr Šplíchal" + +[gh] +type = github +url = https://api.github.com/ +login = psss + +# Limit to issues & pull request under the 'teemtee/tmt' repository +repo = teemtee/fmf diff --git a/tests/github/test.sh b/tests/github/test.sh new file mode 100755 index 00000000..0665800b --- /dev/null +++ b/tests/github/test.sh @@ -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 diff --git a/tests/github/user.ini b/tests/github/user.ini new file mode 100644 index 00000000..efdbdd47 --- /dev/null +++ b/tests/github/user.ini @@ -0,0 +1,10 @@ +[general] +email = "Petr Šplíchal" + +[gh] +type = github +url = https://api.github.com/ +login = psss + +# Limit to issues & pull request under user 'psss' +user = psss diff --git a/tests/smoke/main.fmf b/tests/smoke/main.fmf index fadd74c1..4069541f 100644 --- a/tests/smoke/main.fmf +++ b/tests/smoke/main.fmf @@ -1 +1,2 @@ summary: Basic smoke test against github +tag: basic