name: nf-core linting
# This workflow is triggered on pushes and PRs to the repository.
# It runs the `nf-core lint` and markdown lint tests to ensure that the code meets the nf-core guidelines
on:
  push:
  pull_request:
  release:
    types: [published]

jobs:
  Markdown:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
      - name: Install markdownlint
        run: npm install -g markdownlint-cli
      - name: Run Markdownlint
        run: markdownlint .

      # If the above check failed, post a comment on the PR explaining the failure
      - name: Post PR comment
        if: failure()
        uses: mshick/add-pr-comment@v1
        with:
          message: |
            ## Markdown linting is failing

            To keep the code consistent with lots of contributors, we run automated code consistency checks.
            To fix this CI test, please run:

            * Install `markdownlint-cli`
                * On Mac: `brew install markdownlint-cli`
                * Everything else: [Install `npm`](https://www.npmjs.com/get-npm) then [install `markdownlint-cli`](https://www.npmjs.com/package/markdownlint-cli) (`npm install -g markdownlint-cli`)
            * Fix the markdown errors
                * Automatically: `markdownlint . --fix`
                * Manually resolve anything left from `markdownlint .`

            Once you push these changes the test should pass, and you can hide this comment :+1:

            We highly recommend setting up markdownlint in your code editor so that this formatting is done automatically on save. Ask about it on Slack for help!

            Thanks again for your contribution!
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          allow-repeats: false

  EditorConfig:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - uses: actions/setup-node@v2

      - name: Install editorconfig-checker
        run: npm install -g editorconfig-checker

      - name: Run ECLint check
        run: editorconfig-checker -exclude README.md $(git ls-files | grep -v test)

  YAML:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@master
      - name: 'Yamllint'
        uses: karancode/yamllint-github-action@master
        with:
          yamllint_file_or_dir: '.'
          yamllint_config_filepath: '.yamllint.yml'

      # If the above check failed, post a comment on the PR explaining the failure
      - name: Post PR comment
        if: failure()
        uses: mshick/add-pr-comment@v1
        with:
          message: |
            ## YAML linting is failing

            To keep the code consistent with lots of contributors, we run automated code consistency checks.
            To fix this CI test, please run:

            * Install `yamllint`
                * Install `yamllint` following [this](https://yamllint.readthedocs.io/en/stable/quickstart.html#installing-yamllint)
                instructions or alternative install it in your [conda environment](https://anaconda.org/conda-forge/yamllint)
            * Fix the markdown errors
                * Run the test locally: `yamllint $(find . -type f -name "*.yml" -o -name "*.yaml") -c ./.yamllint.yml`
                * Fix any reported errors in your YAML files

            Once you push these changes the test should pass, and you can hide this comment :+1:

            We highly recommend setting up yaml-lint in your code editor so that this formatting is done automatically on save. Ask about it on Slack for help!

            Thanks again for your contribution!
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          allow-repeats: false

  nf-core:
    runs-on: ubuntu-latest
    steps:

      - name: Check out pipeline code
        uses: actions/checkout@v2

      - name: Install Nextflow
        env:
          CAPSULE_LOG: none
        run: |
          wget -qO- get.nextflow.io | bash
          sudo mv nextflow /usr/local/bin/

      - uses: actions/setup-python@v1
        with:
          python-version: '3.6'
          architecture: 'x64'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install nf-core

      - name: Run nf-core lint
        env:
          GITHUB_COMMENTS_URL: ${{ github.event.pull_request.comments_url }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GITHUB_PR_COMMIT: ${{ github.event.pull_request.head.sha }}
        run: nf-core -l lint_log.txt lint --dir ${GITHUB_WORKSPACE} --markdown lint_results.md

      - name: Save PR number
        if: ${{ always() }}
        run: echo ${{ github.event.pull_request.number }} > PR_number.txt

      - name: Upload linting log file artifact
        if: ${{ always() }}
        uses: actions/upload-artifact@v2
        with:
          name: linting-logs
          path: |
            lint_log.txt
            lint_results.md
            PR_number.txt

