Skip to content

Commit

Permalink
Enable the isReviewed option for reviewed package
Browse files Browse the repository at this point in the history
  • Loading branch information
Kgitman committed Oct 9, 2024
1 parent a8cec75 commit bf110c5
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 10 deletions.
24 changes: 24 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"tasks": {
"start": "deno run --allow-env --allow-net index.ts",
"test": "deno test --allow-env --allow-net index_test.ts"
},
"compilerOptions": {
"lib": ["deno.ns", "dom"]
},
"lint": {
"rules": {
"exclude": [
"no-explicit-any"
]
}
},
"fmt": {
"options": {
"useTabs": false,
"indentWidth": 2,
"lineWidth": 80
}
}
}

5 changes: 4 additions & 1 deletion supabase/functions/fetch_package/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getSupabaseClient } from './supabase_client.ts';
import { main as updateAllowlist } from './isReviewed.ts';

async function fetchPackagistData(packageName: string) {
const response = await fetch(`https://packagist.org/packages/${packageName}.json`);
Expand Down Expand Up @@ -75,7 +76,7 @@ async function storeInSupabase(supabaseClient: any, packageData: any) {
}
}

const { description, keywords, homepage, version: ver, version_normalized, license, authors, source, dist, type, support, funding, time, extra } = version;
const { description, keywords, homepage, version: ver, version_normalized, license, authors, source, dist, type, support, funding, time, extra } = version as any;

const { data: versionDataResponse, error: versionError } = await supabaseClient
.from('versions')
Expand Down Expand Up @@ -144,6 +145,8 @@ async function main(req: Request) {
await fetchPackages(url, supabaseClient);
}

await updateAllowlist();

return new Response('Success', { status: 200 });
}

Expand Down
35 changes: 35 additions & 0 deletions supabase/functions/fetch_package/isReviewed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { getSupabaseClient } from './supabase_client.ts';

const token = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY") || "";
const supabase = getSupabaseClient(token);

async function fetchJson(url: string) {
const response = await fetch(url);
return await response.json();
}

async function updateDatabase(packageName: string) {
const { data, error } = await supabase
.from('packages')
.update({ isreviewed: true })
.eq('name', packageName);

if (error) {
console.error('Error updating the isReviewd column:', packageName, error);
} else {
console.log('isReviewed updated successfully for package:', packageName);
}
}

export async function main() {
const url1 = "https://raw.githubusercontent.com/mautic/marketplace-allowlist/main/allowlist.json";

const data1 = await fetchJson(url1);

const listPackages = data1.allowlist.map((item: { package: string }) => item.package);
for (const packageName of listPackages) {
await updateDatabase(packageName);
}
}

main().catch(console.error);
4 changes: 2 additions & 2 deletions supabase/migrations/20240822183046_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ CREATE TABLE versions (

--review table

CREATE TABLE review(
CREATE TABLE reviews(
id SERIAL PRIMARY KEY,
created_at TIMESTAMPTZ DEFAULT NOW(),
user TEXT,
rating INT,
review TEXT,
objectId TEXT,
objectId TEXT NOT NULL REFERENCES packages(name) ON DELETE CASCADE,
picture TEXT,
user_id TEXT
)
11 changes: 9 additions & 2 deletions supabase/migrations/20240822183525_view.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ DECLARE
BEGIN
-- Fetch the total count (without limit/offset)
SELECT COUNT(*) INTO total FROM packages p
WHERE (_query IS NULL OR p.name ILIKE '%' || _query || '%') AND (_type IS NULL OR p.type ILIKE '%' || _type || '%');
WHERE (p.sm = TRUE) AND (_query IS NULL OR p.name ILIKE '%' || _query || '%') AND (_type IS NULL OR p.type ILIKE '%' || _type || '%');
-- Fetch the data with limit and offset
SELECT JSON_AGG(t) INTO todo
FROM (
Expand All @@ -25,11 +25,18 @@ BEGIN
p.downloads ->> 'total' as downloads,
p.favers,
p.type,
p.displayname
--COALESCE(AVG(r.rating), 0) AS average_rating,
COALESCE(ROUND(AVG(r.rating), 1), 0) AS average_rating,
COALESCE(COUNT(r.review), 0) AS total_review
FROM
packages p
LEFT JOIN reviews r ON p.name = r."objectId"
WHERE
(p.sm = TRUE)
AND
(_query IS NULL OR p.name ILIKE '%' || _query || '%') AND (_type IS NULL OR p.type ILIKE '%' || _type || '%')
GROUP BY
p.name, p.url, p.repository, p.description, p.downloads, p.favers, p.type
LIMIT _limit OFFSET _offset
) t;

Expand Down
4 changes: 2 additions & 2 deletions supabase/migrations/20240822183809_detail.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ BEGIN
'favers', p.favers
)
) INTO package_data
FROM packagos p
JOIN versionos v ON p.name = v.package_name
FROM packages p
JOIN versions v ON p.name = v.package_name
LEFT JOIN reviews r ON p.name = r."objectId"
WHERE p.name = packag_name
GROUP BY p.name, p.description, p.time, p.maintainers, p.type, p.repository, p.github_stars, p.github_watchers, p.github_forks, p.github_open_issues, p.language, p.dependents, p.suggesters, p.downloads, p.favers;
Expand Down
6 changes: 3 additions & 3 deletions supabase/migrations/20241004040501_smv_in_view.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ CREATE OR REPLACE FUNCTION update_sm()
RETURNS TRIGGER AS $$
BEGIN
-- Update pdp based on the current state of the version table
UPDATE packagos p
UPDATE packages p
SET sm = CASE
WHEN EXISTS (
SELECT 1
FROM versionos v
FROM versions v
WHERE v.package_name = p.name
AND v.smv LIKE '%^5.0%'
) THEN true
Expand All @@ -21,7 +21,7 @@ $$


CREATE TRIGGER version_change
AFTER INSERT OR DELETE OR UPDATE ON versionos
AFTER INSERT OR DELETE OR UPDATE ON versions
FOR EACH ROW
EXECUTE FUNCTION update_sm();

Expand Down

0 comments on commit bf110c5

Please sign in to comment.