Skip to content

BUG - env overrides not working #5068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
emouawad opened this issue Mar 28, 2025 · 7 comments
Open

BUG - env overrides not working #5068

emouawad opened this issue Mar 28, 2025 · 7 comments
Labels
question Further information is requested

Comments

@emouawad
Copy link

I have implemented a centralized github actions workflow that will git pull, lint and build other targeted repositories.

  • central-repo : Global workflow with megalinter
  • repo-a
  • repo-b

central-repo will analyze and build repo-a or repo-b

Currently everything is working fine - megalinter can lint repo-a or repo-b successfully.

My issue is with the PR Commentor: Error [GitHub Comment Reporter] No pull request has been found, so no comment has been posted.

This happens because megalinter is assuming that the PR is on central-repo not on repo-a

I have tried setting environment variable
GITHUB_REPOSITORY= repo-a
GITHUB_SHA=aaaabbbb
GITHUB_REF=aaaabbbb
but MegaLinter initialization still shows GITHUB_REPOSITORY=central-repo

Basically i need to tell megalinter to target another repository commit/PR to update it with the results.

@emouawad emouawad added the question Further information is requested label Mar 28, 2025
@nvuillam
Copy link
Member

The code to send a PR comment is the following:

if config.get(self.master.request_id, "GITHUB_TOKEN", "") != "":

It lists the comments associated to the found PR and posts a new one or updates existing one... so your override of GITHUB_REPOSITORY should be ok

what is the value of your overridden GITHUB_REF ? it should be able to find PR id in the following regexp

            ref = os.environ.get("GITHUB_REF", "")
            m = re.compile("refs/pull/(\\d+)/merge").match(ref)

@emouawad
Copy link
Author

emouawad commented Mar 30, 2025

Thanks for pointing me to that code here is my env

Image

The error : "[GitHub Comment Reporter] No pull request has been found, so no comment has been posted" is pointing to this regex refs/pull/(\d+)/merge my PR number is 9 so refs/pull/9/merge should match but it isn't for some reason.

Are there unit tests for this? or is there another stable version i can try? using gh action oxsecurity/[email protected]

Also reviewing the code - even if the regex cannot find the pr it tries to get it from commit sha - that too is unable to get the PR : No warning or errors

@emouawad
Copy link
Author

UPDATE: Potential BUG

@nvuillam I have enabled DEBUG log level and now i understand more what's happening:

overriding the envs: GITHUB_REPOSITORY GITHUB_SHA GITHUB_REF is having no effect! This looks a BUG

megalinter is assuming the targeted repo is where the github workflow is running and loading thos envs from the initial webhook while in my case the targeted repo is another github repo not where the workflow is running - therefore it is essential for megalinter to load those envs from the provided overrides.

@emouawad emouawad changed the title Centralized Github actions Workflow BUG - env overrides not working Mar 30, 2025
@echoix
Copy link
Collaborator

echoix commented Mar 30, 2025

Where and how are you setting these overrides?

@emouawad
Copy link
Author

Using github actions

- name: Lint
  uses: oxsecurity/[email protected]
  continue-on-error: true
  env:
    GITHUB_TOKEN: ${{ steps.generate-app-token.outputs.token }}
    GITHUB_REPOSITORY: ${{ env.REPO_NAME }}
    GITHUB_SHA: ${{ env.REPO_REF }}
    GITHUB_REF: refs/pull/${{ env.REPO_PR_NUMBER }}/merge

@nvuillam
Copy link
Member

nvuillam commented Apr 6, 2025

Dear @emouawad

First, there is no "BUG" because it is written nowhere in the doc that MegaLinter supports PR comments on another repos that the one which is currently analyzed :)

Second, you are very welcome to submit a pull request that would make such "NEW FEATURE" work :)

You may succeed to make it work just by setting all the variables requested by the reporter ( the code is here -> https://github.com/oxsecurity/megalinter/blob/main/megalinter/reporters/GithubCommentReporter.py )

@emouawad
Copy link
Author

emouawad commented Apr 7, 2025

hey @nvuillam thanks for taking the time to look into this.

But how overriding the env variables not having any effect is not a bug.
It is documented that one can supply env vars and according to my testing (the screenshot i shared earlier) the env vars are not being loaded.

Now i totally understand that commenting on another repo may not be in the core scope of megalinter - but overriding the env vars that should be trivial.

btw all is working fine for me - i was able to generate the html reports no problem - my issue is only with the commenter not loading from the env vars.

anyway if u don't agree - feel free to close the ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants