|  | @@ -1,6 +1,7 @@
 | 
	
		
			
			| 1 | 1 |  package main
 | 
	
		
			
			| 2 | 2 |  
 | 
	
		
			
			| 3 | 3 |  import (
 | 
	
		
			
			|  | 4 | +	"encoding/json"
 | 
	
		
			
			| 4 | 5 |  	"io/ioutil"
 | 
	
		
			
			| 5 | 6 |  	"strings"
 | 
	
		
			
			| 6 | 7 |  
 | 
	
	
		
			
			|  | @@ -58,16 +59,28 @@ func HandlePush(payload interface{}, header webhooks.Header) {
 | 
	
		
			
			| 58 | 59 |  			continue
 | 
	
		
			
			| 59 | 60 |  		}
 | 
	
		
			
			| 60 | 61 |  
 | 
	
		
			
			| 61 |  | -		// Push all added files, except the ones which name starts with "test"
 | 
	
		
			
			|  | 62 | +		// Push all added files, except the ones describing a dashboard which
 | 
	
		
			
			|  | 63 | +		// name starts with a the prefix specified in the configuration file.
 | 
	
		
			
			| 62 | 64 |  		for _, addedFile := range commit.Added {
 | 
	
		
			
			| 63 |  | -			if !strings.HasPrefix(addedFile, "test") {
 | 
	
		
			
			|  | 65 | +			ignored, err := isIgnored(addedFile)
 | 
	
		
			
			|  | 66 | +			if err != nil {
 | 
	
		
			
			|  | 67 | +				panic(err)
 | 
	
		
			
			|  | 68 | +			}
 | 
	
		
			
			|  | 69 | +
 | 
	
		
			
			|  | 70 | +			if !ignored {
 | 
	
		
			
			| 64 | 71 |  				filesToPush[addedFile] = true
 | 
	
		
			
			| 65 | 72 |  			}
 | 
	
		
			
			| 66 | 73 |  		}
 | 
	
		
			
			| 67 | 74 |  
 | 
	
		
			
			| 68 |  | -		// Push all modified files, except the ones which name starts with "test"
 | 
	
		
			
			|  | 75 | +		// Push all modified files, except the ones describing a dashboard which
 | 
	
		
			
			|  | 76 | +		// name starts with a the prefix specified in the configuration file.
 | 
	
		
			
			| 69 | 77 |  		for _, modifiedFile := range commit.Modified {
 | 
	
		
			
			| 70 |  | -			if !strings.HasPrefix(addedFile, "test") {
 | 
	
		
			
			|  | 78 | +			ignored, err := isIgnored(modifiedFile)
 | 
	
		
			
			|  | 79 | +			if err != nil {
 | 
	
		
			
			|  | 80 | +				panic(err)
 | 
	
		
			
			|  | 81 | +			}
 | 
	
		
			
			|  | 82 | +
 | 
	
		
			
			|  | 83 | +			if !ignored {
 | 
	
		
			
			| 71 | 84 |  				filesToPush[modifiedFile] = true
 | 
	
		
			
			| 72 | 85 |  			}
 | 
	
		
			
			| 73 | 86 |  		}
 | 
	
	
		
			
			|  | @@ -106,3 +119,39 @@ func pushFile(filename string) error {
 | 
	
		
			
			| 106 | 119 |  
 | 
	
		
			
			| 107 | 120 |  	return grafanaClient.CreateOrUpdateDashboard(slug, fileContent)
 | 
	
		
			
			| 108 | 121 |  }
 | 
	
		
			
			|  | 122 | +
 | 
	
		
			
			|  | 123 | +// isIgnored checks whether the file must be ignored, by checking if there's an
 | 
	
		
			
			|  | 124 | +// prefix for ignored files set in the configuration file, and if the dashboard
 | 
	
		
			
			|  | 125 | +// described in the file has a name that starts with this prefix. Returns an
 | 
	
		
			
			|  | 126 | +// error if there was an issue reading or decoding the file.
 | 
	
		
			
			|  | 127 | +// TODO: Optimise this part of the workflow, as all files get open twice (here
 | 
	
		
			
			|  | 128 | +// and in pushFile)
 | 
	
		
			
			|  | 129 | +func isIgnored(filename string) (bool, error) {
 | 
	
		
			
			|  | 130 | +	// If there's no prefix set, no file is ignored
 | 
	
		
			
			|  | 131 | +	if len(cfg.Grafana.IgnorePrefix) == 0 {
 | 
	
		
			
			|  | 132 | +		return false, nil
 | 
	
		
			
			|  | 133 | +	}
 | 
	
		
			
			|  | 134 | +
 | 
	
		
			
			|  | 135 | +	// Read the file's content
 | 
	
		
			
			|  | 136 | +	fileContent, err := ioutil.ReadFile(filename)
 | 
	
		
			
			|  | 137 | +	if err != nil {
 | 
	
		
			
			|  | 138 | +		return false, err
 | 
	
		
			
			|  | 139 | +	}
 | 
	
		
			
			|  | 140 | +
 | 
	
		
			
			|  | 141 | +	// Parse the file's content to find the dashboard's name
 | 
	
		
			
			|  | 142 | +	var dashboardName struct {
 | 
	
		
			
			|  | 143 | +		Name string `json:"title"`
 | 
	
		
			
			|  | 144 | +	}
 | 
	
		
			
			|  | 145 | +	if err = json.Unmarshal(fileContent, &dashboardName); err != nil {
 | 
	
		
			
			|  | 146 | +		return false, err
 | 
	
		
			
			|  | 147 | +	}
 | 
	
		
			
			|  | 148 | +
 | 
	
		
			
			|  | 149 | +	// Compare the lower case dashboar name to the prefix (which has already
 | 
	
		
			
			|  | 150 | +	// been lower cased when loading the configuration file)
 | 
	
		
			
			|  | 151 | +	lowerCaseName := strings.ToLower(dashboardName.Name)
 | 
	
		
			
			|  | 152 | +	if strings.HasPrefix(lowerCaseName, cfg.Grafana.IgnorePrefix) {
 | 
	
		
			
			|  | 153 | +		return true, nil
 | 
	
		
			
			|  | 154 | +	}
 | 
	
		
			
			|  | 155 | +
 | 
	
		
			
			|  | 156 | +	return false, nil
 | 
	
		
			
			|  | 157 | +}
 |