From 194a87599580ac326e305f9e957c734e397e2dd7 Mon Sep 17 00:00:00 2001 From: Paul Fischer Date: Tue, 22 Dec 2020 20:43:05 +0100 Subject: [PATCH] added pre-commit to package managers --- app/models/package_manager/pre_commit.rb | 43 ++++++++ lib/tasks/download.rake | 10 ++ spec/fixtures/vcr_cassettes/all-hooks.yml | 98 +++++++++++++++++++ .../models/package_manager/pre_commit_spec.rb | 43 ++++++++ 4 files changed, 194 insertions(+) create mode 100644 app/models/package_manager/pre_commit.rb create mode 100644 spec/fixtures/vcr_cassettes/all-hooks.yml create mode 100644 spec/models/package_manager/pre_commit_spec.rb diff --git a/app/models/package_manager/pre_commit.rb b/app/models/package_manager/pre_commit.rb new file mode 100644 index 000000000..219dcef74 --- /dev/null +++ b/app/models/package_manager/pre_commit.rb @@ -0,0 +1,43 @@ +module PackageManager + class PreCommit < Base + HAS_VERSIONS = false + HAS_DEPENDENCIES = false + BIBLIOTHECARY_SUPPORT = true + URL = "https://pre-commit.com/" + COLOR = "#3572A5" + + def self.project_names + get("https://pre-commit.com/all-hooks.json") + .values + .flatten + .map { |hook| hook["id"] } + end + + def self.project(name) + repo = get("https://pre-commit.com/all-hooks.json") + .find { |repo,hooks| hooks.any? { |hook| hook["id"] == name } } + + hook = repo[1] + .find { |hook| hook["id"] == name } + + { + repo: repo[0], + id: hook["id"], + language: hook["language"], + description: hook.fetch("description", ""), + } + end + + def self.mapping(project) + { + name: project[:id], + description: project[:description], + repository_url: project[:repo], + } + end + + def self.formatted_name + "pre-commit" + end + end +end diff --git a/lib/tasks/download.rake b/lib/tasks/download.rake index 2e5d66c8c..1b41c6d04 100644 --- a/lib/tasks/download.rake +++ b/lib/tasks/download.rake @@ -232,6 +232,16 @@ namespace :download do PackageManager::PlatformIO.import_async end + desc "Download new pre-commit packages asynchronously" + task precommit: :environment do + PackageManager::PreCommit.import_new_async + end + + desc "Download all pre-commit packages asynchronously" + task precommit_all: :environment do + PackageManager::PreCommit.import_async + end + desc "Download new PureScript packages asynchronously" task purescript: :environment do PackageManager::PureScript.import_new_async diff --git a/spec/fixtures/vcr_cassettes/all-hooks.yml b/spec/fixtures/vcr_cassettes/all-hooks.yml new file mode 100644 index 000000000..69752853e --- /dev/null +++ b/spec/fixtures/vcr_cassettes/all-hooks.yml @@ -0,0 +1,98 @@ +--- +http_interactions: + - request: + method: get + uri: https://pre-commit.com/all-hooks.json + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - '*/*' + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Date: + - Tue, 22 Dec 2020 19:18:40 GMT + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Set-Cookie: + - __cfduid=dd47f15b6be976c1293bfcca37b59d5341608664719; expires=Thu, 21-Jan-21 + 19:18:39 GMT; path=/; domain=.pre-commit.com; HttpOnly; SameSite=Lax; Secure + Last-Modified: + - Mon, 21 Dec 2020 08:50:52 GMT + Vary: + - Accept-Encoding + Access-Control-Allow-Origin: + - '*' + Etag: + - W/"5fe061ec-23901" + Expires: + - Tue, 22 Dec 2020 19:28:40 GMT + Cache-Control: + - max-age=600 + X-Proxy-Cache: + - MISS + X-Github-Request-Id: + - 30EA:02D5:5A7B96:6F9860:5FE24690 + Cf-Cache-Status: + - DYNAMIC + Cf-Request-Id: + - 072d7cba4f0000ce8b8d231000000001 + Expect-Ct: + - max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct" + Report-To: + - '{"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report?s=2pFhFG%2B8I7DpOoTIMwXZpOPyT3UsndRuAu%2B%2FMz2yU9wVzL5YHYUIJagv%2BcQkXZrWJkY%2BEo%2Fj8tppjfQgKPY5IanA4WP7tfD2cy4U5XFrx8dEOXjK59fpHjpobg%3D%3D"}],"group":"cf-nel","max_age":604800}' + Nel: + - '{"report_to":"cf-nel","max_age":604800}' + Server: + - cloudflare + Cf-Ray: + - 605c30a3ba71ce8b-LHR + body: + encoding: ASCII-8BIT + string: |- + { + "https://github.com/pre-commit/pre-commit-hooks": [ + { + "id": "check-added-large-files", + "name": "Check for added large files", + "description": "Prevent giant files from being committed", + "entry": "check-added-large-files", + "language": "python" + }, + { + "id": "check-ast", + "name": "Check python ast", + "description": "Simply check whether the files parse as valid python.", + "entry": "check-ast", + "language": "python", + "types": ["python"] + } + ], + "https://github.com/pre-commit/mirrors-autopep8": [ + { + "id": "autopep8", + "name": "autopep8", + "entry": "autopep8", + "language": "python", + "types": ["python"], + "args": ["-i"], + "require_serial": false, + "additional_dependencies": [] + } + ] + } + http_version: + recorded_at: Tue, 22 Dec 2020 19:18:40 GMT +recorded_with: VCR 4.0.0 diff --git a/spec/models/package_manager/pre_commit_spec.rb b/spec/models/package_manager/pre_commit_spec.rb new file mode 100644 index 000000000..e04f53149 --- /dev/null +++ b/spec/models/package_manager/pre_commit_spec.rb @@ -0,0 +1,43 @@ +require "rails_helper" + +describe PackageManager::PreCommit do + describe "#project_names" do + it "resolves projects correctly" do + VCR.use_cassette("all-hooks") do + project_names = described_class.project_names + + expect(project_names).to eq ["check-added-large-files", "check-ast", "autopep8"] + end + end + end + + describe "#mapping" do + it "maps project data correctly" do + VCR.use_cassette("all-hooks") do + project = described_class.project("check-ast") + mapping = described_class.mapping(project) + + expect(mapping[:name]).to eq "check-ast" + expect(mapping[:description]).to eq "Simply check whether the files parse as valid python." + expect(mapping[:repository_url]).to eq "https://github.com/pre-commit/pre-commit-hooks" + end + end + + it "uses correct description fallback" do + VCR.use_cassette("all-hooks") do + project = described_class.project("autopep8") + mapping = described_class.mapping(project) + + expect(mapping[:name]).to eq "autopep8" + expect(mapping[:description]).to eq "" + expect(mapping[:repository_url]).to eq "https://github.com/pre-commit/mirrors-autopep8" + end + end + end + + describe "#formatted_name" do + it 'has a formatted name of "pre-commit"' do + expect(described_class.formatted_name).to eq("pre-commit") + end + end +end