get_pipeline_failed_jobs
Retrieve console output for failed jobs in GitLab pipelines to identify and resolve build issues.
Instructions
GitLabパイプラインで失敗したジョブのコンソール出力を取得
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "get_pipeline_failed_jobs"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- main.py:46-60 (handler)The handler function implementing the 'get_pipeline_failed_jobs' MCP tool. Decorated with @mcp.tool() for registration. Retrieves current MR ID, fetches failed jobs output using helper, and formats response.
@mcp.tool() def get_pipeline_failed_jobs() -> str: """GitLabパイプラインで失敗したジョブのコンソール出力を取得""" mr_id = get_current_mr_id() failed_jobs_output = get_failed_jobs_output(mr_id=mr_id) if failed_jobs_output: return f""" パイプラインで以下のエラーが出ています。 プロダクトコードの修正で対応が可能な場合は修正を行ってください。 {failed_jobs_output} """ else: return "パイプラインで失敗したジョブが見つかりません。" - src/utils/gitlab_utils.py:160-216 (helper)Helper function that implements the core logic of fetching console output from failed jobs in the latest pipeline of a GitLab MR. Called by the tool handler.
def get_failed_jobs_output(mr_id: int) -> str: """ 指定したMR IDに関連する最後のパイプラインで失敗したジョブのコンソール出力を取得します。 最後のパイプラインに失敗したジョブがなければ、空の文字列を返します。 Args: mr_id (int): Merge Request ID Returns: str: 失敗したジョブのコンソール出力、または空文字列 Raises: ValueError: ジョブ出力の取得に失敗した場合 """ try: project = get_gitlab_project() # MRを取得 mr = project.mergerequests.get(mr_id) # パイプライン情報を取得 if not hasattr(mr, "pipelines") or not mr.pipelines: return "" # パイプラインを取得し、最新のものを選択 pipelines = mr.pipelines.list() if not pipelines: return "" # 最新のパイプラインを取得 (GitLabのAPIはデフォルトで降順) latest_pipeline = pipelines[0] pipeline_detail = project.pipelines.get(latest_pipeline.id) # 失敗したジョブを検索 failed_jobs = [ job for job in pipeline_detail.jobs.list() if job.status == "failed" ] if not failed_jobs: return "" # 失敗したジョブがない場合は空文字列を返す # 失敗したジョブのコンソール出力を取得 outputs = [] for job in failed_jobs: job_detail = project.jobs.get(job.id) job_output = job_detail.trace() outputs.append( f"# ジョブ: {job.name}\n- ステータス: {job.status}\n- 出力:\n```\n{job_output}\n```" ) return "\n\n".join(outputs) except gitlab.exceptions.GitlabGetError: raise ValueError(f"MR ID #{mr_id} が見つかりません。") except Exception as e: raise ValueError(f"失敗したジョブの出力取得に失敗しました: {str(e)}") - main.py:31-43 (helper)Helper function to get the current Merge Request ID from the current branch, used by the tool handler.
def get_current_mr_id() -> int: """ 現在のブランチのMRIDを取得します。 Returns: int: 現在のブランチのMRID """ branch_name = get_current_branch() mr = get_merge_request(branch_name) if mr: return mr.iid else: return "現在のブランチに関連するMerge Requestが見つかりません。"