Skip to content

Commit d56a210

Browse files
authored
Merge pull request #2234 from Kobzol/rustc-pull-error-code
Make rustc pulls on CI more frequent
2 parents f61d56b + c45e1e9 commit d56a210

File tree

3 files changed

+65
-14
lines changed

3 files changed

+65
-14
lines changed

Diff for: .github/workflows/rustc-pull.yml

+33-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ name: rustc-pull
33
on:
44
workflow_dispatch:
55
schedule:
6-
# Run at 04:00 UTC every Monday
7-
- cron: '0 4 * * 1'
6+
# Run at 04:00 UTC every day
7+
- cron: '0 4 * * *'
88

99
jobs:
1010
pull:
@@ -34,15 +34,33 @@ jobs:
3434
git config --global user.name 'The rustc-dev-guide Cronjob Bot'
3535
git config --global user.email '[email protected]'
3636
- name: Perform rustc-pull
37-
run: cargo run --manifest-path josh-sync/Cargo.toml -- rustc-pull
37+
id: rustc-pull
38+
# Turn off -e to disable early exit
39+
shell: bash {0}
40+
run: |
41+
cargo run --manifest-path josh-sync/Cargo.toml -- rustc-pull
42+
exitcode=$?
43+
44+
# If no pull was performed, we want to mark this job as successful,
45+
# but we do not want to perform the follow-up steps.
46+
if [ $exitcode -eq 0 ]; then
47+
echo "pull_result=pull-finished" >> $GITHUB_OUTPUT
48+
elif [ $exitcode -eq 2 ]; then
49+
echo "pull_result=skipped" >> $GITHUB_OUTPUT
50+
exitcode=0
51+
fi
52+
53+
exit ${exitcode}
3854
- name: Push changes to a branch
55+
if: ${{ steps.rustc-pull.outputs.pull_result == 'pull-finished' }}
3956
run: |
4057
# Update a sticky branch that is used only for rustc pulls
4158
BRANCH="rustc-pull"
4259
git switch -c $BRANCH
4360
git push -u origin $BRANCH --force
4461
- name: Create pull request
4562
id: update-pr
63+
if: ${{ steps.rustc-pull.outputs.pull_result == 'pull-finished' }}
4664
run: |
4765
# Check if an open pull request for an rustc pull update already exists
4866
# If it does, the previous push has just updated it
@@ -54,6 +72,7 @@ jobs:
5472
echo "pr_url=$PR_URL" >> $GITHUB_OUTPUT
5573
else
5674
PR_URL=`gh pr list --author github-actions[bot] --state open -q 'map(select(.title=="Rustc pull update")) | .[0].url' --json url,title`
75+
echo "Updating pull request ${PR_URL}"
5776
echo "pr_url=$PR_URL" >> $GITHUB_OUTPUT
5877
fi
5978
env:
@@ -64,16 +83,23 @@ jobs:
6483
runs-on: ubuntu-latest
6584
steps:
6685
- name: Compute message
67-
id: message
86+
id: create-message
6887
run: |
69-
if [ "${{ needs.pull.result }}" == "failure" ];
70-
then
88+
if [ "${{ needs.pull.result }}" == "failure" ]; then
7189
WORKFLOW_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
7290
echo "message=Rustc pull sync failed. Check out the [workflow URL]($WORKFLOW_URL)." >> $GITHUB_OUTPUT
7391
else
74-
echo "message=Rustc pull sync succeeded. Check out the [PR](${{ needs.pull.outputs.pr_url }})." >> $GITHUB_OUTPUT
92+
CREATED_AT=`gh pr list --author github-actions[bot] --state open -q 'map(select(.title=="Rustc pull update")) | .[0].createdAt' --json createdAt,title`
93+
PR_URL=`gh pr list --author github-actions[bot] --state open -q 'map(select(.title=="Rustc pull update")) | .[0].url' --json url,title`
94+
week_ago=$(date +%F -d '7 days ago')
95+
96+
# If there is an open PR that is at least a week old, post a message about it
97+
if [[ -n $DATE_GH && $DATE_GH < $week_ago ]]; then
98+
echo "message=A PR with a Rustc pull has been opened for more a week. Check out the [PR](${PR_URL})." >> $GITHUB_OUTPUT
99+
fi
75100
fi
76101
- name: Send a Zulip message about updated PR
102+
if: ${{ steps.create-message.outputs.message != '' }}
77103
uses: zulip/github-actions-zulip/send-message@e4c8f27c732ba9bd98ac6be0583096dea82feea5
78104
with:
79105
api-key: ${{ secrets.ZULIP_API_TOKEN }}

Diff for: josh-sync/src/main.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clap::Parser;
2-
use crate::sync::GitSync;
2+
use crate::sync::{GitSync, RustcPullError};
33

44
mod sync;
55

@@ -22,7 +22,18 @@ fn main() -> anyhow::Result<()> {
2222
let sync = GitSync::from_current_dir()?;
2323
match args {
2424
Args::RustcPull => {
25-
sync.rustc_pull(None)?;
25+
if let Err(error) = sync.rustc_pull(None) {
26+
match error {
27+
RustcPullError::NothingToPull => {
28+
eprintln!("Nothing to pull");
29+
std::process::exit(2);
30+
}
31+
RustcPullError::PullFailed(error) => {
32+
eprintln!("Pull failure: {error:?}");
33+
std::process::exit(1);
34+
}
35+
}
36+
}
2637
}
2738
Args::RustcPush { github_username, branch } => {
2839
sync.rustc_push(github_username, branch)?;

Diff for: josh-sync/src/sync.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ const JOSH_FILTER: &str = ":/src/doc/rustc-dev-guide";
1111
const JOSH_PORT: u16 = 42042;
1212
const UPSTREAM_REPO: &str = "rust-lang/rust";
1313

14+
pub enum RustcPullError {
15+
/// No changes are available to be pulled.
16+
NothingToPull,
17+
/// A rustc-pull has failed, probably a git operation error has occurred.
18+
PullFailed(anyhow::Error)
19+
}
20+
21+
impl<E> From<E> for RustcPullError where E: Into<anyhow::Error> {
22+
fn from(error: E) -> Self {
23+
Self::PullFailed(error.into())
24+
}
25+
}
26+
1427
pub struct GitSync {
1528
dir: PathBuf,
1629
}
@@ -24,7 +37,7 @@ impl GitSync {
2437
})
2538
}
2639

27-
pub fn rustc_pull(&self, commit: Option<String>) -> anyhow::Result<()> {
40+
pub fn rustc_pull(&self, commit: Option<String>) -> Result<(), RustcPullError> {
2841
let sh = Shell::new()?;
2942
sh.change_dir(&self.dir);
3043
let commit = commit.map(Ok).unwrap_or_else(|| {
@@ -38,7 +51,7 @@ impl GitSync {
3851
})?;
3952
// Make sure the repo is clean.
4053
if cmd!(sh, "git status --untracked-files=no --porcelain").read()?.is_empty().not() {
41-
bail!("working directory must be clean before performing rustc pull");
54+
return Err(anyhow::anyhow!("working directory must be clean before performing rustc pull").into());
4255
}
4356
// Make sure josh is running.
4457
let josh = Self::start_josh()?;
@@ -47,7 +60,7 @@ impl GitSync {
4760

4861
let previous_base_commit = sh.read_file("rust-version")?.trim().to_string();
4962
if previous_base_commit == commit {
50-
return Err(anyhow::anyhow!("No changes since last pull"));
63+
return Err(RustcPullError::NothingToPull);
5164
}
5265

5366
// Update rust-version file. As a separate commit, since making it part of
@@ -94,12 +107,13 @@ impl GitSync {
94107
cmd!(sh, "git reset --hard HEAD^")
95108
.run()
96109
.expect("FAILED to clean up after creating the preparation commit");
97-
return Err(anyhow::anyhow!("No merge was performed, nothing to pull. Rolled back the preparation commit."));
110+
eprintln!("No merge was performed, no changes to pull were found. Rolled back the preparation commit.");
111+
return Err(RustcPullError::NothingToPull);
98112
}
99113

100114
// Check that the number of roots did not increase.
101115
if num_roots()? != num_roots_before {
102-
bail!("Josh created a new root commit. This is probably not the history you want.");
116+
return Err(anyhow::anyhow!("Josh created a new root commit. This is probably not the history you want.").into());
103117
}
104118

105119
drop(josh);

0 commit comments

Comments
 (0)