Repository: readthedocs/readthedocs.org

Issue:
After build is triggered, state is shown as failed
To reproduce, fire off a new build, make sure to catch the build list page while VCS operations are happening. Build will be in a failure state. This is a regression where we are setting the state of the build to failed without checking that the build has completed. This might be a byproduct of using multiple environments during the build process.


Discussion:
@davidfischer noted he also saw this in production, which means it might not even be related to the VCS changes
Mmm...

I'm not able to reproduce this in my local instance with `master` branch. I see the states: `Triggered`, `Clonning` and `Installing` without problem.
Same behaviour in .org production
@humitos I also experiment this behavior on my local instance:

- Build any project
- Enter to the build (failure is shown)
- Wait ~5 seconds, reload the page
- Build is continued and successful

![screenshot-2018-2-21 bookpy read the docs](https://user-images.githubusercontent.com/4975310/36497585-bcb8b084-1709-11e8-8b30-da6743bc8bf1.png)
![screenshot-2018-2-21 bookpy read the docs 1](https://user-images.githubusercontent.com/4975310/36497591-bff0aa2c-1709-11e8-9ffe-0896f0ec44a1.png)
![screenshot-2018-2-21 bookpy read the docs 2](https://user-images.githubusercontent.com/4975310/36497595-c3492faa-1709-11e8-87d0-f8310f40b488.png)

I haven't see this on production, yet.
@stsewd i get the same results as you once i click on the build, but the build list page is where i see a failure.

![image](https://user-images.githubusercontent.com/1140183/36554941-defc2234-17bd-11e8-81ac-65e2c4a99eba.png)

Another clue: I can reproduce on first build of the project, not afterwards though.
@humitos I think the problem is with a project without tags.

If I ran this command on a project without tags, the exit code is `1` (here the build is marked as failure).

![error](https://user-images.githubusercontent.com/4975310/36575270-1cf18bba-1818-11e8-9223-d0071a383827.png)

Then on another step the exit code is forced to be 0 (here is when the build goes back to the normality)
Another clue: if I build the same project, the second build doesn't initially show the build as failing in the build list, but if I quickly select the build, it's in a failed state by the end of the VCS commands:

![image](https://user-images.githubusercontent.com/1140183/36608417-01c1d074-1887-11e8-965d-b5b2006aafd8.png)

This also causes the page to stop loading the build commands from the API, as we halt this process with JS when a build has failed. Reloading the page, I see I failed on the next command.

Resolving patch:
diff --git a/readthedocs/vcs_support/backends/git.py b/readthedocs/vcs_support/backends/git.py
--- a/readthedocs/vcs_support/backends/git.py
+++ b/readthedocs/vcs_support/backends/git.py
@@ -122,8 +122,8 @@ def parse_tags(self, data):
     @property
     def branches(self):
         # Only show remote branches
-        retcode, stdout, _ = self.run('git', 'branch', '-r')
-        # error (or no tags found)
+        retcode, stdout, _ = self.run('git', 'branch', '-r', record_as_success=True)
+        # error (or no branches found)
         if retcode != 0:
             return []
         return self.parse_branches(stdout)


Test patch: