Skip to content

Commit 7945c98

Browse files
authored
Merge pull request #339 from twilio/master-main-template
Master main template
2 parents a544a53 + 954c021 commit 7945c98

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

packages/create-flex-plugin/src/lib/__tests__/commands.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ describe('commands', () => {
113113
};
114114
const parseGitHubUrl = jest
115115
.spyOn(github, 'parseGitHubUrl')
116-
.mockReturnValue(info);
116+
.mockResolvedValue(info);
117117
const downloadRepo = jest
118118
.spyOn(github, 'downloadRepo')
119119
.mockResolvedValue(null);

packages/create-flex-plugin/src/lib/commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const setupConfiguration = (config: FlexPluginArguments): FlexPluginArgum
6161
* @param dir {string} the temp directory to save the downloaded file to
6262
*/
6363
export const downloadFromGitHub = async (url: string, dir: string) => {
64-
const info = github.parseGitHubUrl(url);
64+
const info = await github.parseGitHubUrl(url);
6565

6666
return await github.downloadRepo(info, dir);
6767
};

packages/create-flex-plugin/src/utils/__tests__/github.test.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,53 @@ describe('github', () => {
2626
});
2727

2828
describe('parseGitHubUrl', () => {
29-
it('should get repo and master ref', () => {
30-
const resp = github.parseGitHubUrl(gitHubUrl);
29+
const branchesWithMaster = [{ name: 'master' }, { name: 'feature-branch' }];
30+
const branchesWithMain = [{ name: 'main' }, { name: 'feature-branch' }];
31+
const branchesWithNeither = [{ name: 'something-else' }, { name: 'feature-branch' }];
32+
33+
it('should get repo with master ref', async () => {
34+
mockAxios.onGet().reply(() => Promise.resolve([200, branchesWithMaster]));
35+
const resp = await github.parseGitHubUrl(gitHubUrl);
3136

3237
expect(resp.ref).toEqual('master');
3338
expect(resp.repo).toEqual('flex-plugin-builder');
3439
expect(resp.owner).toEqual('twilio');
3540
});
3641

37-
it('should get repo and some ref', () => {
38-
const resp = github.parseGitHubUrl(`${gitHubUrl}/tree/some-ref`);
42+
it('should get repo with main ref', async () => {
43+
mockAxios.onGet().reply(() => Promise.resolve([200, branchesWithMain]));
44+
const resp = await github.parseGitHubUrl(gitHubUrl);
45+
46+
expect(resp.ref).toEqual('main');
47+
expect(resp.repo).toEqual('flex-plugin-builder');
48+
expect(resp.owner).toEqual('twilio');
49+
});
50+
51+
it('should reject because main/main is not found', async (done) => {
52+
mockAxios.onGet().reply(() => Promise.resolve([200, branchesWithNeither]));
53+
try {
54+
await github.parseGitHubUrl(gitHubUrl);
55+
} catch (e) {
56+
expect(e.message).toEqual(github.ERROR_BRANCH_MASTER_MAIN);
57+
done();
58+
}
59+
});
60+
61+
it('should get repo and some ref', async () => {
62+
const resp = await github.parseGitHubUrl(`${gitHubUrl}/tree/some-ref`);
3963

4064
expect(resp.ref).toEqual('some-ref');
4165
expect(resp.repo).toEqual('flex-plugin-builder');
4266
expect(resp.owner).toEqual('twilio');
4367
});
4468

45-
it('should fail to parse', () => {
46-
expect(() => github.parseGitHubUrl('/broken')).toThrow();
69+
it('should fail to parse', async (done) => {
70+
try {
71+
await github.parseGitHubUrl('/broken');
72+
} catch (e) {
73+
expect(e.message).toEqual(github.ERROR_GITHUB_URL_PARSE);
74+
done();
75+
}
4776
});
4877
});
4978

packages/create-flex-plugin/src/utils/github.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ export interface GitHubInfo {
99
ref: string;
1010
}
1111

12+
export interface GitHubBranches {
13+
name: string;
14+
commit: string;
15+
protected: boolean;
16+
}
17+
1218
export enum GitHubContentType {
1319
File = 'file',
1420
Dir = 'dir',
@@ -31,24 +37,46 @@ export interface GitHubContent {
3137
};
3238
}
3339

40+
export const ERROR_GITHUB_URL_PARSE = 'Could not get owner and repo name from GitHub URL';
41+
export const ERROR_BRANCH_MASTER_MAIN = 'Could not find branch main or master on GitHub';
42+
3443
/**
3544
* Parses the GitHub URL to extract owner and repo information
3645
*
3746
* @param url {string} the GitHub URL
3847
* @return returns the {@link GitHubInfo}
3948
*/
40-
export const parseGitHubUrl = (url: string): GitHubInfo => {
49+
export const parseGitHubUrl = async (url: string): Promise<GitHubInfo> => {
4150
const matches = url.match(/github\.com\/([0-9a-zA-Z-_]+)\/([0-9a-zA-Z-_]+)(\/tree\/([0-9a-zA-Z._-]+))?/);
4251

4352
if (!matches || matches.length < 3) {
44-
throw new Error('Could not get owner and repo name from GitHub URL');
53+
throw new Error(ERROR_GITHUB_URL_PARSE);
4554
}
4655

47-
return {
56+
const info: GitHubInfo = {
4857
owner: matches[1],
4958
repo: matches[2],
5059
ref: matches[4] || 'master',
5160
};
61+
62+
// Check whether master or main exists
63+
if (info.ref === 'master' || info.ref === 'main') {
64+
const branches = await axios.get<GitHubBranches[]>(`https://api.github.com/repos/${info.owner}/${info.repo}/branches`)
65+
.then((resp) => resp.data);
66+
67+
const hasMaster = branches.find((branch) => branch.name === 'master');
68+
const hasMain = branches.find((branch) => branch.name === 'main');
69+
70+
if (hasMain) {
71+
info.ref = 'main';
72+
} else if (hasMaster) {
73+
info.ref = 'master';
74+
} else {
75+
throw new Error(ERROR_BRANCH_MASTER_MAIN);
76+
}
77+
}
78+
79+
return info;
5280
};
5381

5482
/**

0 commit comments

Comments
 (0)