Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"devDependencies": {
"@aws-sdk/client-lambda": "^3.2.0",
"@google-cloud/storage": "^5.7.0",
"@octokit/plugin-retry": "^3.0.7",
"@octokit/rest": "16.35.2",
"@octokit/plugin-retry": "^3.0.9",
"@octokit/rest": "^18.10.0",
"@sentry/node": "4.6.3",
"@sentry/typescript": "^5.17.0",
"@types/async": "^3.0.1",
Expand Down
14 changes: 7 additions & 7 deletions src/artifact_providers/__tests__/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class TestGithubArtifactProvider extends GithubArtifactProvider {

describe('GitHub Artifact Provider', () => {
let githubArtifactProvider: TestGithubArtifactProvider;
let mockClient: { request: jest.Mock };
let mockClient: { actions: { listArtifactsForRepo: jest.Mock } };

beforeEach(() => {
jest.resetAllMocks();
mockClient = {
request: jest.fn(),
actions: { listArtifactsForRepo: jest.fn() },
};
(getGithubClient as jest.MockedFunction<
typeof getGithubClient
Expand All @@ -31,7 +31,7 @@ describe('GitHub Artifact Provider', () => {

describe('listArtifactsForRevision', () => {
test('it should get the artifact with the revision name', async () => {
mockClient.request.mockResolvedValueOnce({
mockClient.actions.listArtifactsForRepo.mockResolvedValueOnce({
status: 200,
data: {
total_count: 2,
Expand Down Expand Up @@ -88,7 +88,7 @@ describe('GitHub Artifact Provider', () => {
});

test('it should get the latest artifact with the same name ', async () => {
mockClient.request.mockResolvedValueOnce({
mockClient.actions.listArtifactsForRepo.mockResolvedValueOnce({
status: 200,
data: {
total_count: 2,
Expand Down Expand Up @@ -145,7 +145,7 @@ describe('GitHub Artifact Provider', () => {
});

test('it should throw when no artifacts are found after 3 retries', async () => {
mockClient.request.mockResolvedValue({
mockClient.actions.listArtifactsForRepo.mockResolvedValue({
status: 200,
data: {
total_count: 0,
Expand All @@ -160,11 +160,11 @@ describe('GitHub Artifact Provider', () => {
`"Failed to discover any artifacts (tries: 3)"`
);

expect(mockClient.request).toBeCalledTimes(3);
expect(mockClient.actions.listArtifactsForRepo).toBeCalledTimes(3);
});

test('it should throw when no artifacts with the name can be found', async () => {
mockClient.request.mockResolvedValue({
mockClient.actions.listArtifactsForRepo.mockResolvedValue({
status: 200,
data: {
total_count: 2,
Expand Down
69 changes: 23 additions & 46 deletions src/artifact_providers/github.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Github from '@octokit/rest';
import { Octokit, RestEndpointMethodTypes } from '@octokit/rest';
import * as fs from 'fs';
import fetch from 'node-fetch';
import * as path from 'path';
Expand All @@ -19,31 +19,14 @@ import { extractZipArchive } from '../utils/system';

const MAX_TRIES = 3;

export interface ArtifactItem {
id: number;
name: string;
size_in_bytes: number;
url: string;
archive_download_url: string;
created_at: string;
expires_at: string;
}

interface ArtifactList {
total_count: number;
artifacts: Array<ArtifactItem>;
}

interface ArchiveResponse extends Github.AnyResponse {
url: string;
}
export type ArtifactItem = RestEndpointMethodTypes['actions']['listArtifactsForRepo']['response']['data']['artifacts'][0];

/**
* Github artifact provider
*/
export class GithubArtifactProvider extends BaseArtifactProvider {
/** Github client */
public readonly github: Github;
public readonly github: Octokit;

public constructor(config: ArtifactProviderConfig) {
super(config);
Expand Down Expand Up @@ -81,14 +64,14 @@ export class GithubArtifactProvider extends BaseArtifactProvider {
const per_page = 100;

// https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#artifacts
Comment thread
BYK marked this conversation as resolved.
const artifactResponse = ((
await this.github.request('GET /repos/{owner}/{repo}/actions/artifacts', {
const artifactResponse = (
await this.github.actions.listArtifactsForRepo({
owner: owner,
repo: repo,
per_page,
page,
})
).data as unknown) as ArtifactList;
).data;

const { artifacts } = artifactResponse;
this.logger.trace(`All available artifacts on page ${page}:`, artifacts);
Expand Down Expand Up @@ -122,7 +105,9 @@ export class GithubArtifactProvider extends BaseArtifactProvider {
// ** AND **
// the descending date order. See the note above
const lastArtifact = artifacts[artifacts.length - 1];
checkNextPage = lastArtifact.created_at >= revisionDate;
checkNextPage =
lastArtifact.created_at == null ||
lastArtifact.created_at >= revisionDate;
}

if (checkNextPage) {
Expand Down Expand Up @@ -151,14 +136,14 @@ export class GithubArtifactProvider extends BaseArtifactProvider {
* @param archiveResponse
*/
private async downloadAndUnpackArtifacts(
archiveResponse: ArchiveResponse
url: string
): Promise<RemoteArtifact[]> {
const artifacts: RemoteArtifact[] = [];
await withTempFile(async tempFilepath => {
const response = await fetch(archiveResponse.url);
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`Unexpected HTTP response from ${archiveResponse.url}: ${response.status} (${response.statusText})`
`Unexpected HTTP response from ${url}: ${response.status} (${response.statusText})`
);
}
await new Promise((resolve, reject) =>
Expand Down Expand Up @@ -190,30 +175,22 @@ export class GithubArtifactProvider extends BaseArtifactProvider {
}

/**
* Returns {@link ArchiveResponse} for a giving {@link ArtifactItem}
* Returns {@link ArtifactResponse} for a giving {@link ArtifactItem}
* @param foundArtifact
*/
private async getArchiveDownloadUrl(
foundArtifact: ArtifactItem
): Promise<ArchiveResponse> {
): Promise<string> {
const { repoName, repoOwner } = this.config;

const archiveResponse = (await this.github.request(
'/repos/{owner}/{repo}/actions/artifacts/{artifact_id}/{archive_format}',
{
owner: repoOwner,
repo: repoName,
artifact_id: foundArtifact.id,
archive_format: 'zip',
}
)) as ArchiveResponse;
const archiveResponse = await this.github.actions.downloadArtifact({
owner: repoOwner,
repo: repoName,
artifact_id: foundArtifact.id,
archive_format: 'zip',
});

if (archiveResponse.status !== 200) {
throw new Error(
`Failed to fetch archive ${JSON.stringify(archiveResponse)}`
);
}
return archiveResponse;
return archiveResponse.url;
}

/**
Expand All @@ -232,10 +209,10 @@ export class GithubArtifactProvider extends BaseArtifactProvider {

this.logger.debug(`Requesting archive URL from Github...`);

const archiveResponse = await this.getArchiveDownloadUrl(foundArtifact);
const archiveUrl = await this.getArchiveDownloadUrl(foundArtifact);

this.logger.debug(`Downloading ZIP from Github artifacts...`);

return await this.downloadAndUnpackArtifacts(archiveResponse);
return await this.downloadAndUnpackArtifacts(archiveUrl);
}
}
26 changes: 15 additions & 11 deletions src/status_providers/github.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Github from '@octokit/rest';
import { Octokit, RestEndpointMethodTypes } from '@octokit/rest';

import { logger } from '../logger';
import {
Expand All @@ -12,12 +12,16 @@ import { ConfigurationError } from '../utils/errors';
import { formatJson } from '../utils/strings';
import { GithubGlobalConfig } from 'src/schemas/project_config';

type ReposGetCombinedStatusForRefResponse = RestEndpointMethodTypes['repos']['getCombinedStatusForRef']['response']['data'];
type ChecksListSuitesForRefResponse = RestEndpointMethodTypes['checks']['listSuitesForRef']['response']['data'];
type ChecksListForRefResponse = RestEndpointMethodTypes['checks']['listForRef']['response']['data'];

/**
* Status provider that talks to GitHub to get commit checks (statuses)
*/
export class GithubStatusProvider extends BaseStatusProvider {
/** Github client */
private readonly github: Github;
private readonly github: Octokit;

public constructor(
config: StatusProviderConfig,
Expand Down Expand Up @@ -145,9 +149,9 @@ export class GithubStatusProvider extends BaseStatusProvider {
*/
private getStatusForContext(
context: string,
revisionStatus: Github.ReposGetCombinedStatusForRefResponse,
revisionCheckSuites: Github.ChecksListSuitesForRefResponse,
revisionChecks: Github.ChecksListForRefResponse
revisionStatus: ReposGetCombinedStatusForRefResponse,
revisionCheckSuites: ChecksListSuitesForRefResponse,
revisionChecks: ChecksListForRefResponse
): CommitStatus {
const results = [
this.getResultFromCommitApiStatus(revisionStatus, context),
Expand Down Expand Up @@ -201,7 +205,7 @@ export class GithubStatusProvider extends BaseStatusProvider {
* @param context If passed, only result of the corresponding context is considered
*/
private getResultFromCommitApiStatus(
combinedStatus: Github.ReposGetCombinedStatusForRefResponse,
combinedStatus: ReposGetCombinedStatusForRefResponse,
context?: string
): CommitStatus {
if (context) {
Expand All @@ -225,8 +229,8 @@ export class GithubStatusProvider extends BaseStatusProvider {
* @param context If provided, only the corresponding run is considered
*/
private getResultFromRevisionChecks(
revisionCheckSuites: Github.ChecksListSuitesForRefResponse,
revisionChecks: Github.ChecksListForRefResponse,
revisionCheckSuites: ChecksListSuitesForRefResponse,
revisionChecks: ChecksListForRefResponse,
context?: string
): CommitStatus {
// Check runs: we have an array of runs, and each of them has a status
Expand Down Expand Up @@ -271,7 +275,7 @@ export class GithubStatusProvider extends BaseStatusProvider {
*/
protected async getCommitApiStatus(
revision: string
): Promise<Github.ReposGetCombinedStatusForRefResponse> {
): Promise<ReposGetCombinedStatusForRefResponse> {
logger.debug(`Fetching combined revision status...`);
const revisionStatusResponse = await this.github.repos.getCombinedStatusForRef(
{
Expand All @@ -297,7 +301,7 @@ export class GithubStatusProvider extends BaseStatusProvider {
*/
protected async getRevisionCheckSuites(
revision: string
): Promise<Github.ChecksListSuitesForRefResponse> {
): Promise<ChecksListSuitesForRefResponse> {
logger.debug('Fetching Checks API status...');
const revisionCheckSuites = (
await this.github.checks.listSuitesForRef({
Expand All @@ -323,7 +327,7 @@ export class GithubStatusProvider extends BaseStatusProvider {
*/
protected async getRevisionChecks(
revision: string
): Promise<Github.ChecksListForRefResponse> {
): Promise<ChecksListForRefResponse> {
logger.debug('Fetching Checks API status...');
const revisionChecksResponse = await this.github.checks.listForRef({
...this.githubConfig,
Expand Down
8 changes: 4 additions & 4 deletions src/targets/awsLambdaLayer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as path from 'path';

import * as Github from '@octokit/rest';
import { Octokit } from '@octokit/rest';
import simpleGit from 'simple-git';
import {
getAuthUsername,
Expand Down Expand Up @@ -47,7 +47,7 @@ export class AwsLambdaLayerTarget extends BaseTarget {
/** Target options */
public readonly awsLambdaConfig: AwsLambdaTargetConfig;
/** GitHub client. */
public readonly github: Github;
public readonly github: Octokit;
/** The directory where the runtime-specific directories are. */
private readonly AWS_REGISTRY_DIR = 'aws-lambda-layers';
/** File containing data fields every new version file overrides */
Expand Down Expand Up @@ -262,8 +262,8 @@ export class AwsLambdaLayerTarget extends BaseTarget {
this.logger.debug('Finished publishing to all regions.');
} catch (error) {
this.logger.error(
`Did not publish layers for ${runtime.name}. ` +
`Something went wrong with AWS: ${error.message}`
`Did not publish layers for ${runtime.name}.`,
error
);
return;
}
Expand Down
10 changes: 5 additions & 5 deletions src/targets/brew.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mapLimit } from 'async';
import * as Github from '@octokit/rest';
import { Octokit } from '@octokit/rest';

import { GithubGlobalConfig, TargetConfig } from '../schemas/project_config';
import { ConfigurationError } from '../utils/errors';
Expand Down Expand Up @@ -47,7 +47,7 @@ export class BrewTarget extends BaseTarget {
/** Target options */
public readonly brewConfig: BrewTargetOptions;
/** Github client */
public readonly github: Github;
public readonly github: Octokit;
/** Github repo configuration */
public readonly githubRepo: GithubGlobalConfig;

Expand Down Expand Up @@ -123,15 +123,15 @@ export class BrewTarget extends BaseTarget {
try {
const tap = this.brewConfig.tapRepo;
this.logger.debug(`Loading SHA for ${tap.owner}/${tap.repo}:${path}`);
const response = await this.github.repos.getContents({
const response = await this.github.repos.getContent({
...tap,
path,
});
if (response.data instanceof Array) {
return undefined;
}
return response.data.sha;
} catch (e) {
} catch (e: any) {
if (e.status === 404) {
return undefined;
}
Expand Down Expand Up @@ -205,7 +205,7 @@ export class BrewTarget extends BaseTarget {
);

if (!isDryRun()) {
await this.github.repos.createOrUpdateFile(params);
await this.github.repos.createOrUpdateFileContents(params);
} else {
this.logger.info(`[dry-run] Skipping file action: ${action}`);
}
Expand Down
4 changes: 2 additions & 2 deletions src/targets/cocoapods.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Github from '@octokit/rest';
import { Octokit } from '@octokit/rest';
import * as fs from 'fs';
import { basename, join } from 'path';
import { promisify } from 'util';
Expand Down Expand Up @@ -34,7 +34,7 @@ export class CocoapodsTarget extends BaseTarget {
/** Target options */
public readonly cocoapodsConfig: CocoapodsTargetOptions;
/** Github client */
public readonly github: Github;
public readonly github: Octokit;
/** Github repo configuration */
public readonly githubRepo: GithubGlobalConfig;

Expand Down
4 changes: 2 additions & 2 deletions src/targets/ghPages.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as path from 'path';

import * as Github from '@octokit/rest';
import { Octokit } from '@octokit/rest';
import simpleGit from 'simple-git';

import { GithubGlobalConfig, TargetConfig } from '../schemas/project_config';
Expand Down Expand Up @@ -44,7 +44,7 @@ export class GhPagesTarget extends BaseTarget {
/** Target options */
public readonly ghPagesConfig: GhPagesConfig;
/** Github client */
public readonly github: Github;
public readonly github: Octokit;
/** Github repo configuration */
public readonly githubRepo: GithubGlobalConfig;

Expand Down
Loading