Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update workflow for dependabot PRs #1356

Merged
merged 2 commits into from
Jul 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions .ci_support/pypi_vs_conda_names.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"tables": "pytables",
"pyiron-base": "pyiron_base",
"pyiron-atomistics": "pyiron_atomistics",
"pyiron-contrib": "pyiron_contrib",
"pyiron-experimental": "pyiron_experimental",
"pyiron-continuum": "pyiron_continuum",
"pyiron-gpl": "pyiron_gpl",
"pyiron-gui": "pyiron_gui"
}
72 changes: 72 additions & 0 deletions .ci_support/update_environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# coding: utf-8
# Copyright (c) Max-Planck-Institut für Eisenforschung GmbH - Computational Materials Design (CM) Department
# Distributed under the terms of "New BSD License", see the LICENSE file.

import json
import re
import sys

import yaml

environment_file = '.ci_support/environment.yml'
name_mapping_file = '.ci_support/pypi_vs_conda_names.json'


class EnvironmentUpdater:
def __init__(self, package_name, from_version, to_version):
"""
Updates the version of a package in the conda environment file.

Parameters:
package_name: Name of the package to update as available on PyPI
from_version: Version the package is before the update
to_version: Version to which the package should be updated
"""
self.from_version = from_version
self.to_version = to_version
with open(name_mapping_file, 'r') as f:
self._name_conversion_dict = json.load(f)

with open(environment_file, 'r') as f:
self.environment = yaml.safe_load(f)

self.package_name = self._convert_package_name(package_name)

def _convert_package_name(self, name):
if name in self._name_conversion_dict.keys():
result = self._name_conversion_dict[name]
else:
result = name
return result

def _update_dependencies(self):
updated_dependencies = []

for dep in self.environment['dependencies']:
updated_dependencies.append(re.sub(
r'(' + self.package_name + '.*)' + self.from_version,
r'\g<1>' + self.to_version,
dep
))

self.environment['dependencies'] = updated_dependencies

def _write(self):
with open(environment_file, 'w') as f:
yaml.safe_dump(self.environment, f)

def update_dependencies(self):
"""Update the version of the requested dependency in the environment file"""
self._update_dependencies()
self._write()


if len(sys.argv) != 7 or not (sys.argv[1] == 'Bump' and sys.argv[3] == 'from' and sys.argv[5] == 'to'):
raise ValueError(f"Title of a dependabot PR 'Bump <package> from <version> to <version>' expected, "
f"but got {' '.join(sys.argv[1:])}")
package_to_update = sys.argv[2]
from_version = sys.argv[4]
to_version = sys.argv[6]

updater = EnvironmentUpdater(package_to_update, from_version, to_version)
updater.update_dependencies()
63 changes: 30 additions & 33 deletions .github/workflows/UpdateDependabotPR.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
name: UpdateDependabotPR

on:
pull_request_target:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest
if: (github.actor == 'dependabot[bot]')
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.ref }} # Check out the head of the actual branch, not the PR
fetch-depth: 0 # otherwise, you will fail to push refs to dest repo
token: ${{ secrets.DEPENDABOT_WORKFLOW_TOKEN }}
- name: UpdateEnvironmentFile
shell: bash -l {0}
run: |
package=$(echo "${{ github.event.pull_request.title }}" | awk '{print $2}')
from=$(echo "${{ github.event.pull_request.title }}" | awk '{print $4}')
to=$(echo "${{ github.event.pull_request.title }}" | awk '{print $6}')
sed -i "/${package}/s/${from}/${to}/g" .ci_support/environment.yml
- name: UpdateDependabotPR commit
run: |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git commit -m "[dependabot skip] Update environment" -a
- name: UpdateDependabotPR push
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.DEPENDABOT_WORKFLOW_TOKEN }}
branch: ${{ github.event.pull_request.head.ref }}
name: UpdateDependabotPR

on:
pull_request_target:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest
if: (github.actor == 'dependabot[bot]')
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.ref }} # Check out the head of the actual branch, not the PR
fetch-depth: 0 # otherwise, you will fail to push refs to dest repo
token: ${{ secrets.DEPENDABOT_WORKFLOW_TOKEN }}
- name: UpdateEnvironmentFile
shell: bash -l {0}
run: |
python .ci_support/update_environment.py ${{ github.event.pull_request.title }}
- name: UpdateDependabotPR commit
run: |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
niklassiemer marked this conversation as resolved.
Show resolved Hide resolved
git commit -m "[dependabot skip] Update environment" -a
- name: UpdateDependabotPR push
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.DEPENDABOT_WORKFLOW_TOKEN }}
branch: ${{ github.event.pull_request.head.ref }}