Can I query the Github API for my repository quota? - github

I'm working on an app that creates private Github repositories (among other things). Every once in a while, we are over quota with our private repos.
I'd like to know how many repositories we have left before making the API call that will fail.
Can that be done, using the Github API? I couldn't find anything in the documentation, but that's doesn't mean it's not possible :)

awendt, I'm not quite familiar with private quotas, but authenticated users receive a Plan object when the API returns the call to /user. This will tell you what plan you have and will tell you how many private_repos you're allowed. With that information and the information from /user which tells you how many private repos you currently have, you should be able to figure it out.
Then again, you could use github3.py and you'd have the User object, with the plan attribute and could use those two together as described above.
Disclaimer I'm github3.py's author.

Related

Github.com read-only API for public content

I am looking for how I can have my website make XHR API calls to Github for my project to display info like # of issues, # open PRs, top contributors, links to latest releases ... etc. It's all public info that you can see going to the public Github website, but i want to be able to pull it and surface/highlight pieces on my project's Web site. Do i just need to make a read-only API token and use that in my code (everyone can see it, but i guess i dont really care if other people see/use it?) .. or if a simpler, no-auth, API Github provides for this sort of info?
I feel like i'm missing something obvious - but my Googling always leads me to more complex use cases.

How to check if a user can create a repository in a GitHub organization via the REST API

I am writing a Github App that is able to create repositories in an installation.
When the App acts on behalf of an authenticated user, I would like to check that the user can (by themselves) create a repository in the org.
I have spent a lot of time on GitHub's API docs, but I cannot find the answer.
My first thought was that this info should be available in the endpoint /user/installations. The endpoint lists the installations that the user can access (either as a member of an organization or as an external collaborator). However, the permissions returned with each entry are actually the permissions for the App, not for the user. So, this is a dead end.
Another direction was looking at the (public+private) organizations of the user using /user/orgs.
(This does not seem the right direction because as an App I would expect to operate only on installations...)
With this endpoint, I can get all the organizations of the user. However, I don't get whether they can create repos and/or what the role of the user is in the organization.
Should I use the teams/roles part of the API?
My App doesn't ask for the members suite of permissions so I would like to avoid this road.
At this point, the only workarounds are:
Try to create the repo as the user, take note if it fails. Bad solution because I don't want to tell the user that they can create a repo if they can't.
In the background, try to create a repo as the user to check if it possible. If it is, delete the repo. This works but it seems an ugly workaround.
Any suggestion?

Github metions in private repo

If I mention an external user in a private repo, does that grant them access to the conversation/branch/repo? And if so, does removing the conversation will withdraw the access?
No, a user's access is not modified by whether you mention them. For example, if at work I mention a former colleague (e.g., “That was written by #octocat, who's no longer here.”), that user does not get notified at all and doesn't get any access to that repository.
Doing so would be tricky because it would allow people to gain access even when they definitely should not have access (former employees, project members who have been asked to leave for inappropriate behavior, etc.).

Required scope for events API on private repos

I'd like to use the GitHub API to read comments, issues and so on for a given user with the GitHub Events API.
I've already accomplished that with OAuth authentication using the repo scope. That's a big overkill since I just want to read events and this scope grants almost everything. The API doc doesn't specify anything, and I can't find a more limited scope.
I've been testing with different personal access token scopes but only repo makes requests return private events.
You will need to use the scope repo if you want to retrieve events originating from private repositories.
At this point this is the only scope for the job.

Get github username by id

Using this link I can see that my (tonylampada) id on github is 218821
https://api.github.com/users/tonylampada
How could I do the opposite? Given the user id = 218821, what's the username?
Update
Answering nulltoken here because it's a long story and it won't fit in a comment.
FreedomSponsors is a django application that uses django-social-auth to enable login with Github (and others).
(You should check it out, btw, please see the about page in the blog :-)
Django-social-auth has a configuration flag that allows the application to store the github username on the database.
A few days ago I deployed a new version of FS with github login enabled, but with "storeGithubUsername" set to false.
A few users registered their github accounts, and now the database has their github ids, but not their usernames.
You can se in my profile that I have github as a "connected account" but there's no link to my github page.
I need it to make the link point to https://github.com/tonylampada
I'm ready to deploy a new version that fixes this, by setting the "storeGithubUsername" (that's not what it is called, I'm just simplifying here) to true.
But I'd like to patch the database with the already github-registered users. I have their github ids, but not their github usernames.
We need to do this on Gitter to deal with the situation where a user has changed their username on GitHub and we get a 404 response when querying their old username.
Here's an undocumented endpoint, so use as your own peril, but it does work for now.
Use the endpoint: https://api.github.com/user/:id, where :id is the ID of the user.
Similar endpoints exist for repos and orgs, at
https://api.github.com/repositories/:id and https://api.github.com/organizations/:id respectively.
Note that the new repository redirects preview API only supports repositories, not renamed users or organizations. In fact, the HTTP 301 redirect actually points to https://api.github.com/repositories/:id, so there's a good chance that these "ID" endpoints may in fact become official soon.
There's no documented feature, nor undocumented ones that I know of, that expose the retrieval of the username from the id. From the GitHub API consumer perspective, the user id is an "implementation detail". The real key is the username.
From what I understand, you only require a batch of usernames given a list of ids. And this would be a one time only request, not a permanent need.
As your request seems legit and limited in its scope, you might get this answer directly from GitHub support by dropping them an email at support#github.com.
Indeed, xpaulbettsx, a GitHubber, even tweeted about this:
Support# is good for Anything you want to tell GitHub - bugs, features, high 5s; everything but security which go to security#
By the time I answer this question, the method that works is:
https://api.github.com/user/USER_ID
Remark: It is similar to what Andrew shared in 2015; you just have to remove the colon in the URL he shared.