Skip to content

Commit

Permalink
fix: handle duplicate jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Dec 11, 2024
1 parent 4170617 commit 8c92fd2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
1 change: 0 additions & 1 deletion crates/gh-workflow-tailcall/src/workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ impl Workflow {
GHWorkflow::new(self.name.clone())
.add_env(self.workflow_flags())
.on(self.workflow_event())
// TODO: adding build and lint should not be required
.add_job("build", self.test_job())
.add_job("lint", self.lint_job(false))
.add_job_when(
Expand Down
30 changes: 27 additions & 3 deletions crates/gh-workflow/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ fn organize_job_dependency(mut workflow: Workflow) -> Workflow {
// Prepare the job_ids
let mut job_ids = Vec::<String>::new();
for dep_job in dep_jobs.iter() {
// If the job is already defined in the workflow
if let Some(id) = workflow.get_id(dep_job) {
job_ids.push(id.to_string());
// If the job is already available
if let Some(id) = find_job(dep_job, &new_jobs, &workflow) {
job_ids.push(id.to_owned());
} else {
// Create a job-id for the job
let id = format!("job-{}", job_id);
Expand All @@ -127,3 +127,27 @@ fn organize_job_dependency(mut workflow: Workflow) -> Workflow {

workflow
}

fn find_job<'a>(
dep_job: &Job,
new_jobs: &'a IndexMap<String, Job>,
workflow: &'a Workflow,
) -> Option<&'a str> {
let in_new_jobs: Option<&'a str> =
new_jobs
.iter()
.find_map(|(k, v)| if v == dep_job { Some(k.as_str()) } else { None });

let in_old_jobs: Option<&'a str> = workflow.jobs.as_ref().and_then(|jobs| {
jobs.0.iter().find_map(|(id, j)| {
if j == dep_job {
Some(id.as_str())
} else {
None
}
})
});

let id = in_new_jobs.or(in_old_jobs);
id

Check failure on line 152 in crates/gh-workflow/src/generate.rs

View workflow job for this annotation

GitHub Actions / Lint

returning the result of a `let` binding from a block
}

0 comments on commit 8c92fd2

Please sign in to comment.