Skip to content

Commit

Permalink
add github action to run generate manually
Browse files Browse the repository at this point in the history
  • Loading branch information
vorant94 committed Jul 24, 2024
1 parent f739074 commit 5c689e6
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 62 deletions.
25 changes: 1 addition & 24 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,6 @@ name: CI/CD
on:
push:
branches: ['master']
workflow_dispatch:

permissions:
contents: read
pages: write
id-token: write

concurrency:
group: 'pages'
cancel-in-progress: false

jobs:
format:
Expand All @@ -33,24 +23,11 @@ jobs:

- run: npm run lint:check

generate:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/setup

- run: npm run build --workspace @sofash/cli

- run: node packages/cli/dist/rav-hen/scripts/create-calendar.js --cinemaId 1058 --date 2024-06-09

- uses: actions/upload-pages-artifact@v3
with:
path: './packages/cli/data'

deploy:
runs-on: ubuntu-latest
needs:
- generate
steps:
- uses: actions/deploy-pages@v4
46 changes: 46 additions & 0 deletions .github/workflows/rav-hen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Generate Calendar for Rav-Hen

on:
workflow_dispatch:
inputs:
branchName:
description: 'Rav Hen branch to search cinemas for'
required: true
type: choice
options:
- givataiim
- dizengoff
- "kiryat ono"
date:
description: 'Date to search cinemas for formatted as YYYY-MM-DD'
required: true
type: string

permissions:
contents: read
pages: write
id-token: write


jobs:
generate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/setup

- run: npm run build --workspace @sofash/cli

- run: node packages/cli/dist/rav-hen/scripts/create-calendar.js -b ${{ inputs.branchName }} -d ${{ inputs.date }}

- uses: actions/upload-pages-artifact@v3
with:
path: './packages/cli/data'

deploy:
runs-on: ubuntu-latest
needs:
- generate
steps:
- uses: actions/deploy-pages@v4
1 change: 1 addition & 0 deletions packages/cli/local.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NODE_ENV=development
9 changes: 9 additions & 0 deletions packages/cli/src/config/globals/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { z } from "zod";

const envSchema = z.object({
// biome-ignore lint/style/useNamingConvention: env variables have different convention
NODE_ENV: z.enum(["development", "production"]).default("development"),
});
export type Env = z.infer<typeof envSchema>;

export const env = envSchema.parse(process.env);
59 changes: 51 additions & 8 deletions packages/cli/src/ical/utils/create-ical-calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,64 @@ import ical, {
type ICalCalendar,
type ICalCalendarJSONData,
} from "ical-generator";
import { env } from "../../config/globals/env.js";
import { getPathsOfDataFiles } from "./get-paths-of-data-files.js";
import { getUrlsOfDataFiles } from "./get-urls-of-data-files.js";

// biome-ignore lint/style/useNamingConvention: ICal is a separate term
export async function createICalCalendar(
jsonPath: string,
name: string,
cinemaName: string,
branchName: string,
): Promise<ICalCalendar> {
let jsonData: string | null = null;
const jsonData: unknown | null =
env.NODE_ENV === "development"
? await getExistingICalCalendarFromLocalFilesystem(cinemaName, branchName)
: await getExistingICalCalendarFromRemoteHost(cinemaName, branchName);

return ical(
jsonData ? (jsonData as ICalCalendarJSONData) : { name: branchName },
);
}

// biome-ignore lint/style/useNamingConvention: ICal is a separate term
async function getExistingICalCalendarFromLocalFilesystem(
cinemaName: string,
branchName: string,
): Promise<unknown | null> {
const [_, jsonPath] = await getPathsOfDataFiles(cinemaName, branchName);

let data: unknown | null = null;

try {
jsonData = await fs.readFile(jsonPath, { encoding: "utf-8" });
consola.debug("Found previous events file, will add new events to it");
data = JSON.parse(await fs.readFile(jsonPath, { encoding: "utf-8" }));
consola.debug(
"Found previous events file locally, will add new events to it",
);
} catch (_e) {
consola.debug("No previous events file found, will create a new one");
}

return ical(
jsonData ? (JSON.parse(jsonData) as ICalCalendarJSONData) : { name },
);
return data;
}

// biome-ignore lint/style/useNamingConvention: ICal is a separate term
async function getExistingICalCalendarFromRemoteHost(
cinemaName: string,
branchName: string,
): Promise<unknown | null> {
const [_, jsonUrl] = getUrlsOfDataFiles(cinemaName, branchName);

let data: unknown | null = null;

try {
const response = await fetch(jsonUrl);
data = await response.json();
consola.debug(
"Found previous events file remotely, will add new events to it",
);
} catch (_e) {
consola.debug("No previous events file found, will create a new one");
}

return data;
}
12 changes: 12 additions & 0 deletions packages/cli/src/ical/utils/get-urls-of-data-files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { URL } from "node:url";

