CATcher-org / CATcher

CATcher is a software application used for peer-testing of software projects.
https://catcher-org.github.io/CATcher/
MIT License
70 stars 68 forks source link

Default branch to `main` #1234

Closed nknguyenhc closed 7 months ago

nknguyenhc commented 9 months ago

Summary:

Fixes CATcher-org/CATcher#1226

Changes Made:

When uploading image, indicate the branch to upload to be main. Since there are no other github API used that makes commits, this means that default branch will be set to main for the first image upload.

We do optimistic processing of uploads, in the case that the repository is not empty. When the upload fails, we create main branch from the latest commit in the repository, and re-attempt the upload.

Proposed Commit Message:

Set default branch to `main`

Previously, image uploads depend on the user's default branch.
Now, we set the branch for image upload to be `main`. Images will 
be uploaded to `main` as a result.
codecov-commenter commented 8 months ago

Codecov Report

Attention: 32 lines in your changes are missing coverage. Please review.

Comparison is base (d4b5422) 53.62% compared to head (0cd1148) 53.41%.

Files Patch % Lines
src/app/core/services/github.service.ts 13.04% 20 Missing :warning:
src/app/core/services/upload.service.ts 0.00% 9 Missing and 3 partials :warning:

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #1234 +/- ## ========================================== - Coverage 53.62% 53.41% -0.21% ========================================== Files 103 103 Lines 2911 2928 +17 Branches 542 544 +2 ========================================== + Hits 1561 1564 +3 - Misses 999 1014 +15 + Partials 351 350 -1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

nknguyenhc commented 8 months ago

Thanks @cheehongw for spotting the bug! Can I check the state of the repo when the error occurred, in particular,

  1. Is the repo empty or does it already have some images?
  2. If it already has some images, what is the default branch of the repo?

I can only reproduce the error when the repo already has some images, and the default branch of the repo is not main. When the repo is empty, even if your default branch in your Github settings is not main, the file uploaded will be in main branch.

I have considered the following solutions:

  1. Create the main branch upon repo creation.

Note that we cannot create a new branch in an empty repository, because branches are only pointers to commits, we would have to create a commit in order to create the branch.

Octokit allows us to create commit, but from the documentation, it does not look like we can indicate the branch upon creating the commit.

Octokit has the option auto_init upon repo creation, however, it looks like we cannot indicate the branch for the initial commit.

Another approach is to upload a dummy file using the same update file API (indicating the branch name to create the branch).

  1. Check for branch existence and create branch before uploading the image.

I believe this solution is not ideal as it adds more API calls, which can cause Github to block our access during the actual PE. Furthermore, this does not address the concern above, as we cannot create a branch out of an empty repository, for the first image upload.


According to the PE instructions, students should only be doing their PE through CATcher interface, so I believe it should be save to assume that repo starts out as an empty repo, and hence, it should be possible to indicate the branch name for updating files? Otherwise, if we were to be safe, should I proceed with creating a dummy upon repo creation?

cheehongw commented 8 months ago

Thanks for the investigation.

  1. Is the repo empty or does it already have some images?
  2. If it already has some images, what is the default branch of the repo?

When I tested it, the default branch was master with some commits. The main branch does not exist.


  1. Specifying branch for empty repos

So for the initial commit, is it possible to specify the initial branch name to be whatever we want?


  1. Check for branch existence and create branch before uploading the image.

I believe this solution is not ideal as it adds more API calls.

I agree. Perhaps we can use an optimistic processing approach instead? In other words, upload the image and then take corrective action if the upload fails due to missing branch


I think it is fine to assume that users start out with an empty repository. https://nus-cs2103-ay2324s2.github.io/website/schedule/week10/project.html#5-smoke-test-catcher-compulsory-counted-for-participation

nknguyenhc commented 8 months ago

So for the initial commit, is it possible to specify the initial branch name to be whatever we want?

Yes, we can use upload file API call to ensure that the default branch is main. However, it only works if the repo is empty; if the repo already has some commits, we have to branch out from the latest commit. This means when a user logs in, we have to handle 3 cases separately:

I believe that for PE, if students follow instructions and do the PE using CATcher, the only way that it falls under 2nd or 3rd case is when they have previously created a repo with the exact same name (i.e. alpha, ped or pe).

Perhaps we can use an optimistic processing approach instead? In other words, upload the image and then take corrective action if the upload fails due to missing branch

I would prefer this solution. I am thinking about if the upload fails, branch out from the latest commit in the repo, naming the new branch main, and retry the upload.

Would love to hear what you think about my suggested solution!

cheehongw commented 8 months ago
  • Create an initial commit by uploading a dummy file to branch main for empty repo.

I tried creating a commit to a empty repo using the createOrUpdateFile API, and specifying the branch name as x. In the empty repo, the branch x is made, so it seems like there is no need to upload a dummy file for an empty repo with the branch we want. I think your PR as it is now should achieve this?

  • Branch out from latest commit for non-empty repo that does not have branch main.

We should do this when we optimistically upload the image file to main.

-Do nothing for non-empty repo that has branch main.

Right, because the branch we are uploading to exists, so there's nothing to do.

nknguyenhc commented 8 months ago

I have updated my PR! So in summary, the changes are only when user uploads a file: