File indexing completed on 2024-05-19 15:24:57

0001 #!/usr/bin/env python3
0002 
0003 import os
0004 import subprocess
0005 from argparse import ArgumentParser
0006 from gitlab_api import GitlabApi, environ
0007 
0008 
0009 def git(*args):
0010     pipe = subprocess.Popen(["git", "-C", os.path.dirname(__file__)] + list(args), stdout=subprocess.PIPE)
0011     out, _ = pipe.communicate()
0012     return out.decode('ascii').strip()
0013 
0014 
0015 parser = ArgumentParser()
0016 parser.add_argument("--fake-env", action="store_true")
0017 parser.add_argument("state", choices=["pending", "running", "success", "failed", "canceled", "cancelled", "failure"])
0018 
0019 ns = parser.parse_args()
0020 
0021 if ns.fake_env:
0022     commit = git("rev-parse",  "HEAD")
0023     name = "Test"
0024     url = "https://github.com/mbasaglia/glaxnimate/actions"
0025     ref = "refs/heads/" + git("branch", "--show-current")
0026 else:
0027     commit = environ("GITHUB_SHA")
0028     name = "%s: %s" % (environ("GITHUB_WORKFLOW"), environ("GITHUB_JOB"))
0029     url = environ("GITHUB_SERVER_URL") + "/" + environ("GITHUB_REPOSITORY") + "/actions/runs/" + environ("GITHUB_RUN_ID")
0030     ref = environ("GITHUB_REF")
0031 
0032 
0033 if ref.startswith("refs/"):
0034     ref = ref.rsplit("/", 1)[1]
0035 
0036 
0037 GitlabApi.fake_env()
0038 api = GitlabApi.from_env()
0039 
0040 status = ns.state
0041 # Convert GitHub => GitLab
0042 if status == "cancelled":
0043     status = "canceled"
0044 elif status == "failure":
0045     status = "failed"
0046 
0047 
0048 data = {
0049     "id": api.project_id,
0050     "sha": commit,
0051     "state": status,
0052     "ref": ref,
0053     "name": name,
0054     "target_url": url
0055 }
0056 
0057 api.project_request("post", ["statuses", commit], json=data)