# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\W*(?:>>>+|@+>>+|@@+>+|== *>>+|={4,}|===>+|= = =|(?:Read More|Click Here)).{0,20}https?:\/\/(?!(?:i\.stack\.imgur\.com|(?:[\w-]+\.)*(?:(?:stack(?:exchange|overflow)|superuser|serverfault|askubuntu|stackapps)\.com|(?:mathoverflow|sstatic)\.net)))(?=.{201,}$)"
test_str = ("<p>Admittedly, I understand how to use it, but I'm having issues setting it up. So far, I have set up a new personal access token <em>(for the purposes of this question I will define as <code>ABCDEFGHIJKLMNOPQRSTUZWXYZ0</code>).</em> Assuming my user name is <code>ABC.123</code> and my email is <code>abc123@foobar.com</code> I have tried create a job in which pushes new updates to a GitLab repository provided that certain conditions are met. I've tried various different methods, I've come across online, namely:</p>\n\n"
"<pre><code>>>> git push\n"
"</code></pre>\n\n"
"<p>which returned</p>\n\n"
"<pre><code>fatal: You are not currently on a branch. To push the history leading to the current (detached HEAD) state now, use\n"
" git push origin HEAD:<name-of-remote-branch>`.\n"
"</code></pre>\n\n"
"<p>Next, I tried:</p>\n\n"
"<pre><code>>>> git push origin HEAD:master --force\n"
"</code></pre>\n\n"
"<p>which returned:</p>\n\n"
"<pre><code>remote: You are not allowed to upload code.\n"
"fatal: unable to access 'https://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@gitlab.mycompany.com/path/repository.git/': The requested URL returned error: 403\n"
"</code></pre>\n\n"
"<p>Then, I tried:</p>\n\n"
"<pre><code>>>> git push https://gitlab-ci-token:ABCDEFGHIJKLMNOPQRSTUZWXYZ0@gitlab.mycompany.com:path/repository.git HEAD:master\n"
"</code></pre>\n\n"
"<p>which returned:</p>\n\n"
"<pre><code>remote: The project you were looking for could not be found.\n"
"fatal: repository 'https://gitlab-ci-token:ABCDEFGHIJKLMNOPQRSTUZWXYZ0@gitlab.mycompany.com:path/repository.git/' not found\n"
"</code></pre>\n\n"
"<p>Following other examples, I then tried:</p>\n\n"
"<pre><code>>>> git push https://ABC.123:ABCDEFGHIJKLMNOPQRSTUZWXYZ0@gitlab.mycompany.com:path/repository.git HEAD:master\n"
"</code></pre>\n\n"
"<p>and the output of this was:</p>\n\n"
"<pre><code>remote: The project you were looking for could not be found.\n"
"fatal: repository 'https://ABC.123:ABCDEFGHIJKLMNOPQRSTUZWXYZ0@gitlab.mycompany.com:path/repository.git/' not found\n"
"</code></pre>\n\n"
"<p>and lastly, I tried adding <code>origin</code> to it:</p>\n\n"
"<pre><code>>>> git push origin https://gitlab-ci-token:ABCDEFGHIJKLMNOPQRSTUZWXYZ0.mycompany.com:path/repository.git HEAD:master\n"
"</code></pre>\n\n"
"<p>And this gave:</p>\n\n"
"<pre><code>error: src refspec https://gitlab-ci-token:ABCDEFGHIJKLMNOPQRSTUZWXYZ0@gitlab.mycompany.com does not match any.\n"
"error: failed to push some refs to 'https://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@gitlab.mycompany.com/path/repository.git'\n"
"</code></pre>\n\n"
"<p>There are plenty more but I can't seem to find a working solution. I'm 90% that maybe I'm just formatting these wrong or that I don't actually have a gitlab-ci token (and that its not the same as a personal access token), but nonetheless, I'd really like to figure this out. One source also said it may not be possible at the moment, for I've found plenty of others would managed to get theirs running properly. I would appreciate any help on both where to find the gitlab-ci token and how format the push properly so that I can achieve the results I am looking for.</p>\n\n"
"<p>My <code>gitlab-ci.yml</code> file has the following:</p>\n\n"
"<pre><code>update:\n"
" script:\n"
" - git config user.email \"abc123@foobar.com\"\n"
" - git config user.name \"ABC.123\"\n"
" - git config --global push.default simple\n"
" - python runUpdate.py\n"
"</code></pre>\n\n"
"<p>My <code>runUpdate.py</code> file has the following:</p>\n\n"
"<pre><code>def update():\n"
" doSomething(); # Some random function\n"
" os.system('git add -A');\n"
" os.system('git commit -m \"Automated Repo Update\"');\n"
" os.system('git push'); # issue triggered here...\n\n"
"update();\n"
"</code></pre>")
matches = re.finditer(regex, test_str, re.IGNORECASE | re.DOTALL)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html