Skip to content

Commit

Permalink
build: resolve authors via .mailmap file for changelog generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeshifter committed Sep 26, 2024
1 parent 6fd2e23 commit 4f49bd5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

// MODULES //

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

// VARIABLES //

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


// FUNCTIONS //

/**
* Resolves a Git user name and email address according to the .mailmap file.
*
* @private
* @param {string} pair - name and email pair
* @returns {string} canonical name and email address if found, otherwise the original input
*/
function resolveNameEmailPair( pair ) {
try {
debug( 'Attempting to resolve name and email: %s.', pair );
return spawn( RESOLVE_NAME_EMAIL_CMD, [ pair ], {
'stdio': [ 'pipe', 'pipe', 'ignore' ] // stdin, stdout, stderr
}).stdout.toString();
} catch ( err ) {
debug( 'Encountered an error resolving name and email: %s.', err.message );
return pair;
}
}

/**
* Extracts a list of contributors from a list of commits.
*
Expand Down Expand Up @@ -66,7 +90,8 @@ function extractContributors( commits ) {
if (
RE_CO_AUTHORED_BY.test( mention.action )
) {
author = replace( mention.ref, /\s*<[^>]+>\s*/, '' );
author = resolveNameEmailPair( mention.ref );
author = replace( author, /\s*<[^>]+>\s*/, '' );
if (
!contains( out, author ) &&
!contains( EXCLUDED_CONTRIBUTORS, author )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env bash
#
# @license Apache-2.0
#
# Copyright (c) 2024 The Stdlib Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Script for resolving a name and email using .mailmap via `git check-mailmap``.

This comment has been minimized.

Copy link
@Planeshifter

Planeshifter Sep 26, 2024

Author Member

Extra "`" at the end. Removed.

#
# Usage: resolve-mailmap.sh "name <email>"
#
# Arguments:
#
# name_email The name and email pair in the format "name <email>"
#

if [ -z "$1" ]; then
echo "Error: must provide a name and email in the format \"name <email>\"." >&2
exit 1
fi

name_email="$1"

resolved=$(git check-mailmap "$name_email" 2>/dev/null)
if [ -n "$resolved" ]; then
echo "$resolved"
else
echo "$name_email"
fi

0 comments on commit 4f49bd5

Please sign in to comment.