Github User Pages and Custom Domain Issue - github

I have a couple of issues I believe are related regarding githubpages.
First, when trying to create my user page by naming the repo the same as my username, I expect the url to be this myusername.github.io.Instead it looks like this myusername.github.io/myusername.
I have been to work around this by naming my repo myusername.github.io, which produces the correct URL.
Now, I want to create a custom domain with this user page. I can use my custom domain with the above workaround, but this now affects all other repos which use github pages. For example, I setup myusername.github.io to use mycustomdomain.com. But now I have another project that uses gh pages called someotherproject. The url now redirects to mycustomdomain.com/someotherproject instead of someotherproject.github.io.
Is this working correctly?

But now I have another project that uses gh pages called someotherproject. The url now redirects to mycustomdomain.com/someotherproject instead of someotherproject.github.io.
This is what described in the official documentation "Supported custom domains"
After you configure a custom domain for a user or organization site, the custom domain will replace the <user>.github.io or <organization>.github.io portion of the URL for any project sites owned by the account that do not have a custom domain configured.
For example, if the custom domain for your user site is www.octocat.com, and you have a project site with no custom domain configured that is published from a repository called octo-project, the GitHub Pages site for that repository will be available at www.octocat.com/octo-project.
That was reported as an issue in 2016:
I'm afraid this can't be disabled. If you have a CNAME file in a User pages site you other project pages will redirect to this Url.
You can override it by adding a different CNAME to your project page though or just create another Project site to act as your user page.
In 2020, here is a possible workaround, mentioned by Oliver Vorasai:
The Problem
Have portfolio repository at https://github.com/username/username.github.io
https://github.com/username/username.github.io uses custom domain at www.username.com
Created another project at https://github.com/username/my-new-project
Create gh-pages branch so Github Pages will host it
my-new-project should be accessible at https://username.github.io/my-new-project/
https://username.github.io/my-new-project/ it is automatically redirected to www.username.com/my-new-project and causes 404.
The Solution
Rename the https://github.com/username/username.github.io repository to https://github.com/username/username-something
https://github.com/username/my-new-project should now be available at https://username.github.io/my-new-project/
www.username.com is still working, subsequent projects should get standard Github Pages urls without custom domains.
Not ideal though, as mentioned by Emma Sax :
I still think the best solution would be an option in the GH Pages settings of a project page to not redirect the domain of a project page.
This way, users would have a choice of having username.github.io/project-page redirect to www.customdomain.com/project-page or having it stay where it is.
It's frustrating to me that we even need to find an alternative solution at all.

Related

How to automatically fill in proposed code changes in the "Edit" URL for a GitHub file?

EDIT: See comments on Schwern's answer for what I'm looking for in general. It doesn't have to be exactly what I'm asking for in the question
I have a web app which is an editor. I would like users to be able to give me a GitHub file URL, my app would automatically load in the file from GitHub, and then I make a process for them which is as easy as possible to submit that change to GitHub again. Ideally the user wouldn't need to save / upload a file or do any copy/pasting.
GitHub has a URL scheme where you can go to an "Edit" page for a file, make your changes, and then create a PR or create a commit (depending on what you would like to do and your permissions). This is an example:
https://github.com/rails/rails/edit/main/README.md
Looking at the HTML for the form I see that some of the fields have names associated. Using those names I can auto-fill the commit title and description:
https://github.com/rails/rails/edit/main/README.md?message=foo&description=bar
But I can't find a way to automatically fill in/replace the actual contents of the file. Is there a way?
I realize that for some browsers URLs can only be so long (maybe that's not true anymore?), so maybe this isn't perfect. I'd also be open to other suggestions on how to accomplish what I'm looking for.
Don't try to do this via web scraping, it's fragile and slow. Use the Github API.
Specifically, you'd get access via OAuth, get the file, let the user edit it, and then send the edited version.
There is no way to do exactly what you want. The ideal tool for this job is an OAuth App. However, creating one with the GitHub API requires that you store a client ID and a client secret, and there is no secure way to store the client secret in a frontend-only app.
Therefore, you'll need to create a backend to create the OAuth app so that you can issue credentials necessary to use the API on behalf of the user or to push data into the repository via the standard protocols.
As Schwern mentioned, you should not try to do this by driving the GitHub web interface. That isn't a stable interface and may break at any time.

Github permissions

We've set up a Github app so that it automatically forks a repository for another individual Github user using the Github API. Now we're running into a problem that not everyone wants to give us full read access to all of their private repositories since sometimes they contain sensitive data.
Is there a way to only get read/write permission to a single repository and not the individual's entire account?
Unfortunately, this is not available yet. This feature is still under development as you can see in the Gihub Apps roadmap (and note that I am talking about Github Apps, not OAuth Github Apps). I don't know if this will ever be possible in OAuth Apps but it seems that it might in Github Apps.
There is already a discussion about this at the dear github repository. You should check for news there.
Is there a way to only get read/write permission to a single repository and not the individual's entire account?
Not that I know of: it is easier to setup a new dedicated GitHub account where you would recreate the private repos you want to give access to.
In that new account, you can consider all the private repos can be accessed.
You would keep the really private ones (with sensitive information) in your original GitHub account.
I believe you're looking for X-OAuth-Scopes. This is a well-defined header so that you may restrict your access scope to, for example, public repositories only.
The github developer documentation here says:
... space-delimited list of scopes. If not provided, scope defaults to an empty list for users that have not authorized any scopes for the application. For users who have authorized scopes for the application, the user won't be shown the OAuth authorization page with the list of scopes. Instead, this step of the flow will automatically complete with the set of scopes the user has authorized for the application. For example, if a user has already performed the web flow twice and has authorized one token with user scope and another token with repo scope, a third web flow that does not provide a scope will receive a token with user and repo scope.

Use GitHub page as domain

I'm using the "github page" to create my personal page, but I'm going to need a hosting service because it will require some queries in the database. How can I use my GitHub Page url as a domain?
GitHub pages is not really designed for this kind of function. It's there to be a static page, where all content on the page is 'hardcoded' (meaning no dynamically generated data). What you're asking falls along the lines of a web application.
But if you're looking to be a maverick, there might be some options out there for you.
I personally haven't done something like this, but found a couple DB services you might want to check out.
Firebase by Google
RdbHost
The above recommendations may be useful if you're expecting data entry from visitors to your page. But if your data is static as well...you might be better off using s JSON file or some alternative where the data can live right in your repo.

Github pages - Disable custom domain redirection for all but a single site?

I've been using github pages since a long time and use the custom domain redirection feature to redirect my user website from prahladyeri.github.io to my own domain which is https://www.prahladyeri.com by adding a CNAME file.
However, for the web-pages (gh-pages branch) of open source projects that I maintain, it automatically redirects a foo project to www.prahladyeri.com/foo when no CNAME is specified. Is it possible to use the default prahladyeri.github.io/foo scheme for my projects, but keep custom redirection on for my main site?
Is that something easily achievable?
Update on 2022-11-14
As of today, I've switched to the proper sub-domain https://prahladyeri.github.io. The old site will soon expire, I'm keeping this post only for academic interest.
As of Jul 2016, I don't think this is possible. (https://help.github.com/articles/custom-domain-redirects-for-github-pages-sites/)
The Github Pages help article states that
Project Pages site owned by a user account, such as username.github.io/projectname, will be Automatically redirected to a subdirectory of a User Pages site custom domain (user.example.com/projectname), unless a different CNMAE is specified, such as project.example.com

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.