This repository was archived by the owner on Dec 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
73 lines (67 loc) · 2.29 KB
/
utils.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
const fs = require('fs')
module.exports = class Utils {
/**
* Setup some variables for the class
*
* @param {import('node-docker-api').Docker} docker The docker instance
* @param {String} prFolder The folder containing the PRs
*/
static setup (docker, prFolder) {
this.docker = docker
this.prFolder = prFolder
}
/**
* Append the given text onto the end of the given comment
*
* @param {import('probot').Context} context The webhook context
* @param {String} repoOwner The user/org that owns the repo
* @param {String} repoName The name of the repo
* @param {import('probot').Octokit.IssuesCreateCommentResponse} comment The existing comment
* @param {String} appendText The message to append
*
* @returns {import('probot').Octokit.IssuesCreateCommentResponse} The new comment
*/
static async appendComment (context, repoOwner, repoName, comment, appendText) {
return (await context.github.issues.updateComment({ owner: repoOwner, repo: repoName, comment_id: comment.id, body: comment.body + appendText })).data
}
/**
* Get a nicely formatted date
*
* @returns {String} A date string in the format YYYY-MM-DD HH:MM:SS UTC
*/
static getNiceDate () {
return new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '') + ' UTC'
}
/**
* Remove a container and call the callback functions as needed
*
* @param {String} containerName The name of the container to remove
* @param {Function} callback The callback function
* @param {Function} errorCallback The error callback function
*/
static removeContainer (containerName, callback, errorCallback) {
const existingContainer = this.docker.container.get(containerName)
existingContainer.stop({ t: 10 })
.then(() => {
if (callback !== undefined) {
callback()
}
})
.catch((error) => {
if (errorCallback !== undefined) {
errorCallback(error)
}
})
}
/**
* Remove the container folder if it exists
*
* @param {String} prNumber The PR to remove the container of
*/
static removeContainerFolder (prNumber) {
const individualPRFolder = `${this.prFolder}/${prNumber}`
if (fs.existsSync(individualPRFolder)) {
fs.rmdirSync(individualPRFolder, { recursive: true })
}
}
}