Skip to content

Instantly share code, notes, and snippets.

@NyaMisty
Last active January 26, 2024 03:44
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NyaMisty/d474fbaa5bb9580a935608d1c331f755 to your computer and use it in GitHub Desktop.
Save NyaMisty/d474fbaa5bb9580a935608d1c331f755 to your computer and use it in GitHub Desktop.
A small snippet to use CoCoPilot without patching Copilot plugin

cocopilot-gh-enterprise

Turn cocopilot into github enterprise server, so that we can use CoCoPilot without patching Copilot plugin.

Usage

  1. Add the following JS to a cloudflare worker.
  2. Add Cloudflare Worker Routes: *cocopilot-gh-enterprise.XXXXXXXX.XXX. You should also add DNS records cocopilot-gh-enterprise.XXXXXXXX.XXX & *.cocopilot-gh-enterprise.XXXXXXXX.XXX to make the route available image
  3. Setting GitHub Enterprise URL to cocopilot-gh-enterprise.XXXXXXXX.XXX image
  4. Setting Copilot settings: Add to settings.json
  "github.copilot.advanced": {
      "authProvider": "github-enterprise",
  },
  1. Login to the fake GitHub Enterprise: click the user profile icon lower right corner to login to copilot image
  2. Click Yes in the dialog at lower right corner: image
  3. Click Continue to GitHub: image
  4. Click Cancel: image
  5. Enter cocopilot's token into new dialog then press Enter: image
  6. Logged in & Enjoy: image
const COCOPILOT_TOKEN_URL = "https://blog.teqiyi.com/cocopilot/" // the url in "dev_override":{"copilot_token_url":"https://XXXXXXX"}}
const GH_ENTERPRISE_USERNAME = "CoCoPilot" // username to be displayed in VSCode
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
switch (url.pathname) {
case '/copilot_internal/v2/token':
var auth = request.headers['Authorization']
// var authpart = auth.trim().split(' ')
// var authbody = authpart[authpart.length - 1]
return fetch(COCOPILOT_TOKEN_URL, {
method: "GET",
headers: {
Accept: "*/*",
Authorization: auth,
Connection: 'close',
'Editor-Plugin-Version': 'copilot/1.137.0',
'Editor-Version': 'vscode/1.81.1',
'User-Agent': 'GithubCopilot/1.137.0',
}
})
case '/api/v3':
return new Response(
JSON.stringify({ }),
{ headers: {
"Content-Type": "application/json; charset=utf-8",
"X-OAuth-Scopes": "user:email",
"X-Accepted-OAuth-Scopes": "",
"x-oauth-client-id": "01ab8ac9400c4e429b23",
"X-GitHub-Media-Type": "github.v3; format=json",
"x-github-api-version-selected": "2022-11-28",
} });
case '/api/v3/user':
return new Response(
JSON.stringify({
"id": 123456,
"location": null,
"login": GH_ENTERPRISE_USERNAME,
"type": "User",
}),
{ headers: {
"Content-Type": "application/json; charset=utf-8",
} });
case '/api/v3/meta':
return new Response(
JSON.stringify({ }),
{ headers: {
"Content-Type": "application/json; charset=utf-8",
} });
}
return new Response(
`cocopilot-gh-enterprise`,
{ headers: { "Content-Type": "text/html" } }
);
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment