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

Properly extract commit sha from PullRequests #186

Closed
wants to merge 7 commits into from
Closed

Conversation

nulltoken
Copy link
Contributor

@nulltoken nulltoken commented May 1, 2020

Fix an issue discovered by @philsturgeon in https://github.com/protect-earth/protect.earth/pull/61/checks?check_run_id=634958740

image

Also:

  • slightly enhance the logging
  • run the analysis before attempting to create a checkrun (this will fail when PR is created from a fork as the token isn't granted with enough rights to create annotations), in order to provide minimal feedback.
  • re-use Spectral stylish formater to produce a cleaner textual report
  • do not fail the action upon insufficient rights (would happen when a PR originates from a fork) => should only fail the build upon fatal linting errors and unexpected errors

@nulltoken nulltoken requested a review from philsturgeon May 1, 2020 12:16
@nulltoken nulltoken self-assigned this May 1, 2020
@nulltoken nulltoken requested a review from XVincentX May 1, 2020 12:17
@nulltoken
Copy link
Contributor Author

@philsturgeon @XVincentX
For PR originating from forks, one enhancement would be to re-use the stylish cli formatter from spectral in order to provide actionable feedback (even in textual form).
Another idea would be to check if using problem matchers (#184) might actually solve the permission issue as well.

@nulltoken
Copy link
Contributor Author

One can check the proposed behavior of that branch in protect-earth/protect.earth#62

nulltoken added 5 commits May 1, 2020 16:03
This gives a chance for feedback when pull requests are opened from
a fork.
In that case, the token is not granted with enough rights to create
annotations in the upstream repository.
@nulltoken
Copy link
Contributor Author

@philsturgeon @XVincentX
For PR originating from forks, one enhancement would be to re-use the stylish cli formatter from spectral in order to provide actionable feedback (even in textual form).

image

@@ -16,18 +23,33 @@ type Event = {

export const createOctokitInstance = (token: string) => TE.fromEither(E.tryCatch(() => new GitHub(token), E.toError));

const tryCreateGithubCheck = async (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@XVincentX Could you please help me rewrite this in a more fp-ts way? I haven't found the proper way to do this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You really can't, since the underlying API has side-effects. You can wrap it (as you're doing) with tryCatch later

Copy link
Contributor

@XVincentX XVincentX left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look and unfortunately I think it's going in the wrong direction regards the usage of fp-ts. Also, bundling so many changes together didn't really help the review.

I would suggest to close this, open a new PR fixing exclusively the bug. In meantime I can try to look at this during the weekend and cleanup some of the stuff.

To give you some context, this was born as a personal pet project to experiment functional concepts that "escalated" to be then the official action linter.

})
)
);
const spectralRuns = parsed.map(v => pipe(runSpectral(spectral, v)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're running only a single operation, you do not need to pipe

Suggested change
const spectralRuns = parsed.map(v => pipe(runSpectral(spectral, v)));
const spectralRuns = parsed.map(v => runSpectral(spectral, v));

Comment on lines +117 to +128
pipe(
getRepositoryInfoFromEvent(GITHUB_EVENT_PATH, INPUT_EVENT_NAME),
TE.map(event => ({ event }))
),
TE.chain(({ event }) =>
pipe(
readFilesToAnalyze(INPUT_FILE_GLOB, GITHUB_WORKSPACE),
TE.chain(fileContents => createSpectralAnnotations(INPUT_SPECTRAL_RULESET, fileContents, GITHUB_WORKSPACE)),
TE.map(annotations => ({ event, annotations }))
)
),
TE.chain(({ event, annotations }) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I'd say this is going in the wrong direction. I'll probably have to check this out.

@@ -16,18 +23,33 @@ type Event = {

export const createOctokitInstance = (token: string) => TE.fromEither(E.tryCatch(() => new GitHub(token), E.toError));

const tryCreateGithubCheck = async (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You really can't, since the underlying API has side-effects. You can wrap it (as you're doing) with tryCatch later

return event.after;
default:
warning(event);
throw new Error(`Unsupported event '${eventName}'`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the idea of fp-ts is not to throw exception, so this is not ok. This should return an Either and a Left instance when something goes wrong.

@nulltoken
Copy link
Contributor Author

I would suggest to close this, open a new PR fixing exclusively the bug.

Done with #187

In meantime I can try to look at this during the weekend and cleanup some of the stuff.

Ok. I'll learn by reading your code, then.

From a usage standpoint, the two most important features that this PR tried to add were:

  • Do not fail the action when the Github token doesn't bear enough rights to create annotations (which happens when the PR to be linted originates from a fork)
  • Provide the user with some textual report (Also helpful when a PR comes from a fork)

Bonus features (for potential later troubleshooting):

  • Log the event to which this action responds to
  • Log the commit sha being processed

@nulltoken nulltoken closed this May 2, 2020
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 this pull request may close these issues.

2 participants