Skip to content

Commit

Permalink
fix enterprise uploadAsset edge case
Browse files Browse the repository at this point in the history
resolves #320
  • Loading branch information
Joe Gallo committed Dec 1, 2016
1 parent 01e07cd commit b020d94
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
25 changes: 25 additions & 0 deletions examples/enterpriseUploadAsset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";

var Client = require("./../lib/index");
var testAuth = require("./../testAuth.json");

var github = new Client({
debug: true,
host: "github.my-GHE-enabled-company.com",
pathPrefix: "/api/v3"
});

github.authenticate({
type: "oauth",
token: testAuth["token"]
});

github.repos.uploadAsset({
owner: "kaizensoze",
repo: "test2",
id: "4801082",
filePath: "/Users/joegallo/z.sh",
name: "z.sh"
}, function(err, res) {
console.log(err, res);
});
1 change: 0 additions & 1 deletion examples/netrcAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ github.repos.uploadAsset({
id: "4801082",
filePath: "/Users/joegallo/z.sh",
name: "z.sh"

}, function(err, res) {
console.log(err, res);
});
31 changes: 21 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var error = require("./error");
var fs = require("fs");
var HttpsProxyAgent = require('https-proxy-agent');
var mime = require("mime");
var netrc = require("netrc");
var Util = require("./util");
Expand Down Expand Up @@ -618,18 +619,29 @@ var Client = module.exports = function(config) {
var hasFileBody = block.hasFileBody;
var hasBody = !hasFileBody && (typeof(msg.body) !== "undefined" || "head|get|delete".indexOf(method) === -1);
var format = getRequestFormat.call(this, hasBody, block);
var protocol = this.config.protocol || this.constants.protocol || "http";
var port = this.config.port || (protocol == "https" ? 443 : 80);
var host = block.host || this.config.host || this.constants.host;

// Edge case for github enterprise uploadAsset:
// 1) In public api, host changes to uploads.github.com. In enterprise, the host remains the same.
// 2) In enterprise, the pathPrefix changes from: /api/v3 to /api/uploads.
if ((this.config.host && this.config.host !== this.constants.host)
&& (block.host && block.host === "uploads.github.com")) { // enterprise uploadAsset
host = this.config.host;
this.config.pathPrefix = "/api/uploads";
}

var obj = getQueryAndUrl(msg, block, format, self.config);
var query = obj.query;
var url = this.config.url ? this.config.url + obj.url : obj.url;
var HttpsProxyAgent = require('https-proxy-agent');
var agent = undefined;

var path = url;
var protocol = this.config.protocol || this.constants.protocol || "http";
var host = block.host || this.config.host || this.constants.host;
var port = this.config.port || (protocol == "https" ? 443 : 80);
if (!hasBody && query.length) {
path += "?" + query.join("&");
}

var proxyUrl;
var ca = this.config.ca;
var agent = undefined;
if (this.config.proxy !== undefined) {
proxyUrl = this.config.proxy;
} else {
Expand All @@ -638,9 +650,8 @@ var Client = module.exports = function(config) {
if (proxyUrl) {
agent = new HttpsProxyAgent(proxyUrl);
}
if (!hasBody && query.length) {
path += "?" + query.join("&");
}

var ca = this.config.ca;

var headers = {
"host": host,
Expand Down

0 comments on commit b020d94

Please sign in to comment.