|
1 | 1 | # .github/scripts/update_publiccode.py |
2 | 2 |
|
3 | | -import os |
4 | 3 | import yaml |
5 | | -import warnings |
6 | | - |
7 | | -# Check the metadata of the repository |
8 | | -# For the simplicity, we're assuming that the metadata are environment variables |
9 | | -# In a real case, you would fetch the metadata from the repository or other sources |
10 | | - |
11 | | -# Metadata variables |
12 | | -variables = [ |
13 | | - "GITHUB_REPOSITORY", |
14 | | - "REPO_DESCRIPTION", |
15 | | - "ORGANISATION", |
16 | | - "TAGS", |
17 | | - "WEBSITE", |
18 | | - "LICENCE" |
19 | | -] |
20 | | - |
21 | | -# Create a dictionary to hold the metadata |
22 | | -metadata = {} |
23 | | - |
24 | | -# Check if each variable is set and if not, issue a warning |
25 | | -for var in variables: |
26 | | - value = os.getenv(var) |
27 | | - if value: |
28 | | - metadata[var] = value |
29 | | - else: |
30 | | - warnings.warn(f"The {var} environment variable is not set") |
31 | | - |
32 | | -# Always set the repository url |
33 | | -metadata["REPO_URL"] = f"https://github.com/{metadata.get('GITHUB_REPOSITORY', 'default_repo_name')}" |
34 | | - |
35 | | -# Read the current publiccode.yaml (if exists) |
| 4 | +import json |
| 5 | +from datetime import datetime |
| 6 | + |
| 7 | +# Read existing publiccode.yaml |
36 | 8 | try: |
37 | | - with open("publiccode.yaml", "r") as file: |
38 | | - publiccode = yaml.safe_load(file) |
| 9 | + with open("publiccode.yaml", "r") as f: |
| 10 | + data = yaml.safe_load(f) |
39 | 11 | except FileNotFoundError: |
40 | | - publiccode = {} |
41 | | - |
42 | | -# Update the publiccode with the new metadata |
43 | | -publiccode["publiccode"] = publiccode.get("publiccode", {}) |
44 | | -publiccode["publiccode"]["name"] = metadata.get("GITHUB_REPOSITORY") |
45 | | -publiccode["publiccode"]["description"] = { |
46 | | - "nl": metadata.get("REPO_DESCRIPTION") |
47 | | -} |
48 | | -publiccode["publiccode"]["url"] = metadata.get("REPO_URL") |
49 | | -publiccode["publiccode"]["tags"] = metadata.get("TAGS") |
50 | | -publiccode["publiccode"]["website"] = metadata.get("WEBSITE") |
51 | | -publiccode["publiccode"]["license"] = metadata.get("LICENCE") |
52 | | -publiccode["publiccode"]["owner"] = metadata.get("ORGANISATION") |
53 | | - |
54 | | -# Write back the publiccode.yaml |
55 | | -with open("publiccode.yaml", "w") as file: |
56 | | - yaml.safe_dump(publiccode, file) |
| 12 | + data = {} |
| 13 | + |
| 14 | +# Convert created_at to date format |
| 15 | +# created_at_date = datetime.fromisoformat("$REPO_CREATED_AT".replace("Z", "+00:00")).strftime('%Y-%m-%d') |
| 16 | +created_at_date = datetime.now().strftime('%Y-%m-%d') |
| 17 | + |
| 18 | +# Convert topics JSON string to Python list and then to comma-separated string |
| 19 | + |
| 20 | +# Update or append values |
| 21 | +if "$REPO_NAME" != "null" and "$REPO_NAME": |
| 22 | + data['name'] = "$REPO_NAME" |
| 23 | +if "$REPO_URL" != "null" and "$REPO_URL": |
| 24 | + data['url'] = "$REPO_URL" |
| 25 | +if "$REPO_DESC" != "null" and "$REPO_DESC": |
| 26 | + data['description'] = "$REPO_DESC" |
| 27 | +if "$REPO_HOMEPAGE" != "null" and "$REPO_HOMEPAGE": |
| 28 | + data['url'] = "$REPO_HOMEPAGE" |
| 29 | +#if "$REPO_TOPICS" != "null" and "$REPO_TOPICS": |
| 30 | +# data['topics'] = "$REPO_TOPICS" |
| 31 | +if "$REPO_LICENSE" != "null" and "$REPO_LICENSE": |
| 32 | + data['license'] = "$REPO_LICENSE" |
| 33 | + |
| 34 | +# Add releaseDate if not present |
| 35 | +if 'releaseDate' not in data: |
| 36 | + data['releaseDate'] = created_at_date |
| 37 | + |
| 38 | +# Create or update nested 'organisation' array |
| 39 | +if 'organisation' not in data: |
| 40 | + data['organisation'] = {} |
| 41 | +if "$ORGANISATION_NAME" != "null" and "$ORGANISATION_NAME": |
| 42 | + data['organisation']['name'] = "$ORGANISATION_NAME" |
| 43 | +if "$ORGANISATION_AVATAR" != "null" and "$ORGANISATION_AVATAR": |
| 44 | + data['organisation']['logo'] = "$ORGANISATION_AVATAR" |
| 45 | +if "$ORGANISATION_URL" != "null" and "$ORGANISATION_URL": |
| 46 | + data['organisation']['url'] = "$ORGANISATION_URL" |
| 47 | +if "$ORGANISATION_DESCRIPTION" != "null" and "$ORGANISATION_DESCRIPTION": |
| 48 | + data['organisation']['description'] = "$ORGANISATION_DESCRIPTION" |
| 49 | + |
| 50 | +# Create or update nested 'nl' array |
| 51 | +if 'nl' not in data: |
| 52 | + data['nl'] = {} |
| 53 | + |
| 54 | +# Write updated publiccode.yaml |
| 55 | +with open("publiccode.yaml", "w") as f: |
| 56 | + yaml.safe_dump(data, f) |
| 57 | +END |
0 commit comments