-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilds.R
176 lines (158 loc) · 4.67 KB
/
builds.R
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#' @title Retrieve Metadata from Circle CI Builds
#' @description Query information about pipelines, workflows or jobs on
#' Circle CI.
#' The S3 `print()` method for these functions returns the respective
#' pipeline IDs.
#' To inspect the details of each pipeline, save the return value in an object
#' and inspect the respective sub-lists.
#'
#' If no pipeline or workflow is supplied to `get_workflows()`/`get_jobs()`,
#' the ten most recent pipelines/jobs are queried, respectively.
#'
#' @details While the `get_*()` functions query information about the respective
#' build level details (pipeline - workflow - job), `retry_workflow()` let's
#' users rerun a specific workflow.
#' By default, the workflow from the most recent pipeline will be rerun if
#' no pipeline ID was supplied.
#'
#' @template repo
#' @template user
#' @template vcs
#' @param limit `[integer]`\cr
#' How many builds should be returned? Maximum allowed by Circle is
#' 30.
#' @template api_version
#' @return An object of class `circle_collection` containing list
#' information on the queried Circle CI pipelines/workflows/jobs.
#'
#' @name builds
#' @export
#' @examples
#' \dontrun{
#' pipelines <- get_pipelines()
#'
#' workflows <- get_workflows()
#'
#' jobs <- get_jobs()
#'
#' # rerun most recent workflow
#' retry_workflow()
#' }
get_pipelines <- function(repo = github_info()$name,
user = github_info()$owner$login,
limit = 30,
vcs_type = "gh",
api_version = "v2") {
if (is.null(repo)) {
repo <- github_info()$name # nocov
}
resp <- circle("GET",
path = sprintf(
"/project/%s/%s/%s/pipeline",
vcs_type, user, repo
),
api_version = api_version,
query = list(limit = limit)
)
stop_for_status(
resp$response,
sprintf("get pipelines for repo %s/%s on Circle CI", user, repo)
)
return(new_circle_pipelines(content(resp$response)))
}
#' @param pipeline_id `[character]`\cr
#' A Circle CI pipeline ID.
#' @rdname builds
#' @export
get_workflows <- function(pipeline_id = NULL,
repo = github_info()$name,
user = github_info()$owner$login) {
if (is.null(pipeline_id)) {
cli::cli_ul("{.fun get_pipelines}: ID not given, querying {.field 10} most recent
pipelines.")
pipeline_id <- vapply(seq_along(1:10), function(x) {
get_pipelines(repo = repo, user = user)[[x]]$id
}, FUN.VALUE = character(1))
}
resp <- lapply(pipeline_id, function(x) {
resp <- circle("GET",
path = sprintf(
"/pipeline/%s/workflow",
x
),
api_version = "v2"
)
stop_for_status(
resp$response,
sprintf(
"get workflows for pipeline '%s' for repo %s/%s on Circle CI.",
x, user, repo
)
)
new_circle_workflows(content(resp$response))
})
resp <- unlist(resp, recursive = FALSE)
class(resp) <- c("circle_collection", "circle")
return(resp)
}
#' @param workflow_id `[character]`\cr
#' A Circle CI workflow ID.
#' @rdname builds
#' @export
get_jobs <- function(workflow_id = NULL,
repo = github_info()$name,
user = github_info()$owner$login,
vcs_type = "gh") {
if (is.null(workflow_id)) {
cli::cli_ul("{.fun get_workflows}: ID not given, querying {.field 10} most
recent workflows.")
workflow_id <- get_workflows(repo = repo, user = user)
workflow_id <- vapply(workflow_id, function(x) x$id,
FUN.VALUE = character(1)
)
}
resp <- lapply(workflow_id, function(x) {
resp <- circle("GET",
path = sprintf(
"/workflow/%s/job",
x
),
api_version = "v2"
)
stop_for_status(
resp$response,
sprintf(
"get jobs for workflow '%s' for repo %s/%s on Circle CI.",
x, user, repo
)
)
new_circle_jobs(content(resp$response))
})
resp <- unlist(resp, recursive = FALSE)
class(resp) <- c("circle_collection", "circle")
return(resp)
}
#' @param workflow_id `[character]`\cr
#' A Circle CI workflow ID.
#' @rdname builds
#' @export
retry_workflow <- function(workflow_id = NULL) {
if (is.null(workflow_id)) { # nocov start
cli::cli_ul("{.fun retry_workflow}: ID not given, getting the last workflow.")
workflow_id <- get_workflows(
repo = github_info()$name,
user = github_info()$owner$login
)[[1]]$id
} # nocov end
resp <- circle("POST",
path = sprintf(
"/workflow/%s/rerun",
workflow_id
)
)
stop_for_status(
resp$response,
sprintf("restarting workflow '%s' on Circle CI", workflow_id)
)
return(resp)
}