forked from MicrosoftDocs/pipelines-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-client.js
More file actions
75 lines (64 loc) · 2.34 KB
/
Copy pathgithub-client.js
File metadata and controls
75 lines (64 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const axios = require('axios').default;
class MergeError extends Error {
constructor(...params) {
super(...params);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, MergeError);
} else {
this.stack = (new Error()).stack;
}
this.name = 'MergeError';
}
}
class GitHubClient {
constructor(accessToken, repoOwner, repo) {
this.client = axios.create({
baseURL: 'https://api.github.com/',
headers: {
'Accept': 'application/vnd.github.v3+json',
'Content-Type': 'application/json',
'Authorization': `token ${accessToken}`
}
});
this.repoOwner = repoOwner;
this.repo = repo;
}
getBranchInfo(branch) {
const GET_BRANCHES_URL = `repos/${this.repoOwner}/${this.repo}/branches/${branch}`;
return this.client.get(GET_BRANCHES_URL);
}
createSyncBranch(developSha, syncBranchName) {
const CREATE_BRANCH_URL = `repos/${this.repoOwner}/${this.repo}/git/refs`;
const postData = {
ref: `refs/heads/${syncBranchName}`,
sha: developSha
};
return this.client.post(CREATE_BRANCH_URL, postData);
}
async mergeBranches(fromBranch, toBranch) {
// This method needs to be async so an error with the merge can be caught and wrapped in a
// custom MergeError. That way we can handle it differently.
const MERGE_BRANCH_URL = `/repos/${this.repoOwner}/${this.repo}/merges`;
const postData = {
head: fromBranch,
base: toBranch,
commit_message: `Merge ${fromBranch} into ${toBranch}`
};
try {
return await this.client.post(MERGE_BRANCH_URL, postData);
} catch (error) {
throw new MergeError(error.message);
}
}
createPullRequest(fromBranch, toBranch) {
const CREATE_PULL_REQUEST_URL = `/repos/${this.repoOwner}/${this.repo}/pulls`;
const postData = {
title: `Merge ${fromBranch} into ${toBranch}`,
body: `Merge ${fromBranch} into ${toBranch}`,
head: fromBranch,
base: toBranch
};
return this.client.post(CREATE_PULL_REQUEST_URL, postData);
}
}
module.exports = { GitHubClient, MergeError }