Skip to content

Commit

Permalink
Support user, org, repo in the github plugin (#373)
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.

Fix #86.
  • Loading branch information
psss authored Oct 4, 2024
1 parent 384cccb commit 5b266fa
Show file tree
Hide file tree
Showing 18 changed files with 323 additions and 12 deletions.
39 changes: 35 additions & 4 deletions did/plugins/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@
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>
Multiple users, organization or repositories can be searched as
well. Use ``,`` as the separator, for example::
org = one,two,three
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 +58,30 @@
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 = {}

self.token = token
# Prepare the org, user, repo filter
def condition(key: str, names: str) -> list[str]:
""" Prepare one or more conditions for given key & names """
if not names:
return []
return [f"+{key}:{name}" for name in re.split(r"\s*,\s*", names)]

self.filter = "".join(
condition("user", user) +
condition("org", org) +
condition("repo", repo))

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 @@ -255,15 +278,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: "tier:0"

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

This file was deleted.

1 change: 1 addition & 0 deletions tests/docs/main.fmf → tests/basic/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
File renamed without changes.
1 change: 1 addition & 0 deletions tests/basic/main.fmf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tier: 0
1 change: 1 addition & 0 deletions tests/smoke/main.fmf → tests/basic/smoke/main.fmf
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
summary: Basic smoke test against github
tag: basic
File renamed without changes.
7 changes: 7 additions & 0 deletions tests/github/config-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
10 changes: 10 additions & 0 deletions tests/github/config-more.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 multiple organizations
org = teemtee,packit
10 changes: 10 additions & 0 deletions tests/github/config-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/config-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/fmf' repository
repo = teemtee/fmf
10 changes: 10 additions & 0 deletions tests/github/config-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
14 changes: 14 additions & 0 deletions tests/github/help.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
. /usr/share/beakerlib/beakerlib.sh || exit 1

rlJournalStart
rlPhaseStartTest "Help"
rlRun -s "did --config ./config-default.ini --help"
rlAssertGrep "GitHub work" $rlRun_LOG
for what in issues pull-requests; do
for action in created commented closed; do
rlAssertGrep "--gh-$what-$action" $rlRun_LOG
done
done
rlPhaseEnd
rlJournalEnd
86 changes: 86 additions & 0 deletions tests/github/issues.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/bin/bash
. /usr/share/beakerlib/beakerlib.sh || exit 1

YEAR_2021="--since 2021-01-01 --until 2021-12-31"
YEAR_2022="--since 2022-01-01 --until 2022-12-31"
YEAR_2023="--since 2023-01-01 --until 2023-12-31"

rlJournalStart

# Issues Created

rlPhaseStartTest "Issues Created"
rlRun -s "did --config ./config-default.ini --gh-issues-created $YEAR_2022"
rlAssertGrep "Issues created on gh: 31$" $rlRun_LOG
rlAssertGrep "teemtee/tmt#1737 - Introduce a new step for cleanup tasks" $rlRun_LOG
rlAssertGrep "teemtee/fmf#149 - Checkout of the default branch fails" $rlRun_LOG
rlAssertGrep "packit/packit-service#1645 - Manually trigger internal jobs" $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "Issues Created (org:teemtee)"
rlRun -s "did --config ./config-org.ini --gh-issues-created $YEAR_2022"
rlAssertGrep "Issues created on gh: 30$" $rlRun_LOG
rlAssertGrep "teemtee/tmt#1737 - Introduce a new step for cleanup tasks" $rlRun_LOG
rlAssertGrep "teemtee/fmf#149 - Checkout of the default branch fails" $rlRun_LOG
rlAssertNotGrep "packit/packit-service#1645 - Manually trigger internal jobs" $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "Issues Created (org:teemtee,packit)"
rlRun -s "did --config ./config-more.ini --gh-issues-created $YEAR_2023"
rlAssertGrep "Issues created on gh: 33$" $rlRun_LOG
rlAssertGrep "teemtee/tmt#2493 - Implement retry functionality" $rlRun_LOG
rlAssertGrep "packit/packit#1989 - Mention branch name" $rlRun_LOG
rlAssertNotGrep "readthedocs/sphinx_rtd_theme#1525 - Left menu" $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "Issues Created (repo:teemtee/fmf)"
rlRun -s "did --config ./config-repo.ini --gh-issues-created $YEAR_2022"
rlAssertGrep "Issues created on gh: 2$" $rlRun_LOG
rlAssertNotGrep "teemtee/tmt#1737 - Introduce a new step for cleanup tasks" $rlRun_LOG
rlAssertGrep "teemtee/fmf#149 - Checkout of the default branch fails" $rlRun_LOG
rlAssertNotGrep "packit/packit-service#1645 - Manually trigger internal jobs" $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "Issues Created (user:psss)"
rlRun -s "did --config ./config-user.ini --gh-issues-created $YEAR_2021"
rlAssertGrep "Issues created on gh: 1$" $rlRun_LOG
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

# Issues Closed

rlPhaseStartTest "Issues Closed"
rlRun -s "did --config ./config-default.ini --gh-issues-closed $YEAR_2022"
rlAssertGrep "Issues closed on gh: 17$" $rlRun_LOG
rlAssertGrep "teemtee/fmf#014 - Define a way how to undefine an attribute" $rlRun_LOG
rlAssertGrep "teemtee/tmt#991 - Incompatible environment variable name" $rlRun_LOG
rlAssertGrep "psss/did#269 - Invalid plugin type 'google'" $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "Issues Closed (org:teemtee)"
rlRun -s "did --config ./config-org.ini --gh-issues-closed $YEAR_2022"
rlAssertGrep "Issues closed on gh: 15$" $rlRun_LOG
rlAssertGrep "teemtee/fmf#014 - Define a way how to undefine an attribute" $rlRun_LOG
rlAssertGrep "teemtee/tmt#991 - Incompatible environment variable name" $rlRun_LOG
rlAssertNotGrep "psss/did#269 - Invalid plugin type 'google'" $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "Issues Closed (repo:teemtee/fmf)"
rlRun -s "did --config ./config-repo.ini --gh-issues-closed $YEAR_2022"
rlAssertGrep "Issues closed on gh: 3$" $rlRun_LOG
rlAssertGrep "teemtee/fmf#014 - Define a way how to undefine an attribute" $rlRun_LOG
rlAssertNotGrep "teemtee/tmt#991 - Incompatible environment variable name" $rlRun_LOG
rlAssertNotGrep "psss/did#269 - Invalid plugin type 'google'" $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "Issues Closed (user:psss)"
rlRun -s "did --config ./config-user.ini --gh-issues-closed $YEAR_2022"
rlAssertGrep "Issues closed on gh: 1$" $rlRun_LOG
rlAssertNotGrep "teemtee/fmf#014 - Define a way how to undefine an attribute" $rlRun_LOG
rlAssertNotGrep "teemtee/tmt#991 - Incompatible environment variable name" $rlRun_LOG
rlAssertGrep "psss/did#269 - Invalid plugin type 'google'" $rlRun_LOG
rlPhaseEnd

rlJournalEnd
11 changes: 11 additions & 0 deletions tests/github/main.fmf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/help:
summary: Check the help message and option sanity
test: ./help.sh

/pulls:
summary: Verify pull request stats
test: ./pulls.sh

/issues:
summary: Verify issue stats
test: ./issues.sh
108 changes: 108 additions & 0 deletions tests/github/pulls.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/bin/bash
. /usr/share/beakerlib/beakerlib.sh || exit 1

YEAR_2022="--since 2022-01-01 --until 2022-12-31"
YEAR_2021="--since 2021-01-01 --until 2021-12-31"

rlJournalStart

# Pull Requests Created

rlPhaseStartTest "Pull Requests Created"
rlRun -s "did --config ./config-default.ini --gh-pull-requests-created $YEAR_2022"
rlAssertGrep "Pull requests created on gh: 94$" $rlRun_LOG
rlAssertGrep "teemtee/tmt#1750 - Include the new web link in verbose" $rlRun_LOG
rlAssertGrep "teemtee/fmf#170 - Implement a directive for disabling inheritance" $rlRun_LOG
rlAssertGrep "teemtee/try#002 - Check logs for test with a hash sign in" $rlRun_LOG
rlAssertGrep "psss/did#275 - Speed up local testing" $rlRun_LOG
rlAssertGrep "psss/python-nitrate#039 - Enable basic sanity" $rlRun_LOG
rlAssertGrep "packit/packit.dev#399 - Update \`tmt\` examples" $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "Pull Requests Created (org:teemtee)"
rlRun -s "did --config ./config-org.ini --gh-pull-requests-created $YEAR_2022"
rlAssertGrep "Pull requests created on gh: 85$" $rlRun_LOG
rlAssertGrep "teemtee/tmt#1750 - Include the new web link in verbose" $rlRun_LOG
rlAssertGrep "teemtee/fmf#170 - Implement a directive for disabling inheritance" $rlRun_LOG
rlAssertGrep "teemtee/try#002 - Check logs for test with a hash sign in" $rlRun_LOG
rlAssertNotGrep "psss/did#275 - Speed up local testing" $rlRun_LOG
rlAssertNotGrep "psss/python-nitrate#039 - Enable basic sanity" $rlRun_LOG
rlAssertNotGrep "packit/packit.dev#399 - Update \`tmt\` examples" $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "Pull Requests Created (org:teemtee,packit)"
rlRun -s "did --config ./config-more.ini --gh-pull-requests-created $YEAR_2022"
rlAssertGrep "Pull requests created on gh: 86$" $rlRun_LOG
rlAssertGrep "teemtee/tmt#1750 - Include the new web link in verbose" $rlRun_LOG
rlAssertGrep "teemtee/fmf#170 - Implement a directive for disabling inheritance" $rlRun_LOG
rlAssertGrep "teemtee/try#002 - Check logs for test with a hash sign in" $rlRun_LOG
rlAssertNotGrep "psss/did#275 - Speed up local testing" $rlRun_LOG
rlAssertNotGrep "psss/python-nitrate#039 - Enable basic sanity" $rlRun_LOG
rlAssertGrep "packit/packit.dev#399 - Update \`tmt\` examples" $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "Pull Requests Created (repo:teemtee/fmf)"
rlRun -s "did --config ./config-repo.ini --gh-pull-requests-created $YEAR_2022"
rlAssertGrep "Pull requests created on gh: 7$" $rlRun_LOG
rlAssertNotGrep "teemtee/tmt#1750 - Include the new web link in verbose" $rlRun_LOG
rlAssertGrep "teemtee/fmf#170 - Implement a directive for disabling inheritance" $rlRun_LOG
rlAssertNotGrep "teemtee/try#002 - Check logs for test with a hash sign in" $rlRun_LOG
rlAssertNotGrep "psss/did#275 - Speed up local testing" $rlRun_LOG
rlAssertNotGrep "psss/python-nitrate#039 - Enable basic sanity" $rlRun_LOG
rlAssertNotGrep "packit/packit.dev#399 - Update \`tmt\` examples" $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "Pull Requests Created (user:psss)"
rlRun -s "did --config ./config-user.ini --gh-pull-requests-created $YEAR_2022"
rlAssertGrep "Pull requests created on gh: 7$" $rlRun_LOG
rlAssertNotGrep "teemtee/tmt#1750 - Include the new web link in verbose" $rlRun_LOG
rlAssertNotGrep "teemtee/fmf#170 - Implement a directive for disabling inheritance" $rlRun_LOG
rlAssertNotGrep "teemtee/try#002 - Check logs for test with a hash sign in" $rlRun_LOG
rlAssertGrep "psss/did#275 - Speed up local testing" $rlRun_LOG
rlAssertGrep "psss/python-nitrate#039 - Enable basic sanity" $rlRun_LOG
rlAssertNotGrep "packit/packit.dev#399 - Update \`tmt\` examples" $rlRun_LOG
rlPhaseEnd

# Pull Requests Closed

rlPhaseStartTest "Pull Requests Closed"
rlRun -s "did --config ./config-default.ini --gh-pull-requests-closed $YEAR_2022"
rlAssertGrep "Pull requests closed on gh: 315$" $rlRun_LOG
rlAssertGrep "psss/did#272 - Koji plugin" $rlRun_LOG
rlAssertGrep "psss/python-nitrate#038 - Properly handle string" $rlRun_LOG
rlAssertGrep "teemtee/fmf#177 - Shallow git clone if no reference" $rlRun_LOG
rlAssertGrep "teemtee/tmt#1050 - Run commands via bash" $rlRun_LOG
rlAssertGrep "teemtee/upgrade#006 - Store the old packages " $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "Pull Requests Closed (org:teemtee)"
rlRun -s "did --config ./config-org.ini --gh-pull-requests-closed $YEAR_2022"
rlAssertGrep "Pull requests closed on gh: 305$" $rlRun_LOG
rlAssertNotGrep "psss/did#272 - Koji plugin" $rlRun_LOG
rlAssertNotGrep "psss/python-nitrate#038 - Properly handle string" $rlRun_LOG
rlAssertGrep "teemtee/fmf#177 - Shallow git clone if no reference" $rlRun_LOG
rlAssertGrep "teemtee/tmt#1050 - Run commands via bash" $rlRun_LOG
rlAssertGrep "teemtee/upgrade#006 - Store the old packages " $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "Pull Requests Closed (repo:teemtee/fmf)"
rlRun -s "did --config ./config-repo.ini --gh-pull-requests-closed $YEAR_2022"
rlAssertGrep "Pull requests closed on gh: 13$" $rlRun_LOG
rlAssertNotGrep "psss/did#272 - Koji plugin" $rlRun_LOG
rlAssertNotGrep "psss/python-nitrate#038 - Properly handle string" $rlRun_LOG
rlAssertGrep "teemtee/fmf#177 - Shallow git clone if no reference" $rlRun_LOG
rlAssertNotGrep "teemtee/tmt#1050 - Run commands via bash" $rlRun_LOG
rlAssertNotGrep "teemtee/upgrade#006 - Store the old packages " $rlRun_LOG
rlPhaseEnd

rlPhaseStartTest "Pull Requests Closed (user:psss)"
rlRun -s "did --config ./config-user.ini --gh-pull-requests-closed $YEAR_2022"
rlAssertGrep "Pull requests closed on gh: 10$" $rlRun_LOG
rlAssertGrep "psss/did#272 - Koji plugin" $rlRun_LOG
rlAssertGrep "psss/python-nitrate#038 - Properly handle string" $rlRun_LOG
rlAssertNotGrep "teemtee/fmf#177 - Shallow git clone if no reference" $rlRun_LOG
rlAssertNotGrep "teemtee/tmt#1050 - Run commands via bash" $rlRun_LOG
rlAssertNotGrep "teemtee/upgrade#006 - Store the old packages " $rlRun_LOG
rlPhaseEnd

rlJournalEnd
1 change: 1 addition & 0 deletions tests/main.fmf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
test: ./test.sh
framework: beakerlib
require: did
tier: 1

1 comment on commit 5b266fa

@packit-as-a-service
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on your Packit configuration the settings of the psss/did Copr project would need to be updated as follows:

field old value new value
bootstrap on default

Packit was unable to update the settings above as it is missing admin permissions on the psss/did Copr project.

To fix this you can do one of the following:

  • Grant Packit admin permissions on the psss/did Copr project on the permissions page.
  • Change the above Copr project settings manually on the settings page to match the Packit configuration.
  • Update the Packit configuration to match the Copr project settings.

Please retrigger the build, once the issue above is fixed.

Please sign in to comment.