Skip to content

Commit 5437ebd

Browse files
committed
project automation first pass
1 parent 5aac63d commit 5437ebd

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Project automation
2+
3+
on:
4+
schedule:
5+
- cron: "0 0 * * *"
6+
workflow_dispatch: {}
7+
8+
jobs:
9+
sync-project-issues:
10+
name: Add issue/pull requests to project
11+
runs-on: ubuntu-latest
12+
env:
13+
GH_PROJECT_TOKEN: ${{ secrets.GH_PROJECT_TOKEN }}
14+
steps:
15+
- uses: actions/checkout@v2
16+
- run: |
17+
npm install @actions/github date-fns
18+
node scripts/project-automation.js

scripts/project-automation.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const github = require('@actions/github');
2+
const subDays = require('date-fns/subDays');
3+
4+
// read from secrets
5+
const GITHUB_TOKEN = process.env.GH_PROJECT_TOKEN;
6+
7+
if (!GITHUB_TOKEN)
8+
throw new Error("no token provided");
9+
10+
// read from secrets
11+
const config = {
12+
owner:"metosin",
13+
ownerType: "organization",
14+
projectNumber: 4,
15+
datesBack: 7
16+
};
17+
18+
const repos = ["malli", "reitit"];
19+
20+
const octokit = github.getOctokit(GITHUB_TOKEN);
21+
22+
const syncRepoIssuesToProject = async (config, repo) => {
23+
const projectResult = await octokit.graphql(`
24+
query {
25+
${config.ownerType}(login: "${config.owner}"){
26+
projectV2(number: ${config.projectNumber}) {
27+
id
28+
}
29+
}
30+
}`);
31+
32+
const projectId = projectResult[config.ownerType].projectV2.id;
33+
34+
const openIssuesAndPullRequests = await octokit.rest.issues.listForRepo(
35+
{
36+
owner: config.owner,
37+
repo: repo,
38+
state: 'open',
39+
since: subDays(new Date(), config.datesBack).toISOString()
40+
});
41+
42+
for (issue of openIssuesAndPullRequests.data) {
43+
44+
console.log(`Add ${repo}/#${issue.number} ${issue.title}`);
45+
46+
try {
47+
const result = await octokit.graphql(`
48+
mutation {
49+
addProjectV2ItemById(input: {projectId: "${projectId}" contentId: "${issue.node_id}"}) {
50+
item {
51+
id
52+
}
53+
}
54+
}`);
55+
} catch (error){
56+
console.log("Error while syncing issue");
57+
console.log(issue);
58+
console.log(error);
59+
}
60+
}
61+
};
62+
63+
const syncRepos = async (config, repos) => {
64+
for (repo of repos) {
65+
await syncRepoIssuesToProject(config, repo);
66+
}
67+
};
68+
69+
// run the sync
70+
syncRepos(config, repos);

0 commit comments

Comments
 (0)