Skip to content

Commit

Permalink
Merge pull request #145 from phracek/downstream2upstream
Browse files Browse the repository at this point in the history
Sync files from downstream into upstream
  • Loading branch information
phracek committed Mar 19, 2019
2 parents 5b0250e + 5f43b1d commit 7c512d4
Show file tree
Hide file tree
Showing 15 changed files with 452 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.py[co]
bigger-secret
biggest-secret
.tox
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ This list contains workflows covered by packit tool and links to the documentati
* [Update Fedora dist-git with an upstream release.](/docs/propose_update.md)
* [Build content of a Fedora dist-git branch in koji.](/docs/build.md)
* [Create a bodhi update.](/docs/create_bodhi_update.md)
* [Create a SRPM from the current content in the upstream repository](/docs/srpm.md)
* [Create a SRPM from the current content in the upstream repository.](/docs/srpm.md)
* [Sync content of the Fedora dist-git repo into the upstream repository.](/docs/sync-from-downstream.md)


## Configuration
Expand Down
68 changes: 68 additions & 0 deletions docs/sync-from-downstream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# `packit sync-from-downstream`

This is a detailed documentation for the downstream sync functionality of packit. The
command creates a new pull request in upstream repository using a
selected branch (master by default) from Fedora dist-git repository.


## Requirements

* Fedora dist-git repository.
* Packit config file placed in the upstream repository.
* Pagure API tokens for Fedora Dist-git.
* Github API token.


## Tutorial

1. Pagure dist-git is configured in a way that it requires 2 API tokens in
order to perform a pull request using the API (which packit is using).
Please set these three environment variables using the appropriate tokens:
1. `export PAGURE_USER_TOKEN=<token>` — this token is needed to access data
in pagure. This is meant to be an API token of your user:
https://src.fedoraproject.org/settings#nav-api-tab
2. `export PAGURE_FORK_TOKEN=<token>` — packit needs this token to create a
pull request:
https://src.fedoraproject.org/fork/YOU/rpms/PACKAGE/settings#apikeys-tab
If the fork does not exist, you have to create it in Pagure's web
interface. We are working with Fedora team to relax this requirement.
3. `export GITHUB_TOKEN=<token>` — you can obtain the token over here:
https://github.com/settings/tokens

2. Files which are synced are mentioned in `.packit.yaml` as `synced_files` value.

3. Once you want to sync Fedora dist-git repo into the upstream repo,
run `packit sync-from-downstream` in a working directory of your upstream
repository:
```bash
$ git clone https://github.com/user-cont/colin.git

$ cd colin

$ packit sync-from-downstream
upstream active branch master
Cloning repo: https://src.fedoraproject.org/rpms/colin.git -> /tmp/tmph9npe78e
using master dist-git branch
syncing /tmp/tmph9npe78e/colin.spec
PR created: https://api.github.com/repos/phracek/colin/pulls/3
```


## `packit sync-from-downstream --help`

