Skip to content

Commit 2cdd73c

Browse files
authored
Release: Create cherrypick task work for enterprise repo (grafana#19424)
1 parent 8f01e9c commit 2cdd73c

File tree

5 files changed

+58
-11
lines changed

5 files changed

+58
-11
lines changed

packages/grafana-toolkit/src/cli/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ export const run = (includeInternalScripts = false) => {
6868

6969
program
7070
.command('cherrypick')
71+
.option('-e, --enterprise', 'Run task for grafana-enterprise')
7172
.description('Helps find commits to cherry pick')
7273
.action(async cmd => {
73-
await execTask(cherryPickTask)({});
74+
await execTask(cherryPickTask)({ enterprise: !!cmd.enterprise });
7475
});
7576

7677
program

packages/grafana-toolkit/src/cli/tasks/cherrypick.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Task, TaskRunner } from './task';
22
import GithubClient from '../utils/githubClient';
33

4-
interface CherryPickOptions {}
4+
interface CherryPickOptions {
5+
enterprise: boolean;
6+
}
57

6-
const cherryPickRunner: TaskRunner<CherryPickOptions> = async () => {
7-
const githubClient = new GithubClient();
8+
const cherryPickRunner: TaskRunner<CherryPickOptions> = async ({ enterprise }) => {
9+
const githubClient = new GithubClient({ enterprise });
810
const client = githubClient.client;
911

1012
const res = await client.get('/issues', {

packages/grafana-toolkit/src/cli/tasks/closeMilestone.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface CloseMilestoneOptions {
66
}
77

88
const closeMilestoneTaskRunner: TaskRunner<CloseMilestoneOptions> = async ({ milestone }) => {
9-
const githubClient = new GithubClient(true);
9+
const githubClient = new GithubClient({ required: true });
1010

1111
const cherryPickLabel = 'cherry-pick needed';
1212
const client = githubClient.client;

packages/grafana-toolkit/src/cli/utils/githubClient.test.ts

+41-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ afterEach(() => {
1515
describe('GithubClient', () => {
1616
it('should initialise a GithubClient', () => {
1717
const github = new GithubClient();
18+
const githubEnterprise = new GithubClient({ enterprise: true });
1819
expect(github).toBeInstanceOf(GithubClient);
20+
expect(githubEnterprise).toBeInstanceOf(GithubClient);
1921
});
2022

2123
describe('#client', () => {
22-
it('it should contain a client', () => {
24+
it('it should contain a grafana client', () => {
2325
// @ts-ignore
2426
const spy = jest.spyOn(GithubClient.prototype, 'createClient').mockImplementation(() => fakeClient);
2527

@@ -33,6 +35,20 @@ describe('GithubClient', () => {
3335
expect(client).toEqual(fakeClient);
3436
});
3537

38+
it('it should contain a grafana enterprise client', () => {
39+
// @ts-ignore
40+
const spy = jest.spyOn(GithubClient.prototype, 'createClient').mockImplementation(() => fakeClient);
41+
42+
const github = new GithubClient({ enterprise: true });
43+
const client = github.client;
44+
45+
expect(spy).toHaveBeenCalledWith({
46+
baseURL: 'https://api.github.com/repos/grafana/grafana-enterprise',
47+
timeout: 10000,
48+
});
49+
expect(client).toEqual(fakeClient);
50+
});
51+
3652
describe('when the credentials are required', () => {
3753
it('should create the client when the credentials are defined', () => {
3854
const username = 'grafana';
@@ -44,7 +60,7 @@ describe('GithubClient', () => {
4460
// @ts-ignore
4561
const spy = jest.spyOn(GithubClient.prototype, 'createClient').mockImplementation(() => fakeClient);
4662

47-
const github = new GithubClient(true);
63+
const github = new GithubClient({ required: true });
4864
const client = github.client;
4965

5066
expect(spy).toHaveBeenCalledWith({
@@ -56,11 +72,33 @@ describe('GithubClient', () => {
5672
expect(client).toEqual(fakeClient);
5773
});
5874

75+
it('should create the enterprise client when the credentials are defined', () => {
76+
const username = 'grafana';
77+
const token = 'averysecureaccesstoken';
78+
79+
process.env.GITHUB_USERNAME = username;
80+
process.env.GITHUB_ACCESS_TOKEN = token;
81+
82+
// @ts-ignore
83+
const spy = jest.spyOn(GithubClient.prototype, 'createClient').mockImplementation(() => fakeClient);
84+
85+
const github = new GithubClient({ required: true, enterprise: true });
86+
const client = github.client;
87+
88+
expect(spy).toHaveBeenCalledWith({
89+
baseURL: 'https://api.github.com/repos/grafana/grafana-enterprise',
90+
timeout: 10000,
91+
auth: { username, password: token },
92+
});
93+
94+
expect(client).toEqual(fakeClient);
95+
});
96+
5997
describe('when the credentials are not defined', () => {
6098
it('should throw an error', () => {
6199
expect(() => {
62100
// tslint:disable-next-line
63-
new GithubClient(true);
101+
new GithubClient({ required: true });
64102
}).toThrow(/operation needs a GITHUB_USERNAME and GITHUB_ACCESS_TOKEN environment variables/);
65103
});
66104
});

packages/grafana-toolkit/src/cli/utils/githubClient.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
22

3-
const baseURL = 'https://api.github.com/repos/grafana/grafana';
3+
const grafanaURL = 'https://api.github.com/repos/grafana/grafana';
4+
const enterpriseURL = 'https://api.github.com/repos/grafana/grafana-enterprise';
45

56
// Encapsulates the creation of a client for the Github API
67
//
@@ -10,15 +11,20 @@ const baseURL = 'https://api.github.com/repos/grafana/grafana';
1011
// they're not required - the library will use them. This allows us to overcome
1112
// any API rate limiting imposed without authentication.
1213

14+
interface GithubClientProps {
15+
required?: boolean;
16+
enterprise?: boolean;
17+
}
18+
1319
class GithubClient {
1420
client: AxiosInstance;
1521

16-
constructor(required = false) {
22+
constructor({ required = false, enterprise = false }: GithubClientProps = {}) {
1723
const username = process.env.GITHUB_USERNAME;
1824
const token = process.env.GITHUB_ACCESS_TOKEN;
1925

2026
const clientConfig: AxiosRequestConfig = {
21-
baseURL: baseURL,
27+
baseURL: enterprise ? enterpriseURL : grafanaURL,
2228
timeout: 10000,
2329
};
2430

0 commit comments

Comments
 (0)