-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcreate_pull_request.rs
More file actions
88 lines (78 loc) · 3.51 KB
/
create_pull_request.rs
File metadata and controls
88 lines (78 loc) · 3.51 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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use async_trait::async_trait;
use tracing::instrument;
use crate::rfd::PersistedRfd;
use super::{
RfdUpdateAction, RfdUpdateActionContext, RfdUpdateActionErr, RfdUpdateActionResponse,
RfdUpdateMode,
};
#[derive(Debug)]
pub struct CreatePullRequest;
#[async_trait]
impl RfdUpdateAction for CreatePullRequest {
#[instrument(skip(self, ctx, new), err(Debug))]
async fn run(
&self,
ctx: &mut RfdUpdateActionContext,
new: &mut PersistedRfd,
mode: RfdUpdateMode,
) -> Result<RfdUpdateActionResponse, RfdUpdateActionErr> {
let RfdUpdateActionContext {
ctx,
update,
pull_requests,
previous,
..
} = ctx;
// We only ever create pull requests if the RFD is in the discussion or ideation state, we are not
// handling an update on the default branch, and there are no previous pull requests for
// for this branch. This includes Closed pull requests, therefore this action will not
// re-open or create a new pull request for a branch that previously had an open PR
if update.location.branch != update.location.default_branch
&& (new.is_state("discussion") || new.is_state("ideation"))
&& pull_requests.is_empty()
{
tracing::info!("RFD is in the discussion state but there are no open pull requests, creating a new pull request");
if mode == RfdUpdateMode::Write {
let pull = ctx
.github
.client
.pulls()
.create(
&update.location.owner,
&update.location.repo,
&octorust::types::PullsCreateRequest {
title: new.name(),
head: format!(
"{}:{}",
ctx.github.repository.owner, update.location.branch
),
base: update.location.default_branch.to_string(),
body: "Automatically opening the pull request since the document \
is marked as being in discussion. If you wish to not have \
a pull request open, change the state of your document and \
close this pull request."
.to_string(),
draft: Some(false),
maintainer_can_modify: Some(true),
issue: 0,
},
)
.await
.map_err(|err| RfdUpdateActionErr::Continue(Box::new(err)))?
.body;
tracing::info!(
old_state = ?previous.map(|rfd| &rfd.revision.state), new_state = new.revision.state, new_pr = pull.number,
"Opened new pull request for discussion"
);
// Add the newly created pull request into the context for future actions
pull_requests.push(pull.into());
}
} else {
tracing::debug!("RFD does not require a pull request or one already exists");
}
Ok(RfdUpdateActionResponse::default())
}
}