Skip to content

Commit

Permalink
Merge pull request #12 from d3b-center/repocfg
Browse files Browse the repository at this point in the history
✨ Move static arguments into config file
  • Loading branch information
fiendish committed Jun 12, 2020
2 parents 8a39956 + 4e7c728 commit 7c8e407
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 22 deletions.
17 changes: 16 additions & 1 deletion README.md
Expand Up @@ -29,7 +29,22 @@ new release version and generates your assets and then creates
`.github/release_assets.txt` containing a list of the files you want to upload,
one per line, in the order that you want them to appear.

## Part 3 for Python setuptools packages: Get package version from repository metadata
## Part 3 release_cfg.json (semi-optional)

Add a `release_maker_cfg.json` file to your repository's master branch. Its
contents should be as follows:

```json
{
"project_title": "Your Repository Project Title",
"pre_release_script": "script/to/run/before/making/release/PR.sh"
}
```

If this file doesn't exist, it will be automatically added during the release
after prompts from the release maker tool.

## Part 4 for Python setuptools packages: Get package version from repository metadata

If your repository is a Python setuptools package, you'll want to tie the
package version to the repository release version. The easiest way to do that
Expand Down
16 changes: 2 additions & 14 deletions d3b_release_maker/cli.py
Expand Up @@ -40,20 +40,8 @@ def preview_changelog_cmd(repo, blurb_file):

@click.command(name="build", short_help="Generate a new release on GitHub")
@options
@click.option(
"--project_title",
prompt="The title of the project",
default="",
help="This will be put before the release number in the generated notes",
)
@click.option(
"--pre_release_script",
prompt="Shell script to run before pushing the release to GitHub",
default="",
help="Shell script to run before pushing the release to GitHub",
)
def make_release_cmd(repo, project_title, blurb_file, pre_release_script):
make_release(repo, project_title, blurb_file, pre_release_script)
def make_release_cmd(repo, blurb_file):
make_release(repo, blurb_file)


cli.add_command(preview_changelog_cmd)
Expand Down
51 changes: 44 additions & 7 deletions d3b_release_maker/release_maker.py
@@ -1,3 +1,4 @@
import json
import os
import shutil
import stat
Expand All @@ -7,6 +8,7 @@
from collections import defaultdict
from datetime import datetime

import click
import emoji
import regex
import semver
Expand All @@ -20,6 +22,7 @@
GH_RAW = config.GITHUB_RAW

CHANGEFILE = "CHANGELOG.md"
CONFIGFILE = "release_maker_cfg.json"

MAJOR = "major"
MINOR = "minor"
Expand Down Expand Up @@ -360,18 +363,45 @@ def new_changelog(repo, blurb_file):
return new_version, new_markdown, changelog


def make_release(repo, project_title, blurb_file, pre_release_script):
def load_config():
"""
Get repo config file
"""
try:
with open(CONFIGFILE, "r") as f:
cfg = json.load(f)
except Exception:
cfg = {}

update = False
if "project_title" not in cfg:
cfg["project_title"] = click.prompt(
f"Project title not found in repo {CONFIGFILE}. Please enter one now"
)
update = True
if "pre_release_script" not in cfg:
cfg["pre_release_script"] = click.prompt(
f"Pre-release script path not found in repo {CONFIGFILE}."
" Enter one now if desired or just return to continue:",
default="",
)
update = True

if update:
with open(CONFIGFILE, "w") as f:
json.dump(cfg, f, indent=2)

return cfg


def make_release(repo, blurb_file):
"""
Generate a new changelog, run the script, and then make a PR on GitHub
"""
gh_token = os.getenv(config.GH_TOKEN_VAR)

new_version, new_markdown, changelog = new_changelog(repo, blurb_file)

if changelog:
# Attach project header
changelog = f"# {project_title} Change History\n\n{changelog}"

# Freshly clone repo
tmp = os.path.join(tempfile.gettempdir(), "release_maker")
shutil.rmtree(tmp, ignore_errors=True)
Expand All @@ -390,10 +420,17 @@ def make_release(repo, project_title, blurb_file, pre_release_script):
)
os.chdir(tmp)

# Get the configuration
cfg = load_config()

# Attach project header
changelog = f"# {cfg['project_title']} Change History\n\n{changelog}"

print("Writing updated changelog file ...")
with open(CHANGEFILE, "w") as cl:
cl.write(changelog)
with open(CHANGEFILE, "w") as f:
f.write(changelog)

pre_release_script = cfg.get("pre_release_script")
if pre_release_script:
print(f"Executing pre-release script {pre_release_script} ...")
mode = os.stat(pre_release_script).st_mode
Expand Down

0 comments on commit 7c8e407

Please sign in to comment.