???Linux??GitLab API??,?????????????:
1. ??????
- ?????GitLab???
- ????????,?? "Settings" (??)?
- ??????,?? "Access Tokens" (????)?
- ? "Personal Access Tokens" ??,????????????,??????????(Scopes)?
- ?? "Create personal access token" ???????
2. ????
- GitLab API????????????????
3. ????
?????????????API??,??Python?Java?Ruby?????????????HTTP????,????HTTP???
??Python?????:
import requests # ??GitLab API URL????? GITLAB_API_URL = "https://your-gitlab-instance.com/api/v4" PROJECT_PATH = "user/ntp-sync" FILE_PATH = "time.txt" TOKEN = getpass("Enter your GitLab personal access token: ") # ?????? def get_file_from_gitlab(project_id, file_path, ref, access_token): url = f"{GITLAB_API_URL}/projects/{project_id}/repository/files/{file_path}/raw" headers = {"PRIVATE-TOKEN": access_token} params = {"ref": ref} response = requests.get(url, headers=headers, params=params) if response.status_code == 200: return response.text else: response.raise_for_status() # ??? def main(): # ???????? current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # ??????? response = get_file_from_gitlab(PROJECT_PATH, FILE_PATH, "main", TOKEN) # ?????? print("File content:") print(response) if __name__ == "__main__": main()
4. ??API??
??curl
???????????????HTTP???????????,??curl
??????:
curl --header "PRIVATE-TOKEN: your_access_token" "https://gitlab.example.com/api/v4/projects/13083/repository/files/app/models/key.rb?ref=master"
5. ??API??
GitLab API??????JSON????????????????JSON???????????
????
- ????
GITLAB_API_URL
?PROJECT_PATH
????GitLab??URL?????? - ??
getpass
??????GitLab??????? - ??????????main???????????,??????
branch
???
????????????????Linux?????GitLab API??????????????,???????????????