Skip to content

Commit

Permalink
Merge branch 'main' into RHTAP-3026
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmid1 authored Oct 15, 2024
2 parents 765bf7c + 2ad01d8 commit c45f3e5
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 141 deletions.
57 changes: 18 additions & 39 deletions integration-tests/scripts/rhtap-e2e-runner.sh
Original file line number Diff line number Diff line change
@@ -1,33 +1,10 @@
#!/bin/sh

set -e

#JOB_SPEC env:
# {
# "container_image": "quay.io/rhtap/rhtap-cli@sha256:d2c1a65eda860ff667b30bffde2ec325c1ea7375ae2a68d4defd7aedbd0effdf",
# "konflux_component": "rhtap-cli",
# "git": {
# "pull_request_number": 195,
# "pull_request_author": "flacatus",
# "git_org": "redhat-appstudio",
# "git_repo": "rhtap-cli",
# "commit_sha": "ff9aacf8c902f1d6a5004e258522771d48f2b629",
# "event_type": "pull_request",
# "source_repo_url": "https://github.com/flacatus/rhtap-cli",
# "source_repo_branch": "fix_pr"
# }
# }
# Check if the JOB_SPEC environment variable is set.
if [ -z "${JOB_SPEC}" ]; then
echo "Error: JOB_SPEC environment variable must be set."
exit 1
fi
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail

# Important variables to start tests
export ARTIFACT_DIR="${ARTIFACT_DIR:-$(mktemp -d)}"
export GIT_REPO="${GIT_REPO:-$(echo "$JOB_SPEC" | jq -r '.git.git_repo')}"
export GIT_REVISION="${GIT_REVISION:-$(echo "$JOB_SPEC" | jq -r '.git.commit_sha')}"
export GIT_URL="${GIT_URL:-$(echo "$JOB_SPEC" | jq -r '.git.source_repo_url')}"

# Load secrets from files
export GITLAB_TOKEN="$(cat /usr/local/rhtap-cli-install/gitlab_token)"
Expand All @@ -38,7 +15,15 @@ export OCI_STORAGE_USERNAME="$(jq -r '."quay-username"' /usr/local/konflux-test-
export APPLICATION_ROOT_NAMESPACE="rhtap-app"
export GITHUB_ORGANIZATION="rhtap-rhdh-qe"
export GITLAB_ORGANIZATION="rhtap-qe"
export QUAY_IMAGE_ORG="rhtap"

