MCPcopy Index your code
hub / github.com/github/docs / getGHExample

Function getGHExample

components/lib/get-rest-code-samples.ts:104–154  ·  view source on GitHub ↗
(operation: Operation, codeSample: CodeSample)

Source from the content-addressed store, hash-verified

102 -fref,topic-branch=0,payload,{ "deploy": "migrate" }=1,description,Deploy request from hubot=2
103*/
104export function getGHExample(operation: Operation, codeSample: CodeSample) {
105 const defaultAcceptHeader = getAcceptHeader(codeSample)
106 const hostname = operation.serverUrl !== 'https://api.github.com' ? '--hostname HOSTNAME' : ''
107
108 let requestPath = codeSample?.request?.parameters
109 ? parseTemplate(operation.requestPath).expand(codeSample.request.parameters)
110 : operation.requestPath
111
112 const requiredQueryParams = getRequiredQueryParamsPath(operation, codeSample)
113 requestPath += requiredQueryParams ? `?${requiredQueryParams}` : ''
114
115 let requestBodyParams = ''
116 // Most of the time the example body parameters have a name and value
117 // and are included in an object. But, some cases are a single value
118 // and the type is a string.
119 const { bodyParameters } = codeSample.request
120 if (bodyParameters) {
121 if (typeof bodyParameters === 'object' && !Array.isArray(bodyParameters)) {
122 const bodyParamValues = Object.values(codeSample.request.bodyParameters)
123 // GitHub CLI does not support sending Objects and arrays using the -F or
124 // -f flags. That support may be added in the future. It is possible to
125 // use gh api --input to take a JSON object from standard input
126 // constructed by jq and piped to gh api. However, we'll hold off on adding
127 // that complexity for now.
128 if (bodyParamValues.some((elem) => typeof elem === 'object')) {
129 return undefined
130 }
131 requestBodyParams = Object.keys(codeSample.request.bodyParameters)
132 .map((key) => {
133 if (typeof codeSample.request.bodyParameters[key] === 'string') {
134 return `-f ${key}='${codeSample.request.bodyParameters[key]}' `
135 } else {
136 return `-F ${key}=${codeSample.request.bodyParameters[key]} `
137 }
138 })
139 .join('\\\n ')
140 } else {
141 requestBodyParams = `-f '${codeSample.request.bodyParameters}'`
142 }
143 }
144 const args = [
145 operation.verb !== 'get' && `--method ${operation.verb.toUpperCase()}`,
146 `-H "Accept: ${defaultAcceptHeader}"`,
147 hostname,
148 requestPath,
149 requestBodyParams,
150 ].filter(Boolean)
151 return `# GitHub CLI api\n# https://cli.github.com/manual/gh_api\n\ngh api \\\n ${args.join(
152 ' \\\n '
153 )}`
154}
155
156/*
157 Generates an octokit.js example

Callers 1

RestCodeSamplesFunction · 0.90

Calls 2

getAcceptHeaderFunction · 0.85

Tested by

no test coverage detected