|
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 4 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 5 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 6 | + function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } |
| 7 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 8 | + }); |
| 9 | +}; |
| 10 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 11 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 12 | +}; |
| 13 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 14 | +const fs_1 = __importDefault(require("fs")); |
| 15 | +const path_1 = __importDefault(require("path")); |
| 16 | +const misc_1 = require("./misc"); |
| 17 | +exports.filesToBlobs = (files, octokit, context) => __awaiter(this, void 0, void 0, function* () { return yield Promise.all(Object.values(files).map(file => createBlob(file, octokit, context))); }); |
| 18 | +exports.createTree = (blobs, octokit, context) => __awaiter(this, void 0, void 0, function* () { |
| 19 | + return yield octokit.git.createTree({ |
| 20 | + owner: context.repo.owner, |
| 21 | + repo: context.repo.repo, |
| 22 | + base_tree: (yield getCommit(octokit, context)).data.tree.sha, |
| 23 | + tree: Object.values(blobs).map(blob => ({ |
| 24 | + path: blob.path, |
| 25 | + type: 'blob', |
| 26 | + mode: '100644', |
| 27 | + sha: blob.sha, |
| 28 | + })), |
| 29 | + }); |
| 30 | +}); |
| 31 | +exports.createCommit = (tree, octokit, context) => __awaiter(this, void 0, void 0, function* () { |
| 32 | + return yield octokit.git.createCommit({ |
| 33 | + owner: context.repo.owner, |
| 34 | + repo: context.repo.repo, |
| 35 | + tree: tree.data.sha, |
| 36 | + parents: [context.sha], |
| 37 | + message: misc_1.getCommitMessage(), |
| 38 | + }); |
| 39 | +}); |
| 40 | +exports.updateRef = (commit, name, octokit, context) => __awaiter(this, void 0, void 0, function* () { |
| 41 | + if (!(yield existsRef(name, octokit, context))) { |
| 42 | + yield createRef(name, octokit, context); |
| 43 | + } |
| 44 | + yield octokit.git.updateRef({ |
| 45 | + owner: context.repo.owner, |
| 46 | + repo: context.repo.repo, |
| 47 | + ref: getRef(name), |
| 48 | + sha: commit.data.sha, |
| 49 | + }); |
| 50 | +}); |
| 51 | +const getCommit = (octokit, context) => __awaiter(this, void 0, void 0, function* () { |
| 52 | + return yield octokit.git.getCommit({ |
| 53 | + owner: context.repo.owner, |
| 54 | + repo: context.repo.repo, |
| 55 | + commit_sha: context.sha, |
| 56 | + }); |
| 57 | +}); |
| 58 | +const existsRef = (name, octokit, context) => { |
| 59 | + return new Promise(resolve => { |
| 60 | + octokit.git.getRef({ |
| 61 | + owner: context.repo.owner, |
| 62 | + repo: context.repo.repo, |
| 63 | + ref: getRef(name), |
| 64 | + }).then(() => { |
| 65 | + resolve(true); |
| 66 | + }).catch(() => { |
| 67 | + resolve(false); |
| 68 | + }); |
| 69 | + }); |
| 70 | +}; |
| 71 | +const getRef = (name) => `refs/heads/${name}`; |
| 72 | +const createBlob = (filePath, octokit, context) => __awaiter(this, void 0, void 0, function* () { |
| 73 | + const file = path_1.default.resolve(misc_1.getWorkspace(), filePath); |
| 74 | + const isExists = fs_1.default.existsSync(file); |
| 75 | + const blob = yield octokit.git.createBlob({ |
| 76 | + owner: context.repo.owner, |
| 77 | + repo: context.repo.repo, |
| 78 | + content: isExists ? new Buffer(fs_1.default.readFileSync(file)).toString('base64') : '', |
| 79 | + encoding: 'base64', |
| 80 | + }); |
| 81 | + return ({ path: filePath, sha: blob.data.sha }); |
| 82 | +}); |
| 83 | +const createRef = (name, octokit, context) => __awaiter(this, void 0, void 0, function* () { |
| 84 | + yield octokit.git.createRef({ |
| 85 | + owner: context.repo.owner, |
| 86 | + repo: context.repo.repo, |
| 87 | + ref: getRef(name), |
| 88 | + sha: context.sha, |
| 89 | + }); |
| 90 | +}); |
0 commit comments