Skip to content

Commit

Permalink
Merge pull request #39 from humanitec/humanitec-autogen
Browse files Browse the repository at this point in the history
feat: use humanitec client
  • Loading branch information
johanneswuerbach authored Jan 19, 2023
2 parents d460393 + ef2109a commit 49e2de2
Show file tree
Hide file tree
Showing 6 changed files with 42,443 additions and 5,283 deletions.
49 changes: 12 additions & 37 deletions action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {describe, expect, test, beforeEach, afterAll, afterEach} from '@jest/glo
import {join as pathJoin} from 'node:path';
import {runAction} from './action';
import {randomBytes} from 'crypto';
import fetch, {RequestInit} from 'node-fetch';
import {mkdir} from 'node:fs/promises';
import {createApiClient} from './humanitec';

// Emulate https://github.com/actions/toolkit/blob/819157bf8/packages/core/src/core.ts#L128
const setInput = (name: string, value: string): void => {
Expand All @@ -22,20 +22,7 @@ const ensureEnv = (name: string): string => {
};

const token = ensureEnv('HUMANITEC_TOKEN');


const humanitecReq = (path: string, options: RequestInit) => {
options = {
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'User-Agent': 'gh-action-build-push-to-humanitec/latest',
},
...options,
};

return fetch(`https://api.humanitec.io/${path}`, options);
};
const humanitecClient = createApiClient('api.humanitec.io', token);

const orgId = ensureEnv('HUMANITEC_ORG');

Expand All @@ -47,23 +34,20 @@ describe('action', () => {


afterAll(async () => {
const res = await humanitecReq(`orgs/${orgId}/artefacts?type=container`, {method: 'GET'});
const res = await humanitecClient.orgsOrgIdArtefactsGet(orgId, 'container');

// eslint-disable-next-line jest/no-standalone-expect
expect(res.status).toBe(200);

const body = await res.json();

for (const artefact of body) {
for (const artefact of res.data) {
if (!artefact.name.startsWith(`registry.humanitec.io/${orgId}/test-`)) {
continue;
}

if (Date.now() - Date.parse(artefact.createdAt) < tenMinInMs) {
if (!artefact.created_at || Date.now() - Date.parse(artefact.created_at) < tenMinInMs) {
continue;
}

const res = await humanitecReq(`orgs/${orgId}/artefacts/${artefact.id}`, {method: 'DELETE'});
const res = await humanitecClient.orgsOrgIdArtefactsArtefactIdDelete(orgId, artefact.id);

// Multiple tests might delete artifacts
// eslint-disable-next-line jest/no-standalone-expect
Expand Down Expand Up @@ -95,12 +79,9 @@ describe('action', () => {
await runAction();
expect(process.exitCode).toBeFalsy();

const res = await humanitecReq(`orgs/${orgId}/artefact-versions`, {method: 'GET'});
const res = await humanitecClient.orgsOrgIdArtefactVersionsGet(orgId);
expect(res.status).toBe(200);

const body = await res.json();

expect(body).toEqual(
expect(res.data).toEqual(
expect.arrayContaining(
[
expect.objectContaining({
Expand Down Expand Up @@ -130,12 +111,9 @@ describe('action', () => {
await runAction();
expect(process.exitCode).toBeFalsy();

const res = await humanitecReq(`orgs/${orgId}/artefact-versions`, {method: 'GET'});
const res = await humanitecClient.orgsOrgIdArtefactVersionsGet(orgId);
expect(res.status).toBe(200);

const body = await res.json();

expect(body).toEqual(
expect(res.data).toEqual(
expect.arrayContaining(
[
expect.objectContaining({
Expand All @@ -156,12 +134,9 @@ describe('action', () => {
await runAction();
expect(process.exitCode).toBeFalsy();

const res = await humanitecReq(`orgs/${orgId}/artefact-versions`, {method: 'GET'});
const res = await humanitecClient.orgsOrgIdArtefactVersionsGet(orgId);
expect(res.status).toBe(200);

const body = await res.json();

expect(body).toEqual(
expect(res.data).toEqual(
expect.arrayContaining(
[
expect.objectContaining({
Expand Down
30 changes: 17 additions & 13 deletions action.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as docker from './docker';
import {humanitecFactory} from './humanitec';
import {createApiClient} from './humanitec';

import {existsSync} from 'node:fs';
import * as core from '@actions/core';
import {AddArtefactVersionPayloadRequest} from '@humanitec/autogen';

const DOC_URL = 'https://docs.humanitec.com/guides/connect-ci-setup/connect-ci-pipelines#github-actions-workflow';
const humanitecRegId = 'humanitec';

/**
* Performs the GitHub action.
Expand Down Expand Up @@ -44,20 +46,17 @@ export async function runAction() {
return;
}

const humanitec = humanitecFactory(token, orgId, apiHost);
const humanitec = createApiClient(apiHost, token);

if (externalRegistryUrl == '') {
let registryCreds;
try {
registryCreds = await humanitec.getRegistryCredentials();
} catch (error) {
core.error('Unable to fetch repository credentials.');
core.error(`Did you add the token to your Github Secrets? ${DOC_URL}`);
core.setFailed('Unable to access Humanitec.');
return;
const registryCreds = await humanitec.orgsOrgIdRegistriesRegIdCredsGet(orgId, humanitecRegId);
if (registryCreds.status != 200) {
throw new Error(
`Unexpected response fetching registry credentials: ${registryCreds.status}, ${registryCreds.data}`,
);
}

if (!docker.login(registryCreds.username, registryCreds.password, registryHost)) {
if (!docker.login(registryCreds.data.username, registryCreds.data.password, registryHost)) {
core.setFailed('Unable to connect to the humanitec registry.');
return;
}
Expand Down Expand Up @@ -91,7 +90,7 @@ export async function runAction() {
return;
}

const payload = {
const payload: AddArtefactVersionPayloadRequest = {
name: `${registryHost}/${imageName}`,
type: 'container',
version,
Expand All @@ -100,7 +99,12 @@ export async function runAction() {
};

try {
await humanitec.addNewVersion(payload);
const versionReq = await humanitec.orgsOrgIdArtefactVersionsPost(orgId, payload);
if (versionReq.status != 204) {
throw new Error(
`Unexpected response creating artefact version: ${versionReq.status}, ${versionReq.data}`,
);
}
} catch (error) {
core.error('Unable to notify Humanitec about build.');
core.error(`Did you add the token to your Github Secrets? ${DOC_URL}`);
Expand Down
Loading

0 comments on commit 49e2de2

Please sign in to comment.