From 2cdd73cfd48d1ac4d94e814b9e7b710834e72ffd Mon Sep 17 00:00:00 2001 From: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Date: Thu, 26 Sep 2019 10:30:02 +0200 Subject: [PATCH] Release: Create cherrypick task work for enterprise repo (#19424) --- packages/grafana-toolkit/src/cli/index.ts | 3 +- .../src/cli/tasks/cherrypick.ts | 8 ++-- .../src/cli/tasks/closeMilestone.ts | 2 +- .../src/cli/utils/githubClient.test.ts | 44 +++++++++++++++++-- .../src/cli/utils/githubClient.ts | 12 +++-- 5 files changed, 58 insertions(+), 11 deletions(-) diff --git a/packages/grafana-toolkit/src/cli/index.ts b/packages/grafana-toolkit/src/cli/index.ts index 03d87ccdbec..3730642cfb6 100644 --- a/packages/grafana-toolkit/src/cli/index.ts +++ b/packages/grafana-toolkit/src/cli/index.ts @@ -68,9 +68,10 @@ export const run = (includeInternalScripts = false) => { program .command('cherrypick') + .option('-e, --enterprise', 'Run task for grafana-enterprise') .description('Helps find commits to cherry pick') .action(async cmd => { - await execTask(cherryPickTask)({}); + await execTask(cherryPickTask)({ enterprise: !!cmd.enterprise }); }); program diff --git a/packages/grafana-toolkit/src/cli/tasks/cherrypick.ts b/packages/grafana-toolkit/src/cli/tasks/cherrypick.ts index 20e1c73686d..2aa37ba7e77 100644 --- a/packages/grafana-toolkit/src/cli/tasks/cherrypick.ts +++ b/packages/grafana-toolkit/src/cli/tasks/cherrypick.ts @@ -1,10 +1,12 @@ import { Task, TaskRunner } from './task'; import GithubClient from '../utils/githubClient'; -interface CherryPickOptions {} +interface CherryPickOptions { + enterprise: boolean; +} -const cherryPickRunner: TaskRunner = async () => { - const githubClient = new GithubClient(); +const cherryPickRunner: TaskRunner = async ({ enterprise }) => { + const githubClient = new GithubClient({ enterprise }); const client = githubClient.client; const res = await client.get('/issues', { diff --git a/packages/grafana-toolkit/src/cli/tasks/closeMilestone.ts b/packages/grafana-toolkit/src/cli/tasks/closeMilestone.ts index 3bdbbd46a21..3cc10b66538 100644 --- a/packages/grafana-toolkit/src/cli/tasks/closeMilestone.ts +++ b/packages/grafana-toolkit/src/cli/tasks/closeMilestone.ts @@ -6,7 +6,7 @@ interface CloseMilestoneOptions { } const closeMilestoneTaskRunner: TaskRunner = async ({ milestone }) => { - const githubClient = new GithubClient(true); + const githubClient = new GithubClient({ required: true }); const cherryPickLabel = 'cherry-pick needed'; const client = githubClient.client; diff --git a/packages/grafana-toolkit/src/cli/utils/githubClient.test.ts b/packages/grafana-toolkit/src/cli/utils/githubClient.test.ts index dbdadda0d92..6ca36c2dd7e 100644 --- a/packages/grafana-toolkit/src/cli/utils/githubClient.test.ts +++ b/packages/grafana-toolkit/src/cli/utils/githubClient.test.ts @@ -15,11 +15,13 @@ afterEach(() => { describe('GithubClient', () => { it('should initialise a GithubClient', () => { const github = new GithubClient(); + const githubEnterprise = new GithubClient({ enterprise: true }); expect(github).toBeInstanceOf(GithubClient); + expect(githubEnterprise).toBeInstanceOf(GithubClient); }); describe('#client', () => { - it('it should contain a client', () => { + it('it should contain a grafana client', () => { // @ts-ignore const spy = jest.spyOn(GithubClient.prototype, 'createClient').mockImplementation(() => fakeClient); @@ -33,6 +35,20 @@ describe('GithubClient', () => { expect(client).toEqual(fakeClient); }); + it('it should contain a grafana enterprise client', () => { + // @ts-ignore + const spy = jest.spyOn(GithubClient.prototype, 'createClient').mockImplementation(() => fakeClient); + + const github = new GithubClient({ enterprise: true }); + const client = github.client; + + expect(spy).toHaveBeenCalledWith({ + baseURL: 'https://api.github.com/repos/grafana/grafana-enterprise', + timeout: 10000, + }); + expect(client).toEqual(fakeClient); + }); + describe('when the credentials are required', () => { it('should create the client when the credentials are defined', () => { const username = 'grafana'; @@ -44,7 +60,7 @@ describe('GithubClient', () => { // @ts-ignore const spy = jest.spyOn(GithubClient.prototype, 'createClient').mockImplementation(() => fakeClient); - const github = new GithubClient(true); + const github = new GithubClient({ required: true }); const client = github.client; expect(spy).toHaveBeenCalledWith({ @@ -56,11 +72,33 @@ describe('GithubClient', () => { expect(client).toEqual(fakeClient); }); + it('should create the enterprise client when the credentials are defined', () => { + const username = 'grafana'; + const token = 'averysecureaccesstoken'; + + process.env.GITHUB_USERNAME = username; + process.env.GITHUB_ACCESS_TOKEN = token; + + // @ts-ignore + const spy = jest.spyOn(GithubClient.prototype, 'createClient').mockImplementation(() => fakeClient); + + const github = new GithubClient({ required: true, enterprise: true }); + const client = github.client; + + expect(spy).toHaveBeenCalledWith({ + baseURL: 'https://api.github.com/repos/grafana/grafana-enterprise', + timeout: 10000, + auth: { username, password: token }, + }); + + expect(client).toEqual(fakeClient); + }); + describe('when the credentials are not defined', () => { it('should throw an error', () => { expect(() => { // tslint:disable-next-line - new GithubClient(true); + new GithubClient({ required: true }); }).toThrow(/operation needs a GITHUB_USERNAME and GITHUB_ACCESS_TOKEN environment variables/); }); }); diff --git a/packages/grafana-toolkit/src/cli/utils/githubClient.ts b/packages/grafana-toolkit/src/cli/utils/githubClient.ts index a3eff8c532b..22207984ac8 100644 --- a/packages/grafana-toolkit/src/cli/utils/githubClient.ts +++ b/packages/grafana-toolkit/src/cli/utils/githubClient.ts @@ -1,6 +1,7 @@ import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'; -const baseURL = 'https://api.github.com/repos/grafana/grafana'; +const grafanaURL = 'https://api.github.com/repos/grafana/grafana'; +const enterpriseURL = 'https://api.github.com/repos/grafana/grafana-enterprise'; // Encapsulates the creation of a client for the Github API // @@ -10,15 +11,20 @@ const baseURL = 'https://api.github.com/repos/grafana/grafana'; // they're not required - the library will use them. This allows us to overcome // any API rate limiting imposed without authentication. +interface GithubClientProps { + required?: boolean; + enterprise?: boolean; +} + class GithubClient { client: AxiosInstance; - constructor(required = false) { + constructor({ required = false, enterprise = false }: GithubClientProps = {}) { const username = process.env.GITHUB_USERNAME; const token = process.env.GITHUB_ACCESS_TOKEN; const clientConfig: AxiosRequestConfig = { - baseURL: baseURL, + baseURL: enterprise ? enterpriseURL : grafanaURL, timeout: 10000, };