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

Moctokit is not working with Act runEvent #72

Closed
david-all-win-software opened this issue May 17, 2023 · 8 comments · Fixed by kiegroup/act-js#50
Closed

Moctokit is not working with Act runEvent #72

david-all-win-software opened this issue May 17, 2023 · 8 comments · Fixed by kiegroup/act-js#50

Comments

@david-all-win-software
Copy link

Describe the bug
There is an issue when trying to set up a GitHub API mock using Moctokit to generate the mock and send it when running an event with act-js.

To Reproduce

  1. Set up a YAML file with a GitHub script that queries the GitHub API, for example, to retrieve a repository.
  2. Create a test using act-js and Moctokit with a mock to run the workflow, modifying the original response.
  3. Run tests e.g. npm test.

Screen Shot 2023-05-17 at 15 20 45

Expected behavior
The response from running the workflow should contain the mocked information.

@shubhbapna
Copy link
Collaborator

From the act-js documentation (I will add steps on how you can use it with octokit here soon)

You can use Mockapi and Moctokit to mock any kind of HTTP and HTTPS requests during your workflow run provided that the client being used honours HTTP_PROXY and HTTPS_PROXY env variables. Depending on the client, for HTTPS they might issue a CONNECT request to open a secure TCP tunnel. In this case Act won't be able to mock the HTTPS request.

If the client you are using does not honor these env variables, then the only way to actually make this work would be to make the client use a proxy forcefully. For octokit you can do something like this:
https://github.com/kiegroup/github-action-build-chain/blob/main/src/service/git/octokit.ts#L39

The only way I could think of bypassing this problem of CONNECT request was to setup a MITM server and somehow configure all the containers created by act to use that server as a proxy for all outgoing and incoming requests. This however is out of scope for this library, at least for now. Maybe if there is enough interest we can potentially start discussing ideas to implement it.

@david-all-win-software
Copy link
Author

david-all-win-software commented May 19, 2023

@shubhbapna
Thank you for the clarifications. I thought it would be simpler without requiring extra modifications. A container-level proxy server seems like a good solution to explore.

@shubhbapna
Copy link
Collaborator

@david-all-win-software Actually you might be able to use it without extra modification in example you mentioned.

@actions/github-script uses getOctokit from the @actions/github library - https://github.com/actions/github-script/blob/main/src/main.ts#L49

In the @actions/github library it seems that have a proxy configured which points to https://api.github.com :

  1. https://github.com/actions/toolkit/blob/main/packages/github/src/utils.ts#L16
  2. https://github.com/octokit/octokit.js#proxy-servers-nodejs-only

It looks like we can control the base url of the proxy using the GITHUB_API_URL env variable:
https://github.com/actions/toolkit/blob/main/packages/github/src/internal/utils.ts#L23

So in your test file all you need to do is initialize moctokit to use http instead of https and set the env before executing act:

const moctokit = new Moctokit("http://api.github.com");
const result = await act.setEnv("GITHUB_API_URL", "http://api.github.com").runEvent("workflow_dispatch", {mockApi: [...]})

Hopefully this works. I haven't gotten the chance to try it out but let me know if it works. I would like to document it

@david-all-win-software
Copy link
Author

david-all-win-software commented May 19, 2023

@shubhbapna Thanks, very good suggestion, I just tried it, but it's generating a 500 error status. It seems that it's not respecting the HTTP protocol we are setting.

::error::Unhandled error: HttpError: maximum redirect reached at: https://api.github.com:80/repos/kiegroup/mock-github

Screen Shot 2023-05-19 at 14 17 46

@BorePlusPlus
Copy link

I believe you're not initialising the moctokit properly. You are not passing the http protocol as suggested by @shubhbapna

const moctokit = new Moctokit("http://api.github.com");

I had a similar issue and this helped solve it.

@shubhbapna
Copy link
Collaborator

Hmm I finally was able to try this out using the http protocol and I am still getting the same error. It seems like the proxy setting in github-script behaves a bit weirdly. It seems like for some reason when using an http protocol, actions/github-script just adds port 80 to the host but doesn't change the protocol. I get the following error:

HttpError: maximum redirect reached at: https://api.github.com:80/repos/kiegroup/mock-github

I will investigate and see if I can fix it in act-js

@shubhbapna
Copy link
Collaborator

Just to update y'all, I think the issue over here is the proxy configuration in actions/github which results in establishing a TLS connection even when the proxy and the target are using http protocol.
I have reported the bug, lets see if the maintainers agree

@shubhbapna
Copy link
Collaborator

Hi @david-all-win-software sorry for the delay, after talking to various people it seems like the proxy configuration in @actions/github is valid but unnecessary so I am not entirely sure they will accept my PR.

In the mean time I was able to figure out how to get this to work in my library itself. See the following PR for detailed explanation - kiegroup/act-js#50

I have released the new version of @kie/act-js which contains this feature and you should be able to test your workflow easily now (hopefully). I have also added an example of the same here, feel free to refer to it:
https://github.com/shubhbapna/mock-github-act-js-examples/blob/main/workflow/github-script/README.md

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

Successfully merging a pull request may close this issue.

3 participants