GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add support for prepare-commit-msg hooks by marcjay · Pull Request #1004 · pre-commit/pre-commit

Conversation

@marcjay

@marcjay marcjay commented Apr 21, 2019

Copy link
Copy Markdown
Contributor

Adds a prepare-commit-msg hook stage which allows for hooks which add
dynamic suggested/placeholder text to commit messages that an author can
use as a starting point for writing a commit message.

An example being to pre-populate the first line of the commit message with some dynamic information such as the ticket ID and description (if branch name contains the ticket ID let's say). The benefit of this over a commit-msg hook in this scenario being that it doesn't modify an author's commit message 'behind their back'.

May help with #833 and relates to #884

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mjay-taskize
Adds support for prepare-commit-msg hooks
e60f541
Adds a prepare-commit-msg hook stage which allows for hooks which add
dynamic suggested/placeholder text to commit messages that an author can
use as a starting point for writing a commit message
asottile
asottile reviewed Apr 21, 2019
View reviewed changes

@asottile asottile left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great! Thanks for the work on this :D

just a few small things and this should be good to go!

Curious if you've tried it out as well (and maybe have some sample configs I can give a spin!)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/conftest.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread tests/commands/run_test.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pre_commit/commands/run.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

mjay-taskize added 2 commits April 21, 2019 21:54
@mjay-taskize
Fix broken test_manifest_hooks test
64467f6
@mjay-taskize
Use set rather than list for commit message related stages, remove
82969e4
default file open modes, tidy up bash call for failing hook test
@marcjay marcjay mentioned this pull request Apr 21, 2019
Document prepare-commit-msg hooks pre-commit/pre-commit.com#226
Merged
asottile
asottile approved these changes Apr 21, 2019
View reviewed changes

@asottile asottile left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@marcjay

marcjay commented Apr 21, 2019

Copy link
Copy Markdown
Contributor Author

Cheers, and thanks for the pointers - nice job on building something so easily extendible!

Yup, I'll put up the beginnings of a hook I've been working on for this, one sec...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@asottile

asottile commented Apr 21, 2019

Copy link
Copy Markdown
Member

don't worry about the windows failures, I'll fix those -- thanks for the PR 👍

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@asottile
asottile merged commit b830026 into pre-commit:master Apr 21, 2019
@marcjay

marcjay commented Apr 21, 2019

Copy link
Copy Markdown
Contributor Author

Great, thank you!

In terms of an example, this was something quick I put together for testing - it's the reason for wanting this feature. It has no error checking or anything yet, just a dirty script at this stage!

  • Uses the https://pypi.org/project/jira/ library
  • Expects branch names to start with a JIRA ticket ID, e.g. ABC-123-replace-foo
def get_branch_name():
    return subprocess.check_output(
        [
            'git',
            'rev-parse',
            '--abbrev-ref',
            'HEAD',
        ],
    ).decode('UTF-8')


def get_ticket_id_from_branch_name(branch_name):
    return re.findall('^[A-Z]{1,10}-[0-9]{1,5}', branch_name)[0]


def main():
    if os.environ.get('GIT_EDITOR') == ':':
        # According to git docs, the editor will not show, e.g. because
        # of a `git commit -m "..."` supplied message. Therefore don't touch
        # the commit message as there is no opportunity for the user to edit it
        return

    jira = JIRA(server=JIRA_SERVER)

    branch_name = get_branch_name()
    ticket_id = get_ticket_id_from_branch_name(branch_name)

    issue = jira.issue(ticket_id)
    summary = issue.fields.summary

    with io.open(sys.argv[1], 'r+') as f:
        contents = f.readlines()
        contents.insert(0, u'{} - {}\n'.format(ticket_id, summary))
        f.seek(0, 0)
        f.writelines(contents)

git commit results in a commit message that starts:

ABC-123 - Replace Foo with Bar

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@marcjay
marcjay deleted the add_prepare-commit-msg_stage branch April 21, 2019 22:58
@asottile

asottile commented Apr 21, 2019

Copy link
Copy Markdown
Member

neat!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@asottile asottile mentioned this pull request Aug 16, 2019
Support post-checkout #1120
Closed
@benmezger

benmezger commented May 25, 2020

Copy link
Copy Markdown

@marcjay Nice! I wrote something similar, which allows customization of templates and handles amends and non-jira branches

https://github.com/benmezger/gjira/tree/master/gjira

marcjay reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@asottile asottile asottile approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@marcjay @asottile @benmezger @mjay-taskize
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.