Skip to content

Commit

Permalink
Add workflow to fetch project data (#330)
Browse files Browse the repository at this point in the history
* Add implementation for Github workflow that runs at 3am and updates github-data.json with project data.

* Use a working email address for the commit_user_email

* Clarify when action runs

Co-authored-by: Cynthia Kiser <cynthiakiser@gmail.com>
  • Loading branch information
KianBadie and cnk authored Mar 2, 2020
1 parent 6c54df3 commit 2f13f20
Show file tree
Hide file tree
Showing 6 changed files with 913 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Build

# Run this action every day at 3am Pacific (11 am UTC)
on:
schedule:
- cron: '0 11 * * *'

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: bahmutov/npm-install@v1
with:
working-directory: github-actions
- name: Generate github-data.json
id: json
uses: ./github-actions
with:
token: ${{ secrets.GITHUB_TOKEN }}
- uses: stefanzweifel/git-auto-commit-action@v3.0.0
with:
# Optional glob pattern of files which should be added to the commit
file_pattern: _data/github-data.json

commit_message: Update contributor and language data

# Optional commit user and author settings
commit_user_name: GitHub Actions Bot
commit_user_email: hackforla-bot@hackforla.org
1 change: 1 addition & 0 deletions _data/github-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
8 changes: 8 additions & 0 deletions github-actions/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: 'Get Project Data'
description: Calls the github API for all projects with the hack-for-la tag, gets contributor and language data and writes it to a JSON file
outputs:
json: # id of output
description: 'json data we need for project cards'
runs:
using: 'node12'
main: './get-project-data.js'
115 changes: 115 additions & 0 deletions github-actions/get-project-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
const core = require('@actions/core');
const fs = require('fs');
const path = require('path');
const request = require('request-promise');

var github = {
token: null,
apiData: [],
getAllTaggedRepos: function() {
return request({
"method": "GET",
"uri": "https://api.github.com/search/repositories?q=topic:hack-for-la&sort=updated&order=desc",
"json": true,
"headers": {
"Authorization": "token " + github.token,
"User-Agent": "Hack For LA"
}
}).then(function(body) {
body.items.forEach(function(project) {
github.apiData.push({
id: project.id,
name: project.name,
languages: { url: project.languages_url, data: [] },
contributors: { url: project.contributors_url, data: [] }
});
});
}).catch(function(err) {
return err.message;
});
},
getLanguageInfo: function(url) {
return request({
"method": "GET",
"uri": url,
"json": true,
"headers": {
"Authorization": "token " + github.token,
"User-Agent": "Hack For LA"
}
}).then(function(body) {
// The body contains an ordered list of languge + lines of code.
// We care about the order of the names but not the number of lines of code.
return Promise.resolve(Object.keys(body));
}).catch(function(err) {
return err.message;
});
},
getContributorsInfo: function(url) {
return request({
"method": "GET",
"uri": url,
"json": true,
"headers": {
"Authorization": "token " + github.token,
"User-Agent": "Hack For LA"
}
}).then(function(body) {
// return a list of contributors sorted by number of commits
let contributors = [];
body.forEach(function(user) {
contributors.push({
"id": user.id,
"github_url": user.html_url,
"avatar_url": user.avatar_url,
"gravatar_id": user.gravatar_id
});
});
return Promise.resolve(contributors);
}).catch(function(err) {
return err.message;
});
}
}

async function main(params) {
console.log('In the async function main');
github.token = params.token;

await github.getAllTaggedRepos();
let lps = [], ldone = false
let cps = [], cdone = false
for (i = 0; i < github.apiData.length; i++) {
lps.push(github.getLanguageInfo(github.apiData[i].languages.url));
cps.push(github.getContributorsInfo(github.apiData[i].contributors.url));
}
Promise.all(lps)
.then(function(ls) {
for (i = 0; i < ls.length; i++) {
github.apiData[i].languages.data = ls[i]
}
ldone = true
if (cdone) finish()
})
.catch(function(e) {
console.log(e)
});
Promise.all(cps)
.then(function(cs) {
for (i = 0; i < cs.length; i++) {
github.apiData[i].contributors.data = cs[i]
}
cdone = true
if (ldone) finish()
})
.catch(function(e) {
console.log(e)
});
function finish(){
console.log(JSON.stringify(github.apiData, null, 2));
fs.writeFileSync('_data/github-data.json', JSON.stringify(github.apiData, null, 2));
}
}

const token = core.getInput('token');
main({ "token": token });
Loading

0 comments on commit 2f13f20

Please sign in to comment.