Check if Git repository is private or public (e.g. for GitHub) using Java

11,652

Specifically GitHub provides REST API to list user repositories. As per the Developer documentation you could use a GET to get all repos of a user and "Visibility" is a parameter

GET /user/repos

Parameter: visibility Type: string Value: all, public, private

The response also has a parameter "private": false that can be easily parsed from the response.

More here:https://developer.github.com/v3/repos/#list-user-repositories

Share:
11,652

Related videos on Youtube

LexSav
Author by

LexSav

Updated on June 04, 2022

Comments

  • LexSav
    LexSav almost 2 years

    Before we begin: I'm a newbie, so my apologies

    Quick description:

    I have a Jira plugin that can track current state of the specified repository in the selected git provider, e.g. GitHub (tracking commits, branches etc.) and display it in JIRA issue view

    I need to check if the repository I'm adding to the plugin is private or public. If private, I need to request credentials for it.

    Is there a way to check this condition in Java (e.g. via JGit) ?

    P.S. I'm adding repo to my plugin through it's HTTPS link

    • fredrik
      fredrik about 5 years
      This would vary depending on if it's GitHub, GitLab, BitBucket or some other hosting service. Git in itself does not have a concept of "public" or "private". Though I'd guess that if you try and access it and get a 403 response - it's private.
    • LexSav
      LexSav about 5 years
      @fredrik Therefore I typed "e.g. GitHub"
    • LexSav
      LexSav about 5 years
      @fredrik I found some GitHub API. Maybe there is an answer :) I assume I should look for such APIs for exactly the provider I need ... Seems there is no general solution
    • fredrik
      fredrik about 5 years
      On the contrary, each solution is unique. What I suppose you actually mean is that there is no generic solution - which I stated already.
    • LexSav
      LexSav about 5 years
      @fredrik maybe. My wonderful English :)
  • Major
    Major about 5 years
    So does this mean that someone besides me or someone at Github can get information about my private repos?
  • Bhargav Kinnal
    Bhargav Kinnal about 5 years
    There are two different APIs. One to list your own repos, which allows to list even private repos. One to list anyone's repos which does not list private repos (That would be ironic and opposite to what private means!). I believe @LexSav is listing his own repositories.