Skip to content

Commit

Permalink
fix: github.context not available in Docker action
Browse files Browse the repository at this point in the history
  • Loading branch information
cybersokari committed Dec 28, 2024
1 parent 4583ef3 commit 4148832
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 40 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ on:
prefix:
description: 'The storage bucket path to back up Allure results and history files'
required: false
slack_channel:
description: 'Slack channel ID'
required: false

jobs:
test-docker:
Expand Down Expand Up @@ -89,13 +86,15 @@ jobs:
- name: Allure Deployer Action
uses: ./apps/action
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
SLACK_TOKEN: ${{secrets.SLACK_TOKEN}}
GOOGLE_CREDENTIALS_JSON: ${{ secrets.GCP_CREDENTIALS}}
GOOGLE_CREDENTIALS_JSON: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS}}
with:
allure_results_path: 'assets/allure-results'
storage_bucket: ${{github.event.inputs.storage-bucket}}
report_name: ${{ github.event.inputs.report-name }}
slack_channel: ${{github.event.inputs.slack_channel}}
slack_channel: ${{secrets.SLACK_CHANNEL}}
show_retries: ${{github.event.inputs.show-retries}}
show_history: ${{github.event.inputs.show-history}}
prefix: ${{github.event.inputs.prefix}}
prefix: ${{github.event.inputs.prefix}}
update_pr: 'comment'
26 changes: 13 additions & 13 deletions apps/cli/src/commands/deploy.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import fs from "fs/promises";
import path from "node:path";
import {KEY_BUCKET, KEY_PROJECT_ID, KEY_SLACK_CHANNEL, KEY_SLACK_TOKEN} from "../utilities/constants.js";
import chalk from "chalk";
import {ArgsInterface, GitHubPRUpdateType} from "../interfaces/args.interface.js";
import {ArgsInterface} from "../interfaces/args.interface.js";

const ERROR_MESSAGES = {
EMPTY_RESULTS: "Error: The specified results directory is empty.",
NO_RESULTS_DIR: "Error: No Allure result files in the specified directory.",
MISSING_CREDENTIALS: "Error: Firebase/GCP credentials must be set using 'gcp-json:set' or provided via '--gcp-json'.",
MISSING_BUCKET: "Error: A Firebase/GCP bucket must be set using 'bucket:set' or provided via '--bucket'.",
MISSING_BUCKET: "Storage bucket not provided. History and Retries will not be available in report.",
INVALID_SLACK_CRED: `Invalid Slack credential. ${chalk.blue('slack_channel')} and ${chalk.blue('slack_token')} must be provided together`,
NO_JAVA: 'Error: JAVA_HOME not found. Allure 2.32 requires JAVA installed'
};
Expand Down Expand Up @@ -48,7 +48,7 @@ async function getFirebaseCredentials(gcpJson: string | undefined): Promise<stri
function validateBucket(options: any): void {
if (!options.bucket && !db.get(KEY_BUCKET)) {
if (options.showRetries || options.showHistory) {
throw new Error(ERROR_MESSAGES.MISSING_BUCKET);
console.warn(ERROR_MESSAGES.MISSING_BUCKET)
}
}
}
Expand All @@ -69,6 +69,14 @@ function getGitHubBuildUrl(): string|undefined {
return undefined
}

function validateUpdatePR(value: string): string {
if(value === 'comment' || value === 'summary') {
return value
}
console.warn(`Invalid value "${value}" for --update-pr. Falling back to "summary".`);
return 'summary'
}

