-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add automatic update license workflow
Signed-off-by: Andrew-Chen-Wang <[email protected]>
- Loading branch information
1 parent
0d42cfb
commit a33be2a
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,34 @@ | ||
name: Update Licenses | ||
|
||
on: | ||
# Every day at 2am | ||
schedule: | ||
- cron: "0 2 * * *" | ||
# Manual trigger | ||
workflow_dispatch: | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: 3.8 | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements.txt | ||
- name: Update list | ||
run: python scripts/update_licenses.py | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Commit changes | ||
uses: stefanzweifel/[email protected] | ||
with: | ||
commit_message: Update Licenses | ||
file_pattern: cookiecutter.json {{cookiecutter.project_slug}}/licenses/*.txt |
This file contains 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,35 @@ | ||
import json | ||
import os | ||
from pathlib import Path | ||
from github import Github | ||
|
||
CURRENT_FILE = Path(__file__) | ||
ROOT = CURRENT_FILE.parents[1] | ||
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN", None) | ||
|
||
|
||
def main() -> None: | ||
""" | ||
Script entry point. | ||
""" | ||
repo = Github(login_or_token=GITHUB_TOKEN).get_repo("github/choosealicense.com") | ||
license_dir = ROOT / "{{cookiecutter.project_slug}}" / "licenses" | ||
titles = [] | ||
for file in repo.get_contents("_licenses", "gh-pages"): | ||
content = file.decoded_content.decode(file.encoding) | ||
titles.append(content.split("\n", maxsplit=2)[1].replace("title: ", "")) | ||
(license_dir / file.name).write_text(content) | ||
# Put "Not open source" at front so people know it's an option | ||
update_cookiecutter(["Not open source"] + sorted(titles)) | ||
|
||
|
||
def update_cookiecutter(titles: list): | ||
with open("cookiecutter.json") as f: | ||
data = json.load(f) | ||
data["open_source_license"] = titles | ||
with open("cookiecutter.json", "wt") as f: | ||
json.dump(data, f, indent=2) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |