|
1 | 1 | import re |
| 2 | +import subprocess |
2 | 3 | from rest_framework import viewsets |
3 | 4 | from rest_framework.response import Response |
4 | 5 | from rest_framework.views import APIView |
@@ -1058,3 +1059,62 @@ def get(self, request, pokemon_id): |
1058 | 1059 | ) |
1059 | 1060 |
|
1060 | 1061 | return Response(encounters_list) |
| 1062 | + |
| 1063 | + |
| 1064 | +@extend_schema( |
| 1065 | + description="Returns metadata about the current deployed version of the API, including the git commit hash, deploy date, and tag (if any).", |
| 1066 | + tags=["utility"], |
| 1067 | + responses={ |
| 1068 | + 200: { |
| 1069 | + "type": "object", |
| 1070 | + "properties": { |
| 1071 | + "deploy_date": {"type": "string", "nullable": True}, |
| 1072 | + "hash": {"type": "string", "nullable": True}, |
| 1073 | + "tag": {"type": "string", "nullable": True}, |
| 1074 | + }, |
| 1075 | + } |
| 1076 | + }, |
| 1077 | +) |
| 1078 | +class PokeapiMetaViewset(viewsets.ViewSet): |
| 1079 | + def list(self, request): |
| 1080 | + try: |
| 1081 | + git_hash = ( |
| 1082 | + subprocess.check_output( |
| 1083 | + ["git", "rev-parse", "HEAD"], stderr=subprocess.DEVNULL |
| 1084 | + ) |
| 1085 | + .decode() |
| 1086 | + .strip() |
| 1087 | + ) |
| 1088 | + except Exception: |
| 1089 | + git_hash = None |
| 1090 | + |
| 1091 | + try: |
| 1092 | + deploy_date = ( |
| 1093 | + subprocess.check_output( |
| 1094 | + ["git", "log", "-1", "--format=%cI"], stderr=subprocess.DEVNULL |
| 1095 | + ) |
| 1096 | + .decode() |
| 1097 | + .strip() |
| 1098 | + ) |
| 1099 | + except Exception: |
| 1100 | + deploy_date = None |
| 1101 | + |
| 1102 | + try: |
| 1103 | + tag_output = ( |
| 1104 | + subprocess.check_output( |
| 1105 | + ["git", "tag", "--points-at", "HEAD"], stderr=subprocess.DEVNULL |
| 1106 | + ) |
| 1107 | + .decode() |
| 1108 | + .strip() |
| 1109 | + ) |
| 1110 | + tag = tag_output if tag_output else None |
| 1111 | + except Exception: |
| 1112 | + tag = None |
| 1113 | + |
| 1114 | + return Response( |
| 1115 | + { |
| 1116 | + "deploy_date": deploy_date, |
| 1117 | + "hash": git_hash, |
| 1118 | + "tag": tag, |
| 1119 | + } |
| 1120 | + ) |
0 commit comments