3

This question is quite simple. I have a heroku app deployed, and I cannot find it's source code.

How can we retrieve the github repository url for a given heroku app via either the heroku CLI or by the heroku web interface?

Edit: yes, this has been troubling me for over a year now

What I've tried

I have tried heroku git:clone -a APP-NAME which copies the repository (from heroku?) to local. After cding into it, and trying to get the github remote url

git config --get remote.origin.url

but that returns blank. I also tried

git remote show origin  
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
stevec
  • 41,291
  • 27
  • 223
  • 311
  • Are you sure that there _is_ a GitHub repository associated with the app? There might not be. Who initially deployed it? – ChrisGPT was on strike Jun 20 '19 at 21:17
  • @Chris I deployed it, and it was definitely deployed from github repo (I don't know any other way). I just don't know which github repo this app stems from, and don't want to delete it in case there is something important there. I think heroku should state the github url somewhere, that would be an easy solve, but I can't seem to locate it – stevec Jun 21 '19 at 01:40
  • 1
    So... you deployed it from GitHub, but you don't remember which specific repo you deployed from? You can see the commit hash using [`heroku releases`](https://devcenter.heroku.com/articles/releases). I'm not sure that anything shows the GitHub repo, but try checking there. – ChrisGPT was on strike Jun 21 '19 at 02:15
  • @Chris Yes exactly. This is not hard to envisage if you have hundreds of apps. Heroku's strength is that it makes it trivial to deploy an app (e.g. ~30 seconds from laptop, to git, to heroku). This is great for quickly deploying microservices and the like. Every so often, I clean up unused or necessary apps, which hasn't been a problem until now since most apps have something at their root url. But in one case, it doesn't, and I don't want to delete it until I am sure it's not something important. – stevec Jun 21 '19 at 02:19
  • @Chris Running `heroku releases -a myappname` returns expected output (but no mention of the repo). I try `git log -n 1 9643b02d -a myappname` (where the commit is the most recent from the output of the first command), and get `fatal: not a git repository (or any of the parent directories): .git` – stevec Jun 21 '19 at 02:27

2 Answers2

2

I just tried heroku slugs:download -a myapp and get

Warning: slugs:download is not a heroku command.

You would need to install the heroku/heroku-slugs plugin first, in order for that command to be recognized.

heroku plugins:install heroku-slugs

But try first, as you did

heroku git:clone -a APP-NAME

And then git log -1: that will give you the SHA1 of the latest commit.

You can then do on GitHub a Commit search by hash.
Example:

https://github.com/github/gitignore/search?q=hash%3A124a9a0ee1d8f1e15e833aff432fbb3b02632105&type=Commits


The OP stevec mentions in the comments using "How to clone all repos at once from GitHub?" to handle more than 100 repositories and (critically) download the private ones too (not just public).
That is: write a bash script that clones every repository on my account and somehow searches for a particular SHA.

I suggests a simple git log -1 SHA, or;, following "How to check if the commit exists in a Git repository by its SHA-1":

git rev-parse -q --verify "$sha^{commit}" > /dev/null

The OP adds:

I realised your initial solution definitely works (even for private repos).

heroku git:clone -a APP-NAME
git log -1, 
https://github.com/search?q=hash%3A<SHA>
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks! It all makes sense. But after getting the commit SHA and searching for it, I see `We couldn’t find any commits matching ..`. I have tinkered a little to ensure I'm searching all of github, tried the search box with `hash:...` and altering the URL a couple of ways (trying both `:` and `%3A`), but I don't seem to be able to get any results. I will try `git log -2` (and so on) and hopefully one matches. If you have any other ideas they'd be appreciated! – stevec Aug 16 '20 at 10:05
  • @stevec was your original repo private? A search would work only on the master branch of public repos. – VonC Aug 16 '20 at 10:11
  • I guess it may have been. I am thinking one solution from here could be to write a bash script that clones every repository on my account and somehow searches for a particular SHA. If this goes smoothly I guess it wouldn't take all that long. Do you know of any better/easier way? – stevec Aug 16 '20 at 10:19
  • @stevec "somehow searches for a particular SHA.": a `simple git log ` in each of those cloned repos would be enough. – VonC Aug 16 '20 at 15:27
  • Thanks! I will try to adapt [this](https://stackoverflow.com/a/32833411/) to handle > 100 repositories and (critically) download the private ones too (not just public) – stevec Aug 16 '20 at 20:01
  • @stevec Good approach. I have included your comments and a few suggestions in the answer, for more visibility. – VonC Aug 16 '20 at 20:05
  • I had extra complications due to some mistakes I made, but I realised your initial solution definitely works (even for private repos). `heroku git:clone -a APP-NAME`, `git log -1`, then visiting `https://github.com/search?q=hash%3A` definitely works – stevec Aug 16 '20 at 20:40
0

See the documentation on downloading your code. Heroku allows you to clone the git repo from their servers - so as long as you have access to the app in Heroku, you can see the exact code that has been deployed.

gwcodes
  • 5,632
  • 1
  • 11
  • 20
  • ...[but](https://devcenter.heroku.com/articles/git-clone-heroku-app) "A Heroku app’s Git repository is intended for deployment purposes only. Cloning from this repository is not officially supported as a feature and should be attempted only as a last resort. **Do not** use this repository as your app’s canonical “origin” repository. Instead, use your own Git server or a version control service such as GitHub." – ChrisGPT was on strike Jun 20 '19 at 21:16
  • I just tried `heroku slugs:download -a myapp` and get `› Warning: slugs:download is not a heroku command.` – stevec Aug 16 '20 at 05:46