Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
snehapar9 committed Sep 28, 2023
1 parent ba6abd5 commit 17ceee9
Show file tree
Hide file tree
Showing 7 changed files with 356 additions and 374 deletions.
104 changes: 57 additions & 47 deletions azurecontainerapps.ts

Large diffs are not rendered by default.

406 changes: 194 additions & 212 deletions dist/index.js

Large diffs are not rendered by default.

98 changes: 49 additions & 49 deletions src/ContainerAppHelper.ts

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions src/ContainerRegistryHelper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as os from 'os';
import { Utility } from './Utility';
import { GithubActionsToolHelper } from './GithubActionsToolHelper';
import { GitHubActionsToolHelper } from './GithubActionsToolHelper';

const githubActionsToolHelper = new GithubActionsToolHelper();
const toolHelper = new GitHubActionsToolHelper();
const util = new Utility();

export class ContainerRegistryHelper {
Expand All @@ -13,11 +13,12 @@ export class ContainerRegistryHelper {
* @param acrPassword - the password for authentication
*/
public async loginAcrWithUsernamePassword(acrName: string, acrUsername: string, acrPassword: string) {
githubActionsToolHelper.debug(`Attempting to log in to ACR instance "${acrName}" with username and password credentials`);
toolHelper.writeDebug(`Attempting to log in to ACR instance "${acrName}" with username and password credentials`);
try {
await githubActionsToolHelper.exec('docker', [`login`, `--password-stdin`, `--username`, `${acrUsername}`, `${acrName}.azurecr.io`], { input: Buffer.from(acrPassword) });
let dockerTool = await toolHelper.which("docker", true);
await util.executeAndThrowIfError(dockerTool, [`login`, `--username`, `${acrUsername}`, `--password`, `${acrPassword}`, `${acrName}.azurecr.io`], Buffer.from(acrPassword));
} catch (err) {
githubActionsToolHelper.error(`Failed to log in to ACR instance "${acrName}" with username and password credentials`);
toolHelper.writeError(`Failed to log in to ACR instance "${acrName}" with username and password credentials`);
throw err;
}
}
Expand All @@ -28,13 +29,13 @@ export class ContainerRegistryHelper {
* @param acrName - the name of the ACR instance to authenticate calls to.
*/
public async loginAcrWithAccessTokenAsync(acrName: string) {
githubActionsToolHelper.debug(`Attempting to log in to ACR instance "${acrName}" with access token`);
toolHelper.writeDebug(`Attempting to log in to ACR instance "${acrName}" with access token`);
try {
let command: string = `CA_ADO_TASK_ACR_ACCESS_TOKEN=$(az acr login --name ${acrName} --output json --expose-token --only-show-errors | jq -r '.accessToken'); docker login ${acrName}.azurecr.io -u 00000000-0000-0000-0000-000000000000 -p $CA_ADO_TASK_ACR_ACCESS_TOKEN > /dev/null 2>&1`;
let commandLine = os.platform() === 'win32' ? 'pwsh' : 'bash';
await util.executeAndThrowIfError(commandLine, ['-c', command]);
} catch (err) {
githubActionsToolHelper.error(`Failed to log in to ACR instance "${acrName}" with access token`)
toolHelper.writeError(`Failed to log in to ACR instance "${acrName}" with access token`)
throw err;
}
}
Expand All @@ -44,12 +45,12 @@ export class ContainerRegistryHelper {
* @param imageToPush - the name of the image to push to ACR
*/
public async pushImageToAcr(imageToPush: string) {
githubActionsToolHelper.debug(`Attempting to push image "${imageToPush}" to ACR`);
toolHelper.writeDebug(`Attempting to push image "${imageToPush}" to ACR`);
try {
let dockerTool: string = await githubActionsToolHelper.which("docker", true);
let dockerTool: string = await toolHelper.which("docker", true);
await util.executeAndThrowIfError(dockerTool, [`push`, `${imageToPush}`]);
} catch (err) {
githubActionsToolHelper.error(`Failed to push image "${imageToPush}" to ACR. Error: ${err.message}`);
toolHelper.writeError(`Failed to push image "${imageToPush}" to ACR. Error: ${err.message}`);
throw err;
}
}
Expand Down
48 changes: 31 additions & 17 deletions src/GithubActionsToolHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,34 @@ import * as core from '@actions/core';
import * as io from '@actions/io';
import * as exec from '@actions/exec';

export class GithubActionsToolHelper {
export class GitHubActionsToolHelper {

public getGithubRunId(): string {
public getbuildId(): string {
return process.env['GITHUB_RUN_ID'] || '';
}

public getGithubRunNumber(): string {
public getbuildNumber(): string {
return process.env['GITHUB_RUN_NUMBER'] || '';
}

public info(message: string): void {
public writeInfo(message: string): void {
core.info(message);
}

public error(message: string): void {
public writeError(message: string): void {
core.error(message);
}

public warning(message: string): void {
public writeWarning(message: string): void {
core.warning(message);
}

public debug(message: string): void {
public writeDebug(message: string): void {
core.debug(message);
}

public async exec(commandLine: string, args?: string[], execOptions?: exec.ExecOptions): Promise<number> {
return await exec.exec(commandLine, args, execOptions);
}

public async ExecOptions(): Promise<exec.ExecOptions> {
public async exec(commandLine: string, args?: string[], inputOptions?: Buffer): Promise<{ exitCode: number, stdout: string, stderr: string }> {
try{
let stdout = '';
let stderr = '';

Expand All @@ -45,8 +42,21 @@ export class GithubActionsToolHelper {
stderr += data.toString();
},
},
input: inputOptions
};
return options;

let exitCode = await exec.exec(commandLine, args, options);
return new Promise((resolve, reject) => {
let executionResult = {
exitCode: exitCode,
stdout: stdout,
stderr: stderr
}
resolve(executionResult);
});
}catch(err){
throw err;
}
}

public getInput(name: string, options?: core.InputOptions): string {
Expand All @@ -61,15 +71,19 @@ export class GithubActionsToolHelper {
return io.which(tool, check);
}

public getContainerAppName(containerAppName: string): string {
containerAppName = `gh-action-app-${this.getGithubRunId()}-${this.getGithubRunNumber()}`;
public getDefaultContainerAppName(containerAppName: string): string {
containerAppName = `gh-action-app-${this.getbuildId()}-${this.getbuildNumber()}`;
// Replace all '.' characters with '-' characters in the Container App name
containerAppName = containerAppName.replace(/\./gi, "-");
this.info(`Default Container App name: ${containerAppName}`);
return containerAppName
this.writeInfo(`Default Container App name: ${containerAppName}`);
return containerAppName;
}

public getTelemetryArg(): string {
return `CALLER_ID=github-actions-v1`;
}

public getEventName(): string {
return `ContainerAppsGitHubActionV1`;
}
}
26 changes: 9 additions & 17 deletions src/TelemetryHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Utility } from './Utility';
import { GithubActionsToolHelper } from './GithubActionsToolHelper';
import { GitHubActionsToolHelper } from './GithubActionsToolHelper';

const ORYX_CLI_IMAGE: string = "mcr.microsoft.com/oryx/cli:debian-buster-20230207.2";

Expand All @@ -11,7 +11,7 @@ const DOCKERFILE_SCENARIO: string = "used-dockerfile";
const IMAGE_SCENARIO: string = "used-image";

const util = new Utility();
const githubActionsToolHelper = new GithubActionsToolHelper();
const toolHelper = new GitHubActionsToolHelper();

export class TelemetryHelper {
readonly disableTelemetry: boolean;
Expand Down Expand Up @@ -68,7 +68,7 @@ export class TelemetryHelper {
public async sendLogs() {
let taskLengthMilliseconds: number = Date.now() - this.taskStartMilliseconds;
if (!this.disableTelemetry) {
githubActionsToolHelper.info(`Telemetry enabled; logging metadata about task result, length and scenario targeted.`);
toolHelper.writeInfo(`Telemetry enabled; logging metadata about task result, length and scenario targeted.`);
try {
let resultArg: string = '';
if (!util.isNullOrEmpty(this.result)) {
Expand All @@ -85,22 +85,14 @@ export class TelemetryHelper {
errorMessageArg = `--property errorMessage=${this.errorMessage}`;
}

let args: string[] = [`run`, `--rm`, `${ORYX_CLI_IMAGE}`, `/bin/bash`, `-c`, `oryx telemetry --event-name ContainerAppsGitHubActionV1 ` + `--processing-time ${taskLengthMilliseconds} ${resultArg} ${scenarioArg} ${errorMessageArg}`];
await executeDockerCommand(args, true)
let eventName = toolHelper.getEventName();
let args: string[] = [`run`, `--rm`, `${ORYX_CLI_IMAGE}`, `/bin/bash`, `-c`, `oryx telemetry --event-name ${eventName} ` + `--processing-time ${taskLengthMilliseconds} ${resultArg} ${scenarioArg} ${errorMessageArg}`];
let dockerTool: string = await toolHelper.which("docker", true);

await toolHelper.exec(dockerTool, args);
} catch (err) {
githubActionsToolHelper.warning(`Skipping telemetry logging due to the following exception: ${err.message}`);
toolHelper.writeWarning(`Skipping telemetry logging due to the following exception: ${err.message}`);
}
}
}
}

const executeDockerCommand = async (args: string[], continueOnError: boolean = false): Promise<void> => {
try {
let dockerTool: string = await githubActionsToolHelper.which("docker", true);
await util.executeAndThrowIfError(dockerTool, args, continueOnError);
}
catch (err) {
githubActionsToolHelper.error(`Error: ${err.message}`);
throw err; // Re-throw the error
}
}
27 changes: 5 additions & 22 deletions src/Utility.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Note: This file is used to define utility functions that can be used across the project.
import { GithubActionsToolHelper } from './GithubActionsToolHelper';
import { GitHubActionsToolHelper } from './GithubActionsToolHelper';

const githubActionsToolHelper = new GithubActionsToolHelper();
const toolHelper = new GitHubActionsToolHelper();

export class Utility {
/**
Expand All @@ -10,28 +10,11 @@ export class Utility {
* @param continueOnError - whether or not to continue execution if the command fails
*/

public async executeAndThrowIfError(commandLine: string, args: string[], continueOnError: boolean = false): Promise<{ exitCode: number, stdout: string, stderr: string }> {
public async executeAndThrowIfError(commandLine: string, args: string[], inputOptions?:Buffer): Promise<{ exitCode: number, stdout: string, stderr: string }> {
try {
let options = await githubActionsToolHelper.ExecOptions();
let stderr = options.listeners.stderr.toString();
let stdout = options.listeners.stdout.toString();

let exitCode = await githubActionsToolHelper.exec(commandLine, args, options);

if (!continueOnError && exitCode !== 0) {
githubActionsToolHelper.error(`Command failed with exit code ${exitCode}. Error stream: ${stderr}`);
throw new Error(`Command failed with exit code ${exitCode}. Error stream: ${stderr}`);
}
return new Promise((resolve, reject) => {
let executionResult = {
exitCode: exitCode,
stdout: stdout,
stderr: stderr
}
resolve(executionResult);
});
return await toolHelper.exec(commandLine, args, inputOptions);
} catch (error) {
githubActionsToolHelper.error(`Error: ${error.message}`);
toolHelper.writeError(`Error: ${error.message}`);
throw error; // Re-throw the error
}
}
Expand Down

0 comments on commit 17ceee9

Please sign in to comment.