Skip to content

Commit

Permalink
find labels
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed May 14, 2024
1 parent e392796 commit 8e33944
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/zerocracy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# SOFTWARE.
---
name: zerocracy
on:
'on':
push:
# schedule:
# - cron: '0,10,20,30,50,50 * * * *'
Expand All @@ -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/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
13 changes: 10 additions & 3 deletions entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion judges/scan/github_events/quick-scan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
58 changes: 0 additions & 58 deletions judges/scan/github_events/scan.rb

This file was deleted.

86 changes: 86 additions & 0 deletions judges/scan/github_events/scan_github_events.rb
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions judges/scan/github_labels/find-labels.yml
Original file line number Diff line number Diff line change
@@ -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']
40 changes: 40 additions & 0 deletions judges/scan/github_labels/scan_github_labels.rb
Original file line number Diff line number Diff line change
@@ -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
40 changes: 40 additions & 0 deletions lib/octokit.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8e33944

Please sign in to comment.