pull_request_templates 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +74 -0
- data/exe/pull_request_templates +3 -0
- data/lefthook.yml +7 -0
- data/lib/pull_request_templates/cli.rb +92 -0
- data/lib/pull_request_templates/version.rb +1 -1
- data/lib/pull_request_templates.rb +3 -2
- metadata +20 -5
- data/sig/pull_request_templates.rbs +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e611a2cbdb56bda1b85bc97bcd1e9ba850944a956aca3772e2479e2080780ed0
|
4
|
+
data.tar.gz: f22afc24c2cab63778da2174ffdbde15a375a0f6d96b8643ffe7e5ec9a10dfd5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e285e78b50d6a53f31a3bf8c1c8162816e6310175a1ddda470d01490e82ff1744d6663a392870992adead8bd2856a609024e62e93218629aa3b2bfe925a9beee
|
7
|
+
data.tar.gz: a0c10b8ee580963bd2a109f4d262a311681d76558e486ae0ecfa2f08c007134a6ef99b9762deff4ab0fb3edb7398843e7e0a3f2c98b6dc88d60e18a57db593b8
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.2.0] - 2024-06-11
|
4
|
+
|
5
|
+
- Added support for multiple templates via `.mapping.yml` configuration
|
6
|
+
- Implemented MECE template selection based on changed files
|
7
|
+
- Improved error handling for ambiguous template selection
|
8
|
+
- Clarified YAML parsing issues with examples in tests and README
|
9
|
+
|
3
10
|
## [0.1.0] - 2025-04-11
|
4
11
|
|
5
12
|
- Initial release
|
data/README.md
CHANGED
@@ -12,6 +12,80 @@ PullRequestTemplates helps teams by:
|
|
12
12
|
|
13
13
|
Works seamlessly both in local development and GitHub Actions workflows.
|
14
14
|
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Install the gem and add to the application's Gemfile by executing:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
bundle add pull_request_templates
|
21
|
+
```
|
22
|
+
|
23
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
24
|
+
|
25
|
+
```bash
|
26
|
+
gem install pull_request_templates
|
27
|
+
```
|
28
|
+
|
29
|
+
## Configuration
|
30
|
+
|
31
|
+
To support multiple templates and automatically select the right one based on changed files, add a `.mapping.yml` file to your template directory:
|
32
|
+
|
33
|
+
```yaml
|
34
|
+
# .github/PULL_REQUEST_TEMPLATE/.mapping.yml
|
35
|
+
feature.md:
|
36
|
+
- "**/feature*.txt"
|
37
|
+
bug_fix.md:
|
38
|
+
- "**/fix*.txt"
|
39
|
+
```
|
40
|
+
|
41
|
+
- Each key is a template filename.
|
42
|
+
- Each value is a list of glob patterns (quoted for YAML compatibility).
|
43
|
+
- The tool will select the template whose patterns match all changed files (MECE logic).
|
44
|
+
- If more than one template matches, you'll get an error listing the ambiguous templates and files.
|
45
|
+
|
46
|
+
## Usage
|
47
|
+
|
48
|
+
> **Note:** This is pre-release software. Currently, it only works with a single template in the pull request template directory.
|
49
|
+
|
50
|
+
### Setting Up Templates
|
51
|
+
|
52
|
+
Place your PR templates in the `.github/PULL_REQUEST_TEMPLATE/` directory. For example:
|
53
|
+
|
54
|
+
```
|
55
|
+
.github/
|
56
|
+
└── PULL_REQUEST_TEMPLATE/
|
57
|
+
├── feature.md
|
58
|
+
├── bug_fix.md
|
59
|
+
└── .mapping.yml
|
60
|
+
```
|
61
|
+
|
62
|
+
The `.mapping.yml` file should define which templates apply to which files:
|
63
|
+
|
64
|
+
```yaml
|
65
|
+
# .github/PULL_REQUEST_TEMPLATE/.mapping.yml
|
66
|
+
feature.md:
|
67
|
+
- "**/feature*.txt"
|
68
|
+
bug_fix.md:
|
69
|
+
- "**/fix*.txt"
|
70
|
+
```
|
71
|
+
|
72
|
+
This setup allows the tool to automatically select the appropriate template based on your changes.
|
73
|
+
|
74
|
+
### Creating a Pull Request
|
75
|
+
|
76
|
+
When you're ready to create a pull request:
|
77
|
+
|
78
|
+
```bash
|
79
|
+
pull_request_templates pr-url
|
80
|
+
```
|
81
|
+
|
82
|
+
This command:
|
83
|
+
- Selects an appropriate template based on your changes
|
84
|
+
- Generates a GitHub PR URL with the template parameter
|
85
|
+
- Outputs the URL to your terminal
|
86
|
+
|
87
|
+
Open the URL in your browser to create a pull request with the template pre-applied.
|
88
|
+
|
15
89
|
## Development
|
16
90
|
|
17
91
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/exe/pull_request_templates
CHANGED
data/lefthook.yml
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
require "thor"
|
2
|
+
require "yaml"
|
3
|
+
require "pathname"
|
4
|
+
|
5
|
+
module PullRequestTemplates
|
6
|
+
class Cli < Thor
|
7
|
+
# Set exit_on_failure to ensure Thor exits with non-zero status on errors
|
8
|
+
def self.exit_on_failure? = true
|
9
|
+
|
10
|
+
desc "pr-url", "Generate a pull request URL based on changes"
|
11
|
+
def pr_url
|
12
|
+
# Find all available templates
|
13
|
+
templates = get_available_templates
|
14
|
+
|
15
|
+
if templates.empty?
|
16
|
+
raise Thor::Error, "No templates found"
|
17
|
+
end
|
18
|
+
|
19
|
+
# Check if we're on the default branch
|
20
|
+
current_branch = `git rev-parse --abbrev-ref HEAD`.strip
|
21
|
+
|
22
|
+
if current_branch == "main"
|
23
|
+
raise Thor::Error, "Cannot generate PR URL while on default branch"
|
24
|
+
end
|
25
|
+
|
26
|
+
# Check if there are any changes to detect
|
27
|
+
changes = get_changes
|
28
|
+
|
29
|
+
if changes.empty?
|
30
|
+
raise Thor::Error, "No changes detected"
|
31
|
+
end
|
32
|
+
|
33
|
+
# Select appropriate template
|
34
|
+
template = select_template(templates, changes)
|
35
|
+
|
36
|
+
# Generate the pull request URL
|
37
|
+
url = generate_pr_url(current_branch, template)
|
38
|
+
say url
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def get_available_templates
|
44
|
+
template_dir = ".github/PULL_REQUEST_TEMPLATE"
|
45
|
+
Dir.glob("#{template_dir}/*.md").map { |path| File.basename(path) }
|
46
|
+
end
|
47
|
+
|
48
|
+
def get_changes
|
49
|
+
# Get changes between current branch and main branch
|
50
|
+
# Returns an array of changed file paths or an empty array if no changes
|
51
|
+
changes = `git diff --name-only main...HEAD`.strip
|
52
|
+
changes.split("\n").reject(&:empty?)
|
53
|
+
end
|
54
|
+
|
55
|
+
def select_template(templates, changes)
|
56
|
+
mapping_file = ".github/PULL_REQUEST_TEMPLATE/.mapping.yml"
|
57
|
+
candidates = []
|
58
|
+
if File.exist?(mapping_file)
|
59
|
+
mapping = YAML.load_file(mapping_file)
|
60
|
+
matches = Hash.new { |h, k| h[k] = [] }
|
61
|
+
changes.each do |file|
|
62
|
+
mapping.each do |template, patterns|
|
63
|
+
Array(patterns).each do |pattern|
|
64
|
+
matches[template] << file if File.fnmatch(pattern, file, File::FNM_PATHNAME | File::FNM_EXTGLOB)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
selected = matches.select { |_, files| files.sort == changes.sort }
|
69
|
+
candidates = selected.keys if selected.any?
|
70
|
+
end
|
71
|
+
candidates = templates if candidates.empty?
|
72
|
+
if candidates.length == 1
|
73
|
+
return candidates.first
|
74
|
+
end
|
75
|
+
raise AmbiguousTemplateSelection, <<~MESSAGE
|
76
|
+
Unable to pick one template from #{candidates} for the changes to #{changes.count} files:
|
77
|
+
* #{changes.join("\n* ")}
|
78
|
+
MESSAGE
|
79
|
+
end
|
80
|
+
|
81
|
+
def generate_pr_url(branch, template)
|
82
|
+
# Get the repository URL from git config
|
83
|
+
remote_url = `git config --get remote.origin.url`.strip
|
84
|
+
|
85
|
+
# Extract repository path from SSH URL format (git@github.com:user/repo.git)
|
86
|
+
repo_path = remote_url.sub(/^git@github\.com:/, "").sub(/\.git$/, "")
|
87
|
+
|
88
|
+
# Generate GitHub pull request URL with template parameter
|
89
|
+
"https://github.com/#{repo_path}/compare/#{branch}?expand=1&quick_pull=1&template=#{template}"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -1,8 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative "pull_request_templates/version"
|
4
|
+
require_relative "pull_request_templates/cli"
|
4
5
|
|
5
6
|
module PullRequestTemplates
|
6
|
-
|
7
|
-
|
7
|
+
Error = Class.new(StandardError)
|
8
|
+
AmbiguousTemplateSelection = Class.new(Error)
|
8
9
|
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pull_request_templates
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Caleb Buxton
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-
|
12
|
-
dependencies:
|
11
|
+
date: 2025-05-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: thor
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
13
27
|
description: A tool that reduces pull request description toil and provides the right
|
14
28
|
context for changes.
|
15
29
|
email:
|
@@ -27,9 +41,10 @@ files:
|
|
27
41
|
- README.md
|
28
42
|
- Rakefile
|
29
43
|
- exe/pull_request_templates
|
44
|
+
- lefthook.yml
|
30
45
|
- lib/pull_request_templates.rb
|
46
|
+
- lib/pull_request_templates/cli.rb
|
31
47
|
- lib/pull_request_templates/version.rb
|
32
|
-
- sig/pull_request_templates.rbs
|
33
48
|
homepage: https://github.com/cpb/pull_request_templates
|
34
49
|
licenses:
|
35
50
|
- MIT
|
@@ -52,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
67
|
- !ruby/object:Gem::Version
|
53
68
|
version: '0'
|
54
69
|
requirements: []
|
55
|
-
rubygems_version: 3.
|
70
|
+
rubygems_version: 3.5.22
|
56
71
|
signing_key:
|
57
72
|
specification_version: 4
|
58
73
|
summary: Match pull request templates to your changes.
|