From 8e3394440b87f0c4fb677ae1f984b20df8b16235 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Tue, 14 May 2024 16:06:05 -0400 Subject: [PATCH] find labels --- .github/workflows/test.yml | 2 +- .github/workflows/zerocracy.yml | 4 +- Dockerfile | 2 +- Makefile | 2 +- entry.sh | 13 ++- .../{catch.rb => catch_accepted_bug.rb} | 0 .../{reward.rb => reward_for_good_bug.rb} | 0 judges/scan/github_events/quick-scan.yml | 2 +- judges/scan/github_events/scan.rb | 58 ------------- .../scan/github_events/scan_github_events.rb | 86 +++++++++++++++++++ judges/scan/github_labels/find-labels.yml | 29 +++++++ .../scan/github_labels/scan_github_labels.rb | 40 +++++++++ lib/octokit.rb | 40 +++++++++ 13 files changed, 211 insertions(+), 67 deletions(-) rename judges/catch/accepted_bug/{catch.rb => catch_accepted_bug.rb} (100%) rename judges/reward/good_bug/{reward.rb => reward_for_good_bug.rb} (100%) delete mode 100644 judges/scan/github_events/scan.rb create mode 100644 judges/scan/github_events/scan_github_events.rb create mode 100644 judges/scan/github_labels/find-labels.yml create mode 100644 judges/scan/github_labels/scan_github_labels.rb create mode 100644 lib/octokit.rb diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4d99c7e7..058e135c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -34,7 +34,7 @@ jobs: - uses: ./ with: options: | - github_repository=yegor256/judges + github_repositories=yegor256/judges github_max_events=1 factbase: test.fb - run: test -e test.fb diff --git a/.github/workflows/zerocracy.yml b/.github/workflows/zerocracy.yml index 4384d795..960f592c 100644 --- a/.github/workflows/zerocracy.yml +++ b/.github/workflows/zerocracy.yml @@ -19,7 +19,7 @@ # SOFTWARE. --- name: zerocracy -on: +'on': push: # schedule: # - cron: '0,10,20,30,50,50 * * * *' @@ -41,7 +41,7 @@ jobs: options: | github_token=${{ secrets.GITHUB_TOKEN }} github_max_events=2 - github_repository=${{ github.repository }} + github_repositories=yegor256/judges,yegor256/factbase,zerocracy/judges-action factbase: recent.fb - run: mkdir gh-pages && cp recent.yaml gh-pages - uses: JamesIves/github-pages-deploy-action@v4.6.0 diff --git a/Dockerfile b/Dockerfile index c7b977a8..f8b9aa07 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,7 +26,7 @@ LABEL "repository"="https://github.com/zerocracy/judges-action" LABEL "maintainer"="Yegor Bugayenko" RUN gem install \ - judges:0.0.15 \ + judges:0.0.17 \ octokit:8.1.0 \ faraday-retry:2.2.1 diff --git a/Makefile b/Makefile index fae5a088..c1d30f13 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ test: target/docker-image.txt entry: target/docker-image.txt img=$$(cat target/docker-image.txt) ( - echo 'github_repository=yegor256/judges' + echo 'github_repositories=yegor256/judges' echo 'github_max_events=3' ) > target/opts.txt docker run --rm \ diff --git a/entry.sh b/entry.sh index d18ed99d..2c75dbdb 100755 --- a/entry.sh +++ b/entry.sh @@ -47,8 +47,15 @@ while IFS= read -r o; do options+=("--option=${v}") done <<< "${INPUT_OPTIONS}" +if [ -e "${fb}" ]; then + # Remove facts that are too old + judges --verbose trim "${fb}" +fi + +# Add new facts, using the judges (Ruby scripts) in the /judges directory judges --verbose update "${options[@]}" /judges "${fb}" -judges print --format=yaml --auto "${fb}" -judges print --format=xml --auto "${fb}" -judges print --format=json --auto "${fb}" +# Convert the factbase to a few human-readable formats +for f in yaml xml json; do + judges print --format "${f}" --auto "${fb}" +done diff --git a/judges/catch/accepted_bug/catch.rb b/judges/catch/accepted_bug/catch_accepted_bug.rb similarity index 100% rename from judges/catch/accepted_bug/catch.rb rename to judges/catch/accepted_bug/catch_accepted_bug.rb diff --git a/judges/reward/good_bug/reward.rb b/judges/reward/good_bug/reward_for_good_bug.rb similarity index 100% rename from judges/reward/good_bug/reward.rb rename to judges/reward/good_bug/reward_for_good_bug.rb diff --git a/judges/scan/github_events/quick-scan.yml b/judges/scan/github_events/quick-scan.yml index 30991ea4..fac8f708 100644 --- a/judges/scan/github_events/quick-scan.yml +++ b/judges/scan/github_events/quick-scan.yml @@ -21,7 +21,7 @@ input: [] options: github_max_events: 1 - github_repository: yegor256/judges + github_repositories: yegor256/judges expected: - /fb[count(f)=1] - /fb/f[id] diff --git a/judges/scan/github_events/scan.rb b/judges/scan/github_events/scan.rb deleted file mode 100644 index fa802cb1..00000000 --- a/judges/scan/github_events/scan.rb +++ /dev/null @@ -1,58 +0,0 @@ -# MIT License -# -# Copyright (c) 2024 Zerocracy -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -require 'octokit' - -octokit = Octokit::Client.new -unless $options.github_token.nil? - token = $options.github_token - octokit = Octokit::Client.new(access_token: token) - $loog.info("Accessing GitHub with a token (#{token.length} chars)") -end - -seen = 0 -octokit.repository_events($options.github_repository).each do |e| - next unless $fb.query("(eq github_event_id #{e[:id]})").extend(Enumerable).to_a.empty? - $loog.info("Detected new event ##{e[:id]} in #{e[:repo][:name]}: #{e[:type]}") - n = $fb.insert - n.kind = 'GitHub event' - n.time = e[:created_at].iso8601 - n.github_event_type = e[:type] - n.github_event_id = e[:id] - n.github_repository = e[:repo][:name] - n.github_repository_id = e[:repo][:id] - n.github_action = e[:payload][:type] if e[:payload][:type] - n.github_actor = e[:actor][:login] if e[:actor] - n.github_actor_id = e[:actor][:id] if e[:actor] - if e[:type] == 'PushEvent' - n.github_push_id = e[:payload][:push_id] - end - seen += 1 - if !$options.github_max_events.nil? && seen >= $options.github_max_events - $loog.info("Already scanned #{seen} events, that's enough (due to 'github_max_events' option)") - break - end - if octokit.rate_limit.remaining < 10 - $loog.info("To much GitHub API quota consumed already, stopping") - break - end -end diff --git a/judges/scan/github_events/scan_github_events.rb b/judges/scan/github_events/scan_github_events.rb new file mode 100644 index 00000000..8949d889 --- /dev/null +++ b/judges/scan/github_events/scan_github_events.rb @@ -0,0 +1,86 @@ +# MIT License +# +# Copyright (c) 2024 Zerocracy +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +require_relative '../../../lib/octokit' + +seen = 0 +repositories do |repo| + octokit.repository_events(repo).each do |e| + next unless $fb.query("(eq github_event_id #{e[:id]})").extend(Enumerable).to_a.empty? + $loog.info("Detected new event ##{e[:id]} in #{e[:repo][:name]}: #{e[:type]}") + n = $fb.insert + n.kind = 'GitHub event' + + if e[:type] == 'PushEvent' + n.github_action = 'push' + n.github_push_id = e[:payload][:push_id] + + elsif e[:type] == 'IssuesEvent' + n.github_issue = e[:payload][:issue][:number] + if e[:payload][:action] == 'closed' + n.github_action = 'issue-closed' + elsif e[:payload][:action] == 'opened' + n.github_action = 'issue-opened' + end + + elsif e[:type] == 'IssueCommentEvent' + n.github_issue = e[:payload][:issue][:number] + if e[:payload][:action] == 'created' + n.github_action = 'comment-posted' + n.github_comment_id = e[:payload][:comment][:id] + n.github_comment_body = e[:payload][:comment][:body] + n.github_comment_author = e[:payload][:comment][:user][:login] + n.github_comment_author_id = e[:payload][:comment][:user][:id] + end + + elsif e[:type] == 'ReleaseEvent' + n.github_release_id = e[:payload][:release][:id] + if e[:payload][:action] == 'published' + n.github_action = 'release-published' + n.github_release_author = e[:payload][:release][:author][:login] + n.github_release_author_id = e[:payload][:release][:author][:id] + end + + elsif e[:type] == 'CreateEvent' + if e[:payload][:ref_type] == 'tag' + n.github_action = 'tag-created' + n.github_tag = e[:payload][:ref] + end + end + n.time = e[:created_at].iso8601 + n.github_event_type = e[:type] + n.github_event_id = e[:id] + n.github_repository = e[:repo][:name] + n.github_repository_id = e[:repo][:id] + n.github_actor = e[:actor][:login] if e[:actor] + n.github_actor_id = e[:actor][:id] if e[:actor] + seen += 1 + if !$options.github_max_events.nil? && seen >= $options.github_max_events + $loog.info("Already scanned #{seen} events, that's enough (due to 'github_max_events' option)") + break + end + if octokit.rate_limit.remaining < 10 + $loog.info("To much GitHub API quota consumed already, stopping") + break + end + end +end diff --git a/judges/scan/github_labels/find-labels.yml b/judges/scan/github_labels/find-labels.yml new file mode 100644 index 00000000..1efd52f3 --- /dev/null +++ b/judges/scan/github_labels/find-labels.yml @@ -0,0 +1,29 @@ +# Copyright (c) 2024 Yegor Bugayenko +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the 'Software'), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +--- +input: [] +options: + github_max_labels: 1 + github_repositories: yegor256/judges +expected: + - /fb/f[id] + - /fb/f[kind='GitHub event'] + - /fb/f[time] + - /fb/f[github_event_type='LabelAttached'] diff --git a/judges/scan/github_labels/scan_github_labels.rb b/judges/scan/github_labels/scan_github_labels.rb new file mode 100644 index 00000000..336ee82b --- /dev/null +++ b/judges/scan/github_labels/scan_github_labels.rb @@ -0,0 +1,40 @@ +# MIT License +# +# Copyright (c) 2024 Zerocracy +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +require_relative '../../../lib/octokit' + +repositories do |repo| + octokit.search_issues("repo:#{repo} label:bug,enhancement,question")[:items].each do |e| + e[:labels].each do |label| + n = if_absent($fb) do |f| + f.github_event_type = 'LabelAttached' + f.github_repository = repo + f.github_issue = e[:number] + f.github_label = label[:name] + end + next if n.nil? + $loog.info("Detected new label '##{label[:name]}' at #{repo}##{e[:number]}") + n.kind = 'GitHub event' + n.time = Time.now + end + end +end diff --git a/lib/octokit.rb b/lib/octokit.rb new file mode 100644 index 00000000..0d47d4b8 --- /dev/null +++ b/lib/octokit.rb @@ -0,0 +1,40 @@ +# MIT License +# +# Copyright (c) 2024 Zerocracy +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +require 'octokit' + +def octokit + octokit = Octokit::Client.new + unless $options.github_token.nil? + token = $options.github_token + octokit = Octokit::Client.new(access_token: token) + $loog.info("Accessing GitHub with a token (#{token.length} chars)") + end + octokit +end + +def repositories + $options.github_repositories.split(',').each do |repo| + $loog.info("Scanning #{repo}...") + yield repo + end +end \ No newline at end of file