UNPKG

fabric-ias

Version:

Node.JS Service for Microsoft Fabric supporting infrastructure as code

93 lines (88 loc) 3.94 kB
"use strict"; const Base = require("./base"); const Operation = require("./operation"); /** * @class Git * @classdesc * Manages Git integration for a Microsoft Fabric Workspace. * Provides methods to set connections, check status, and update from Git. * Extends the Base class for authentication and API request handling. * * @extends Base * * @property {string} endpoint - The API endpoint for Git operations in the workspace. * @property {string} _workspaceHead - The current workspace head commit hash. * @property {string} _remoteCommitHash - The current remote commit hash. * @property {Array} _changes - List of changes between workspace and remote. */ class Git extends Base { /** * Constructs a Git instance for a given workspace. * @param {AzOauth} OAuthHandler - OAuth handler for authentication. * @param {string} workspace_id - The workspace ID. */ constructor(OAuthHandler, handler, workspace_id) { super(OAuthHandler, handler); this.endpoint = `${this.endpoint}/workspaces/${workspace_id}/git`; } /** * Sets the workspace Git connection using a connection ID. * @param {string} connection_id - Connection ID registered through Microsoft Fabric. * @returns {Promise<Git|Error>} Returns this instance if successful. * @throws {Error} If the connection cannot be set. */ async setConnection(connection_id) { const payload = { source: 'ConfiguredConnection', connectionId: connection_id }; const response = await this._patch('/myGitCredentials', payload, {}, function (response) { if (response.data.connectionId != connection_id) throw Error('Invalid Workspace Connection: Failed to set specified connection_id'); }); return this; } /** * Fetches the status of Git in the Fabric Workspace. * Updates internal state with workspace head, remote commit hash, and changes. * @returns {Promise<Array|Error>} Array of changes if successful. * @throws {Error} If the status cannot be retrieved. */ async status() { const response = await this._get(`${this.endpoint}/status`, {}, function (response) { if (response.data.errorCode != undefined && response.data.errorCode == 'GitCredentialsNotConfigured') throw Error('Invalid Workspace Connection: Failed to retreive Git Status'); if (response.data == undefined || response.data.changes == undefined) throw Error('Invalid Response: Payload expected to contain changes'); }); this._workspaceHead = response.data.workspaceHead; this._remoteCommitHash = response.data.remoteCommitHash; this._changes = response.data.changes; return this._changes; } /** * Updates the workspace from Git using provided options. * Initiates an update operation and returns an Operation instance. * @param {object} gitoptions - Optional settings for the update call. * @returns {Promise<Operation|Error>} Operation instance if successful. * @throws {Error} If the update cannot be initiated. */ async update(gitoptions) { const defaults = { workspaceHead: this._workspaceHead, remoteCommitHash: this._remoteCommitHash, conflictResolution: { conflictResolutionType: "Workspace", conflictResolutionPolicy: "PreferRemote" }, options: { allowOverrideItems: true } }; const payload = Object.assign({}, defaults, gitoptions); const response = await this._post(`${this.endpoint}/updateFromGit`, payload, {}, function (response) { if (response.headers.location == undefined || response.headers["retry-after"] == undefined) throw Error('Invalid Headers: Location and retry-after expected'); }); operationID = response.headers["x-ms-operation-id"]; retryAfter = response.headers["retry-after"] * 1000; return new Operation(this.OAuth, this.ErrorHandler, operationID, retryAfter); } } module.exports = Git;