Skip to content

feat: refactor string package remove-first #15

feat: refactor string package remove-first

feat: refactor string package remove-first #15

#/
# @license Apache-2.0
#
# Copyright (c) 2023 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.
#/
# Workflow name:
name: run_tests_coverage.yml
# Workflow triggers:
on:
push:
paths:
# List paths for which changes should trigger this workflow:
- 'lib/**/bin/**'
- 'lib/**/data/**'
- 'lib/**/etc/**'
- 'lib/**/include/**'
- 'lib/**/lib/**'
- 'lib/**/src/**'
- 'lib/**/test/**'
- 'lib/**/*.gyp'
- 'lib/**/*.gypi'
- 'lib/**/manifest.json'
- 'package.json'
# List paths for which changes should *not* trigger this workflow:
- '!lib/**/_tools/**'
pull_request:
types:
- opened
- synchronize
- reopened
paths:
# List paths for which changes should trigger this workflow:
- 'lib/**/bin/**'
- 'lib/**/data/**'
- 'lib/**/etc/**'
- 'lib/**/include/**'
- 'lib/**/lib/**'
- 'lib/**/src/**'
- 'lib/**/test/**'
- 'lib/**/*.gyp'
- 'lib/**/*.gypi'
- 'lib/**/manifest.json'
- 'package.json'
# List paths for which changes should *not* trigger this workflow:
- '!lib/**/_tools/**'
workflow_dispatch:
inputs:
directories:
description: 'List of changed directories for which to run tests (space separated):'
# Workflow jobs:
jobs:
# Define a job for calculating test coverage for changed files...
coverage:
# Define a display name:
name: 'Calculate test coverage for packages'
# Define the type of virtual host machine:
runs-on: ubuntu-latest
# Define the sequence of job steps...
steps:
# Checkout the repository:
- name: 'Checkout repository'
uses: actions/checkout@v3
with:
# Specify whether to remove untracked files before checking out the repository:
clean: true
# Limit clone depth to the last 1000 commits:
fetch-depth: 1000
# Specify whether to download Git-LFS files:
lfs: false
timeout-minutes: 10
# Install Node.js:
- name: 'Install Node.js'
uses: actions/setup-node@v3
with:
node-version: '16' # 'lts/*'
timeout-minutes: 5
# Install dependencies (accounting for possible network failures, etc, when installing node module dependencies):
- name: 'Install dependencies'
run: |
make install-node-modules || make install-node-modules || make install-node-modules
timeout-minutes: 15
# Initialize development environment:
- name: 'Initialize development environment'
run: |
make init
timeout-minutes: 5
# Get list of changed directories from PR and push events:
- name: 'Get list of changed directories'
if: github.event_name != 'workflow_dispatch'
id: changed-directories
run: |
if [ -n "${{ github.event.pull_request.number }}" ]; then
# Get the list of changed files in pull request:
ancestor_commit=$(git merge-base ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }})
files=$(git diff --diff-filter=AM --name-only $ancestor_commit ${{ github.event.pull_request.head.sha }})
else
# Get changed files by comparing the current commit to the commit before the push event or with its parent:
if [ "${{ github.event.before }}" == "0000000000000000000000000000000000000000" ]; then
files=$(git diff --diff-filter=AM --name-only HEAD~ ${{ github.event.after }})
else
files=$(git diff --diff-filter=AM --name-only ${{ github.event.before }} ${{ github.event.after }})
fi
fi
directories=$(for file in $files; do dirname $file; done | uniq | tr '\n' ' ' | sed 's/ $//')
echo "directories=${directories}" >> $GITHUB_OUTPUT
# Get list of changed directories from workflow dispatch event:
- name: 'Get list of changed directories (from user input)'
if: github.event_name == 'workflow_dispatch'
id: changed-directories-user-input
run: |
echo "directories=${{ github.event.inputs.directories }}" >> $GITHUB_OUTPUT
timeout-minutes: 5
# Run JavaScript tests:
- name: 'Run JavaScript tests'
run: |
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
directories="${{ steps.changed-directories-user-input.outputs.directories }}"
else
directories="${{ steps.changed-directories.outputs.directories }}"
fi
. "$GITHUB_WORKSPACE/.github/workflows/scripts/run_tests_coverage" "$directories"
timeout-minutes: 30
# Extract coverage values and assign to output:
- name: 'Extract coverage values and assign to output'
id: extract-coverage
run: |
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
directories="${{ steps.changed-directories-user-input.outputs.directories }}"
else
directories="${{ steps.changed-directories.outputs.directories }}"
fi
directories=$(echo "${directories}" | tr ' ' '\n' | grep '^lib/node_modules/@stdlib') || true
directories=$(echo "${directories}" | tr ' ' '\n' | sed -E 's/\/(benchmark|bin|data|docs|etc|examples|include|lib|src|test)(\/.*)?$//')
directories=$(echo "${directories}" | uniq)
# For each package, extract coverage value from the respective coverage report:
coverage=''
for package in $directories; do
pkg=`echo $package | sed -E 's/^.*stdlib\///'`
pkg_cov=`cat reports/coverage/lcov-report/$pkg/lib/index.html | grep "fraction" | grep -oP '\d+/\d+' | printf %s "$(cat)" | jq -R -s -c 'split("\n")'`
pkg_url="https://github.com/${{ github.repository }}/tree/${{ github.ref }}/$package"
pkg_link="<a href="$pkg_url">$pkg</a>"
coverage="$coverage\n| $pkg_link $pkg_cov"
done
# Format coverage as Markdown table row:
table_body=`echo $coverage | sed -e 's/,/|/g; s/"/ /g; s/\[/|/g; s/\]/|/g'`
table_header="| Package | Statements | Branches | Functions | Lines |\n| --------- | ------------ | ---------- | ----------- | ----- |"
table="${table_header}${table_body}"
if [ "${{ github.event_name }}" == "pull_request" ]; then
compare_url="https://github.com/stdlib-js/stdlib/compare/${{ github.event.pull_request.base.sha }}...${{ github.event.pull_request.head.sha }}"
compare_txt="The above coverage report was generated for the [changes in this PR]($compare_url)."
else if [ "${{ github.event_name }}" == "push" ]; then
compare_url="https://github.com/stdlib-js/stdlib/compare/${{ github.event.before }}...${{ github.event.after }}"
compare_txt="The above coverage report was generated for the [changes in this push]($compare_url)."
else
compare_txt=""
fi
report="## Coverage Report\n\n${table}\n\n${compare_txt}"
echo "report=$report" >> $GITHUB_OUTPUT
# Post report as comment to PR:
- name: 'Post report as comment to PR'
if: github.event_name == 'pull_request'
uses: actions/github-script@v4
with:
github-token: ${{ secrets.CHATBOT_GITHUB_TOKEN }}
script: |
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '${{ steps.extract-coverage.outputs.report }}'
})
# Post report as comment to commit:
- name: 'Post report as comment to commit'
if: github.event_name == 'push'
uses: actions/github-script@v4
with:
github-token: ${{ secrets.CHATBOT_GITHUB_TOKEN }}
script: |
github.repos.createCommitComment({
commit_sha: context.sha,
owner: context.repo.owner,
repo: context.repo.repo,
body: '${{ steps.extract-coverage.outputs.report }}'
})