|
| 1 | +name: Update |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # The scheduled jobs run only on `master`. Then we checkout `ja-all-enable`. |
| 6 | + # <https://github.community/t5/GitHub-Actions/Scheduled-builds-of-non-default-branch/td-p/32270> |
| 7 | + - cron: '0 0 * * *' |
| 8 | + push: |
| 9 | + branches: |
| 10 | + - master |
| 11 | + - ja-all-enabled |
| 12 | + |
| 13 | +jobs: |
| 14 | + update: |
| 15 | + name: Update |
| 16 | + runs-on: ubuntu-18.04 |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: checkout |
| 20 | + uses: actions/checkout@v2 |
| 21 | + with: |
| 22 | + ref: ja-all-enabled |
| 23 | + |
| 24 | + - name: setup-python |
| 25 | + uses: actions/setup-python@v1 |
| 26 | + with: |
| 27 | + python-version: 3.8.0 |
| 28 | + |
| 29 | + - name: rust-toolchain |
| 30 | + uses: actions-rs/toolchain@v1 |
| 31 | + with: |
| 32 | + toolchain: stable-x86_64-unknown-linux-gnu |
| 33 | + default: true |
| 34 | + profile: minimal |
| 35 | + |
| 36 | + - name: '`cargo install --debug --bin cargo-add cargo-edit`' |
| 37 | + uses: actions-rs/cargo@v1 |
| 38 | + with: |
| 39 | + command: install |
| 40 | + args: --debug --bin cargo-add cargo-edit |
| 41 | + |
| 42 | + - name: '`cargo install --debug cargo-outdated`' |
| 43 | + uses: actions-rs/cargo@v1 |
| 44 | + with: |
| 45 | + command: install |
| 46 | + args: --debug cargo-outdated |
| 47 | + |
| 48 | + - name: Upgrade the dependencies |
| 49 | + id: upgrade |
| 50 | + run: | |
| 51 | + import json |
| 52 | + import subprocess |
| 53 | + from collections import defaultdict |
| 54 | +
|
| 55 | + cargo_outdated = json.loads(subprocess.run( |
| 56 | + ['cargo', 'outdated', '--format', 'json', '-d', '1'], |
| 57 | + stdout=subprocess.PIPE, check=True, |
| 58 | + ).stdout.decode('utf8')) |
| 59 | +
|
| 60 | + crates = defaultdict(list) |
| 61 | + body = '<ul>' |
| 62 | +
|
| 63 | + for dependency in cargo_outdated['dependencies']: |
| 64 | + if dependency['kind'] != 'Normal': |
| 65 | + raise NotImplemented() |
| 66 | + crates[dependency['platform']].append( |
| 67 | + (dependency['name'], dependency['latest']), |
| 68 | + ) |
| 69 | + body += f'<li>' \ |
| 70 | + f'<a href="https://crates.io/crates/{dependency["name"]}">' \ |
| 71 | + f'{dependency["name"]}' \ |
| 72 | + f'</a> ' \ |
| 73 | + f'v{dependency["project"]} → v{dependency["latest"]}' \ |
| 74 | + f'</li>' |
| 75 | + body += '</ul>' |
| 76 | + if body == '<ul></ul>': |
| 77 | + body = 'Only dependencies of dependencies.' |
| 78 | +
|
| 79 | + for target, crates in crates.items(): |
| 80 | + args = ['cargo', 'add'] |
| 81 | + if target: |
| 82 | + args.extend(['--target', target]) |
| 83 | + args.extend(f'{name}@={ver}' for name, ver in crates) |
| 84 | + subprocess.run(args, check=True) |
| 85 | +
|
| 86 | + print(f'::set-output name=body::{body}') |
| 87 | + shell: python3 {0} |
| 88 | + |
| 89 | + - name: '`cargo update`' |
| 90 | + uses: actions-rs/cargo@v1 |
| 91 | + with: |
| 92 | + command: update |
| 93 | + |
| 94 | + - name: Create Pull Request |
| 95 | + uses: peter-evans/create-pull-request@v2 |
| 96 | + with: |
| 97 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 98 | + commit-message: '[bot] Update the crates' |
| 99 | + title: '[bot] Update the crates' |
| 100 | + body: ${{ steps.upgrade.outputs.body }} |
| 101 | + branch: ja-all-enabled-update-the-crates |
| 102 | + base: ja-all-enabled |
0 commit comments