1 | require('lazy-ass');
|
2 | var check = require('check-more-types');
|
3 | var R = require('ramda');
|
4 |
|
5 | var repoSchema = {
|
6 | user: check.unemptyString,
|
7 | repo: check.unemptyString
|
8 | };
|
9 |
|
10 | var isRepoQuestion = R.partial(check.schema, repoSchema);
|
11 | function verifyRepoOptions(options) {
|
12 | la(isRepoQuestion(options), 'missing repo info', options);
|
13 | }
|
14 |
|
15 | function isGithubUrl(url) {
|
16 | return check.unemptyString(url) &&
|
17 | /github\.com/.test(url);
|
18 | }
|
19 |
|
20 | function verifyGithub(repo) {
|
21 | la(check.object(repo) &&
|
22 | repo.type === 'git' &&
|
23 | isGithubUrl(repo.url),
|
24 | 'not a github repo', repo);
|
25 | }
|
26 |
|
27 | var GitHubApi = require('github');
|
28 | var github = new GitHubApi({
|
29 |
|
30 | version: '3.0.0',
|
31 |
|
32 | debug: false
|
33 | });
|
34 |
|
35 | function parseGithubUrl(url) {
|
36 | la(isGithubUrl(url), 'not a github url', url);
|
37 | var githubUrlRegex = /github\.com[\/:]([a-zA-Z-]+?)\/([a-zA-Z-]+?)(\.git)?$/;
|
38 | var matches = githubUrlRegex.exec(url);
|
39 | la(check.array(matches),
|
40 | 'could not extract user and repo name from github url', url);
|
41 | return {
|
42 | user: matches[1],
|
43 | repo: matches[2]
|
44 | };
|
45 | }
|
46 |
|
47 | function shortenSha(str) {
|
48 | la(check.unemptyString(str), 'expected long commit sha string', str);
|
49 | return str.substr(0, 7);
|
50 | }
|
51 |
|
52 | function trimVersion(str) {
|
53 | la(check.unemptyString(str), 'missig tag', str);
|
54 | var startsWithV = /^v\d+/;
|
55 | if (startsWithV.test(str)) {
|
56 | return str.substr(1);
|
57 | }
|
58 | return str;
|
59 | }
|
60 |
|
61 | function firstLine(str) {
|
62 | la(check.string(str), 'expected str', str);
|
63 | str = str.trim();
|
64 | var firstNewline = str.indexOf('\n');
|
65 | if (firstNewline !== -1) {
|
66 | return str.substr(0, firstNewline);
|
67 | }
|
68 | return str;
|
69 | }
|
70 |
|
71 |
|
72 | var userRepo = /^([\w-]+)?\/([\w-]+)?$/;
|
73 | function isGithubName(str) {
|
74 | return check.unemptyString(str) &&
|
75 | userRepo.test(str);
|
76 | }
|
77 |
|
78 | function parseGithubName(str) {
|
79 | la(isGithubName(str), 'not a github name', str);
|
80 | var matches = userRepo.exec(str);
|
81 | return {
|
82 | user: matches[1],
|
83 | repo: matches[2]
|
84 | };
|
85 | }
|
86 |
|
87 | module.exports = {
|
88 | isRepoQuestion: isRepoQuestion,
|
89 | verifyRepoOptions: verifyRepoOptions,
|
90 | github: github,
|
91 | verifyGithub: verifyGithub,
|
92 | parseGithubUrl: parseGithubUrl,
|
93 | trimVersion: trimVersion,
|
94 | firstLine: firstLine,
|
95 | shortenSha: shortenSha,
|
96 | isGithubName: isGithubName,
|
97 | parseGithubName: parseGithubName
|
98 | };
|