Skip to content

Instantly share code, notes, and snippets.

@VanTanev
Created May 2, 2023 13:01
Show Gist options
  • Select an option

  • Save VanTanev/a8fa00edac06442413c15ab8a61d8151 to your computer and use it in GitHub Desktop.

Select an option

Save VanTanev/a8fa00edac06442413c15ab8a61d8151 to your computer and use it in GitHub Desktop.

Revisions

  1. VanTanev created this gist May 2, 2023.
    13 changes: 13 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    # A github workflow to track the progress of converting JavaScript to TypeScript.

    Last year, we migrated a large project form JS to TS.
    We did this with [ts-migrate](https://github.com/airbnb/ts-migrate), and we renamed all instances of `any` to `$TSFixMe`, to differentiate them from real uses of `any`.

    ## Tracking the convertion over time

    In order to see our progress on removing `$TSFixMe`, we build the a github workflow that adds comments to PRs with our progress, eg:

    ![image](https://user-images.githubusercontent.com/202527/235672978-52fa563d-44f0-46fe-b53a-add62e5d8d72.png)


    I've shared the workflow and bash script below.
    54 changes: 54 additions & 0 deletions tsfixme-progress.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    # .github/workflows/tsfixme-progress.yml

    name: $TSFixMe progress

    on:
    pull_request:
    types: [opened, synchronize]
    branches:
    - master

    concurrency:
    group: tsfixme-progress-{{ github.ref }}
    cancel-in-progress: true

    jobs:
    tsfixme-progress:
    runs-on: "ubuntu-latest"
    steps:
    - name: Fetch hub
    run: |
    (
    cd /tmp
    curl -s -L https://github.com/github/hub/releases/download/v2.12.3/hub-linux-amd64-2.12.3.tgz > hub-linux-amd64-2.12.3.tgz
    tar xzf hub-linux-amd64-2.12.3.tgz hub-linux-amd64-2.12.3/bin/hub
    mv hub-linux-amd64-2.12.3/bin/hub hub
    )
    - name: Install ripgrep
    run: |
    (
    cd /tmp
    curl -LO https://github.com/BurntSushi/ripgrep/releases/download/13.0.0/ripgrep_13.0.0_amd64.deb
    sudo dpkg -i ripgrep_13.0.0_amd64.deb
    )
    - uses: actions/checkout@v3
    with:
    ref: ${{ github.event.pull_request.base.sha }}

    - name: Count base instances of $TSFixMe
    run: |
    rg "\\\$TSFixMe" --stats . | rg '.*matches$' | head -1 | awk '{ print $1 }' > /tmp/base_count_tsfixme_instances
    rg "ts-expect-error ts-migrate" --stats . | rg '.*matches$' | head -1 | awk '{ print $1 }' > /tmp/base_count_tserror_instances
    rg "\\\$TSFixMe" --stats . | rg '.*matches$' | tail -1 | awk '{ print $1 }' > /tmp/base_count_tsfixme_files
    - uses: actions/checkout@v3

    - name: Execute progress script
    env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    GITHUB_USER: github
    PR_NUMBER: ${{ github.event.pull_request.number }}
    run: |
    ./.github/scripts/tsfixme_progress.sh
    88 changes: 88 additions & 0 deletions tsfixme_progress.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    #!/usr/bin/env bash
    # .github/scripts/tsfixme_progress.sh

    set -ex

    # Measured on 2022-06-24
    INITIAL_COUNT_TSFIXME_FILES=1938
    INITIAL_COUNT_TSFIXME_INSTANCES=8398
    INITIAL_COUNT_TSERROR_INSTANCES=1763

    MAGIC_COMMENT_HINT="<!-- tsfixme:progress:comment -->"

    # Generated by a previous workflow step
    base_count_tsfixme_instances=$(cat /tmp/base_count_tsfixme_instances)
    base_count_tserror_instances=$(cat /tmp/base_count_tserror_instances)
    base_count_tsfixme_files=$(cat /tmp/base_count_tsfixme_files)

    count_tsfixme_instances=$(rg "\\\$TSFixMe" --stats . | rg '.*matches$' | head -1 | awk '{ print $1 }')
    count_tserror_instances=$(rg 'ts-expect-error ts-migrate' --stats . | rg '.*matches$' | head -1 | awk '{ print $1 }')
    count_tsfixme_files=$(rg "\\\$TSFixMe" --stats . | rg '.*matches$' | tail -1 | awk '{ print $1 }')

    main() {
    if [ "$count_tsfixme_files" -gt "$base_count_tsfixme_files" ] || [ "$count_tsfixme_instances" -gt "$base_count_tsfixme_instances" ] || [ "$count_tserror_instances" -gt "$base_count_tserror_instances" ]; then
    write_comment "$(error_comment)"
    exit 1
    elif [ "$count_tsfixme_files" -lt "$base_count_tsfixme_files" ] || [ "$count_tsfixme_instances" -lt "$base_count_tsfixme_instances" ] || [ "$count_tserror_instances" -lt "$base_count_tserror_instances" ]; then
    write_comment "$(success_comment)"
    else
    cleanup_comment
    fi
    }

    success_comment() {
    echo "$MAGIC_COMMENT_HINT
    ## :tada: Amount of \`\$TSFixMe\` decreased!
    $(table)
    "
    }

    error_comment() {
    echo "$MAGIC_COMMENT_HINT
    ## :x: Amount of \`\$TSFixMe\` increased!
    $(table)
    "
    }

    write_comment() {
    comment_body="$1"

    magic_comment_id=$(/tmp/hub api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments?per_page=100" | jq -r ".[] | select(.body | startswith(\"${MAGIC_COMMENT_HINT}\")) | .id" | head -n 1)

    if [ -z "$magic_comment_id" ]; then
    /tmp/hub api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" --raw-field "body=${comment_body}"
    else
    /tmp/hub api --method PATCH "repos/${GITHUB_REPOSITORY}/issues/comments/${magic_comment_id}" --raw-field "body=${comment_body}"
    fi
    }

    cleanup_comment() {
    magic_comment_id=$(/tmp/hub api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments?per_page=100" | jq -r ".[] | select(.body | startswith(\"${MAGIC_COMMENT_HINT}\")) | .id" | head -n 1)
    if [ -n "$magic_comment_id" ]; then
    /tmp/hub api --method DELETE "repos/${GITHUB_REPOSITORY}/issues/comments/${magic_comment_id}"
    fi
    }

    table() {
    diff_tsfixme_instances=$(printf "%+0.2f" "$(bc -q <<< "scale=4; (($count_tsfixme_instances / $base_count_tsfixme_instances) -1 ) * 100")")
    progress_tsfixme_instances=$(printf "%0.2f" "$(bc -q <<< "scale=4; (($INITIAL_COUNT_TSFIXME_INSTANCES - $count_tsfixme_instances) / $INITIAL_COUNT_TSFIXME_INSTANCES) * 100")")

    diff_tsfixme_files=$(printf "%+0.2f" "$(bc -q <<< "scale=4; (($count_tsfixme_files / $base_count_tsfixme_files) -1 ) * 100")")
    progress_tsfixme_files=$(printf "%0.2f" "$(bc -q <<< "scale=4; (($INITIAL_COUNT_TSFIXME_FILES - $count_tsfixme_files) / $INITIAL_COUNT_TSFIXME_FILES) * 100")")

    diff_tserror_instances=$(printf "%+0.2f" "$(bc -q <<< "scale=4; (($count_tserror_instances / $base_count_tserror_instances) -1 ) * 100")")
    progress_tserror_instances=$(printf "%0.2f" "$(bc -q <<< "scale=4; (($INITIAL_COUNT_TSERROR_INSTANCES - $count_tserror_instances) / $INITIAL_COUNT_TSERROR_INSTANCES) * 100")")


    echo "
    | Count | Base Branch | This Branch | Cumulative Progress since 2022-06-24 |
    |------------------------------------|-------------------------------|-------------------------------------------------------|------------------------------------------------------------------------|
    | \`\$TSFixMe\` | $base_count_tsfixme_instances | $count_tsfixme_instances (${diff_tsfixme_instances}%) | ${progress_tsfixme_instances}% (from $INITIAL_COUNT_TSFIXME_INSTANCES) |
    | Files containing \`\$TSFixMe\` | $base_count_tsfixme_files | $count_tsfixme_files (${diff_tsfixme_files}%) | ${progress_tsfixme_files}% (from $INITIAL_COUNT_TSFIXME_FILES) |
    | \`// ts-expect-error ts-migrate\` | $base_count_tserror_instances | $count_tserror_instances (${diff_tserror_instances}%) | ${progress_tserror_instances}% (from $INITIAL_COUNT_TSERROR_INSTANCES) |
    "
    }

    main