```
Usage: packit sync-from-downstream [OPTIONS] [PATH_OR_URL]

Copy synced files from Fedora dist-git into upstream by opening a pull
request.

PATH_OR_URL argument is a local path or a URL to the upstream git
repository, it defaults to the current working directory

Options:
--dist-git-branch TEXT Source branch in dist-git for sync.
--upstream-branch TEXT Target branch in upstream to sync to.
--no-pr Pull request is not create.
-h, --help Show this message and exit.

```
58 changes: 58 additions & 0 deletions packit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,64 @@ def sync_release(
if not use_local_content:
up.local_project.git_repo.git.checkout(current_up_branch.checkout())

def sync_from_downstream(
self, dist_git_branch: str, upstream_branch: str, no_pr: bool = None
):
"""
Update upstream package from Fedora
"""
up = Upstream(config=self.config, package_config=self.package_config)

dg = DistGit(config=self.config, package_config=self.package_config)

logger.info(f"upstream active branch {up.active_branch}")
try:
dg.update_branch(dist_git_branch)
dg.checkout_branch(dist_git_branch)

local_pr_branch = f"{dist_git_branch}-downstream-sync"
logger.info(f'using "{dist_git_branch}" dist-git branch')

up.create_branch(local_pr_branch)
up.checkout_branch(local_pr_branch)

up.sync_files(dg.local_project)

if not no_pr:
description = (
f"Downstream commit: {dg.local_project.git_repo.head.commit}\n"
)

self.sync_upstream(
upstream=up,
commit_msg=f"{dist_git_branch} downstream sync",
pr_title=f"Update from downstream branch {dist_git_branch}",
pr_description=description,
upstream_branch=upstream_branch,
commit_msg_description=description,
)
finally:
pass

def sync_upstream(
self,
upstream: Upstream,
commit_msg: str,
pr_title: str,
pr_description: str,
upstream_branch: str,
commit_msg_description: str = None,
):
upstream.commit(title=commit_msg, msg=commit_msg_description)
# the branch may already be up, let's push forcefully
upstream.push_to_branch(upstream.local_project.ref, force=True)
upstream.create_pull(
pr_title,
pr_description,
source_branch=str(upstream.local_project.ref),
target_branch=upstream_branch,
)

def sync(
self,
distgit: DistGit,
Expand Down
2 changes: 2 additions & 0 deletions packit/cli/packit_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from packit.cli.create_update import create_update
from packit.cli.srpm import srpm
from packit.cli.update import update
from packit.cli.sync_from_downstream import sync_from_downstream
from packit.cli.watch_upstream_release import watch_releases
from packit.config import Config, get_context_settings
from packit.utils import set_logging
Expand Down Expand Up @@ -46,6 +47,7 @@ def version():
# packit_base.add_command(watch_pr)
packit_base.add_command(watch_releases)
packit_base.add_command(update)
packit_base.add_command(sync_from_downstream)
packit_base.add_command(build)
packit_base.add_command(create_update)
packit_base.add_command(srpm)
Expand Down
38 changes: 38 additions & 0 deletions packit/cli/sync_from_downstream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Update selected component from upstream in Fedora
"""

import logging
import os

import click

from packit.cli.types import LocalProjectParameter
from packit.cli.utils import cover_packit_exception, get_packit_api
from packit.config import pass_config, get_context_settings

logger = logging.getLogger(__file__)


@click.command("sync-from-downstream", context_settings=get_context_settings())
@click.option(
"--dist-git-branch", help="Source branch in dist-git for sync.", default="master"
)
@click.option(
"--upstream-branch", help="Target branch in upstream to sync to.", default="master"
)
@click.option("--no-pr", is_flag=True, help="Pull request is not create.")
@click.argument(
"path_or_url", type=LocalProjectParameter(), default=os.path.abspath(os.path.curdir)
)
@pass_config
@cover_packit_exception
def sync_from_downstream(config, dist_git_branch, upstream_branch, no_pr, path_or_url):
"""
Copy synced files from Fedora dist-git into upstream by opening a pull request.
PATH_OR_URL argument is a local path or a URL to the upstream git repository,
it defaults to the current working directory
"""
api = get_packit_api(config=config, local_project=path_or_url)
api.sync_from_downstream(dist_git_branch, upstream_branch, no_pr)
10 changes: 8 additions & 2 deletions packit/local_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import requests

from ogr.abstract import GitProject, GitService
from packit.utils import is_git_repo, get_repo
from packit.utils import is_git_repo, get_repo, get_namespace

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -141,9 +141,15 @@ def __init__(
# since this can point to a fork
# .urls returns generator
self.git_url = list(self.git_repo.remote().urls)[0]
logger.debug("remote url of the repo is %s", self.git_url)
logger.debug(f"remote url of the repo is {self.git_url}")
change = True

if self.git_repo and not self.namespace:
self.namespace = get_namespace(self.git_url)
logger.debug(f"namespace from url is {self.namespace}")
if self.namespace:
change = True

if ref:

if ref not in self.git_repo.branches:
Expand Down
Loading

0 comments on commit 7c512d4

Please sign in to comment.