Allow pushing changes to github via scale_build

This commit is contained in:
Waqar Ahmed
2021-08-14 16:01:20 +05:00
parent 11dc2d2633
commit a5dc792b7c
4 changed files with 30 additions and 3 deletions

View File

@@ -2,8 +2,9 @@ import logging
import os
import shutil
from .config import BRANCH_OUT_NAME
from .config import BRANCH_OUT_NAME, GITHUB_TOKEN
from .exceptions import CallError
from .utils.git_utils import push_changes
from .utils.logger import LoggingContext
from .utils.package import get_packages
from .utils.paths import BRANCH_OUT_LOG_DIR
@@ -27,3 +28,15 @@ def branch_out_repos():
logger.debug('Branching out %r', package.name)
with LoggingContext(os.path.join('branchout', package.name), 'w'):
package.branch_out()
def push_branched_out_repos():
if not GITHUB_TOKEN:
raise CallError('In order to push branched out packages, "GITHUB_TOKEN" must be specified')
logger.info('Starting pushing new %r branch', BRANCH_OUT_NAME)
for package in get_packages():
logger.debug('Pushing %r package\'s branch', package.name)
with LoggingContext(os.path.join('branchout', package.name), 'a+'):
push_changes(package.source_path, GITHUB_TOKEN, BRANCH_OUT_NAME)

View File

@@ -9,6 +9,7 @@ BUILD_TIME_OBJ = datetime.fromtimestamp(BUILD_TIME)
BUILDER_DIR = os.getenv('BUILDER_DIR', './')
BRANCH_OUT_NAME = os.getenv('NEW_BRANCH_NAME')
BRANCH_OVERRIDES = {k[:-(len('_OVERRIDE'))]: v for k, v in os.environ.items() if k.endswith('_OVERRIDE')}
GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
PARALLEL_BUILD = int(os.getenv('PARALLEL_BUILDS', max(os.cpu_count(), 8) / 4))
PKG_DEBUG = os.getenv('PKG_DEBUG', False)
SIGNING_KEY = os.getenv('SIGNING_KEY')

View File

@@ -3,7 +3,7 @@ import coloredlogs
import logging
import sys
from .branch_out import branch_out_repos, validate_branch_out_config
from .branch_out import branch_out_repos, push_branched_out_repos, validate_branch_out_config
from .checkout import checkout_sources
from .clean import complete_cleanup
from .config import BRANCH_OVERRIDES
@@ -90,5 +90,7 @@ def main():
elif args.action == 'branchout':
validate_branch_out_config()
branch_out_repos()
if not args.skip_push:
push_branched_out_repos()
else:
parser.print_help()

View File

@@ -1,5 +1,7 @@
import re
from urllib.parse import urlparse
from .run import run
from .paths import GIT_MANIFEST_PATH
@@ -14,7 +16,7 @@ def update_git_manifest(git_remote, git_sha, mode='a+'):
def retrieve_git_remote_and_sha(path):
return {
'url': run(['git', '-C', path, 'remote', 'get-url', 'origin'], log=False).stdout.strip(),
'url': get_origin_uri(path),
'sha': run(['git', '-C', path, 'rev-parse', '--short', 'HEAD'], log=False).stdout.strip(),
}
@@ -30,3 +32,12 @@ def branch_exists_in_repository(origin, branch):
def create_branch(path, base_branch, new_branch):
run(['git', '-C', path, 'checkout', '-b', new_branch, base_branch])
def get_origin_uri(path):
return run(['git', '-C', path, 'remote', 'get-url', 'origin'], log=False).stdout.strip()
def push_changes(path, api_token, branch):
url = urlparse(get_origin_uri(path))
run(['git', '-C', path, 'push', f'https://{api_token}@{url.hostname}{url.path}', branch])