Skip to content

Commit 4a095e4

Browse files
Initial commit
0 parents  commit 4a095e4

26 files changed

Lines changed: 953 additions & 0 deletions
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
name: Publish model
3+
description: Update model with doi and publish
4+
title: "Publish model"
5+
labels: ["model published"]
6+
7+
body:
8+
9+
- type: input
10+
id: doi
11+
attributes:
12+
label: -> doi
13+
placeholder: "https://doi.org/10.47366/sabia.v5n1a3"
14+
description: "Provide the doi of your published model"
15+
validations:
16+
required: true

.github/foo.txt

Whitespace-only changes.

.github/scripts/check_published.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
from github import Github, Auth
3+
4+
# Environment variables
5+
token = os.environ.get("GITHUB_TOKEN")
6+
repo_name = os.environ.get("REPO_NAME")
7+
8+
# Get repo
9+
auth = Auth.Token(token)
10+
g = Github(auth=auth)
11+
repo = g.get_repo(repo_name)
12+
13+
# Find if any of the issues has the published label
14+
published = False
15+
16+
for issue in repo.get_issues():
17+
for label in issue.labels:
18+
if 'published' in label.name:
19+
published = True
20+
21+
print(published)

.github/scripts/copy_files.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import os
2+
import base64
3+
from github import Github, Auth
4+
5+
# Environment variables
6+
token = os.environ.get("GITHUB_TOKEN")
7+
source_repo_owner = os.environ.get("SOURCE_REPO_OWNER")
8+
source_repo_name = os.environ.get("SOURCE_REPO_NAME")
9+
source_path = os.environ.get("SOURCE_PATH")
10+
target_repo_owner = os.environ.get("TARGET_REPO_OWNER")
11+
target_repo_name = os.environ.get("TARGET_REPO_NAME")
12+
target_branch_name = os.environ.get("TARGET_REPO_BRANCH")
13+
target_path = os.environ.get("TARGET_PATH")
14+
15+
auth = Auth.Token(token)
16+
g = Github(auth=auth)
17+
source_repo = g.get_repo(f"{source_repo_owner}/{source_repo_name}")
18+
target_repo = g.get_repo(f"{target_repo_owner}/{target_repo_name}")
19+
20+
def copy_files(contents, target_path):
21+
for content in contents:
22+
if content.type == "dir":
23+
# Get the contents of the directory and copy recursively
24+
copy_files(source_repo.get_contents(content.path), f"{target_path}/{content.name}")
25+
else:
26+
# Check if the file already exists in the target repo
27+
try:
28+
target_file = target_repo.get_contents(f"{target_path}/{content.name}", ref=target_branch_name)
29+
# File exists, compare contents
30+
if content.sha != target_file.sha:
31+
# Contents differ, update the file
32+
source_file_content = base64.b64decode(source_repo.get_git_blob(content.sha).content)
33+
target_repo.update_file(f"{target_path}/{content.name}",f"Updating {content.name}", source_file_content, target_file.sha, branch=target_branch_name)
34+
except:
35+
# Copy file to target repository
36+
source_file_content = base64.b64decode(source_repo.get_git_blob(content.sha).content)
37+
target_repo.create_file(f"{target_path}/{content.name}", f"Copying {content.name}", source_file_content, branch=target_branch_name)
38+
39+
# Get contents of source directory
40+
source_contents = source_repo.get_contents(source_path)
41+
42+
# Start copying files
43+
copy_files(source_contents, target_path)

.github/scripts/create_branch.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import os
2+
from github import Github, Auth
3+
4+
# Environment variables
5+
token = os.environ.get("GITHUB_TOKEN")
6+
repo_owner = os.environ.get("REPO_OWNER")
7+
repo_name = os.environ.get("REPO_NAME")
8+
branch_name = os.environ.get("BRANCH_NAME")
9+
10+
auth = Auth.Token(token)
11+
g = Github(auth=auth)
12+
repo = g.get_repo(f"{repo_owner}/{repo_name}")
13+
14+
# Check if the branch name already exists
15+
try:
16+
assert repo.get_git_ref(f"heads/{branch_name}").ref is not None
17+
print("Branch already exists")
18+
19+
# Create new branch if it doesn't
20+
except:
21+
base_ref = repo.get_git_ref(f"heads/{repo.default_branch}")
22+
23+
repo.create_git_ref(f"refs/heads/{branch_name}", base_ref.object.sha)