export function getUrlsOfDataFiles(
cinemaName: string,
branchName: string,
): [URL, URL] {
const base = "https://vorant94.github.io/";
const icsUrl = new URL(`sofash/${cinemaName}-${branchName}.ics`, base);
const jsonUrl = new URL(`sofash/${cinemaName}-${branchName}.json`, base);

return [icsUrl, jsonUrl];
}
2 changes: 0 additions & 2 deletions packages/cli/src/rav-hen/globals/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { URL } from "node:url";
import { cinemaIdToName } from "../types/cinema-id.js";

export const config = {
baseUrl: new URL("https://www.rav-hen.co.il"),
tenantId: 10104,
timeZone: "Asia/Jerusalem",
cinemaIdToName,
} as const;
6 changes: 3 additions & 3 deletions packages/cli/src/rav-hen/models/film-event.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import consola from "consola";
import { format } from "date-fns";
import { z } from "zod";
import { config } from "../globals/config.js";
import type { CinemaId } from "../types/cinema-id.js";
import type { BranchId } from "../types/branch-id.js";
import { filmEventSchema } from "./film-event.model.js";
import { filmSchema } from "./film.model.js";

export async function findFilmEvents(
cinemaId: CinemaId,
branchId: BranchId,
date: Date,
): Promise<FindFilmEventsResponseBody> {
const formattedDate = format(date, "yyyy-MM-dd");

const url = new URL(
`/rh/data-api-service/v1/quickbook/${config.tenantId}/film-events/in-cinema/${cinemaId}/at-date/${formattedDate}`,
`/rh/data-api-service/v1/quickbook/${config.tenantId}/film-events/in-cinema/${branchId}/at-date/${formattedDate}`,
config.baseUrl,
);

Expand Down
26 changes: 12 additions & 14 deletions packages/cli/src/rav-hen/scripts/create-calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,30 @@ import { differenceInDays } from "date-fns";
import { z } from "zod";
import { createICalCalendar } from "../../ical/utils/create-ical-calendar.js";
import { getPathsOfDataFiles } from "../../ical/utils/get-paths-of-data-files.js";
import { config } from "../globals/config.js";
import { findFilmEvents } from "../models/film-event.client.js";
import { cinemaIdSchema } from "../types/cinema-id.js";
import {
branchNameSchema,
branchNameToBranchId,
} from "../types/branch-name.js";
import { fillCalendarWithFilmEvents } from "../utils/fill-calendar-with-film-events.js";

consola.start("Creating Rav-Hen calendar");

consola.info("Parsing CLI args");
const args = parseArgs({
options: {
cinemaId: {
branchName: {
type: "string",
short: "b",
},
date: {
type: "string",
short: "d",
},
},
});
const argsSchema = z.object({
cinemaId: cinemaIdSchema,
branchName: branchNameSchema,
date: z.coerce.date().superRefine((value, context) => {
const date = new Date(value);
if (differenceInDays(date, new Date()) < 0) {
Expand All @@ -35,22 +39,16 @@ const argsSchema = z.object({
}
}),
});
const { cinemaId, date } = argsSchema.parse(args.values);
const { branchName, date } = argsSchema.parse(args.values);

consola.info("Ensuring data dir is in place");
const [icsPath, jsonPath] = await getPathsOfDataFiles(
"rav-hen",
config.cinemaIdToName[cinemaId],
);
const [icsPath, jsonPath] = await getPathsOfDataFiles("rav-hen", branchName);

consola.info("Creating calendar");
const calendar = await createICalCalendar(
jsonPath,
config.cinemaIdToName[cinemaId],
);
const calendar = await createICalCalendar("rav-hen", branchName);

consola.info("Fetching film events...");
const data = await findFilmEvents(cinemaId, date);
const data = await findFilmEvents(branchNameToBranchId[branchName], date);

consola.info("Filling calendar with fetched events...");
fillCalendarWithFilmEvents(calendar, data);
Expand Down
12 changes: 12 additions & 0 deletions packages/cli/src/rav-hen/types/branch-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { z } from "zod";
import type { BranchName } from "./branch-name.js";

export const branchIds = ["1058", "1071", "1062"] as const;
export type BranchId = (typeof branchIds)[number];
export const branchIdSchema = z.enum(branchIds);

export const branchIdToBranchName = {
1058: "givataiim",
1071: "dizengoff",
1062: "kiryat ono",
} as const satisfies Record<BranchId, BranchName>;
12 changes: 12 additions & 0 deletions packages/cli/src/rav-hen/types/branch-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { z } from "zod";
import type { BranchId } from "./branch-id.js";

export const branchNames = ["givataiim", "dizengoff", "kiryat ono"] as const;
export type BranchName = (typeof branchNames)[number];
export const branchNameSchema = z.enum(branchNames);

export const branchNameToBranchId = {
givataiim: "1058",
dizengoff: "1071",
"kiryat ono": "1062",
} as const satisfies Record<BranchName, BranchId>;
11 changes: 0 additions & 11 deletions packages/cli/src/rav-hen/types/cinema-id.ts

This file was deleted.

0 comments on commit 5c689e6

Please sign in to comment.