export function addDeployCommand(defaultProgram: Command, onCommand: (args: ArgsInterface) => Promise<void>): Command {
return defaultProgram
.command("deploy")
Expand All @@ -82,15 +90,7 @@ export function addDeployCommand(defaultProgram: Command, onCommand: (args: Args
.addOption(new Option("-st, --slack-token <token>","Slack token"))
.addOption(new Option("-p, --prefix <prefix>", "The storage bucket path to back up Allure results and history files"))
.addOption(new Option("--update-pr <type>", "Update pull request with report url and info")
.default(GitHubPRUpdateType.summary.toString(), 'summary/comment').hideHelp()
.argParser((value)=> {
if(Object.values(GitHubPRUpdateType).includes(value)){
return value
}
// Fallback to default if value is invalid
console.warn(`Invalid value "${value}" for --update-pr. Falling back to "summary".`);
return 'summary'
}))
.default('comment', 'summary/comment').hideHelp().argParser(validateUpdatePR))
.action(async (resultPath, reportName, options) => {
try {
if(!isJavaInstalled()){
Expand Down Expand Up @@ -125,7 +125,7 @@ export function addDeployCommand(defaultProgram: Command, onCommand: (args: Args
slack_channel: options.slackChannel || db.get(KEY_SLACK_CHANNEL, undefined),
slack_token: options.slackToken || db.get(KEY_SLACK_TOKEN, undefined),
buildUrl: getGitHubBuildUrl(),
updatePr: options.updatePr as GitHubPRUpdateType
updatePr: options.updatePr
};

await onCommand(cliArgs);
Expand Down
8 changes: 4 additions & 4 deletions apps/cli/src/features/hosting/firebase-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {ArgsInterface} from "../../interfaces/args.interface.js";
import path from "node:path";
import {generate} from "random-words";
import {StringBuilder} from "../../utilities/string-builder.js";
import {FirebaseService} from "../../services/firebase.service.js";
import {FirebaseInterface} from "../../interfaces/firebase.interface.js";

// Max allowed Firebase sites to prevent exceeding quota
// https://firebase.google.com/docs/hosting/multisites
Expand All @@ -20,13 +20,13 @@ export class FirebaseHost implements HostingProvider {
private hostedSiteUrl: string | undefined;
private configPath?: string;
private readonly newSiteId: string;
private readonly service: FirebaseService;
private readonly service: FirebaseInterface;

// Initialize class properties from input arguments
constructor(readonly args: ArgsInterface, service?: FirebaseService) {
constructor(readonly args: ArgsInterface, service: FirebaseInterface) {
this.reportDir = this.args.REPORTS_DIR;
this.newSiteId = this.getSiteId();
this.service = service ?? new FirebaseService(this.args.firebaseProjectId);
this.service = service
}

// Generates a unique site ID using random words and timestamps
Expand Down
11 changes: 4 additions & 7 deletions apps/cli/src/features/messaging/github-notifier.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Notifier} from "../../interfaces/notifier.interface.js";
import {NotificationData} from "../../models/notification.model.js";
import {ArgsInterface, GitHubPRUpdateType} from "../../interfaces/args.interface.js";
import {ArgsInterface} from "../../interfaces/args.interface.js";
import {GithubInterface} from "../../interfaces/github.interface.js";

export class GitHubNotifier implements Notifier {
Expand Down Expand Up @@ -32,15 +32,12 @@ export class GitHubNotifier implements Notifier {
const promises: Promise<void>[] = [];
promises.push(this.client.updateOutput(`report_url=${data.reportUrl}`))

if(this.args.updatePr === GitHubPRUpdateType.comment){
const githubToken = process.env.GITHUB_TOKEN;
if (githubToken) {
promises.push(this.client.updatePr({message: markdown, token: githubToken}))
}
const githubToken = process.env.GITHUB_TOKEN;
if(githubToken && this.args.updatePr === 'comment'){
promises.push(this.client.updatePr({message: markdown, token: githubToken}))
} else {
promises.push(this.client.updateSummary(markdown.trim()))
}

await Promise.all(promises)
}
}
6 changes: 1 addition & 5 deletions apps/cli/src/interfaces/args.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,5 @@ export interface ArgsInterface {
slack_channel?: string
slack_token?: string
buildUrl?: string;
updatePr?: GitHubPRUpdateType;
}
export enum GitHubPRUpdateType {
comment,
summary
updatePr?: string;
}
6 changes: 6 additions & 0 deletions apps/cli/src/interfaces/firebase.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface FirebaseInterface {
deleteSite({siteId, configPath}:{siteId: string, configPath: string}): Promise<void>
deployHosting(configPath: string): Promise<void>
createSite(siteId: string): Promise<any>
listSites(): Promise<any[]>
}
4 changes: 3 additions & 1 deletion apps/cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {Storage as GCPStorage} from '@google-cloud/storage'
import {readJsonFile} from "./utilities/file-util.js";
import {ResultsStatus} from "./interfaces/counter.interface.js";
import {GitHubService} from "./services/github.service.js";
import {FirebaseService} from "./services/firebase.service.js";

// Entry point for the application
export function main() {
Expand Down Expand Up @@ -49,7 +50,8 @@ function setupCommands(program: Command) {
// Executes the deployment process
async function runDeploy(args: ArgsInterface) {
const allure = new Allure({args});
const firebaseHost = new FirebaseHost(args);
const firebaseService = new FirebaseService(args.firebaseProjectId);
const firebaseHost = new FirebaseHost(args, firebaseService);
try {
const cloudStorage = await initializeCloudStorage(args); // Initialize storage bucket
const [reportUrl, resultsStatus] = await setupStaging(firebaseHost, cloudStorage, allure, args);
Expand Down
3 changes: 2 additions & 1 deletion apps/cli/src/services/firebase.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// @ts-ignore
import firebase from 'firebase-tools'
import {FirebaseInterface} from "../interfaces/firebase.interface.js";

export class FirebaseService {
export class FirebaseService implements FirebaseInterface{
constructor(private readonly projectId: string) {}

/**
Expand Down
6 changes: 3 additions & 3 deletions apps/cli/src/services/github.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {GithubInterface} from "../interfaces/github.interface.js";
import fs from "fs/promises";
import github from "@actions/github";
import process from "node:process";

export class GitHubService implements GithubInterface {
outputPath: string;
Expand All @@ -20,8 +21,9 @@ export class GitHubService implements GithubInterface {
}

async updatePr({message, token}: { message: string, token: string }): Promise<void> {
const {owner, repo} = github.context.repo

try {
const [owner, repo] = process.env.GITHUB_REPOSITORY!.split('/')
const pr = github.context.payload.pull_request!
const octokit = github.getOctokit(token)
// Update the PR body
Expand All @@ -34,8 +36,6 @@ export class GitHubService implements GithubInterface {
console.log(`Pull Request #${pr.number} updated successfully!`);
} catch (e) {
console.warn('Failed to update PR:', e);
console.log(`Repository Owner: ${owner}`);
console.log(`Repository Name: ${repo}`);
}
}

Expand Down

0 comments on commit 4148832

Please sign in to comment.