Repository: srvaroa/labeler
Pull request #4 (2020-03-26 18:31:24)

Dogfood
Signed-off-by: Galo Navarro <anglorvaroa@gmail.com>


Patch:
diff --git a/.github/labeler.yml b/.github/labeler.yml
new file mode 100644
index 0000000..9be56d7
--- /dev/null
+++ b/.github/labeler.yml
@@ -0,0 +1,2 @@
+  TestLabel:
+        title: ".*"
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
new file mode 100644
index 0000000..8020f88
--- /dev/null
+++ b/.github/workflows/main.yml
@@ -0,0 +1,14 @@
+name: Label PRs
+
+on:
+  - pull_request
+
+jobs:
+  build:
+
+    runs-on: ubuntu-latest
+
+    steps:
+      - uses: srvaroa/labeler@master
+        env:
+          GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
diff --git a/README.md b/README.md
index c190e52..f9a5ba8 100644
--- a/README.md
+++ b/README.md
@@ -10,7 +10,7 @@ richer set of options.
 
 ## Installing
 
-Add a .github/workflows/main.yml file to your repository with these
+Add a file `.github/workflows/main.yml` to your repository with these
 contents:
 
 	name: Label PRs
@@ -28,8 +28,16 @@ contents:
 		  env:
 			GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
 
-Then, add a ./github/labeler.yml with the configuration as described
-below.
+Then add a new file `./github/labeler.yml` with the configuration as
+described below in the `Configuration` section.
+
+This action will avoid failing in all cases, so it's worth looking at
+execution logs just in case.  Typical errors are:
+
+* The configuration file is non existent, or has invalid yaml.
+* Running the action from a fork, as the `GITHUB_TOKEN` has not enough
+  permissions to label the main repository ([issue for
+  solving this](https://github.com/srvaroa/labeler/issues/3))
 
 ## Configuration
 
diff --git a/cmd/action.go b/cmd/action.go
index d1818ab..9401a00 100644
--- a/cmd/action.go
+++ b/cmd/action.go
@@ -2,7 +2,6 @@ package main
 
 import (
 	"context"
-	"fmt"
 	"io/ioutil"
 	"log"
 	"os"
@@ -27,12 +26,18 @@ func main() {
 	// actions to use this approach, and I will need to consider
 	// whatever branch is set as main in the repo settings, so leaving
 	// as this for now.
-	configRaw := getRepoFile(gh,
+	configRaw, err := getRepoFile(gh,
 		os.Getenv("GITHUB_REPOSITORY"),
 		os.Getenv("INPUT_CONFIG_PATH"),
 		os.Getenv("GITHUB_SHA"))
+	if err != nil {
+		return
+	}
 
-	config := getLabelerConfig(configRaw)
+	config, err := getLabelerConfig(configRaw)
+	if err != nil {
+		return
+	}
 
 	log.Printf("Re-evaluating labels on %s@%s",
 		os.Getenv("GITHUB_REPOSITORY"),
@@ -40,14 +45,14 @@ func main() {
 
 	log.Printf("Trigger event: %s", os.Getenv("GITHUB_EVENT_NAME"))
 
-	err := newLabeler(gh, config).HandleEvent(eventName, eventPayload)
+	err = newLabeler(gh, config).HandleEvent(eventName, eventPayload)
 	if err != nil {
 		log.Printf("Unable to execute action: %+v", err)
 	}
 
 }
 
-func getRepoFile(gh *github.Client, repo, file, sha string) *[]byte {
+func getRepoFile(gh *github.Client, repo, file, sha string) (*[]byte, error) {
 
 	t := strings.Split(repo, "/")
 	owner, repoName := t[0], t[1]
@@ -65,28 +70,25 @@ func getRepoFile(gh *github.Client, repo, file, sha string) *[]byte {
 	}
 
 	if err != nil {
-		log.Fatalf("Unable to load configuration from %s@%s/%s: %s",
+		log.Printf("Unable to load configuration from %s@%s/%s: %s",
 			repo, sha, file, err)
+		return nil, err
 	}
 
 	log.Printf("Loaded config from %s@%s:%s\n--\n%s", repo, sha, file, content)
 
 	raw := []byte(content)
-	return &raw
+	return &raw, err
 }
 
 // getLabelerConfig builds a LabelerConfig from a raw yaml
-func getLabelerConfig(configRaw *[]byte) *labeler.LabelerConfig {
-
+func getLabelerConfig(configRaw *[]byte) (*labeler.LabelerConfig, error) {
 	var c labeler.LabelerConfig
-
 	err := yaml.Unmarshal(*configRaw, &c)
 	if err != nil {
-		log.Fatalf("Unable to unmarshall config --\n%s\n--, %s", configRaw, err)
+		log.Printf("Unable to unmarshall config --\n%s\n--, %s", configRaw, err)
 	}
-	fmt.Printf("The config: %+v", c)
-
-	return &c
+	return &c, err
 }
 
 func getGithubClient() *github.Client {


Test patch:
diff --git a/cmd/action_test.go b/cmd/action_test.go
index d5b89ca..12c7bcd 100644
--- a/cmd/action_test.go
+++ b/cmd/action_test.go
@@ -21,7 +21,12 @@ func TestGetLabelerConfig(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	c := getLabelerConfig(&contents)
+	var c *l.LabelerConfig
+	c, err = getLabelerConfig(&contents)
+	if err != nil {
+		t.Fatal(err)
+	}
+
 	expect := l.LabelerConfig{
 		"WIP": l.LabelMatcher{
 			Title: "^WIP:.*",