#TODO: This is a temporary workaround. We need to find a way to get the git repository name from the pipeline
#when the pipeline is triggered from the rhtap-cli repository, which is a full installation of the rhtap-cli, the image should be pushed to the rhtap organization
#otherwise, the image should be pushed to the rhtap_qe organization
if [ "$GIT_REPO" = "rhtap-cli" ]; then
export QUAY_IMAGE_ORG="rhtap"
else
export QUAY_IMAGE_ORG="rhtap_qe"
fi
export IMAGE_REGISTRY="$(kubectl -n rhtap-quay get route rhtap-quay-quay -o 'jsonpath={.spec.host}')"
export OCI_CONTAINER="${OCI_CONTAINER:-""}"
export RED_HAT_DEVELOPER_HUB_URL="https://$(kubectl get route backstage-developer-hub -n rhtap -o jsonpath='{.spec.host}')"
Expand Down Expand Up @@ -75,15 +60,9 @@ post_actions() {

trap post_actions EXIT

cd "$(mktemp -d)"

if [[ "${GIT_REPO}" = "rhtap-e2e" ]]; then
echo -e "INFO: Cloning repository '$GIT_REPO' with revision '$GIT_REVISION' from URL '$GIT_URL'"
git clone "${GIT_URL}" .
git checkout "${GIT_REVISION}"
## This is a temporary workaround, when the pipeline is triggered from the rhtap-cli repository, it just runs the quarkus tests
if [ "$GIT_REPO" = "rhtap-cli" ]; then
yarn && yarn test tests/gpts/github/quarkus.tekton.test.ts
else
echo -e "INFO: Cloning repository 'redhat-appstudio/rhtap-e2e' with revision 'main'"
git clone https://github.com/redhat-appstudio/rhtap-e2e.git .
fi

yarn && yarn test tests/gpts/github/quarkus.tekton.test.ts
yarn && yarn test
fi
26 changes: 9 additions & 17 deletions integration-tests/tasks/rhtap-e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,16 @@ spec:
fi
if [ "$GIT_REPO" = "rhtap-e2e" ]; then
export SOURCE_REPO_URL="$(echo "$JOB_SPEC" | jq -r '.git.source_repo_url')"
export SOURCE_REPO_BRANCH="$(echo "$JOB_SPEC" | jq -r '.git.source_repo_branch')"
GIT_REVISION="${GIT_REVISION:-$(echo "$JOB_SPEC" | jq -r '.git.commit_sha')}"
GIT_URL="${GIT_URL:-$(echo "$JOB_SPEC" | jq -r '.git.source_repo_url')}"
echo "[INFO] SOURCE_REPO_URL is set to $SOURCE_REPO_URL"
echo "[INFO] SOURCE_REPO_BRANCH is set to $SOURCE_REPO_BRANCH"
echo -e "INFO: Cloning repository '$GIT_REPO' with revision '$GIT_REVISION' from URL '$GIT_URL'"
git clone "${GIT_URL}" .
git checkout "${GIT_REVISION}"
if [ -z "$SOURCE_REPO_URL" ] || [ -z "$SOURCE_REPO_BRANCH" ]; then
echo "[ERROR] SOURCE_REPO_URL or SOURCE_REPO_BRANCH is not set"
exit 1
fi
echo -e "[INFO] Running e2e tests from source: $SOURCE_REPO_URL and branch $SOURCE_REPO_BRANCH"
curl -fsSL https://raw.githubusercontent.com/$SOURCE_REPO_URL/$SOURCE_REPO_BRANCH/integration-tests/scripts/rhtap-e2e-runner.sh -o rhtap-e2e-runner.sh
chmod +x rhtap-e2e-runner.sh
./rhtap-e2e-runner.sh
./integration-tests/scripts/rhtap-e2e-runner.sh
else
echo -e "[INFO] Running e2e tests from: redhat-appstudio/rhtap-e2e and branch main"
curl -fsSL https://raw.githubusercontent.com/redhat-appstudio/rhtap-e2e/main/integration-tests/scripts/rhtap-e2e-runner.sh -o rhtap-e2e-runner.sh
chmod +x rhtap-e2e-runner.sh
./rhtap-e2e-runner.sh
echo -e "INFO: Cloning repository 'redhat-appstudio/rhtap-e2e' with revision 'main'"
git clone https://github.com/redhat-appstudio/rhtap-e2e.git .
./integration-tests/scripts/rhtap-e2e-runner.sh
fi
10 changes: 5 additions & 5 deletions src/apis/backstage/developer-hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import * as https from 'https';
* A client for interacting with the Red Hat Developer Hub backend.
*/
export class DeveloperHubClient extends Utils {
private RHDHUrl: string;
private axiosInstance : Axios;
private readonly RHDHUrl: string;
private readonly axiosInstance : Axios;

/**
* Constructs a new instance of DeveloperHubClient.
*
* @throws {Error} Throws an error if the 'RED_HAT_DEVELOPER_HUB_URL' environment variable is not set.
*/
constructor() {
constructor(developerHubUrl: string) {
super();

if (!process.env.RED_HAT_DEVELOPER_HUB_URL) {
if (!developerHubUrl) {
throw new Error("Cannot initialize DeveloperHubClient, missing 'RED_HAT_DEVELOPER_HUB_URL' environment variable");
}

this.RHDHUrl = process.env.RED_HAT_DEVELOPER_HUB_URL;
this.RHDHUrl = developerHubUrl;
this.axiosInstance = axios.create({
httpAgent: new https.Agent({
rejectUnauthorized: false
Expand Down
60 changes: 37 additions & 23 deletions src/apis/ci/jenkins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,37 @@ import { Utils } from '../git-providers/utils';

export class JenkinsCI extends Utils {
// Jenkins server details
private JENKINS_URL: string;
private JENKINS_USER: string;
private JENKINS_API_TOKEN: string;
private axiosInstance: Axios;
private readonly jenkinsUrl: string;
private readonly jenkinsUsername: string;
private readonly jenkinsApiToken: string;
private readonly axiosInstance: Axios;

/**
* Constructs a new instance of DeveloperHubClient.
*
* @throws {Error} Throws an error if the 'JENKINS_URL' environment variable is not set.
*/
constructor() {
constructor(jenkinsURL: string, jenkinsUsername: string, jenkinsToken: string) {
super();

if (!process.env.JENKINS_URL) {
throw new Error("Cannot initialize DeveloperHubClient, missing 'JENKINS_URL' environment variable");
}

this.JENKINS_URL = process.env.JENKINS_URL;
this.JENKINS_USER = process.env.JENKINS_USERNAME!;
this.JENKINS_API_TOKEN = process.env.JENKINS_TOKEN!;
this.jenkinsUrl = jenkinsURL;
this.jenkinsUsername = jenkinsUsername;
this.jenkinsApiToken = jenkinsToken;

this.axiosInstance = axios.create({
baseURL: this.JENKINS_URL,
baseURL: this.jenkinsUrl,
headers: {
"Content-Type": "application/xml",
},
auth: {
username: this.JENKINS_USER,
password: this.JENKINS_API_TOKEN,
username: this.jenkinsUsername,
password: this.jenkinsApiToken,
},
});
}

// createJenkinsJob creates a new Jenkins job
public async createJenkinsJob(gitProvider: string, organization: string, jobName: string) {
const url = `${this.JENKINS_URL}/createItem?name=${jobName}`;
const url = `${this.jenkinsUrl}/createItem?name=${jobName}`;
const jobConfigXml = `
<flow-definition plugin="[email protected]">
<actions/>
Expand Down Expand Up @@ -87,7 +82,7 @@ export class JenkinsCI extends Utils {

// jobExists checks if a job exists
public async jobExists(jobName: string): Promise<boolean> {
const url = `${this.JENKINS_URL}/job/${jobName}/api/json`;
const url = `${this.jenkinsUrl}/job/${jobName}/api/json`;
try {
const response = await this.axiosInstance.post(url);
if (response.status === 200) {
Expand Down Expand Up @@ -121,7 +116,7 @@ export class JenkinsCI extends Utils {

// buildJenkinsJob triggers a build for a Jenkins job
public async buildJenkinsJob(jobName: string): Promise<string | null> {
const url = `${this.JENKINS_URL}/job/${jobName}/build`;
const url = `${this.jenkinsUrl}/job/${jobName}/build`;
try {
const response = await this.axiosInstance.post(url);
if (response.status === 201) {
Expand All @@ -139,7 +134,7 @@ export class JenkinsCI extends Utils {

// getBuildNumber gets the build number from the queue item URL
public async getBuildNumber(queueItemUrl: string): Promise<number | null> {
const url = `${this.JENKINS_URL}${queueItemUrl}api/json`;
const url = `${this.jenkinsUrl}${queueItemUrl}api/json`;
try {
const response = await this.axiosInstance.post(url);
if (response.data.executable) {
Expand All @@ -158,7 +153,7 @@ export class JenkinsCI extends Utils {

// waitForBuildToFinish waits for a build to finish and get its result
public async waitForBuildToFinish(jobName: string, buildNumber: number) {
const url = `${this.JENKINS_URL}/job/${jobName}/${buildNumber}/api/json`;
const url = `${this.jenkinsUrl}/job/${jobName}/${buildNumber}/api/json`;

while (true) {
try {
Expand All @@ -179,7 +174,7 @@ export class JenkinsCI extends Utils {

// getLatestBuildNumber gets the latest build number for a Jenkins job
public async getLatestBuildNumber(jobName: string): Promise<number | null> {
const url = `${this.JENKINS_URL}/job/${jobName}/api/json?tree=lastBuild[number]`;
const url = `${this.jenkinsUrl}/job/${jobName}/api/json?tree=lastBuild[number]`;

try {
const response = await this.axiosInstance.post(url);
Expand All @@ -196,4 +191,23 @@ export class JenkinsCI extends Utils {
return null;
}
}
}

public async deleteJenkinsJob(jobName: string) {
const url = `${this.jenkinsUrl}/job/${jobName}/doDelete`;

try {
const response = await this.axiosInstance.post(url);

if (response.status === 200) {
console.log(`Job '${jobName}' deleted successfully.`);
} else {
console.error(`Failed to delete job. Status: ${response.status}`);
}
} catch (error) {
console.error('Error deleting job:', error);
}
}



}
4 changes: 2 additions & 2 deletions src/apis/git-providers/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { generateRandomChars } from "../../utils/generator";
export class GitHubProvider extends Utils {
private readonly octokit: Octokit

constructor() {
constructor(githubToken: string) {
super()

this.octokit = new Octokit({
baseUrl: 'https://api.github.com',
userAgent: 'rhtap-e2e',
auth: process.env.GITHUB_TOKEN,
auth: githubToken,
})
}

Expand Down
9 changes: 4 additions & 5 deletions src/apis/git-providers/gitlab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ export class GitLabProvider extends Utils {
private readonly gitlab;
private readonly extractImagePatternFromGitopsManifest;

constructor() {
constructor(gitlabToken: string) {
super()

if (!process.env.GITLAB_TOKEN) {
throw new Error("missed environment GITLAB_TOKEN");

if (!gitlabToken) {
throw new Error("Missing environment variable GITLAB_TOKEN");
}

this.extractImagePatternFromGitopsManifest = /- image: (.*)/;
this.gitlab = new Gitlab({
host: 'https://gitlab.com',
token: process.env.GITLAB_TOKEN
token: gitlabToken
})
}

Expand Down
Loading

0 comments on commit c45f3e5

Please sign in to comment.