Skip to content

Commit 4f49bd5

Browse files
committed
build: resolve authors via .mailmap file for changelog generation
1 parent 6fd2e23 commit 4f49bd5

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

lib/node_modules/@stdlib/_tools/changelog/generate/lib/format_contributors.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
// MODULES //
2222

23+
var join = require( 'path' ).join;
24+
var spawn = require( 'child_process' ).spawnSync; // eslint-disable-line node/no-sync
25+
var logger = require( 'debug' );
2326
var contains = require( '@stdlib/assert/contains' );
2427
var replace = require( '@stdlib/string/replace' );
2528
var map = require( '@stdlib/utils/map' );
@@ -31,11 +34,32 @@ var EXCLUDED_CONTRIBUTORS = require( './excluded_contributors.json' );
3134

3235
// VARIABLES //
3336

37+
var debug = logger( 'changelog:generate:format-contributors' );
3438
var RE_CO_AUTHORED_BY = /co-authored-by/i;
39+
var RESOLVE_NAME_EMAIL_CMD = join( __dirname, '..', 'scripts', 'resolve_name_email.sh' );
3540

3641

3742
// FUNCTIONS //
3843

44+
/**
45+
* Resolves a Git user name and email address according to the .mailmap file.
46+
*
47+
* @private
48+
* @param {string} pair - name and email pair
49+
* @returns {string} canonical name and email address if found, otherwise the original input
50+
*/
51+
function resolveNameEmailPair( pair ) {
52+
try {
53+
debug( 'Attempting to resolve name and email: %s.', pair );
54+
return spawn( RESOLVE_NAME_EMAIL_CMD, [ pair ], {
55+
'stdio': [ 'pipe', 'pipe', 'ignore' ] // stdin, stdout, stderr
56+
}).stdout.toString();
57+
} catch ( err ) {
58+
debug( 'Encountered an error resolving name and email: %s.', err.message );
59+
return pair;
60+
}
61+
}
62+
3963
/**
4064
* Extracts a list of contributors from a list of commits.
4165
*
@@ -66,7 +90,8 @@ function extractContributors( commits ) {
6690
if (
6791
RE_CO_AUTHORED_BY.test( mention.action )
6892
) {
69-
author = replace( mention.ref, /\s*<[^>]+>\s*/, '' );
93+
author = resolveNameEmailPair( mention.ref );
94+
author = replace( author, /\s*<[^>]+>\s*/, '' );
7095
if (
7196
!contains( out, author ) &&
7297
!contains( EXCLUDED_CONTRIBUTORS, author )
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env bash
2+
#
3+
# @license Apache-2.0
4+
#
5+
# Copyright (c) 2024 The Stdlib Authors.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
# Script for resolving a name and email using .mailmap via `git check-mailmap``.
20+
#
21+
# Usage: resolve-mailmap.sh "name <email>"
22+
#
23+
# Arguments:
24+
#
25+
# name_email The name and email pair in the format "name <email>"
26+
#
27+
28+
if [ -z "$1" ]; then
29+
echo "Error: must provide a name and email in the format \"name <email>\"." >&2
30+
exit 1
31+
fi
32+
33+
name_email="$1"
34+
35+
resolved=$(git check-mailmap "$name_email" 2>/dev/null)
36+
if [ -n "$resolved" ]; then
37+
echo "$resolved"
38+
else
39+
echo "$name_email"
40+
fi

0 commit comments

Comments
 (0)