def push(repo_dir): """ _push_ Push local branch to remote """ repo = git.Repo(repo_dir) ret = repo.remotes.origin.push(repo.head) # Check to make sure that we haven't errored out. for r in ret: if r.flags >= r.ERROR: raise RuntimeError(unicode_(r.summary)) return ret
def current_branch_mark_status(repo_dir, state): """ _current_branch_mark_status_ Mark the CI status of the current branch. :param repo_dir: directory of git repository :param state: state of the last test run, such as "success" or "failure" """ LOGGER.info(u"Setting CI status for current branch to {}".format(state)) config = load_configuration() token = get_github_auth()[1] sha = git.Repo(repo_dir).head.commit.hexsha try: # @HACK: Do a push that we expect will fail -- we just want to # tell the server about our sha. A more elegant solution would # probably be to push a detached head. push(repo_dir) except RuntimeError as ex: if "rejected" not in unicode_(ex): raise url = "https://api.github.com/repos/{org}/{repo}/statuses/{sha}".format( org=config.organisation_name(), repo=config.package_name(), sha=sha ) headers = { 'Authorization': 'token {0}'.format(token), 'Content-Type': 'application/json' } data = json.dumps( { "state": state, "description": "State after cirrus check.", # @HACK: use the travis context, which is technically # true, because we wait for Travis tests to pass before # cutting a release. In the future, we need to setup a # "cirrus" context, for clarity. "context": "continuous-integration/travis-ci" } ) resp = requests.post(url, headers=headers, data=data) resp.raise_for_status()
def set_branch_state(self, state, context, branch=None): """ _current_branch_mark_status_ Mark the CI status of the current branch. :param state: state of the last test run, such as "success" or "failure" :param context: The GH context string to use for the state, eg "continuous-integration/travis-ci" :param branch: Optional branch name or sha to set state on, defaults to current active branch """ if branch is None: branch = self.repo.active_branch.name LOGGER.info(u"Setting CI status for branch {} to {}".format(branch, state)) sha = self.repo.head.commit.hexsha try: # @HACK: Do a push that we expect will fail -- we just want to # tell the server about our sha. A more elegant solution would # probably be to push a detached head. push(self.repo_dir) except RuntimeError as ex: if "rejected" not in unicode_(ex): raise url = "https://api.github.com/repos/{org}/{repo}/statuses/{sha}".format( org=self.config.organisation_name(), repo=self.config.package_name(), sha=sha ) data = json.dumps( { "state": state, "description": "State after cirrus check.", "context": context } ) resp = self.session.post(url, data=data) resp.raise_for_status()
def push_branch(self, branch_name=None): """ _push_branch_ Push the named branch to remote, if branch_name isnt provided the current active branch is pushed """ if branch_name is not None: self.repo.git.checkout(branch_name) try: ret = self.repo.remotes.origin.push(self.repo.head) except GitCommandError as ex: msg = "GitCommandError during push: {}".format(ex) LOGGER.error(msg) raise RuntimeError(msg) # Check to make sure that we haven't errored out. for r in ret: if r.flags >= r.ERROR: raise RuntimeError(unicode_(r.summary)) return ret
def verify_branch(self, branch_name, origin_name='origin', remote=True): """ standardise a branch/origin setup ensuring that a commit exists in a new repo """ if remote and self.check_origin(origin_name): LOGGER.info("Fetching from {}".format(origin_name)) self.repo.remotes[origin_name].fetch() if not self.branch_exists_locally(branch_name): LOGGER.info("creating new branch {}".format(branch_name)) self.repo.git.commit(allow_empty=True, message="initialise repo") self.repo.create_head(branch_name, 'HEAD') else: LOGGER.info("checking out existing branch {}".format(branch_name)) self.repo.git.checkout(branch_name) local_branch = self.repo.heads[branch_name] if remote: if not self.branch_exists_origin(branch_name, origin_name): LOGGER.info("Pushing {} to {}".format(branch_name, origin_name)) rem = self.repo.remotes[origin_name] ret = rem.push(branch_name) # Check to make sure that we haven't errored out. for r in ret: if r.flags >= r.ERROR: LOGGER.error("Unable to push to remote") raise RuntimeError(unicode_(r.summary)) tracking_branch = local_branch.tracking_branch() if not tracking_branch: LOGGER.info( "Setting tracking branch for {}".format(branch_name)) rref = self.repo.remotes[origin_name].refs[branch_name] local_branch.set_tracking_branch(rref) else: LOGGER.info( "No remote option used, may need to git push {} {}".format( origin_name, branch_name))
def verify_branch(self, branch_name, origin_name='origin', remote=True): """ standardise a branch/origin setup ensuring that a commit exists in a new repo """ if remote and self.check_origin(origin_name): LOGGER.info("Fetching from {}".format(origin_name)) self.repo.remotes[origin_name].fetch() if not self.branch_exists_locally(branch_name): LOGGER.info("creating new branch {}".format(branch_name)) self.repo.git.commit(allow_empty=True, message="initialise repo") self.repo.create_head(branch_name, 'HEAD') else: LOGGER.info("checking out existing branch {}".format(branch_name)) self.repo.git.checkout(branch_name) local_branch = self.repo.heads[branch_name] if remote: if not self.branch_exists_origin(branch_name, origin_name): LOGGER.info("Pushing {} to {}".format(branch_name, origin_name)) rem = self.repo.remotes[origin_name] ret = rem.push(branch_name) # Check to make sure that we haven't errored out. for r in ret: if r.flags >= r.ERROR: LOGGER.error("Unable to push to remote") raise RuntimeError(unicode_(r.summary)) tracking_branch = local_branch.tracking_branch() if not tracking_branch: LOGGER.info("Setting tracking branch for {}".format(branch_name)) rref = self.repo.remotes[origin_name].refs[branch_name] local_branch.set_tracking_branch(rref) else: LOGGER.info( "No remote option used, may need to git push {} {}".format( origin_name, branch_name ) )