.github/scripts/file_utils.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import json
2+
from ruamel.yaml import YAML
3+
import csv
4+
import os
5+
from io import StringIO
6+
7+
def create_or_update_json_entry(rocrate, keys_path, new_value):
8+
"""
9+
Create or update a nested JSON entry in a ro-crate structure.
10+
11+
Args:
12+
rocrate (dict): The main ro-crate dictionary.
13+
keys_path (str): Dot-separated path to the key that needs updating.
14+
new_value (any): New value to be inserted or updated.
15+
"""
16+
# Split the keys path into individual components
17+
keys = keys_path.split('.')
18+
prefix = ""
19+
structure = rocrate
20+
21+
# Traverse through the nested structure using keys except the last one
22+
for key in keys[:-1]:
23+
key = prefix + key
24+
25+
# Handle potential './' prefix logic
26+
if key == "":
27+
prefix = "."
28+
continue
29+
else:
30+
prefix = ""
31+
32+
if isinstance(structure, list):
33+
# Find the item with matching '@id' key
34+
for item in structure:
35+
if item.get("@id") == key:
36+
structure = item
37+
break
38+
else:
39+
print(f"Key '{key}' not found.")
40+
return
41+
elif key in structure:
42+
structure = structure[key]
43+
else:
44+
print(f"Key '{key}' not found.")
45+
return
46+
47+
# The final key where the new value should be placed
48+
last_key = keys[-1]
49+
50+
# Update the value at the final key
51+
if last_key in structure:
52+
if isinstance(structure[last_key], list):
53+
# Prepend only if the new value is not already in the list
54+
if new_value not in structure[last_key]:
55+
structure[last_key].insert(0, new_value)
56+
else:
57+
# Convert existing non-list value to a list if needed
58+
structure[last_key] = [new_value, structure[last_key]]
59+
else:
60+
# If the key doesn't exist, create a new list with the new value
61+
structure[last_key] = [new_value]
62+
63+
64+
def navigate_and_assign(source, path, value):
65+
"""Navigate through a nested dictionary and assign a value to the specified path."""
66+
keys = path.split('.')
67+
for i, key in enumerate(keys[:-1]):
68+
if key.isdigit(): # If the key is a digit, it's an index for a list
69+
key = int(key)
70+
while len(source) <= key: # Extend the list if necessary
71+
source.append({})
72+
source = source[key]
73+
else:
74+
if i < len(keys) - 2 and keys[i + 1].isdigit(): # Next key is a digit, so ensure this key leads to a list
75+
source = source.setdefault(key, [])
76+
else: # Otherwise, it leads to a dictionary
77+
source = source.setdefault(key, {})
78+
# Assign the value to the final key
79+
if keys[-1].isdigit(): # If the final key is a digit, it's an index for a list
80+
key = int(keys[-1])
81+
while len(source) <= key: # Extend the list if necessary
82+
source.append(None)
83+
source[key] = value
84+
else:
85+
source[keys[-1]] = value
86+
87+
88+
def read_yaml_with_header(file_path):
89+
"""
90+
Read YAML content inside YAML header delimiters '---'
91+
"""
92+
93+
with open(file_path,'r') as file:
94+
data = file.read()
95+
96+
yaml = YAML()
97+
yaml_content = yaml.load(data.strip('---\n'))
98+
99+
return yaml_content
100+
101+
def update_csv_content(file_path, field, value):
102+
# Read the CSV file and update the field value
103+
updated_rows = []
104+
field_exists = False
105+
with open(file_path, mode='r', newline='') as file:
106+
reader = csv.reader(file)
107+
for row in reader:
108+
if row and row[0] == field:
109+
row[1] = value
110+
field_exists = True
111+
updated_rows.append(row)
112+
113+
# If the field does not exist, add a new line
114+
if not field_exists:
115+
updated_rows.append([field, value])
116+
117+
# Convert the updated rows back into a CSV-formatted string
118+
updated_csv_content = StringIO()
119+
writer = csv.writer(updated_csv_content)
120+
writer.writerows(updated_rows)
121+
updated_csv_string = updated_csv_content.getvalue()
122+
123+
return updated_csv_string

.github/scripts/find_repos.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
import json
3+
import requests
4+
from github import Github, Auth
5+
6+
# Environment variables
7+
token = os.environ.get("TOKEN")
8+
repo_name = os.environ.get("REPO")
9+
org = os.environ.get("ORG")
10+
11+
repos = []
12+
13+
# Get org
14+
auth = Auth.Token(token)
15+
g = Github(auth=auth)
16+
org = g.get_organization(org)
17+
18+
# Find repos created from this template
19+
for repo in org.get_repos():
20+
repo_json = requests.get(repo.url).json()
21+
if 'template_repository' in repo_json:
22+
if repo_json['template_repository']['name'] == repo_name:
23+
repos.append(repo.name)
24+
25+
print(json.dumps(repos))

0 commit comments

Comments
 (0)