-
-
Notifications
You must be signed in to change notification settings - Fork 1
Add EUVD mirror pipeline #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Samk1710
wants to merge
10
commits into
aboutcode-org:main
Choose a base branch
from
Samk1710:add_EUVD_mirror_pipeline
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8e69c78
Add EUVD mirror pipeline
Samk1710 f75cf19
Update DEFAULT_START_YEAR to Unix Epoch
Samk1710 91cbede
Refactor EUVD sync as per suggestions
Samk1710 ecee45c
Add logging to unexpected results from API
Samk1710 217d3ae
Address review feedback for EUVD catalog mirror pipeline
Samk1710 c5ac22f
Improve code readability and optimize initial fetches
Samk1710 26c826f
Refactor to checkpoint based mirror sync
Samk1710 2b065b9
Update REQUEST_TIMEOUT
Samk1710 e9578fe
Remove empty line in sync_catalog.py
Samk1710 9e9a6ff
Address review comments
Samk1710 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| name: Daily sync of EUVD catalog | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| schedule: | ||
| - cron: '0 0 * * *' | ||
|
|
||
| permissions: | ||
| contents: write | ||
|
|
||
| jobs: | ||
| scheduled: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up Python | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: '3.10' | ||
|
|
||
| - name: Install required packages | ||
| run: pip install -r requirements.txt | ||
|
|
||
| - name: Run sync | ||
| run: python sync_catalog.py | ||
|
|
||
| - name: Commit and push if it changed | ||
| run: |- | ||
| git config user.name "AboutCode Automation" | ||
| git config user.email "automation@aboutcode.org" | ||
| git add -A | ||
| timestamp=$(date -u) | ||
| git commit -m "$(echo -e "Sync EUVD catalog: $timestamp\n\nSigned-off-by: AboutCode Automation <automation@aboutcode.org>")" || exit 0 | ||
| git push |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Various junk and temp files | ||
| .DS_Store | ||
| *~ | ||
| .*.sw[po] | ||
| .build | ||
| .ve | ||
| *.bak | ||
| var | ||
| share | ||
| selenium | ||
| local | ||
| /dist/ | ||
| /.*cache/ | ||
| /.venv/ | ||
| /.python-version | ||
| /.pytest_cache/ | ||
| /scancodeio.egg-info/ | ||
| *.rdb | ||
| *.aof | ||
| .vscode | ||
| .ipynb_checkpoints |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| requests==2.32.5 | ||
| aboutcode.pipeline==0.2.1 | ||
| python-dateutil==2.9.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| # | ||
| # Copyright (c) nexB Inc. and others. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # See http://www.apache.org/licenses/LICENSE-2.0 for the license text. | ||
| # See https://aboutcode.org for more information about nexB OSS projects. | ||
| # | ||
|
|
||
| import json | ||
| import math | ||
| import sys | ||
| from datetime import date, datetime, timezone | ||
| from pathlib import Path | ||
|
|
||
| from dateutil.parser import parse | ||
| import requests | ||
| from aboutcode.pipeline import BasePipeline, LoopProgress | ||
| from requests.adapters import HTTPAdapter | ||
| from urllib3.util.retry import Retry | ||
|
|
||
| ROOT_PATH = Path(__file__).parent | ||
| ADVISORIES_PATH = ROOT_PATH / "advisories" | ||
| CHECKPOINT_FILE = ROOT_PATH / "checkpoint.json" | ||
|
|
||
| HEADERS = { | ||
| "Accept": "application/json", | ||
| } | ||
|
|
||
| PAGE_SIZE = 100 | ||
| REQUEST_TIMEOUT = 15 | ||
|
|
||
|
|
||
| class EUVDAdvisoryMirror(BasePipeline): | ||
| url = "https://euvdservices.enisa.europa.eu/api/search" | ||
|
|
||
| @classmethod | ||
| def steps(cls): | ||
| return ( | ||
| cls.load_checkpoint, | ||
| cls.create_session, | ||
| cls.collect_new_advisory, | ||
| cls.save_checkpoint, | ||
| ) | ||
|
|
||
| def load_checkpoint(self): | ||
| """ | ||
| - Load the ``last run`` date from checkpoint.json to fetch only new advisories. | ||
| - If the checkpoint.json does not exist, fetch all advisories. | ||
| """ | ||
| self.fetch_params = {} | ||
| if not CHECKPOINT_FILE.exists(): | ||
| return | ||
| with CHECKPOINT_FILE.open() as f: | ||
| checkpoint = json.load(f) | ||
| if last_run := checkpoint.get("last_run"): | ||
| self.fetch_params["fromUpdatedDate"] = last_run | ||
|
|
||
| def create_session(self): | ||
| retry = Retry( | ||
| total=3, backoff_factor=1, status_forcelist=[429, 500, 502, 503, 504] | ||
| ) | ||
| adapter = HTTPAdapter(max_retries=retry) | ||
| self.session = requests.Session() | ||
| self.session.headers.update(HEADERS) | ||
| self.session.mount("https://", adapter) | ||
|
|
||
| def collect_new_advisory(self): | ||
| """ | ||
| Fetch new advisories from the EUVD API with paginated requests. | ||
|
|
||
| - Fetch the ``total`` advisories and determine the number of pages to iterate over. | ||
| - Iterate through all pages, fetching up to PAGE_SIZE advisories per request. | ||
| - Save each advisory as a JSON file at ``/advisories/{year}/{month}/{EUVD_ID}.json``. | ||
| - Advisories with missing publication dates are stored as at ``/advisories/unpublished/{EUVD_ID}.json``. | ||
| """ | ||
| count_page = self.fetch_page({**self.fetch_params, "size": 1, "page": 0}) | ||
| total = count_page.get("total", 0) | ||
|
|
||
| total_pages = math.ceil(total / PAGE_SIZE) | ||
| self.log(f"Collecting {total} advisories across {total_pages} pages") | ||
|
|
||
| progress = LoopProgress(total_iterations=total_pages, logger=self.log) | ||
|
|
||
| for page in progress.iter(range(total_pages)): | ||
| data = self.fetch_page( | ||
| {**self.fetch_params, "size": PAGE_SIZE, "page": page} | ||
| ) | ||
| for advisory in data.get("items", []): | ||
| self.save_advisory(advisory) | ||
|
|
||
| def save_advisory(self, advisory): | ||
| destination = "unpublished" | ||
| euvd_id = advisory["id"] | ||
|
|
||
| if published := advisory.get("datePublished"): | ||
| published_date = parse(published) | ||
| destination = f"{published_date.year}/{published_date.month:02d}" | ||
|
|
||
| path = ADVISORIES_PATH / f"{destination}/{euvd_id}.json" | ||
| path.parent.mkdir(parents=True, exist_ok=True) | ||
| with open(path, "w", encoding="utf-8") as f: | ||
| json.dump(advisory, f, indent=2) | ||
|
|
||
| def save_checkpoint(self): | ||
| with CHECKPOINT_FILE.open("w") as f: | ||
| json.dump({"last_run": date.today().isoformat()}, f, indent=2) | ||
|
|
||
| def fetch_page(self, params): | ||
| response = self.session.get(self.url, params=params, timeout=REQUEST_TIMEOUT) | ||
| response.raise_for_status() | ||
| return response.json() or {} | ||
|
|
||
| def log(self, message): | ||
| now_local = datetime.now(timezone.utc).astimezone() | ||
| timestamp = now_local.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] | ||
| print(f"{timestamp} {message}") | ||
|
|
||
| if __name__ == "__main__": | ||
| mirror = EUVDAdvisoryMirror() | ||
| status_code, error_message = mirror.execute() | ||
| if error_message: | ||
| print(error_message) | ||
| sys.exit(status_code) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.