Skip to content
This repository was archived by the owner on Sep 1, 2022. It is now read-only.

Commit 5b98936

Browse files
committed
more config and responses
1 parent 3cbc8d3 commit 5b98936

6 files changed

Lines changed: 217 additions & 23 deletions

File tree

config.yml

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ steps:
196196
workflowFile: "%payload.repository.html_url%/edit/master/.github/workflows/my-workflow.yml"
197197
actionsUrl: "%payload.repository.html_url%/actions"
198198

199-
- title: Add a second Action
200-
description: Create a pull request to add another Action
199+
- title: Prevent the workflow from running
200+
description: Comment out the lines in the workflow to prevent unwanted runs
201201
event: pull_request
202202
# link: "{{ repoUrl }}/issue1/1"
203203
actions:
@@ -214,19 +214,39 @@ steps:
214214
- type: gate
215215
left: "%payload.pull_request.title%"
216216
operator: ===
217-
right: Second Action
217+
right: External APIs
218218

219219
# if those statments FAIL... do this
220220
else:
221221
- type: respond
222222
with: e-rename-pr.md
223223
data:
224-
title: Initial Workflow
224+
title: External APIs
225225
# if those gates === true Then do this stuff
226226
###############################################################################################################
227227
- type: closeIssue
228228
issue: Knock Knock...
229229
- type: respond
230-
with: 10_some-markdown.md
231-
data:
232-
actionsUrl: "%payload.repository.html_url%/actions"
230+
with: 10_action-two.md
231+
232+
- title: Create your new Action metadata
233+
description: Add action.yml file for new joke action
234+
event: pull_request.synchronize
235+
actions:
236+
- type: gate
237+
left: "%payload.pull_request.title%"
238+
operator: ===
239+
right: External APIs
240+
- type: respond
241+
with: 10_create-metadata.md
242+
243+
- title: Create your Actions JavaScript files
244+
description: Add joke.j and main.js to your Action
245+
event: pull_request.synchronize
246+
actions:
247+
- type: gate
248+
left: "%payload.pull_request.title%"
249+
operator: ===
250+
right: External APIs
251+
- type: respond
252+
with: 10_create-js-files.md

responses/09_new-action.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ You will still see the workflow trying to execute with every push if you look at
99
### :keyboard: Activity: Setting up the next Action
1010

