|
| 1 | +## Step 4: Create the JavaScript files for your action |
| 2 | + |
| 3 | +_Good job adding the metadata file! :dancer:_ |
| 4 | + |
| 5 | +## Files |
| 6 | + |
| 7 | +As you probably know, in JavaScript and other programming languages it is common to break your code into modules so that it is easier to read and maintain going forward. Since JavaScript actions are just programs written in JavaScript that run based on a specific trigger we are able to make our action code modular as well. |
| 8 | + |
| 9 | +To do so we will create two files. One of them will contain the logic to reach out to an external API and retrieve a joke for us, the other will call that module and print the joke to the actions console for us. We will be extending this functionality in our third and final action. |
| 10 | + |
| 11 | +### Fetching a joke |
| 12 | + |
| 13 | +**Joke API** |
| 14 | + |
| 15 | +The first file will be `joke.js` and it will fetch our joke for us. We will be using the [icanhazdadjoke API](https://icanhazdadjoke.com/api) for our action. This API does not require any authentication, but it does however that we set a few parameters in the [HTTP headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers). We need to point out what those are when we get to the code, however it is outside of the scope of this course to cover HTTP in any depth. |
| 16 | + |
| 17 | +When we make our request to this API we will get back a JSON Object in the response. That Object looks like this: |
| 18 | + |
| 19 | +``` |
| 20 | +{ |
| 21 | + id: '0LuXvkq4Muc', |
| 22 | + joke: "I knew I shouldn't steal a mixer from work, but it was a whisk I was willing to take.", |
| 23 | + status: 200 |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +It contains 3 key/value pairs of data that we can use in our own program or service. In our case, we are only interested in the `joke` field. |
| 28 | + |
| 29 | +**Joke Module** |
| 30 | + |
| 31 | +We will create a file named `joke.js` and it will reside in the `.github/action/joke-action` directory. |
| 32 | + |
| 33 | +The joke module will look like this: |
| 34 | + |
| 35 | +```javascript |
| 36 | +const request = require("request-promise"); |
| 37 | + |
| 38 | +const options = { |
| 39 | + method: "GET", |
| 40 | + uri: "https://icanhazdadjoke.com/", |
| 41 | + headers: { |
| 42 | + Accept: "application/json", |
| 43 | + "User-Agent": "Writing JavaScript action GitHub Skills course.", |
| 44 | + }, |
| 45 | + json: true, |
| 46 | +}; |
| 47 | + |
| 48 | +async function getJoke() { |
| 49 | + const res = await request(options); |
| 50 | + return res.joke; |
| 51 | +} |
| 52 | + |
| 53 | +module.exports = getJoke; |
| 54 | +``` |
| 55 | + |
| 56 | +Need an advanced description of the <code>joke.js</code> source code? |
| 57 | + |
| 58 | +We first bring in the `request-promise` library that we installed earlier using `npm`. |
| 59 | + |
| 60 | +Next we define a set of `options` that the `request-promise` library will use when it makes the request. |
| 61 | + |
| 62 | +Read more about [request-promise](https://github.com/request/request-promise/) |
| 63 | + |
| 64 | +Inside of the `options` block we add a key named `headers`. This defines the HTTP headers that the **icanhazdadjoke** API expects in each request that comes it's way. |
| 65 | + |
| 66 | +**icanhazdadjoke** cares the most about the keys, **Accept** and **User-Agent**, so we need to make sure we fill them in. |
| 67 | + |
| 68 | +Next we define an **asynchronous JavaScript function** to make the request for us, storing the JSON Object that is returned in a variable named `res`. |
| 69 | + |
| 70 | +Lastly, we `return` the `res.joke` which is only the value associated with the `joke` key of the JSON Object. This value will be random every time our action runs because of how we are interacting with the **icanhazdadjoke** API. |
| 71 | + |
| 72 | +This file finishes up by exporting the newly created function so that we can use it in our `main.js` file. |
| 73 | + |
| 74 | +### Creating the main entry point for your action |
| 75 | + |
| 76 | +**Main Module** |
| 77 | + |
| 78 | +We will also create a file named `main.js` that resides inside of the `.github/actions/joke-action` directory. |
| 79 | + |
| 80 | +That file will look like this: |
| 81 | + |
| 82 | +```javascript |
| 83 | +const getJoke = require("./joke"); |
| 84 | +const core = require("@actions/core"); |
| 85 | + |
| 86 | +async function run() { |
| 87 | + const joke = await getJoke(); |
| 88 | + console.log(joke); |
| 89 | + core.setOutput("joke-output", joke); |
| 90 | +} |
| 91 | + |
| 92 | +run(); |
| 93 | +``` |
| 94 | + |
| 95 | +Need an advanced description of the <code>main.js</code> source code? |
| 96 | + |
| 97 | +Like we did in the `joke.js` file, we are first going to bring in our dependencies. Only this time, our dependencies include something we wrote! To do that we simply use `require()` to point to the location of the file we wish to bring in. |
| 98 | + |
| 99 | +We also bring in `@actions/core` so that we can set the output of our action. |
| 100 | + |
| 101 | +Next we write another **asynchronous JavaScript function** that stores the return value of `getJoke()` in a variable called **joke**. |
| 102 | + |
| 103 | +Then we log the joke to the console. |
| 104 | + |
| 105 | +Finally we finish the function with by setting the contents of the joke as the value of the `joke-output` output parameter. We will use this output later in the course. |
| 106 | +_Don't forget to call the `run()` function._ |
| 107 | + |
| 108 | +### :keyboard: Activity 1: Creating the JavaScript files for your new action. |
| 109 | + |
| 110 | +1. Create and add the following contents to the `.github/actions/joke-action/joke.js` file: |
| 111 | + |
| 112 | + ```javascript |
| 113 | + const request = require("request-promise"); |
| 114 | + |
| 115 | + const options = { |
| 116 | + method: "GET", |
| 117 | + uri: "https://icanhazdadjoke.com/", |
| 118 | + headers: { |
| 119 | + Accept: "application/json", |
| 120 | + "User-Agent": "Writing JavaScript action GitHub Skills course.", |
| 121 | + }, |
| 122 | + json: true, |
| 123 | + }; |
| 124 | + |
| 125 | + async function getJoke() { |
| 126 | + const res = await request(options); |
| 127 | + return res.joke; |
| 128 | + } |
| 129 | + |
| 130 | + module.exports = getJoke; |
| 131 | + ``` |
| 132 | + |
| 133 | +2. Save the `joke.js` file. |
| 134 | +3. Create and add the following contents to the `.github/actions/joke-action/main.js` file: |
| 135 | + |
| 136 | + ```javascript |
| 137 | + const getJoke = require("./joke"); |
| 138 | + const core = require("@actions/core"); |
| 139 | + |
| 140 | + async function run() { |
| 141 | + const joke = await getJoke(); |
| 142 | + console.log(joke); |
| 143 | + core.setOutput("joke-output", joke); |
| 144 | + } |
| 145 | + |
| 146 | + run(); |
| 147 | + ``` |
| 148 | + |
| 149 | +4. Save the `main.js` file. |
| 150 | +5. Commit the changes to this branch and push them to GitHub: |
| 151 | + ```shell |
| 152 | + git add joke.js main.js |
| 153 | + git commit -m 'creating joke.js and main.js' |
| 154 | + git pull |
| 155 | + git push |
| 156 | + ``` |
| 157 | +6. Wait about 20 seconds then refresh this page (the one you're following instructions from). [GitHub Actions](https://docs.github.com/en/actions) will automatically update to the next step. |
0 commit comments