Skip to content

Commit f9caff4

Browse files
authored
Merge pull request #62 from gisce/add_sastre_cli_for_apply_pr
Add sastre and tailor CLI options for apply_pr
2 parents 167ce78 + 0e3a4b6 commit f9caff4

File tree

3 files changed

+133
-90
lines changed

3 files changed

+133
-90
lines changed

README.md

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@ from GitHub and set to the `GITHUB_TOKEN` environment variable.
1313
This repository uses the [Click](http://click.pocoo.org/5/) package to
1414
register commands that call the fabric scripts.
1515

16-
The following commands are supported with apply_pr:
17-
18-
| Console Command | Description | Wiki page |
19-
|:---------------:|:----------------------------------------------------------------------------|:--------------------------------------------------------------------------------------|
20-
| apply_pr | Apply a PR to a remote server | [Apply a Pull Request](https://github.com/gisce/apply_pr/wiki/Apply-a-Pull-Request) |
21-
| check_pr | Check if the PR's commits are applied on the server | [Check Applied patches](https://github.com/gisce/apply_pr/wiki/Check-Applied-patches) |
22-
| status_pr | Update the deploy status on GitHub for the PR | [Mark deploy status](https://github.com/gisce/apply_pr/wiki/Mark-deploy-status) |
23-
| check_prs_status| Check the PRs status (whether they are or not merged and at which milestone)| [Check PRs Status](https://github.com/gisce/apply_pr/wiki/Check-the-PRs-status) |
24-
| create_changelog| Creates the changelog for all PRs merged in a milestone | [Create Changelog](https://github.com/gisce/apply_pr/wiki/Create-Changelog) |
16+
The following commands are supported with `sastre`:
2517

18+
| Console Command | Description | Wiki page |
19+
|:---------------: |:--------------------------------------------------------------------|:---------------------------------------------------------------------------------------------------|
20+
| `deploy` | Apply a PR to a remote server | [Deploy a pull request](https://github.com/gisce/apply_pr/wiki/Apply-a-Pull-Request) |
21+
| `check_prs` | Check the status of the PRs for a set of PRs | [Check pull requests status](https://github.com/gisce/apply_pr/wiki/Check-pull-requests-status) |
22+
| `status` | Update the status of a deploy into GitHub | [Mark deploy status](https://github.com/gisce/apply_pr/wiki/Mark-deploy-status) |
23+
| `create_changelog` | Create a chnagelog for the given milestone | [Create Changelog](https://github.com/gisce/apply_pr/wiki/Create-Changelog) |
24+
| `check_pr` | **Deprecated:** Check if the PR's commits are applied on the server | [Check Applied patches](https://github.com/gisce/apply_pr/wiki/Check-applied-patches-(deprecated)) |
2625

2726
## Install
2827

@@ -35,14 +34,14 @@ pip install apply_pr
3534

3635
**NOTE**: do not include braces on the following commands
3736

38-
### APPLY PR
37+
### DEPLOY
3938

4039
```bash
41-
Usage: apply_pr [OPTIONS]
40+
Usage: deploy [OPTIONS]
4241

4342
Options:
44-
--pr TEXT Pull request to apply [required]
45-
--host TEXT Host to apply [required]
43+
--pr TEXT Pull request to deploy [required]
44+
--host TEXT Host to where to be deployed [required]
4645
--from-number INTEGER From commit number
4746
--from-commit TEXT From commit hash (included)
4847
--force-hostname TEXT Force hostname [default: False]
@@ -52,24 +51,10 @@ Options:
5251
--help Show this message and exit.
5352
```
5453

55-
### CHECK PR
56-
57-
```bash
58-
Usage: check_pr [OPTIONS]
59-
60-
Options:
61-
--pr TEXT Pull request to check [required]
62-
--host TEXT Host to check [required]
63-
--owner TEXT GitHub owner name [default: gisce]
64-
--repository TEXT GitHub repository name [default: erp]
65-
--src TEXT Remote src path [default: /home/erp/src]
66-
--help Show this message and exit.
67-
```
68-
69-
### STATUS PR
54+
### STATUS
7055

7156
```bash
72-
Usage: status_pr [OPTIONS]
57+
Usage: status [OPTIONS]
7358

7459
Options:
7560
--deploy-id TEXT Deploy id to mark
@@ -80,10 +65,10 @@ Options:
8065
--help Show this message and exit.
8166
```
8267

83-
### CHECK PRS STATUS
68+
### CHECK PRS
8469

8570
```bash
86-
Usage: check_prs_status [OPTIONS]
71+
Usage: check_prs [OPTIONS]
8772

8873
Options:
8974
--prs TEXT List of pull request separated by space (by default)
@@ -108,4 +93,18 @@ Options:
10893
--owner TEXT GitHub owner name [default: gisce]
10994
--repository TEXT GitHub repository name [default: erp]
11095
--help Show this message and exit.
96+
```
97+
98+
### CHECK PR (deprecated)
99+
100+
```bash
101+
Usage: check_pr [OPTIONS]
102+
103+
Options:
104+
--pr TEXT Pull request to check [required]
105+
--host TEXT Host to check [required]
106+
--owner TEXT GitHub owner name [default: gisce]
107+
--repository TEXT GitHub repository name [default: erp]
108+
--src TEXT Remote src path [default: /home/erp/src]
109+
--help Show this message and exit.
111110
```

apply_pr/cli.py

Lines changed: 101 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,37 @@
44

55
from fabric.tasks import execute, WrappedCallableTask
66
from fabric.api import env
7+
from fabric import colors
78
import click
89

910
DEFAULT_LOG_LEVEL = 'ERROR'
1011

12+
github_options = [
13+
click.option('--owner', help='GitHub owner name', default='gisce', show_default=True),
14+
click.option('--repository', help='GitHub repository name', default='erp', show_default=True),
15+
16+
]
17+
18+
deployment_options = github_options + [
19+
click.option("--host", help="Host to apply", required=True),
20+
click.option('--src', help='Remote src path', default='/home/erp/src', show_default=True),
21+
]
22+
23+
apply_pr_options = deployment_options + [
24+
click.option("--pr", help="Pull request to apply", required=True),
25+
click.option("--from-number", help="From commit number", default=0),
26+
click.option("--from-commit", help="From commit hash (included)", default=None),
27+
click.option("--force-hostname", help="Force hostname", default=False)
28+
]
29+
30+
31+
def add_options(options):
32+
def _add_options(func):
33+
for option in reversed(options):
34+
func = option(func)
35+
return func
36+
return _add_options
37+
1138

1239
def configure_logging():
1340
log_level = getattr(logging, os.environ.get(
@@ -16,29 +43,36 @@ def configure_logging():
1643
logging.basicConfig(level=log_level)
1744

1845

19-
@click.command(name="apply_pr")
20-
@click.option("--pr", help="Pull request to apply", required=True)
21-
@click.option("--host", help="Host to apply", required=True)
22-
@click.option("--from-number", help="From commit number", default=0)
23-
@click.option("--from-commit", help="From commit hash (included)",
24-
default=None, show_default=True)
25-
@click.option("--force-hostname", help="Force hostname",
26-
default=False, show_default=True)
27-
@click.option('--owner', help='GitHub owner name',
28-
default='gisce', show_default=True)
29-
@click.option('--repository', help='GitHub repository name',
30-
default='erp', show_default=True)
31-
@click.option('--src', help='Remote src path',
32-
default='/home/erp/src', show_default=True)
33-
def apply_pr(
34-
pr, host, from_number, from_commit, force_hostname,
35-
owner, repository, src
36-
):
46+
@click.group(name='tailor')
47+
def tailor(**kwargs):
3748
from apply_pr.version import check_version
3849
check_version()
3950

40-
from apply_pr import fabfile
4151

52+
def apply_pr(
53+
pr, host, from_number, from_commit, force_hostname,
54+
owner, repository, src
55+
):
56+
"""
57+
Deploy a PR into a remote server via Fabric
58+
:param pr: Number of the PR to deploy
59+
:type pr: str
60+
:param host: Host to connect
61+
:type host: str
62+
:param from_number: Number of the commit to deploy from
63+
:type from_number: str
64+
:param from_commit: Hash of the commit to deploy from
65+
:type from_commit: str
66+
:param force_hostname: Hostname used in GitHub
67+
:type force_hostname: str
68+
:param owner: Owner of the repository of GitHub
69+
:type owner: str
70+
:param repository: Name of the repository of GitHub
71+
:type repository: str
72+
:param src: Source path to the repository directory
73+
:type src: str
74+
"""
75+
from apply_pr import fabfile
4276
url = urlparse(host)
4377
env.user = url.username
4478
env.password = url.password
@@ -53,16 +87,25 @@ def apply_pr(
5387
)
5488

5589

56-
@click.command(name='check_pr')
90+
@tailor.command(name="deploy")
91+
@add_options(apply_pr_options)
92+
def deploy(**kwargs):
93+
"""Deploy a PR into a remote server via Fabric"""
94+
return apply_pr(**kwargs)
95+
96+
97+
@tailor.command(name='check_pr')
5798
@click.option('--pr', help='Pull request to check', required=True)
58-
@click.option('--host', help='Host to check', required=True)
59-
@click.option('--owner', help='GitHub owner name',
60-
default='gisce', show_default=True)
61-
@click.option('--repository', help='GitHub repository name',
62-
default='erp', show_default=True)
63-
@click.option('--src', help='Remote src path',
64-
default='/home/erp/src', show_default=True)
65-
def check_pr(pr, src, owner, repository, host):
99+
@click.option('--force/--no-force', default=False,
100+
help='Forces the usage of this command')
101+
@add_options(deployment_options)
102+
def check_pr(pr, force, src, owner, repository, host):
103+
"""DEPRECATED - Check for applied commits on PR"""
104+
print(colors.red("This option has been deprecated as it doesn't work"))
105+
if not force:
106+
print(colors.red(
107+
"Use '--force' to force the usage for this command (as is)"))
108+
exit()
66109
from apply_pr import fabfile
67110

68111
url = urlparse(host)
@@ -76,15 +119,8 @@ def check_pr(pr, src, owner, repository, host):
76119
src=src, owner=owner, repository=repository, host=url.hostname)
77120

78121

79-
@click.command(name='status_pr')
80-
@click.option('--deploy-id', help='Deploy id to mark')
81-
@click.option('--status', type=click.Choice(['success', 'error', 'failure']),
82-
help='Status to set', default='success', show_default=True)
83-
@click.option('--owner', help='GitHub owner name',
84-
default='gisce', show_default=True)
85-
@click.option('--repository', help='GitHub repository name',
86-
default='erp', show_default=True)
87122
def status_pr(deploy_id, status, owner, repository):
123+
"""Update the status of a deploy into GitHub"""
88124
from apply_pr import fabfile
89125

90126
configure_logging()
@@ -94,19 +130,18 @@ def status_pr(deploy_id, status, owner, repository):
94130
owner=owner, repository=repository)
95131

96132

97-
@click.command(name='check_prs_status')
98-
@click.option('--prs', required=True,
99-
help='List of pull request separated by space (by default)')
100-
@click.option('--separator',
101-
help='Character separator of list by default is space',
102-
default=' ', required=True, show_default=True)
103-
@click.option('--version',
104-
help="Compare with milestone and show if included in prs")
105-
@click.option('--owner', help='GitHub owner name',
106-
default='gisce', show_default=True)
107-
@click.option('--repository', help='GitHub repository name',
108-
default='erp', show_default=True)
133+
@tailor.command(name='status')
134+
@click.option('--deploy-id', help='Deploy id to mark')
135+
@click.option('--status', type=click.Choice(['success', 'error', 'failure']),
136+
help='Status to set', default='success', show_default=True)
137+
@add_options(github_options)
138+
def status(**kwargs):
139+
"""Update the status of a deploy into GitHub"""
140+
status_pr(**kwargs)
141+
142+
109143
def check_prs_status(prs, separator, version, owner, repository):
144+
"""Check the status of the PRs for a set of PRs"""
110145
from apply_pr import fabfile
111146

112147
log_level = getattr(logging, os.environ.get('LOG_LEVEL', 'INFO').upper())
@@ -120,18 +155,30 @@ def check_prs_status(prs, separator, version, owner, repository):
120155
version=version)
121156

122157

123-
@click.command(name='check_prs_status')
158+
@tailor.command(name='check_prs')
159+
@click.option('--prs', required=True,
160+
help='List of pull request separated by space (by default)')
161+
@click.option('--separator',
162+
help='Character separator of list by default is space',
163+
default=' ', required=True, show_default=True)
164+
@click.option('--version',
165+
help="Compare with milestone and show if included in prs")
166+
@add_options(github_options)
167+
def check_prs(**kwargs):
168+
"""Check the status of the PRs for a set of PRs"""
169+
check_prs_status(**kwargs)
170+
171+
172+
@tailor.command(name='create_changelog')
124173
@click.option('-m', '--milestone', required=True,
125174
help='Milestone to get the issues from (version)')
126175
@click.option('--issues/--no-issues', default=False, show_default=True,
127176
help='Also get the data on the issues')
128177
@click.option('--changelog_path', default='/tmp', show_default=True,
129178
help='Path to drop the changelog file in')
130-
@click.option('--owner', help='GitHub owner name',
131-
default='gisce', show_default=True)
132-
@click.option('--repository', help='GitHub repository name',
133-
default='erp', show_default=True)
179+
@add_options(github_options)
134180
def create_changelog(milestone, issues, changelog_path, owner, repository):
181+
"""Create a changelog for the given milestone"""
135182
from apply_pr import fabfile
136183

137184
log_level = getattr(logging, os.environ.get('LOG_LEVEL', 'INFO').upper())
@@ -146,4 +193,4 @@ def create_changelog(milestone, issues, changelog_path, owner, repository):
146193

147194

148195
if __name__ == '__main__':
149-
apply_pr()
196+
tailor()

setup.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414
description='Apply Pull Requests from GitHub',
1515
entry_points='''
1616
[console_scripts]
17-
apply_pr=apply_pr.cli:apply_pr
18-
check_pr=apply_pr.cli:check_pr
19-
status_pr=apply_pr.cli:status_pr
20-
check_prs_status=apply_pr.cli:check_prs_status
21-
create_changelog=apply_pr.cli:create_changelog
17+
sastre=apply_pr.cli:tailor
18+
tailor=apply_pr.cli:tailor
2219
''',
2320
install_requires=INSTALL_REQUIRES
2421
)

0 commit comments

Comments
 (0)