Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
147 lines (132 loc) · 4.4 KB
/
Copy pathindex.js
File metadata and controls
147 lines (132 loc) · 4.4 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
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
const core = require('@actions/core');
const exec = require('@actions/exec');
const github = require('@actions/github');
const ghClient = new github.GitHub(process.env.GITHUB_TOKEN);
let execLogs = '';
const execOptions = {
listeners: {
stdout: (data) => {
execLogs += data.toString();
},
stderr: (data) => {
execLogs += data.toString();
}
}
}
const git = (args) => {
return exec.exec('git', args, execOptions);
}
const rebase = async (args) => {
console.log(args)
await git(['config', '--local', 'user.name', args.username ]);
await git(['config', '--local', 'user.email', args.email ]);
await git(['remote', 'add', 'fork', `https://github.com/${args.repo}.git`]);
await git(['fetch', 'origin', args.base_branch]);
await git(['fetch', 'fork', args.head_branch]);
await git(['checkout', '-b', args.head_branch, `fork/${args.head_branch}`]);
await git(['rebase', `origin/${args.base_branch}`]);
await git(['push', '--force-with-lease', 'fork', args.head_branch]);
};
const getMardownGif = async (giphy, phrase) => {
const gif = await giphy.random(phrase);
return ``
}
const gifComment = (comment, gif, details) => {
return `${comment}\n\n${gif}`+
`\n<details><summary>Details</summary>\n` +
`\n<p>\n\n`+
`\`\`\`bash\n`+
`${details}`+
`\n\`\`\`\n`+
`</p>\n`+
`</details>`
}
const run = async () => {
const giphy_key = core.getInput('giphy-key');
const giphy = require('giphy-api')(giphy_key);
const context = await github.context;
const pull_number = context.payload.issue.number;
const owner = context.payload.repository.full_name.split('/')[0];
const repo = context.payload.repository.name;
if (! context.payload.comment.html_url.includes('pull')) {
core.setFailed(`this is not a pr comment: ${JSON.stringify(context.payload)}`);
}
const initialPr = {
owner,
repo,
pull_number
}
let pr = await ghClient.pulls.get(initialPr);
const prInfo = {
rebaseable: pr.data.rebaseable,
merged: pr.data.merged,
base_branch: pr.data.base.ref,
head_branch: pr.data.head.ref
}
// check if rebaseable
if (prInfo.merged) {
const gif = await getMardownGif(giphy, 'what?');
await ghClient.issues.createComment({
owner,
repo,
issue_number: pull_number,
body: gifComment(
`:man_shrugging: github says this pr is already merged...`,
gif,
JSON.stringify(pr.data, null, 2),
),
})
core.setFailed('already merged');
process.exit(1);
} else if (prInfo.rebaseable == 'false') {
const gif = await getMardownGif(giphy, 'no no!');
await ghClient.issues.createComment({
owner,
repo,
issue_number: pull_number,
body: gifComment(
`:no_entry_sign: github says this pr is not rebaseable...`,
gif,
JSON.stringify(pr.data, null, 2),
),
});
core.setFailed('not rebaseable');
process.exit(1);
}
const username = context.payload.comment.user.login;
const { email } = await ghClient.users.getByUsername({ username });
// start actual rebase
try {
await rebase({
repo: context.payload.repository.full_name,
email,
username,
...prInfo
});
const gif = await getMardownGif(giphy, 'whoop whoop');
await ghClient.issues.createComment({
owner,
repo,
issue_number: pull_number,
body: gifComment(
`:sun_with_face: successfully rebased!!!`,
gif,
execLogs
),
});
} catch (error) {
const gif = await getMardownGif(giphy, 'epic fail');
await ghClient.issues.createComment({
owner,
repo,
issue_number: pull_number,
body: gifComment(
`:exclamation: i tried the rebase and failed...`,
gif,
`${error}:\n${execLogs}`
),
});
core.setFailed(execLogs);
}
};
run();