Skip to content

Instantly share code, notes, and snippets.

@kragniz
Created January 14, 2018 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kragniz/db79fc0695e4c1567a5933e1be306aa2 to your computer and use it in GitHub Desktop.
Save kragniz/db79fc0695e4c1567a5933e1be306aa2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import re
import time
import github3
gh = github3.login('kragniz', os.environ.get('password'))
fork_branch = 'data-plane-api-sha-updates'
bazel_path = 'bazel/repository_locations.bzl'
def get_last_commit_sha(repo):
for commit in repo.iter_commits():
return commit.sha
def has_open_pr(repo, sha):
for pull in repo.iter_pulls(state='all', head='kragniz:'+fork_branch):
if sha[:7] in pull.title:
print('already made a pr:', pull.title)
return True
return False
while True:
data_plane_repo = gh.repository('envoyproxy', 'data-plane-api')
envoy_repo = gh.repository('envoyproxy', 'envoy')
envoy_repo_fork = gh.repository('kragniz', 'envoy')
current_data_plane_sha = get_last_commit_sha(data_plane_repo)
bazel_content = envoy_repo.contents(bazel_path).decoded.decode('utf-8')
current_sha = re.search(r'envoy_api = dict\(\s+commit = \"([a-z0-9]+)\"', bazel_content, re.MULTILINE).groups()[0]
if current_sha != current_data_plane_sha:
print(f'current_sha: {current_sha} != current_data_plane_sha: {current_data_plane_sha}')
if not has_open_pr(envoy_repo, current_data_plane_sha):
updated_bazel = bazel_content.replace(current_sha, current_data_plane_sha)
try:
envoy_repo_fork.create_ref('refs/heads/'+fork_branch, get_last_commit_sha(envoy_repo))
except github3.models.GitHubError as e:
if e.code == 422:
envoy_repo_fork.ref('heads/'+fork_branch).update(get_last_commit_sha(envoy_repo), force=True)
current_fork_commit = envoy_repo_fork.contents(bazel_path, ref=fork_branch).sha
envoy_repo_fork.update_file(bazel_path,
'build: update data-plane-api sha\n\nSigned-off-by: Louis Taylor <louis@kragniz.eu>',
updated_bazel.encode('utf-8'),
current_fork_commit,
branch=fork_branch)
print('creating new pull request')
envoy_repo.create_pull('build: update data-plane-api sha to ' + current_data_plane_sha[:7], 'master', 'kragniz:'+fork_branch)
else:
print(f'current_sha: {current_sha} == current_data_plane_sha: {current_data_plane_sha}')
time.sleep(30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment