프로젝트 스니펫

Tier: Free, Premium, Ultimate

Offering: GitLab.com, Self-managed, GitLab Dedicated

스니펫 가시성 수준

스니펫은 GitLab에서 개인, 내부 또는 공개로 설정할 수 있습니다.

visibility 필드를 사용하여 스니펫을 설정할 수 있습니다.

스니펫 가시성 수준의 상수는 다음과 같습니다:

  • Private: 스니펫은 프로젝트 멤버에게만 보입니다.
  • Internal: 스니펫은 외부 사용자를 제외한 모든 인증된 사용자에게 보입니다.
  • Public: 스니펫은 인증 없이 접근할 수 있습니다.
note
2019년 7월부터 Internal 가시성 설정은 새로운 프로젝트, 그룹 및 GitLab.com의 스니펫에 대해 비활성화되었습니다.

기존 프로젝트, 그룹 및 Internal 가시성 설정을 사용하는 스니펫은 이 설정을 유지합니다.

변경 내용에 대해 더 읽고 싶다면 관련 이슈를 참조하세요.

스니펫 목록 가져오기

프로젝트 스니펫 목록을 가져옵니다.

GET /projects/:id/snippets

매개변수:

속성 유형 필수 여부 설명
id integer 또는 string 프로젝트의 ID 또는 URL-인코딩 경로입니다.

단일 스니펫 가져오기

단일 프로젝트 스니펫을 가져옵니다.

GET /projects/:id/snippets/:snippet_id

매개변수:

속성 유형 필수 여부 설명
id integer 또는 string 프로젝트의 ID 또는 URL-인코딩 경로입니다.
snippet_id integer 프로젝트 스니펫의 ID입니다.
{
  "id": 1,
  "title": "test",
  "file_name": "add.rb",
  "description": "Ruby 테스트 스니펫",
  "author": {
    "id": 1,
    "username": "john_smith",
    "email": "john@example.com",
    "name": "John Smith",
    "state": "active",
    "created_at": "2012-05-23T08:00:58Z"
  },
  "updated_at": "2012-06-28T10:52:04Z",
  "created_at": "2012-06-28T10:52:04Z",
  "imported": false,
  "imported_from": "none",
  "project_id": 1,
  "web_url": "http://example.com/example/example/snippets/1",
  "raw_url": "http://example.com/example/example/snippets/1/raw"
}

새로운 스니펫 생성

새로운 프로젝트 스니펫을 생성합니다. 사용자는 새로운 스니펫을 생성할 수 있는 권한이 있어야 합니다.

POST /projects/:id/snippets

매개변수:

속성 유형 필수 여부 설명
id integer 또는 string 프로젝트의 ID 또는 URL-인코딩 경로입니다.
files:content string 스니펫 파일의 내용입니다.
files:file_path string 스니펫 파일의 경로입니다.
title string 스니펫의 제목입니다.
content string 아니요 더 이상 사용되지 않음: 대신 files를 사용하세요. 스니펫의 내용입니다.
description string 아니요 스니펫의 설명입니다.
file_name string 아니요 더 이상 사용되지 않음: 대신 files를 사용하세요. 스니펫 파일의 이름입니다.
files 해시 배열 아니요 스니펫 파일의 배열입니다.
visibility string 아니요 스니펫의 가시성입니다.

예제 요청:

curl --request POST "https://gitlab.com/api/v4/projects/:id/snippets" \
     --header "PRIVATE-TOKEN: <your access token>" \
     --header "Content-Type: application/json" \
     -d @snippet.json

위의 예제 요청에서 사용된 snippet.json:

{
  "title" : "예제 스니펫 제목",
  "description" : "더 상세한 스니펫 설명",
  "visibility" : "private",
  "files": [
    {
      "file_path": "example.txt",
      "content" : "소스 코드 \n 여러 줄 \n"
    }
  ]
}

스니펫 업데이트

기존 프로젝트 스니펫을 업데이트합니다. 사용자는 기존 스니펫을 변경할 수 있는 권한이 있어야 합니다.

여러 파일을 포함하는 스니펫의 업데이트는 files 속성을 사용해야 합니다.

PUT /projects/:id/snippets/:snippet_id

