Skip to content

Commit

Permalink
Add automatic update license workflow
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew-Chen-Wang <[email protected]>
  • Loading branch information
Andrew-Chen-Wang committed Feb 27, 2021
1 parent 0d42cfb commit a33be2a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/update-licenses.yml
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
35 changes: 35 additions & 0 deletions scripts/update_licenses.py
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()

0 comments on commit a33be2a

Please sign in to comment.