From 7dd38e9b44a617fe37f7a982217f0f7703648fe1 Mon Sep 17 00:00:00 2001 From: David M <62346025+aboutdavid@users.noreply.github.com> Date: Thu, 16 May 2024 18:56:20 -0400 Subject: [PATCH] update python cli + add pr checking --- .github/workflows/check-pr.yml | 15 ++++++++++++ add_flight.py | 42 ++++++++++++++++++++++++++++++---- ci.js | 36 +++++++++++++++++++++++++++++ utils.js | 1 - 4 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/check-pr.yml create mode 100644 ci.js diff --git a/.github/workflows/check-pr.yml b/.github/workflows/check-pr.yml new file mode 100644 index 0000000..ef4e0ba --- /dev/null +++ b/.github/workflows/check-pr.yml @@ -0,0 +1,15 @@ +name: gitme +on: [push, pull_request] +jobs: + check-valid-flights: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: "18.x" + - run: "TZ='America/New_York'; export TZ" + - run: npm install + - run: node ci.js + env: + FLIGHT_API_TOKEN: ${{ secrets.AEROAPI_TOKEN_GH }} diff --git a/add_flight.py b/add_flight.py index 4da4295..b8b17ca 100644 --- a/add_flight.py +++ b/add_flight.py @@ -1,20 +1,54 @@ import yaml +import re +from datetime import datetime +def is_valid_iata(code): + return re.fullmatch(r'[A-Z]{2}\d{1,4}', code) is not None +def is_valid_date(date): + # Check if the date matches the format YYYY-MM-DD + try: + datetime.strptime(date, '%Y-%m-%d') + return True + except ValueError: + return False def add_flights(): with open('config.yaml', 'r') as file: data = yaml.safe_load(file) user_name = input("Enter your name: ") - user_id = input("Enter your slack ID: ") + user_id = input("Enter your user ID: ") + + print("Select an event:") + for i, event in enumerate(data['events']): + print(f"{i+1} for {event['name']}") + while True: + try: + event_index = int(input("Enter your choice: ")) - 1 + if 0 <= event_index < len(data['events']): + break + else: + print("Invalid choice. Please try again.") + except ValueError: + print("Invalid input. Please enter a number.") user = {'id': user_id, 'name': user_name, 'flights': []} - data['events'][0]['users'].append(user) # Add the new user to the first event + data['events'][event_index]['users'].append(user) while True: - flight_code = input("Enter your flight code (or 'q' to quit): ") + while True: + flight_code = input("Enter your flight code (or 'q' to quit): ") + if flight_code.lower() == 'q' or is_valid_iata(flight_code): + break + else: + print("Invalid flight code. Please try again.") if flight_code.lower() == 'q': break - flight_date = input("Enter your flight date (YYYY-MM-DD): ") + while True: + flight_date = input("Enter your flight date (YYYY-MM-DD): ") + if is_valid_date(flight_date): + break + else: + print("Invalid date format. Please try again.") user['flights'].append({ 'code': flight_code, diff --git a/ci.js b/ci.js new file mode 100644 index 0000000..bfe8bb6 --- /dev/null +++ b/ci.js @@ -0,0 +1,36 @@ +require("dotenv").config() +const yaml = require('js-yaml'); +const utils = require("./utils") +const fs = require('fs'); +var config = "" + +try { + config = yaml.load(fs.readFileSync('./config.yaml', 'utf8')); +} catch (e) { + console.error("Failed to parse YAML. See below:") + console.error(e) + process.exit(1) +} + +config.events.forEach(async event => { + console.log(`Checking event: ${event.name}`) + event.users.map(async user => { + var flights = await Promise.all(user.flights.map(async flight => { + var res + try { + res = await utils.checkFlight(flight.code, new Date(flight.date)); + } catch (e) { + console.error(`Failed to get flight info for event ${event.name}: ${flight.code} on ${flight.date} for user ${user.name}/${user.id}`) + process.exit(e) + } + if (res.error) { + console.error(`Failed to get flight info for event ${event.name}: ${flight.code} on ${flight.date} for user ${user.name}/${user.id}`) + process.exit(1) + } + console.error(`PASS: ${event.name}: ${flight.code} on ${flight.date} for user ${user.name}/${user.id}`) + })) + flights = flights.filter(item => item).sort(function (a, b) { + return new Date(a.scheduled_out) - new Date(b.scheduled_out); + }); + }) +}) \ No newline at end of file diff --git a/utils.js b/utils.js index 916b887..673433f 100644 --- a/utils.js +++ b/utils.js @@ -3,7 +3,6 @@ const reachedDateLimit = (date) => (date - Date.now()) > 2 * 24 * 60 * 60 * 1000 async function checkFlight(code, date) { if (reachedDateLimit(date)) return { error: true } - if (!/^[A-Za-z]{2}\d{1,4}$/.test(code)) return { invalid: true } const res = await fetch(`https://aeroapi.flightaware.com/aeroapi/flights/${code}?` + new URLSearchParams({ start: new Date(date).toISOString(), max_pages: 1