91 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Nix
		
	
	
	
	
	
			
		
		
	
	
			91 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Nix
		
	
	
	
	
	
| { pulls, branches, ... }:
 | |
| let
 | |
|   # create the json spec for the jobset
 | |
|   makeSpec =
 | |
|     contents:
 | |
|     builtins.derivation {
 | |
|       name = "spec.json";
 | |
|       system = "x86_64-linux";
 | |
|       preferLocalBuild = true;
 | |
|       allowSubstitutes = false;
 | |
|       builder = "/bin/sh";
 | |
|       args = [
 | |
|         (builtins.toFile "builder.sh" ''
 | |
|           echo "$contents" > $out
 | |
|         '')
 | |
|       ];
 | |
|       contents = builtins.toJSON contents;
 | |
|     };
 | |
| 
 | |
|   prs = readJSONFile pulls;
 | |
|   refs = readJSONFile branches;
 | |
| 
 | |
|   # template for creating a job
 | |
|   makeJob =
 | |
|     {
 | |
|       schedulingshares ? 10,
 | |
|       keepnr ? 3,
 | |
|       description,
 | |
|       flake,
 | |
|       enabled ? 1,
 | |
|     }:
 | |
|     {
 | |
|       inherit
 | |
|         description
 | |
|         flake
 | |
|         schedulingshares
 | |
|         keepnr
 | |
|         enabled
 | |
|         ;
 | |
|       type = 1;
 | |
|       hidden = false;
 | |
|       checkinterval = 300; # every 5 minutes
 | |
|       enableemail = false;
 | |
|       emailoverride = "";
 | |
|     };
 | |
| 
 | |
|   giteaHost = "ssh://gitea@nayeonie.com:2222";
 | |
|   repo = "ahuston-0/nix-dotfiles";
 | |
|   # # Create a hydra job for a branch
 | |
|   jobOfRef =
 | |
|     name:
 | |
|     { ref, ... }:
 | |
|     if ((builtins.match "^refs/heads/(.*)$" ref) == null) then
 | |
|       null
 | |
|     else
 | |
|       {
 | |
|         name = builtins.replaceStrings [ "/" ] [ "-" ] "branch-${name}";
 | |
|         value = makeJob {
 | |
|           description = "Branch ${name}";
 | |
|           flake = "git+${giteaHost}/${repo}?ref=${ref}";
 | |
|         };
 | |
|       };
 | |
| 
 | |
|   # Create a hydra job for a PR
 | |
|   jobOfPR = id: info: {
 | |
|     name = if info.draft then "draft-${id}" else "pr-${id}";
 | |
|     value = makeJob {
 | |
|       description = "PR ${id}: ${info.title}";
 | |
|       flake = "git+${giteaHost}/${repo}?ref=${info.head.ref}";
 | |
|       enabled = info.state == "open";
 | |
|     };
 | |
|   };
 | |
| 
 | |
|   # some utility functions
 | |
|   # converts json to name/value dicts
 | |
|   attrsToList = l: builtins.attrValues (builtins.mapAttrs (name: value: { inherit name value; }) l);
 | |
|   # wrapper function for reading json from file
 | |
|   readJSONFile = f: builtins.fromJSON (builtins.readFile f);
 | |
|   # remove null values from a set, in-case of branches that don't exist
 | |
|   mapFilter = f: l: builtins.filter (x: (x != null)) (map f l);
 | |
| 
 | |
|   # Create job set from PRs and branches
 | |
|   jobs = makeSpec (
 | |
|     builtins.listToAttrs (map ({ name, value }: jobOfPR name value) (attrsToList prs))
 | |
|     // builtins.listToAttrs (mapFilter ({ name, value }: jobOfRef name value) (attrsToList refs))
 | |
|   );
 | |
| in
 | |
| {
 | |
|   jobsets = jobs;
 | |
| }
 |