Referencing current branch in github readme.md
Asked Answered
L

4

116

In my github repo's readme.md file I have a Travis-CI badge. I use the following link:

https://travis-ci.org/joegattnet/joegattnet_v3.png?branch=staging

The obvious problem is that the branch is hardcoded. Is it possible to use some sort of variable so that the branch is the one currently being viewed?

Lyndell answered 7/9, 2013 at 13:17 Comment(2)
github.com/github/markup/issues/913Prefatory
It should also be possible to make the repository part a variable also, so that forked repositories don't incorrectly report the status of the original repository they are forked from.Fuddyduddy
D
59

Not that I know of.
GitHub support confirms (through OP Joe Gatt 's comment)

The only way to do this would be to pass the link through my own service which would use the github's http referrer header to determine which branch is being referenced and then fetch the appropriate image from Travis CI

I would rather make one Travis-CI badge per branch, for the reader to choose or consider the appropriate when seeing the README.md.


Update 2016 (3 years later): while nothing has changed on the GitHub side, fedorqui reports in the workaround mentioned in "Get Travis Shield on Github to Reflect Selected Branch Status" by Andrie.
Simply display all the branches and their respective TravisCI badges.

If you have only two or three branches, that could be enough.

Dysphasia answered 7/9, 2013 at 20:41 Comment(11)
Thanks, VonC. I contacted github support, as you suggested and here's their reply:Lyndell
They confirmed that this can't be done. They said the only way to do this would be to pass the link through my own service which would use the github's http referrer header to determine which branch is being referenced and then fetch the appropriate image from Travis CI...Lyndell
@JoeGatt good feedback. I have included that conclusion in the answer for more visibility.Dysphasia
I just tried to pass the link through my own service but unfortunately I don’t get the HTTP_REFERER when the image is loaded from the README on GitHub. :-(Collate
Well, I guess it’s now impossible to achieve because of SSL Proxied Assets.Collate
Are you aware of any change on this? I mean, is it still not available?Manwell
@Manwell As far as I know, it is not available.Dysphasia
Thanks a lot for the feedback. I found an interesting answer using ?branch=... that Travis can handle internally.Manwell
@Manwell Yes, that is what this question uses as well: but you can not pass a variable for that branch name, can you?Dysphasia
No, no that I am aware of :/Manwell
@Manwell Still, I have included your comment in the answer for more visibility.Dysphasia
B
22

I worked around this issue with a git pre-commit hook that re-writes the Travis line in the README.md with the current branch. An example of usage and pre-commit (Python) code (for the question as asked) are below.

Usage

dandye$ git checkout -b feature123 origin/master
Branch feature123 set up to track remote branch master from origin.
Switched to a new branch 'feature123'
dandye$ echo "* Feature123" >> README.md 
dandye$ git add README.md 
dandye$ git commit -m "Added Feature123"
Starting pre-commit hook...
Replacing:
    [![Build Status](https://travis-ci.org/joegattnet/joegattnet_v3.png?branch=master)][travis]

with:
    [![Build Status](https://travis-ci.org/joegattnet/joegattnet_v3.png?branch=feature123)][travis]

pre-commit hook complete.
[feature123 54897ee] Added Feature123
 1 file changed, 2 insertions(+), 1 deletion(-)
dandye$ cat README.md |grep "Build Status"
[![Build Status](https://travis-ci.org/joegattnet/joegattnet_v3.png?branch=feature123)][travis]
dandye$ 

Python code for the pre-commit code

dandye$ cat .git/hooks/pre-commit
#!/usr/bin/python
"""
Referencing current branch in github readme.md[1]

This pre-commit hook[2] updates the README.md file's
Travis badge with the current branch. Gist at[4].

[1] https://mcmap.net/q/187909/-referencing-current-branch-in-github-readme-md
[2] http://www.git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
[3] https://docs.travis-ci.com/user/status-images/
[4] https://gist.github.com/dandye/dfe0870a6a1151c89ed9
"""
import subprocess

# Hard-Coded for your repo (ToDo: get from remote?)
GITHUB_USER="joegattnet"
REPO="joegattnet_v3"

print "Starting pre-commit hook..."

BRANCH=subprocess.check_output(["git",
                                "rev-parse",
                                "--abbrev-ref",
                                "HEAD"]).strip()

# String with hard-coded values
# See Embedding Status Images[3] for alternate formats (private repos, svg, etc)

#  [![Build Status](https://travis-ci.org/
#  joegattnet/joegattnet_v3.png?
#  branch=staging)][travis]

# Output String with Variable substitution
travis="[![Build Status](https://travis-ci.org/" \
       "{GITHUB_USER}/{REPO}.png?" \
       "branch={BRANCH})][travis]\n".format(BRANCH=BRANCH,
                                            GITHUB_USER=GITHUB_USER,
                                            REPO=REPO)

sentinel_str="[![Build Status]"

readmelines=open("README.md").readlines()
with open("README.md", "w") as fh:
    for aline in readmelines:
        if sentinel_str in aline and travis != aline:
            print "Replacing:\n\t{aline}\nwith:\n\t{travis}".format(
                   aline=aline,
                   travis=travis)
            fh.write(travis)
        else:
            fh.write(aline)

subprocess.check_output(["git", "add", "README.md" ])

print "pre-commit hook complete."
Betthezul answered 12/2, 2016 at 17:32 Comment(4)
Getting the repo and github user is tricky and somewhat fragile since there is no guaranteed information of where the repository comes from. You can use the repo-URL if you're prepared to live with the fragility: REPOurl=subprocess.check_output(['git','config','--local', 'remote.origin.url']).decode() Crucible
GITHUB_USER=re.match('.*:([a-zA-Z0-9]*)\/', REPOurl).groups()[0] Crucible
REPO=re.match('.*\/([a-zA-Z0-9]*).git', REPOurl).groups()[0] Crucible
That's cool, I had thought about this but I wish I could have a magic variable that refers to the current branch in github so that my commit history doesn't get polluted.Obolus
C
5

I updated the work of Dan Dye so it's now able to change any git variable into a readme. It also works now with python 3. For example, handling badges by branch for Github actions:

[![Integration Tests](https://github.com/{{ repository.name }}/actions/workflows/integration-tests.yaml/badge.svg?branch={{ current.branch }})](https://github.com/{{ repository.name }}/actions/workflows/integration-tests.yaml?query=branch%3A{{ current.branch }})

And in your pre-commit file add:

.githooks/replace_by_git_vars.py readme.md README.md -v

-v displays the available variables and more

https://gist.github.com/jclaveau/af2271b9fdf05f7f1983f492af5592f8

Thanks a lot for the solution and inspiration!

Contravallation answered 10/6, 2021 at 10:39 Comment(0)
T
0

The best solution for me was to create a server where I send a query with username and repo's name and get a svg image with the build status for all branches.

Tonita answered 20/1, 2019 at 18:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.