-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_dependency.js
150 lines (123 loc) · 3.87 KB
/
update_dependency.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const fetch = require('node-fetch');
/*
TODO:
- add proper handle errors
- add posibility to include multiple packages
- test it properly
*/
const USERNAME = '<username>';
const PASSWORD = '<password>';
const AUTH_TOKEN = null; // <auth_token> this can be repository access token or any other token
const Authorization_Header = AUTH_TOKEN
? `Bearer ${AUTH_TOKEN}`
: `Basic ${Buffer.from(`${USERNAME}:${PASSWORD}`).toString('base64')}`
const repositoryOwner = process.argv[2];
const repositoryName = process.argv[3];
const packageName = process.argv[4];
const packageVersion = process.argv[5];
const BASE_URL = `https://api.bitbucket.org/2.0/repositories/${repositoryOwner}/${repositoryName}`;
const newDeps = {
dependencies: {
[packageName]: packageVersion
}
};
async function getBranch() {
const res = await fetch(
`${BASE_URL}/${repositoryOwner}/${repositoryName}/refs/branches`,
{
headers: {
Authorization: Authorization_Header
}
}
);
if (!res.ok) {
throw new Error(`Failed to get branches: ${res.statusText}`);
}
const { values } = await res.json();
return values[0].name;
}
async function updatePackageJson(branch_name) {
// Get the contents of package.json
const res = await fetch(`${BASE_URL}/src/${branch_name}/package.json`, {
headers: {
Authorization: Authorization_Header,
'Content-Type': 'application/json'
}
});
if (!res.ok) {
throw new Error(`Failed to get package.json: ${res.statusText}`);
}
const packageJson = await res.json();
// Update the dependencies or devDependencies section with the new packages
Object.entries(newDeps.dependencies).forEach((deps) => {
packageJson.dependencies[deps[0]] = deps[1];
});
// Put the updated contents back to the repository
const putRes = await fetch(`${BASE_URL}/src/${branch_name}/package.json`, {
method: 'PUT',
headers: {
'Authorization': Authorization_Header,
'Content-Type': 'application/json'
},
body: JSON.stringify(packageJson)
});
if (!putRes.ok) {
throw new Error(`Failed to update package.json: ${putRes.statusText}`);
}
console.log(`Successfully updated package.json in branch "${branch_name}"`);
}
async function createBranch(branch_name) {
const res = await fetch(
`${BASE_URL}/refs/branches`,
{
method: 'POST',
headers: {
Authorization: Authorization_Header,
'Content-Type': 'application/json'
},
body: JSON.stringify({ name: branch_name })
}
);
if (!res.ok) {
throw new Error(`Failed to create branch: ${res.statusText}`);
}
}
async function createPullRequest(source_branch, destination_branch) {
const res = await fetch(
`${BASE_URL}/pullrequests`,
{
method: 'POST',
headers: {
Authorization: Authorization_Header,
'Content-Type': 'application/json'
},
body: JSON.stringify({
title: `Update ${packageName} to ${packageVersion}`,
description: `This pull request updates the ${packageName} dependency to version ${packageVersion}`,
source: {
branch: {
name: source_branch
}
},
destination: {
branch: {
name: destination_branch
}
},
close_source_branch: true
})
}
);
if (!res.ok) {
throw new Error(`Failed to create pull request: ${res.statusText}`);
}
}
async function updateAndCreatePullRequest() {
const branch_name = `update-${packageName}`;
const source_branch_name = getBranch();
await createBranch(branch_name);
await updatePackageJson(branch_name);
await createPullRequest(branch_name, source_branch_name);
console.log(`Successfully updated ${packageName} to ${packageVersion} and created pull request`);
}
updateAndCreatePullRequest().catch(console.error);