1111
1. [Edit]({{workflowFile}) your workflow file by commenting out every single line.
12-
2. Commit the changes to a new branch.
12+
2. Commit the changes to a new branch and name it `action-two`.
1313
3. Create a pull request named **External APIs**
1414

1515
Like our first Action, I'll respond in the new pull request when I detect it has been opened.

responses/10_action-two.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## {{user.login}} you're doing great 👍
2+
3+
Just like the first Action we wrote, we are going to need to setup a few directories and files.
4+
5+
### :keyboard: Activity: Configure your second Action
6+
7+
Now that you have all the necessary tools installed locally, follow these steps to ensure your environment is configured and ready for Actions.
8+
9+
1. Open the **Terminal** (Mac and Linux) or **Command Prompt** (Windows) on your local machine and navigate to your course repo directory.
10+
2. Checkout the `master` branch
11+
`git checkout master`
12+
3. Update the contents of your Learning Lab repo to your local machine:
13+
`git pull`
14+
4. Checkout the `action-two` branch you created for this pull request.
15+
`git checkout action-two`
16+
5. Create a new folder for our Actions files:
17+
`mkdir -p .github/actions/joke-action`
18+
6. Navigate to the `joke-action` folder you just created:
19+
`cd .github/actions/joke-action`
20+
7. Initialize a new project:
21+
`npm init -y`
22+
8. Install the **request** and **request-promise** dependencies using `npm`:
23+
`npm install --save request request-promise`
24+
9. Commit those newly added files,we will remove the need to upload **node_modules** in a later step:
25+
`git add .`
26+
`git commit -m 'initial joke action'`
27+
10. Push you changes to your repository:
28+
`git push
29+
30+
---
31+
32+
I will respond once you have finished.

responses/10_create-js-files.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
## Files? 🤔
2+
3+
Yes... files... plural. 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.
4+
5+
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.
6+
7+
**Joke API**
8+
9+
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). I'll 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.
10+
11+
When we make our request to this API we will get back a JSON Object in the response. That Object looks like this:
12+
13+
```
14+
{
15+
id: '0LuXvkq4Muc',
16+
joke: "I knew I shouldn't steal a mixer from work, but it was a whisk I was willing to take.",
17+
status: 200
18+
}
19+
```
20+
21+
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.
22+
23+
**Joke Module**
24+
25+
We will create a file named `joke.js` and it will reside in the `.github/action/joke-action` directory.
26+
27+
The joke module will look like this:
28+
29+
![screenshot of joke.js file](https://i.imgur.com/GklnbKN.png)
30+
31+
We first bring in the `request-promise` library that we installed earlier using `npm`.
32+
33+
Next we define a set of `options` that the `request-promise` library will use when it makes the request.
34+
35+
📖Read more about [request-promise](https://github.com/request/request-promise/blob/master/README.md)
36+
37+
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.
38+
39+
**icanhazdadjoke** cares the most about the keys, **Accept** and **User-Agent**, so we need to make sure we fill them in.
40+
41+
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`.
42+
43+
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.
44+
45+
This file finishes up by exporting the newly created function so that we can use it in our `main.js` file.
46+
47+
**Main Module**
48+
49+
We will also create a file named `main.js` that resides inside of the `.github/actions/joke-action` directory.
50+
51+
That file will look like this:
52+
53+
![screenshot of main.js file](https://i.imgur.com/pTBDECt.png)
54+
55+
Like we did in the `joke.js` file, we are first going to bring in our dependencies. Only this time, our dependency isn't something we installed, it's something we wrote! To do that we simply use `require()` to point to the location of the file we wish to bring in.
56+
57+
Next we write another **asynchronous JavaScript function** that stores the return value of `getJoke()` in a variable called **joke**.
58+
59+
Then we finish the function with a simple joke logged to the console.
60+
61+
Don't forget to call the `run()` function.
62+
63+
### :keyboard: Creating the javascript files for your new Action.
64+
65+
1. Add the following contents to the `.github/actions/joke-action/joke.js` file:
66+
67+
```javascript
68+
const request = require("request-promise");
69+
70+
const options = {
71+
method: "GET",
72+
uri: "https://icanhazdadjoke.com/",
73+
headers: {
74+
Accept: "application/json",
75+
"User-Agent":
76+
"Writing JavaScript Action GitHub Learning Lab course. Visit lab.github.com or to contact us."
77+
},
78+
json: true
79+
};
80+
81+
async function getJoke() {
82+
const res = await request(options);
83+
return res;
84+
}
85+
86+
module.exports = getJoke;
87+
```
88+
89+
2. Save the `joke.js` file.
90+
3. Add the following contents to the `.github/actions/joke-action/main.js` file:
91+
92+
```javascript
93+
const getJoke = require("./joke");
94+
95+
async function run() {
96+
const joke = await getJoke();
97+
console.log(joke);
98+
}
99+
100+
run();
101+
```
102+
103+
4. Save the `main.js` file.
104+
5. Commit the changes to the `action-two` branch:
105+
`git add .`
106+
`git commit -m 'creating joke.js and main.js'`
107+
6. Push the changes to your repository:
108+
`git push`
109+
110+
---
111+
112+
I'll respond in this pull request when you have completed these tasks.

responses/10_create-metadata.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
### :keyboard: Activity: Create the metadata file
2+
3+
💡All of the following steps take place inside of the `.github/actions/hello-world` directory.
4+
5+
Our Action does not require much metadata for it to run correctly. We will not be accepting any inputs, nor setting any outputs. Because of that we simply need to name it, give it a description and define its environment.
6+
7+
1. Create a file named `action.yml`
8+
2. Use the `name` parameter to name your Action `"external API action"`
9+
3. Next, add a `description` parameter and give it a value of `"use an external API to retrieve and display a joke"`
10+
4. Lastly, define the `run` parameter to use `"node12"` to execute the `"main.js"`
11+
12+
<details><summary>View the complete file</summary>
13+
14+
```yaml
15+
name: "external API action"
16+
17+
description: "use an external API to retrieve and display a joke"
18+
19+
runs:
20+
using: "node12"
21+
main: "main.js"
22+
```
23+
24+
</details>
25+
26+
5. Save the `action.yml` file
27+
6. commit the changes:
28+
`git add .`
29+
`git commit -m 'update action.yml'`
30+
7. push them to the `action-two` branch:
31+
`git push`

responses/dev-env.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,60 @@
44

55
This is an external library that we will install using `npm` which means that you will need [Node.js](https://nodejs.org/) installed.
66

7-
I find writing Actions to be easier from a local environment vs trying to do everything right here in the repository. Doing these steps locally allows you to use the editor of your choice so that you have all the extensions and snippets you are used to when writing code.
7+
I find writing Actions to be easier from a local environment vs trying to do everything right here in the repository. Doing these steps locally allows you to use the editor of your choice so that you have all the extensions and snippets you are used to when writing code.
88

99
If you do not have a preferred environment then I suggest following along with me exactly as you see on the screen, which means you'll need to install [Visual Studio Code](https://code.visualstudio.com/).
1010

1111
## Don't forget to set up your workstation 😉
1212

13-
Most of your work going forward will take place away from your Learning Lab repository, so before continuing with the course ensure you have the following installed on your **local machine**.
13+
Most of your work going forward will take place away from your Learning Lab repository, so before continuing with the course ensure you have the following installed on your **local machine**.
1414

1515
1. [ ] [Node.js](https://nodejs.org)
1616
2. [ ] [Visual Studio Code](https://code.visualstudio.com/) or your editor of choice
1717
3. [ ] [Git](https://git-scm.com/)
1818

19-
2019
### :keyboard: Activity: Configure your environment
2120

2221
Now that you have all the necessary tools installed locally, follow these steps to ensure your environment is configured and ready for Actions.
2322

2423
1. Open the **Terminal** (Mac and Linux) or **Command Prompt** (Windows) on your local machine
2524
2. Clone your Learning Lab repo to your local machine:
2625
`git clone {{repoUrl}}.git`
26+
2727
<details><summary>View GIF</summary><img src="https://media.giphy.com/media/YnvmISGo2MbXpn2bc5/giphy.gif" alt="git clone example" /></details>
2828
<!-- ![alt text](https://media.giphy.com/media/YnvmISGo2MbXpn2bc5/giphy.gif) -->
2929

3030
3. Navigate to the folder you just cloned:
3131
`cd writing-javascript-actions`
3232
<details><summary>View GIF</summary><img src="https://media.giphy.com/media/duA6JVCuXbt5gKqNLw/giphy.gif" alt="directory navigation" /></details>
3333
<!-- ![alt text](https://media.giphy.com/media/duA6JVCuXbt5gKqNLw/giphy.gif) -->
34-
4. Create a new branch named `hello-world`. This is the branch we will use to write our first Action. **Please do not capitalize letters unless I do, I run case-sensitive checks to make sure I can help you along the way!**
34+
4. Create a new branch named `hello-world`. This is the branch we will use to write our first Action. **Please do not capitalize letters unless I do, I run case-sensitive checks to make sure I can help you along the way!**
3535
`git checkout -b hello-world`
36+
3637
<details><summary>View GIF</summary><img src="https://media.giphy.com/media/hvdeWGkjoy4UdfwbfQ/giphy.gif" alt="git checkout example" /></details>
37-
38+
3839
5. Create a new folder for our Actions files:
3940
`mkdir -p .github/actions/hello-world`
4041
<details><summary>View GIF</summary><img src="https://media.giphy.com/media/Wn03sc0QsywHHD1LeN/giphy.gif" alt="create folder for action example" /></details>
4142
6. Navigate to the `hello-world` folder you just created:
4243
`cd .github/actions/hello-world`
44+
4345
<details><summary>View GIF</summary><img src="https://media.giphy.com/media/ckCMgczjpbjNwkfJq4/giphy.gif" alt="navigate to folder for action example" /></details>
44-
46+
4547
7. Initialize a new project:
4648
`npm init -y`
49+
4750
<details><summary>View GIF</summary><img src="https://media.giphy.com/media/mEW0fJYx4oUjdgYHDV/giphy.gif" alt="navigate to folder for action example" /></details>
4851

49-
8. Install the **core** and **github** dependencies from the [GitHub ToolKit](https://github.com/actions/toolkit):
50-
`npm install --save @actions/core @actions/github`
52+
8. Install the **core** dependency from the [GitHub ToolKit](https://github.com/actions/toolkit):
53+
`npm install --save @actions/core`
5154
<details><summary>View GIF</summary><img src="https://media.giphy.com/media/H3kGNqDI24lNOEKk5k/giphy.gif" alt="navigate to folder for action example" /></details>
52-
9. Commit those newly added files,we will remove the need to upload **node_modules** in a later step:
53-
`git add .`
54-
`git commit -m 'initial hello-world'`
55+
9. Commit those newly added files,we will remove the need to upload **node_modules** in a later step:
56+
`git add .`
57+
`git commit -m 'initial hello-world'`
5558
10. Push you changes to your repository:
5659
`git push -u origin hello-world`
5760

58-
5961
---
6062

6163
I will respond once you have finished.
62-
63-
64-

0 commit comments

Comments
 (0)