매개변수:

속성 타입 필수 설명
id 정수 또는 문자열 프로젝트의 ID 또는 URL-encoded 경로.
files:action 문자열 파일에 대해 수행할 작업의 유형. 값 중 하나: create, update, delete, move.
snippet_id 정수 프로젝트 스니펫의 ID.
content 문자열 아니요 사용 중단: 대신 files를 사용하세요. 스니펫의 내용.
description 문자열 아니요 스니펫의 설명.
files 해시 배열 아니요 스니펫 파일의 배열.
files:content 문자열 아니요 스니펫 파일의 내용.
files:file_path 문자열 아니요 스니펫 파일의 파일 경로.
file_name 문자열 아니요 사용 중단: 대신 files를 사용하세요. 스니펫 파일의 이름.
files:previous_path 문자열 아니요 스니펫 파일의 이전 경로.
title 문자열 아니요 스니펫의 제목.
visibility 문자열 아니요 스니펫의 가시성.

예시 요청:

curl --request PUT "https://gitlab.com/api/v4/projects/:id/snippets/:snippet_id" \
     --header "PRIVATE-TOKEN: <your_access_token>" \
     --header "Content-Type: application/json" \
     -d @snippet.json

위 예시 요청에 사용된 snippet.json:

{
  "title" : "업데이트된 스니펫 제목",
  "description" : "더 상세한 스니펫 설명",
  "visibility" : "private",
  "files": [
    {
      "action": "update",
      "file_path": "example.txt",
      "content" : "업데이트된 소스 코드 \n 여러 줄로\n"
    }
  ]
}

스니펫 삭제

기존 프로젝트 스니펫을 삭제합니다. 이 작업이 성공적으로 수행되면 204 No Content 상태 코드를 반환하거나 리소스를 찾을 수 없는 경우 404를 반환합니다.

DELETE /projects/:id/snippets/:snippet_id

매개변수:

속성 타입 필수 설명
id 정수 또는 문자열 프로젝트의 ID 또는 URL-encoded 경로.
snippet_id 정수 프로젝트 스니펫의 ID.

예시 요청:

curl --request DELETE "https://gitlab.com/api/v4/projects/:id/snippets/:snippet_id" \
     --header "PRIVATE-TOKEN: <your_access_token>"

스니펫 내용

프로젝트 스니펫을 일반 텍스트로 반환합니다.

GET /projects/:id/snippets/:snippet_id/raw

매개변수:

속성 타입 필수 설명
id 정수 또는 문자열 프로젝트의 ID 또는 URL-encoded 경로.
snippet_id 정수 프로젝트 스니펫의 ID.

예시 요청:

curl "https://gitlab.com/api/v4/projects/:id/snippets/:snippet_id/raw" \
     --header "PRIVATE-TOKEN: <your_access_token>"

스니펫 저장소 파일 내용

원시 파일 내용을 일반 텍스트로 반환합니다.

GET /projects/:id/snippets/:snippet_id/files/:ref/:file_path/raw

매개변수:

속성 유형 필수 설명
id 정수 또는 문자열 프로젝트의 ID 또는 URL 인코딩된 경로입니다.
file_path 문자열 파일에 대한 URL 인코딩된 경로, 예: snippet%2Erb.
ref 문자열 브랜치, 태그 또는 커밋의 이름, 예: main.
snippet_id 정수 프로젝트의 스니펫 ID입니다.

예제 요청:

curl "https://gitlab.com/api/v4/projects/1/snippets/2/files/master/snippet%2Erb/raw" \
     --header "PRIVATE-TOKEN: <your_access_token>"

사용자 에이전트 세부정보 가져오기

관리자 접근 권한이 있는 사용자만 사용 가능합니다.

GET /projects/:id/snippets/:snippet_id/user_agent_detail
속성 유형 필수 설명
id 정수 또는 문자열 프로젝트의 ID 또는 URL 인코딩된 경로입니다.
snippet_id 정수 스니펫의 ID입니다.

예제 요청:

curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/snippets/2/user_agent_detail"

예제 응답:

{
  "user_agent": "AppleWebKit/537.36",
  "ip_address": "127.0.0.1",
  "akismet_submitted": false
}