Skip to content

Commit

Permalink
fix: update build
Browse files Browse the repository at this point in the history
  • Loading branch information
weilinzung committed Nov 30, 2024
1 parent 7a71d30 commit 6ad403c
Showing 1 changed file with 133 additions and 33 deletions.
166 changes: 133 additions & 33 deletions bin/action.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -92916,6 +92916,49 @@ exports.getExecOutput = getExecOutput;

});

/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function getChannelId(configuredChannelId, ghContext) {
let tmpChannelId = "";
if (!!configuredChannelId) {
tmpChannelId = configuredChannelId;
} else if (ghContext.payload.pull_request) {
const branchName = ghContext.payload.pull_request.head.ref.substr(0, 20);
tmpChannelId = `pr${ghContext.payload.pull_request.number}-${branchName}`;
}
// Channel IDs can only include letters, numbers, underscores, hyphens, and periods.
const invalidCharactersRegex = /[^a-zA-Z0-9_\-\.]/g;
const correctedChannelId = tmpChannelId.replace(invalidCharactersRegex, "_");
if (correctedChannelId !== tmpChannelId) {
console.log(`ChannelId "${tmpChannelId}" contains unsupported characters. Using "${correctedChannelId}" instead.`);
}
return correctedChannelId;
}
/**
* Extracts the channel ID from the channel name
* @param channelName
* @returns channelId
* Example channelName: projects/my-project/sites/test-staging/channels/pr123-my-branch
*/
function extractChannelIdFromChannelName(channelName) {
const parts = channelName.split("/");
const channelIndex = parts.indexOf("channels") + 1; // The part after "channels"
return parts[channelIndex]; // Returns the channel name after "channels/"
}

/**
* Copyright 2020 Google LLC
*
Expand Down Expand Up @@ -92976,7 +93019,75 @@ async function execWithCredentials(args, projectId, gacFilename, opts) {
}
return deployOutputBuf.length ? deployOutputBuf[deployOutputBuf.length - 1].toString("utf-8") : ""; // output from the CLI
}

async function getAllChannels(gacFilename, deployConfig) {
const {
projectId,
target,
firebaseToolsVersion
} = deployConfig;
const allChannelsText = await execWithCredentials(["hosting:channel:list", ...(target ? ["--site", target] : [])], projectId, gacFilename, {
firebaseToolsVersion
});
const channelResults = JSON.parse(allChannelsText.trim());
if (channelResults.status === "error") {
throw Error(channelResults.error);
} else {
return channelResults.channels || [];
}
}
function getPreviewChannelToRemove(channels, totalPreviewChannelLimit) {
// Filter out live channel(hosting default site) and channels without an expireTime(additional sites)
const previewChannelsOnly = channels.filter(channel => {
var _channel$labels;
return (channel == null || (_channel$labels = channel.labels) == null ? void 0 : _channel$labels.type) !== "live" && !!(channel != null && channel.expireTime);
});
if (previewChannelsOnly.length) {
// Sort preview channels by expireTime
const sortedPreviewChannels = previewChannelsOnly.sort((channelA, channelB) => {
return new Date(channelA.expireTime).getTime() - new Date(channelB.expireTime).getTime();
});
// If the total number of preview channels exceeds the limit, return the ones with earlier expireTime
if (sortedPreviewChannels.length > totalPreviewChannelLimit) {
return sortedPreviewChannels.slice(0, sortedPreviewChannels.length - totalPreviewChannelLimit);
}
} else {
return [];
}
}
/**
* Removes preview channels from the list of active channels if the number exceeds the configured limit
*
* This function identifies the preview channels that need to be removed based on the total limit of
* preview channels allowed (`totalPreviewChannelLimit`).
*
* It then attempts to remove those channels using the `removeChannel` function.
* Errors encountered while removing channels are logged but do not stop the execution of removing other channels.
*/
async function removePreviews({
channels,
gacFilename,
deployConfig
}) {
const toRemove = getPreviewChannelToRemove(channels, deployConfig.totalPreviewChannelLimit);
if (toRemove.length) {
await Promise.all(toRemove.map(async channel => {
try {
await removeChannel(gacFilename, deployConfig, extractChannelIdFromChannelName(channel.name));
} catch (error) {
console.error(`Error removing preview channel ${channel.name}:`, error);
}
}));
}
}
function removeChannel(gacFilename, deployConfig, channelId) {
const {
projectId,
firebaseToolsVersion
} = deployConfig;
return execWithCredentials(["hosting:channel:delete", channelId, "--force"], projectId, gacFilename, {
firebaseToolsVersion
});
}
async function deployPreview(gacFilename, deployConfig) {
const {
projectId,
Expand Down Expand Up @@ -93004,38 +93115,6 @@ async function deployProductionSite(gacFilename, productionDeployConfig) {
return deploymentResult;
}

/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function getChannelId(configuredChannelId, ghContext) {
let tmpChannelId = "";
if (!!configuredChannelId) {
tmpChannelId = configuredChannelId;
} else if (ghContext.payload.pull_request) {
const branchName = ghContext.payload.pull_request.head.ref.substr(0, 20);
tmpChannelId = `pr${ghContext.payload.pull_request.number}-${branchName}`;
}
// Channel IDs can only include letters, numbers, underscores, hyphens, and periods.
const invalidCharactersRegex = /[^a-zA-Z0-9_\-\.]/g;
const correctedChannelId = tmpChannelId.replace(invalidCharactersRegex, "_");
if (correctedChannelId !== tmpChannelId) {
console.log(`ChannelId "${tmpChannelId}" contains unsupported characters. Using "${correctedChannelId}" instead.`);
}
return correctedChannelId;
}

/**
* Copyright 2020 Google LLC
*
Expand Down Expand Up @@ -93182,6 +93261,7 @@ const entryPoint = core.getInput("entryPoint");
const target = core.getInput("target");
const firebaseToolsVersion = core.getInput("firebaseToolsVersion");
const disableComment = core.getInput("disableComment");
const totalPreviewChannelLimit = Number(core.getInput("totalPreviewChannelLimit") || "0");
async function run() {
const isPullRequest = !!github.context.payload.pull_request;
let finish = details => console.log(details);
Expand Down Expand Up @@ -93232,6 +93312,26 @@ async function run() {
return;
}
const channelId = getChannelId(configuredChannelId, github.context);
if (totalPreviewChannelLimit) {
core.startGroup(`Start counting total Firebase preview channel ${channelId}`);
const allChannels = await getAllChannels(gacFilename, {
projectId,
target,
firebaseToolsVersion,
totalPreviewChannelLimit
});
if (allChannels.length) {
await removePreviews({
channels: allChannels,
gacFilename,
deployConfig: {
projectId,
target,
firebaseToolsVersion
}
});
}
}
core.startGroup(`Deploying to Firebase preview channel ${channelId}`);
const deployment = await deployPreview(gacFilename, {
projectId,
Expand Down

0 comments on commit 6ad403c

Please sign in to comment.