Skip to content

Commit

Permalink
Merge pull request #7 from whomwah/dr-cache
Browse files Browse the repository at this point in the history
feature: Add new cache refresh tasks
  • Loading branch information
whomwah authored Sep 25, 2022
2 parents 43eda51 + c0cec3a commit 6be9c98
Show file tree
Hide file tree
Showing 16 changed files with 261 additions and 47 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/deno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Build release
run: ./bin/build_release
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 16
- name: Setup package.json
run: echo '{"name":"alfred-github-workflow", "devDependencies":{"semantic-release":"^19.0.5"}}' > package.json
run: echo '{"name":"alfred-github-workflow", "devDependencies":{"@semantic-release/git":"^10.0.1","@semantic-release/exec":"^6.0.3","semantic-release":"^19.0.5"}}' > package.json
- name: Install dependencies
run: npm install
- name: Release
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
releases
node_modules
package*.json
17 changes: 16 additions & 1 deletion .releaserc
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,31 @@
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
[
"@semantic-release/exec",
{
"verifyReleaseCmd": "./bin/build_release ${nextRelease.version}"
}
],
[
"@semantic-release/github",
{
"assets": [
{
"path": "releases/*.alfredworkflow",
"label": "Alfred 5 Github Workflow"
},
}
]
}
],
[
"@semantic-release/git",
{
"assets": [
"info.plist"
],
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
}
]
]
}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ gh my...
gh ...
```

## Development

You can build your own version of the workflow with:

```
./bin/build_release <version>
# example
./bin/build_release 1.2.3
```

## Resources

- Alfred App:: https://www.alfredapp.com/
Expand Down
1 change: 0 additions & 1 deletion VERSION

This file was deleted.

2 changes: 1 addition & 1 deletion bin/build_release
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env sh

VERSION=$(cat VERSION)
VERSION=$1

echo "updating VERSION to $VERSION in info.plist... ✅"
/usr/libexec/PlistBuddy -c "Set :version $VERSION" info.plist
Expand Down
1 change: 1 addition & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { emptyDir } from "https://deno.land/[email protected]/fs/empty_dir.ts";
export { DB } from "https://deno.land/x/[email protected]/mod.ts";
export { Octokit } from "https://cdn.skypack.dev/@octokit/rest";
export { writeAllSync } from "https://deno.land/[email protected]/streams/conversion.ts";
export { toIMF } from "https://deno.land/[email protected]/datetime/mod.ts";
Binary file added icons/refresh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 12 additions & 4 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,25 @@ export default async function Action(query: string) {
log("Logged out successfully!");
break;
}
// We want to delete the database
case action("###database_delete###"): {
await deleteDatabase(db);
log("Database deleted successfully!");
break;
}
// We want to delete the cache
case action("###cache_delete###"): {
deleteCache(db);
db.close();
log("Cache deleted successfully!");
break;
}
// We want to delete the database
case action("###database_delete###"): {
await deleteDatabase(db);
log("Database deleted successfully!");
// We want to clear a specific cache
case action("###refresh_cache###"): {
const path = query.replace("###refresh_cache###", "");
deleteCache(db, path);
db.close();
log(`Cache for ${path} cleared!`);
break;
}
// Lets assume it's a url
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface BuildItem {
valid?: boolean;
autocomplete?: string | boolean;
skipUID?: boolean;
skipMatch?: boolean;
}

interface Builder {
Expand Down Expand Up @@ -52,7 +53,7 @@ export default function Builder(queryArgs: QueryArgs, items: Item[]) {

return {
addItem: async (item: BuildItem) => {
if (matches(item.title, queryArgs.query)) {
if (item.skipMatch || matches(item.title, queryArgs.query)) {
addListItem(await buildListItem(item));
}
},
Expand All @@ -65,6 +66,7 @@ export function searchGithub(queryArgs: QueryArgs, config: Config) {
autocomplete: false,
arg: `${config.baseUrl}/search?q=${queryArgs.query}`,
skipUID: true,
skipMatch: true,
};
}

Expand Down
27 changes: 25 additions & 2 deletions src/helpers/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export type DbCache = [
string | undefined,
];

export interface CacheItem {
url: string;
timestamp: number;
}

export function cleanCache(db: DB) {
try {
db.query("DELETE FROM request_cache WHERE timestamp < :time", {
Expand All @@ -24,14 +29,32 @@ export function cleanCache(db: DB) {
}
}

export function deleteCache(db: DB) {
export function deleteCache(db: DB, path?: string) {
const query = path
? `DELETE FROM request_cache WHERE LIKE('%${path}?%',url)=1`
: "DELETE FROM request_cache";
try {
db.query("DELETE FROM request_cache");
db.query(query);
} catch (err) {
console.error(err);
}
}

export function cacheItems(db: DB) {
const items: CacheItem[] = [];

const query = db.prepareQuery<[CacheItem["url"], CacheItem["timestamp"]]>(
"SELECT url, timestamp FROM request_cache WHERE parent IS NULL",
);

for (const [url, timestamp] of query.iter()) {
items.push({ url, timestamp });
}
query.finalize();

return items;
}

export function requestFromCache(config: Config, url: string, column: string) {
const stmt = config.db.prepareQuery<
[string, string, string, number],
Expand Down
27 changes: 19 additions & 8 deletions src/helpers/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import { Octokit } from "../../deps.ts";
import { updateCache } from "./cache.ts";
import { Config } from "./config.ts";

interface GithubRoutes {
[key: string]: string;
}

export const GHRoute: GithubRoutes = {
"/user": "your profile",
"/user/repos": "your repos",
"/user/following": "users you follow",
"/user/starred": "starred repos",
};

export interface GhUser {
id: number;
login: string;
Expand All @@ -23,14 +34,6 @@ export interface GhUser {
site_admin: boolean;
}

interface GhPermission {
admin: boolean;
maintain: boolean;
push: boolean;
triage: boolean;
pull: boolean;
}

export interface GhRepo {
id: number;
node_id: string;
Expand Down Expand Up @@ -113,6 +116,14 @@ export interface GhRepo {
permissions: GhPermission;
}

interface GhPermission {
admin: boolean;
maintain: boolean;
push: boolean;
triage: boolean;
pull: boolean;
}

export async function fetchNewDataFromAPIandStore<T>(
config: Config,
url: string,
Expand Down
18 changes: 17 additions & 1 deletion src/helpers/mapping.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { GhRepo, GhUser } from "./github.ts";
import { toIMF } from "../../deps.ts";
import { CacheItem } from "./cache.ts";
import { GhRepo, GHRoute, GhUser } from "./github.ts";
import { QueryArgs } from "./query.ts";

export function mapUserToItem(user: GhUser) {
const prefix = "@";
Expand All @@ -21,3 +24,16 @@ export function mapRepoToItem(repo: GhRepo) {
arg: repo.html_url,
};
}

export function mapCacheItemToItem(item: CacheItem, queryArgs: QueryArgs) {
const url = new URL(item.url);
const label = GHRoute[url.pathname];
const date = new Date(item.timestamp);

return {
title: `${queryArgs.prefix} ${queryArgs.action} ${label}`,
subtitle: `last checked: ${toIMF(date)}`,
arg: `###refresh_cache###${url.pathname}`,
icon: "refresh",
};
}
4 changes: 3 additions & 1 deletion src/helpers/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface QueryArgs {
prefix: string | undefined;
action: string;
parts: string[];
lastPart: string;
query: string;
isSubCmd: boolean;
}
Expand All @@ -12,12 +13,13 @@ export function queryArgs(query: string, prefix?: string): QueryArgs {
.substring(prefix ? prefix.length : 0)
.trim()
.split(" ");
const isSubCmd = (query.endsWith(" ") || parts.length > 1);
const isSubCmd = query.endsWith(" ") || parts.length > 1;

return {
prefix: sanitizedPrefix,
action: parts[0],
parts,
lastPart: parts[parts.length - 1],
query,
isSubCmd,
};
Expand Down
Loading

0 comments on commit 6be9c98

Please sign in to comment.