diff --git a/.github/workflows/update-licenses.yml b/.github/workflows/update-licenses.yml new file mode 100644 index 0000000000..344120e1dc --- /dev/null +++ b/.github/workflows/update-licenses.yml @@ -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/git-auto-commit-action@v4.9.1 + with: + commit_message: Update Licenses + file_pattern: cookiecutter.json {{cookiecutter.project_slug}}/licenses/*.txt diff --git a/scripts/update_licenses.py b/scripts/update_licenses.py new file mode 100644 index 0000000000..457471b612 --- /dev/null +++ b/scripts/update_licenses.py @@ -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()