Skip to content

Fix handling of temporary private forks - #548

Open
Jackenmen wants to merge 4 commits into
josegonzalez:masterfrom
Jackenmen:handle_temporary_private_forks
Open

Jackenmen wants to merge 4 commits into
josegonzalez:masterfrom
Jackenmen:handle_temporary_private_forks

Conversation

@Jackenmen

Copy link
Copy Markdown

Fixes #547

The biggest change involves fetching PR comments - per https://github.com/orgs/community/discussions/213137 /repos/{owner}/{repo}/issues/{issue_number}/comments does not work on temporary private forks. Instead, I switched comment retrieval for pull requests to use a GraphQL query. The same data should be present in both old REST API and new GraphQL API implementation but the way it's structured into fields is not the same for some information. I used the implementation for discussion comments as a starting point so let me know, if there's any other field from IssueComment that you think is worth including.

@Iamrodos

Copy link
Copy Markdown
Contributor

Thanks, good catch on temporary private forks. Supporting them shouldn't change how normal repositories are backed up unless there's a strong reason to, so a few things could do with adjusting.

The main one is the PR comment format. Switching every repository to GraphQL changes the saved structure of comment_regular_data, for example user/html_url/created_at become author/url/createdAt. Existing backups, and any tools reading them, rely on the current structure, and incremental runs would end up with both formats mixed together. GraphQL also needs a token, so PR comment backup would stop working for anyone running without one. Could you keep the REST call as it is and only fall back to the GraphQL query when it returns 404? That way only temporary forks get the new format and everything else stays the same.

On tests: 10 existing tests fail with KeyError: 'has_issues'. repository.get("has_issues", True) fixes that, and also covers GitHub Enterprise Server versions that may not return these fields. A test for the fork case would be great too, i.e. a 404 on the comments endpoint falling back to GraphQL.

The milestones/labels/releases 404 handling looks good.

@Jackenmen

Copy link
Copy Markdown
Author

Thanks, good catch on temporary private forks. Supporting them shouldn't change how normal repositories are backed up unless there's a strong reason to, so a few things could do with adjusting.

The reason is not making PR comment data structure be dependent on whether it was made in a temporary private fork or not (or more specifically, whether the normal issue comments endpoint 404s or not).

The main one is the PR comment format. Switching every repository to GraphQL changes the saved structure of comment_regular_data, for example user/html_url/created_at become author/url/createdAt

If you think it's important to keep this structure, it would be possible to map the fields out to their REST API equivalents. I'm not sure whether that's an acceptable approach for you.

Existing backups, and any tools reading them, rely on the current structure, and incremental runs would end up with both formats mixed together.

Hmm, I guess incremental runs are going to be problematic, if the structure is now changed... If the above suggestion of mapping things out to old fields is acceptable then that would make things easier.

GraphQL also needs a token, so PR comment backup would stop working for anyone running without one.

Hmm, I was under the impression that the token is required for the tool to run anyway. Is that not the case?

Could you keep the REST call as it is and only fall back to the GraphQL query when it returns 404? That way only temporary forks get the new format and everything else stays the same.

I could but then the tools reading the backups could encounter both formats, making things less consistent (unless the above suggestion of mapping out fields is acceptable). I'm not sure that's such a good idea.

On tests: 10 existing tests fail with KeyError: 'has_issues'. repository.get("has_issues", True) fixes that, and also covers GitHub Enterprise Server versions that may not return these fields.

I simply forgot to update the test data to include the has_* fields that are in use now. has_issues and has_discussions are always present, including on GHES (has_discussions is available starting with GHES 3.3 which has been EOL since January 2023 - I doubt there's any need to support versions older than that). Documentation doesn't list has_pull_requests as a field at all, even for github.com so I swapped lookup for that with a get call just to be extra safe.

A test for the fork case would be great too

I can add that once we're clear on the other conversation points. Btw, you don't seem to be a maintainer or collaborator based on your badge - am I right in assuming that your feedback isn't necessarily going to be consistent with what the repo owner will want me to do in order to accept the contribution?

@Jackenmen

Copy link
Copy Markdown
Author

incremental runs would end up with both formats mixed together.

One more thing worth noting is that the formats wouldn't be mixed within a single PR as comments are fetched from scratch every time a pull request is updated ("is due for repository checkpoint"). Between different PRs, however, the structures of comment_regular_data would differ. It's still not ideal to have things mixed at all but it is not as severe as mixing them within a single array/list would be.

@josegonzalez

Copy link
Copy Markdown
Owner

@Iamrodos any other comments before I run tests?

@Iamrodos

Copy link
Copy Markdown
Contributor

@Jackenmen thanks for the updates, and for the patience. You're right that I'm not a maintainer, just another contributor who helps out with reviews. So take this as a steer, whether it merges is Jose's call.

You're also right on the token, sorry for the noise on that one.

My main concern is people whose backups work today. Nothing has changed for them, yet after this their PR comments would be saved in a different format. I take your point that falling back on a 404 means two formats, but that only touches temporary forks, which have no backup of these comments at all today. So I'd try REST first and use your GraphQL query only on a 404. Not as clean as one format, I know. The joys of a tool that has followed GitHub's API around for years.

Jose is about to run CI, so it's worth running the tests and flake8 locally first, the tests pass for me but flake8 flags an unused _issue_template in backup_pulls. The tests for the new cases you offered would be welcome once the approach is settled.

This is a real gap you've found and the rest of the change looks good to me, so it feels close. Thanks for sticking with it.

@josegonzalez that's all from me, feel free to weigh in if you'd steer this differently.

@josegonzalez

Copy link
Copy Markdown
Owner

I've been treating @Iamrodos as the maintainer of the project:

  • I maintain a couple hundred different projects and don't have time to effectively manage this repo
  • @Iamrodos has been good at adding tests, providing reviews, and promptly responding to folks
  • we're migrating to that pattern soon (@ci-backups org, though thats a bad name I guess for the org lol)
  • this tool did what I needed years ago, any improvements now need someone who wants to maintain, and @Iamrodos has been that

Anyways, I'll run tests, but basically whatever @Iamrodos says is cannon to me :)

@josegonzalez

Copy link
Copy Markdown
Owner

In @Iamrodos we trust (just I don't want him to merge a PR that leaks my user-wide actions secrets 😂).

@Jackenmen

Copy link
Copy Markdown
Author

Alright, I'll address his review within the next few days.


I take your point that falling back on a 404 means two formats, but that only touches temporary forks, which have no backup of these comments at all today. So I'd try REST first and use your GraphQL query only on a 404. Not as clean as one format, I know. The joys of a tool that has followed GitHub's API around for years.

@Iamrodos what do you think about my suggestion of mapping out GraphQL fields to REST API ones to avoid temporary private forks using a different format? Worth doing for better consistency or not?

@Iamrodos

Copy link
Copy Markdown
Contributor

Thanks.

I'd leave them unmapped. As a backup tool it's better to stay as close as we can to what GitHub provides than to translate it. A mapping is also one more thing to maintain as either API changes, and it can turn into a rabbit hole, the user info isn't the same shape and neither are the reactions. I'd rather leave that to whoever reads the backup than do it ahead of time for them.

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.

Crash when trying to backup a temporary private fork

3 participants