Skip to content
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

Help with infracost comment on commit #1635

Closed
mpcabral opened this issue May 9, 2022 · 5 comments · Fixed by #1636
Closed

Help with infracost comment on commit #1635

mpcabral opened this issue May 9, 2022 · 5 comments · Fixed by #1636
Assignees
Labels
bug Something isn't working
Projects

Comments

@mpcabral
Copy link

mpcabral commented May 9, 2022

I am trying to do a CI/CD with infracost
For comment on PR all does normaly

But when i try to comment on a commit i receive a error:
Error: Type mismatch on variable $commitSha and argument oid (String! / GitObjectID)

Command:
infracost comment github --policy-path infracost-policy.rego --path infracost.json --repo mpcabral/infracost-test --commit 27351c2c0bd2aefe3e3a52bbf99f8b8be73e382e --github-token ****

Am I doing something wrong?

@vdmgolub
Copy link
Contributor

vdmgolub commented May 9, 2022

@mpcabral Hi! Thank you for creating the issue! I was able to reproduce this and seems like to be a bug in CLI. The error message is from GitHub's GraphQL API saying that it expects commit as a GitObjectID value, but we send it as a string instead when it fetches existing comments.

We tested the functionality thoroughly during implementation and it worked fine. I tried to find any changes in API's changelog, but I can't see anything related to that. The fix looks pretty easy to me though. But it will be available only with the next release.

This happens only with the default --behavior update when CLI tries to find its existing comment and update it. If you add to your command --behavior new it should post a comment successfully. However, this option might be a bit noisy as it will create a new comment on every run. Here more info on behaviors: https://www.infracost.io/docs/features/cli_commands/#comment-on-pull-requests.

Please let me know if this helps. :)

@vdmgolub vdmgolub added the bug Something isn't working label May 9, 2022
@vdmgolub vdmgolub self-assigned this May 9, 2022
@alikhajeh1 alikhajeh1 added this to Todo (prioritized top to bottom) in Main via automation May 9, 2022
@mpcabral
Copy link
Author

Yes this helped me a lot and I managed to complete my pipeline
Just out of curiosity: is there any option to detect a cost policy failure and stop my pipeline without needing to enter the comment in the commit?

@aliscott
Copy link
Member

Just out of curiosity: is there any option to detect a cost policy failure and stop my pipeline without needing to enter the comment in the commit?

@mpcabral we don't have that option in the infracost comment command yet, but you could do it by running OPA separately. Something like this could work:

# Generate Infracost breakdown
infracost breakdown --plan.json --format=json --out-file=infracost.json

# Run the policy
opa eval --input infracost.json -d infracost-policy.rego --format pretty "data.infracost.deny" | tee opa.out

# If the policy fails, log the details and exit
denyReasons=$(<opa.out)
if [ "$denyReasons" != "[]" ]; then
  echo -e "Policy check failed:\n$denyReasons"
  exit 1
# If the policy passes, continue and post the comment
else
  echo "Policy check passed."
  infracost comment github --policy-path infracost-policy.rego --path infracost.json --repo $GITHUB_REPO --commit $GITHUB_COMMIT --github-token $GITHUB_TOKEN
fi

Main automation moved this from Todo (prioritized top to bottom) to Merged to master May 11, 2022
@vdmgolub
Copy link
Contributor

@mpcabral We just merged the fix to master. It will go live with the next release. It automatically closed this issue too. Please feel free to reopen if you think there is anything unresolved. Thanks again for reporting it! :)

BTW, have you had a chance to look at Alistair's comment about the policy? If yes, has it worked for you?

@hugorut
Copy link
Contributor

hugorut commented May 24, 2022

Hey @mpcabral, this has now been released as part of the v0.10 release. Thanks for bringing this to our attention.

@hugorut hugorut moved this from Merged to master to Released in Main May 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Main
Released
Development

Successfully merging a pull request may close this issue.

4 participants