1

I am relatively new to GitHub, and I am trying to write a GitHub workflow to automatically generate a badge that would be displayed on the README.md file. The workflow works without errors, but the file I expected to be generated is nowhere to be found.

I tried using the search feature to locate the two files I expected to be created (pylint.out and pylint_badge.svg), but I found nothing. I tested pylint_test.py on my local machine, and it works fine. Maybe I configured the YAML file incorrectly?

Here's my code:

pylint_test.py (in the root directory)

from pylint.lint import Run
from pylint.reporters.text import TextReporter
from pybadges import badge
import os

grade = None
color = None
abs = "/home/runner/work/DigitsSolver/DigitsSolver/"

with open(abs + "pylint.out", "w+") as f:
    reporter = TextReporter(f)
    Run([abs + "solver"], reporter=reporter, exit=False)

with open(abs + "pylint.out", "r") as f:
    f = f.read()
    target = "Your code has been rated at "
    f = f[f.index(target) + len(target):]
    f = f[:f.index("/")]
    grade = float(f)

if grade <= 1.2:
    color = '#e7241d'
elif grade <= 3.6:
    color = '#ef832c'
elif grade <= 6.0:
    color = '#fffd46'
elif grade <= 8.4:
    color = '#9cfa40'
else:
    color = '#60f83d'

s = badge(left_text='pylint score', right_text=str(grade), right_color=color)

with open(abs + "pylint_badge.svg", "w+") as f:
    f.write(s)

pylint-badge.yml (under the .github/workflow/ directory)

name: Pylint

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ["3.8"]

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v3
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install pybadges
          pip install pylint

      - name: Analyze the code with pylint
        run: |
          python pylint_test.py
Anthon
  • 69,918
  • 32
  • 186
  • 246
fish_brain
  • 21
  • 3

1 Answers1

1

After some investigation, I discovered a critical step that I had overlooked, which is essential for pushing the changes to the GitHub page.

It turns out that the environment in which the workflow runs is separate from the actual GitHub page. To make it work, you need to grant the workflow permission to write changes. You can find a detailed guide on granting permissions in this Stack Overflow post.

To push the changes to the GitHub page, you can include the following code snippet in your workflow:

- name: Pushing the change
  run: |
    git config --global user.name "YOUR NAME"
    git config --global user.email YOUREMAIL@EXAMPLE.COM
    git commit pylint_badge.svg -m "Merge"
    git commit pylint.out -m "Merge"
    git push

Alternatively, if you prefer to save the files without directly committing them to GitHub, you can use artifacts. The following code snippet demonstrates how to upload the pylint.out file as an artifact:

- name: Upload artifact
  uses: actions/upload-artifact@v1
  with:
    name: pylint.out
    path: pylint.out

As a side note, if you plan to use an SVG file in your README.md, it's recommended to use the raw file link instead of a direct path. This ensures smooth rendering within the file.

fish_brain
  • 21
  • 3