1

I work with postman collections in git. Postman does a lot of things well, but the id regeneration that occur when you import are not ideal.

Essentially importing a postman collection, and exporting it again results in a change for every id

e.g. output from git diff-index -p HEAD --

@@ -2404,7 +2412,7 @@
      {
        "listen": "test",
        "script": {
-           "id": "60ff37a6-9bf7-4cb4-b142-2da49ff4b86e",
+           "id": "38c15d28-8382-4eaf-ad17-f053c143212d",
            "exec": [
                "pm.test(\"Status code is 200\", function () {",
                "    pm.response.to.have.status(200);",

I want to go through the changes in the file and undo all the id changes, but preserve all the others.

Essentially I want to automate running git add -p {my_postman.collection.json} answering n to each line with a change to id.

I can see that Git command to programatically add a range of lines of a file to the index? is going the right way as well as Make git automatically remove trailing whitespace before committing as well

JoSSte
  • 2,953
  • 6
  • 34
  • 54

2 Answers2

0

I would suggest to write a script, e.g. restore-existing-ids.sh, which would automate restoring the ids in your file on disk.

This would be more straightforward to write than a program which has to simulate an interactive back & forth with git,
and you could still use it in an alias :

add-postman = '! f () { bash restore-existing-ids.sh "$1" && git add "$1"; }; f'

to "clean" such a file before adding it.

note : I implied this script should be a bash script, it can obviously be written in any language (python, ruby, node ... whatever floats your boat)

LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • 1
    I don't know how postman works, if these ids are *always* regenerated by postman, the script can be as simple as `sed -e 's/"id": "[^"]*",$/"id": "00000000-0000-0000-0000-000000000000",/'` (systematically replace these GUIDs wit a fixed value) – LeGEC Oct 30 '20 at 17:23
  • I hadn't considered assigning them a fixed value - good point – JoSSte Oct 30 '20 at 17:40
  • 1
    If they are re-generated anyway, you can even consider deleting them – LeGEC Oct 30 '20 at 19:27
0

This is my solution based on the suggestions by @LeGEC, put here for others who come looking for an actual solution.


    #!/usr/bin/env bash
    
    # Check the command line argument value exists or not
    if [ $1 != "" ]; then
    
        if [ $1 == "--help" ]; then
    
            echo "Replaces UUIDs in *.postman_collection.json exported files with 00000000-0000-0000-0000-000000000000"
            exit 0
        else
    
            if [ "${1: -24}" == ".postman_collection.json" ]; then
    
                echo "Removing IDs from $1"
                # stat -c %y "$1" # show last modified time
    
                # format:   60ff37a6-9bf7-4cb4-b142-2da499f4b86e
                #           00000000-0000-0000-0000-000000000000
                #           12345678-1234-1234-1234-123456789012
    
                 sed -i -r 's/"(_postman_id|id)": "([a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})"/"\1": "00000000-0000-0000-0000-000000000000"/gm' "$1"
    
            else
    
                echo "Your file MUST end on .postman_collection.json or else it will not be parsed! \"$1\" is not valid"
                exit 1
    
            fi
    
        fi
    fi

The version for .git/hooks/pre-commit The downside to this approach is that the hooks are not a part of the repository. I have uploaded these scripts to github

trying to get my head around the way this works by studying this answer: Unexpected behavior with "git commit ." when pre-commit hook modifies staged files



#!/usr/bin/env bash

#get the changed files
files=`git diff --cached --name-status | awk '$1 != "D" { print $2 }'`
#set changed flag to false
CHANGED=false
for filename in $files; do
    if [ "${filename: -24}" == ".postman_collection.json" ]; then
        sed -i -r 's/"(_postman_id|id)": "([a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12})"/"\1": "00000000-0000-0000-0000-000000000000"/gm' "$filename"
        git add $filename
        # mark changed flag true
        CHANGED=true
    fi
done
# if files have been changed (potentially) display a message and abort the commit
if $CHANGED; then
    echo "PRE-COMMIT: Postman collection found. UUIDs have been sanitized. Please verify and recommit"
    exit 1
fi
JoSSte
  • 2,953
  • 6
  • 34
  • 54