Skip to content

Commit

Permalink
Merge pull request #4 from metosin/project-automation
Browse files Browse the repository at this point in the history
project automation first pass
  • Loading branch information
tvaisanen authored Feb 7, 2023
2 parents 5aac63d + 5437ebd commit 09153e3
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/project-automation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Project automation

on:
schedule:
- cron: "0 0 * * *"
workflow_dispatch: {}

jobs:
sync-project-issues:
name: Add issue/pull requests to project
runs-on: ubuntu-latest
env:
GH_PROJECT_TOKEN: ${{ secrets.GH_PROJECT_TOKEN }}
steps:
- uses: actions/checkout@v2
- run: |
npm install @actions/github date-fns
node scripts/project-automation.js
70 changes: 70 additions & 0 deletions scripts/project-automation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const github = require('@actions/github');
const subDays = require('date-fns/subDays');

// read from secrets
const GITHUB_TOKEN = process.env.GH_PROJECT_TOKEN;

if (!GITHUB_TOKEN)
throw new Error("no token provided");

// read from secrets
const config = {
owner:"metosin",
ownerType: "organization",
projectNumber: 4,
datesBack: 7
};

const repos = ["malli", "reitit"];

const octokit = github.getOctokit(GITHUB_TOKEN);

const syncRepoIssuesToProject = async (config, repo) => {
const projectResult = await octokit.graphql(`
query {
${config.ownerType}(login: "${config.owner}"){
projectV2(number: ${config.projectNumber}) {
id
}
}
}`);

const projectId = projectResult[config.ownerType].projectV2.id;

const openIssuesAndPullRequests = await octokit.rest.issues.listForRepo(
{
owner: config.owner,
repo: repo,
state: 'open',
since: subDays(new Date(), config.datesBack).toISOString()
});

for (issue of openIssuesAndPullRequests.data) {

console.log(`Add ${repo}/#${issue.number} ${issue.title}`);

try {
const result = await octokit.graphql(`
mutation {
addProjectV2ItemById(input: {projectId: "${projectId}" contentId: "${issue.node_id}"}) {
item {
id
}
}
}`);
} catch (error){
console.log("Error while syncing issue");
console.log(issue);
console.log(error);
}
}
};

const syncRepos = async (config, repos) => {
for (repo of repos) {
await syncRepoIssuesToProject(config, repo);
}
};

// run the sync
syncRepos(config, repos);

0 comments on commit 09153e3

Please sign in to comment.