-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
32 lines (28 loc) · 1.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const core = require("@actions/core");
const github = require("@actions/github");
const fetch = require("node-fetch");
const baseCurrency = core.getInput("base-currency");
const targetCurrency = core.getInput("target-currency");
const getExchangeRate = async () => {
const response = await fetch(
`https://api.exchangeratesapi.io/latest?base=${baseCurrency}&symbols=${targetCurrency}`
);
const data = await response.json();
return data;
};
/*This function is used to set the value of the exchange-rate variable
so that other github-actions steps and jobs can access it.
*/
const setResultsToGithubOutput = (response) => {
const exchangeRate = parseFloat(response.rates[targetCurrency]);
console.log(response);
core.setOutput("exchange-rate", exchangeRate.toFixed(2));
};
try {
// Get the JSON webhook payload for the event that triggered the workflow
const payload = JSON.stringify(github.context.payload, undefined, 2);
console.log(`The event payload: ${payload}`);
getExchangeRate().then((response) => setResultsToGithubOutput(response));
} catch (error) {
core.setFailed(error.message);
}